Weighted graphs in Data Structures Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with weighted graphs, it is important to understand how the time to process the graph grows as the graph gets bigger.
We want to know how the number of steps changes when the graph has more nodes and edges.
Analyze the time complexity of the following code snippet.
function sumWeights(graph) {
let total = 0;
for (let node in graph) {
for (let edge of graph[node]) {
total += edge.weight;
}
}
return total;
}
This code adds up all the weights of edges in a weighted graph represented as an adjacency list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding edge weights inside the inner loop.
- How many times: Once for every edge in the graph.
As the number of edges increases, the total steps increase roughly the same amount.
| Input Size (Edges) | Approx. Operations |
|---|---|
| 10 | About 10 additions |
| 100 | About 100 additions |
| 1000 | About 1000 additions |
Pattern observation: The work grows directly with the number of edges.
Time Complexity: O(E)
This means the time to sum weights grows in direct proportion to the number of edges in the graph.
[X] Wrong: "The time depends mostly on the number of nodes, not edges."
[OK] Correct: Because edges hold the weights, the code must look at each edge, so the number of edges controls the time.
Understanding how time grows with edges and nodes helps you explain graph algorithms clearly and confidently in interviews.
"What if the graph was represented as an adjacency matrix instead of a list? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of weights in graphs
Weights on edges represent values like cost, distance, or time between two connected points (vertices).Step 2: Differentiate weights from other graph properties
Weights are not about color, number of vertices, or direction but about measurable values on edges.Final Answer:
The cost or distance between two connected points -> Option AQuick Check:
Weight = cost/distance [OK]
- Confusing weight with edge color
- Thinking weight counts vertices
- Mixing weight with edge direction
Solution
Step 1: Recognize common weighted edge notation
Weighted edges are often represented as tuples like (vertex1, vertex2, weight).Step 2: Check each option's format
(A, B, 5)uses tuple format (A, B, 5), which is standard. Others are incorrect syntax or informal.Final Answer:
(A, B, 5) -> Option AQuick Check:
Weighted edge = (vertex1, vertex2, weight) [OK]
- Using incorrect symbols like braces or colons
- Confusing syntax with dictionaries
- Writing weight as a keyword inside list
(A, B, 3), (B, C, 4), (A, C, 10). What is the shortest path weight from A to C?Solution
Step 1: Identify possible paths from A to C
Paths: Direct (A to C) with weight 10, or via B: A to B (3) + B to C (4).Step 2: Calculate total weights for each path
Direct path weight = 10; via B = 3 + 4 = 7.Final Answer:
7 -> Option DQuick Check:
Shortest path weight = 7 [OK]
- Choosing direct edge without checking alternatives
- Adding weights incorrectly
- Ignoring intermediate vertices
(X, Y, 2), (Y, Z, 5), (X, Z, 4), a student claims the shortest path from X to Z is 7 by going through Y. What is wrong with this claim?Solution
Step 1: Analyze the paths from X to Z
Paths: Direct edge (X, Z) with weight 4, and path via Y with weights 2 + 5 = 7.Step 2: Identify the shortest path
The direct edge weight 4 is less than 7, so shortest path is direct, not via Y.Final Answer:
They ignored the direct edge from X to Z with weight 4 -> Option BQuick Check:
Shortest path uses smallest weight edge [OK]
- Ignoring direct edges
- Incorrectly adding weights
- Mixing up vertex names
Solution
Step 1: Understand the problem context
We want the cheapest route considering toll costs, which are weights on edges.Step 2: Choose an appropriate algorithm
Dijkstra's algorithm finds shortest paths in weighted graphs by minimizing total weight (cost).Final Answer:
Use a shortest path algorithm like Dijkstra's considering weights as toll costs -> Option CQuick Check:
Weighted shortest path = Dijkstra's algorithm [OK]
- Ignoring weights and counting edges only
- Using DFS which ignores weights
- Choosing longest path mistakenly
