Vertex
Example
const graph = {};
for (const user of users) graph[user] = [];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
Mapof adjacency lists, not items in an array searched withindexOf(). - 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.