Graph Terminology Vertices Edges Directed Undirected Weighted in DSA Javascript - Time & Space Complexity
Graphs are made of points and connections. We want to understand how the work grows when we look at all points and connections.
How does the time to check or use these parts grow as the graph gets bigger?
Analyze the time complexity of the following code snippet.
// Simple graph representation
const graph = {
vertices: ['A', 'B', 'C'],
edges: [
{ from: 'A', to: 'B', weight: 5 },
{ from: 'B', to: 'C', weight: 3 }
]
};
// Function to print all edges
function printEdges(graph) {
for (const edge of graph.edges) {
console.log(`${edge.from} -> ${edge.to} (weight: ${edge.weight})`);
}
}
printEdges(graph);
This code stores points and connections with weights, then prints each connection once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all edges in the graph.
- How many times: Once for each edge in the edges list.
As the number of edges grows, the time to print them grows directly with it.
| Input Size (edges) | Approx. Operations |
|---|---|
| 10 | 10 loops to print edges |
| 100 | 100 loops to print edges |
| 1000 | 1000 loops to print edges |
Pattern observation: The work grows in a straight line with the number of edges.
Time Complexity: O(E)
This means the time grows directly with the number of edges in the graph.
[X] Wrong: "The time depends on the number of vertices only."
[OK] Correct: The code loops over edges, so the number of edges controls the time, not just vertices.
Understanding how time grows with vertices and edges helps you explain graph algorithms clearly and confidently.
"What if the graph was directed vs undirected? How would the time complexity of printing edges change?"
Practice
vertex?Solution
Step 1: Understand the definition of vertex
A vertex is a fundamental unit in a graph representing a point or node.Step 2: Differentiate vertex from edges and graph types
Edges connect vertices; weights and graph types describe edges, not vertices.Final Answer:
A point or node in the graph where edges meet -> Option BQuick Check:
Vertex = node/point [OK]
- Confusing edges with vertices
- Thinking weights define vertices
- Mixing graph types with vertex definition
A to vertex B in JavaScript adjacency list?Solution
Step 1: Understand adjacency list for directed edges
In a directed graph, an edge from A to B means B is in A's adjacency list.Step 2: Check each option's adjacency list
{ A: ['B'], B: [] } shows B in A's list and empty list for B, correctly representing A -> B.Final Answer:
{ A: ['B'], B: [] } -> Option CQuick Check:
Directed edge A->B means B in A's list [OK]
- Adding reverse edge for directed graph
- Leaving adjacency list empty for source vertex
- Confusing undirected with directed representation
const graph = {
A: [{ node: 'B', weight: 3 }, { node: 'C', weight: 5 }],
B: [{ node: 'A', weight: 3 }],
C: [{ node: 'A', weight: 5 }]
};
console.log(graph['A'][1].weight);Solution
Step 1: Access the adjacency list for vertex 'A'
graph['A'] is an array with two objects: first with weight 3, second with weight 5.Step 2: Access the second element's weight
graph['A'][1] is the second object { node: 'C', weight: 5 }, so weight is 5.Final Answer:
5 -> Option AQuick Check:
graph['A'][1].weight = 5 [OK]
- Confusing index 0 and 1
- Accessing weight on wrong node
- Assuming undirected means no weights
const graph = {
A: ['B'],
B: ['C'],
C: ['A']
};
// Add edge from B to A
graph.B.push('A');
// Add edge from C to B
graph.C.push('B');
console.log(graph);Solution
Step 1: Check initial graph structure
Graph has directed edges A->B, B->C, C->A forming a cycle, which is allowed.Step 2: Verify edge additions
Adding B->A and C->B edges extends the graph; push method correctly mutates adjacency lists.Final Answer:
No error, code correctly adds edges -> Option DQuick Check:
push adds edges correctly; cycles allowed [OK]
- Thinking cycles are invalid in directed graphs
- Assuming push does not mutate arrays
- Confusing undirected with directed edge additions
const graph = {
A: [{ node: 'B', weight: 2 }, { node: 'C', weight: 4 }],
B: [{ node: 'A', weight: 2 }, { node: 'C', weight: 1 }],
C: [{ node: 'A', weight: 4 }, { node: 'B', weight: 1 }]
};Which of the following statements is true about the edge weights?
Solution
Step 1: Understand undirected weighted graph property
In undirected graphs, edges have no direction, so weights between two vertices are equal both ways.Step 2: Check given adjacency lists for symmetry
Weight from A to B is 2, and from B to A is also 2; similarly for other edges.Final Answer:
Weights are symmetric; weight from A to B equals weight from B to A -> Option AQuick Check:
Undirected edge weights are symmetric [OK]
- Assuming weights differ by direction in undirected graphs
- Ignoring weights in undirected graphs
- Thinking weights must be zero
Ready to prove it?
Pass the test and this pattern moves unknown
