Vertex

Term side of the Card.
Topic: Graph
Unit title: Vertex
Categories: Concept
Explanation side of the Card

Explanation: The point or node in a graph where edges connect. They are used to represent entities and edges represent relationships between them.

Example

Wrongjs
const graph = {};
for (const user of users) graph[user] = [];
Rightjs
const graph = new Map();
for (const user of users) graph.set(user, []);

Plain object keys become strings, so object vertices collide; a Map keeps each object as its own key.

Common mistakes

  • Building the graph only from edges and losing vertices that have no edges.
  • Using objects as keys of a plain object, so every vertex becomes '[object Object]'.

Key points

  • Vertices are keys in a Map of adjacency lists, not items in an array searched with indexOf().
  • Vertices with no edges are stored too, so they appear in traversals and counts.

Interview questions

What is the degree of a vertex?

The number of edges touching it. In a directed graph, in-degree counts incoming edges and out-degree counts outgoing ones.

Vertex vs node: are they the same?

In graph theory they mean the same thing. Vertex is the math term; node is more common in code and in trees.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy