To find the shortest path from a single starting point (source) to all other points (nodes) in a graph with non-negative edge weights.
Click to reveal answer
intermediate
In Dijkstra's Algorithm, what data structure is commonly used to efficiently select the next node with the smallest tentative distance?
A priority queue (often implemented as a min-heap) is used to quickly pick the node with the smallest current distance estimate.
Click to reveal answer
intermediate
Why can't Dijkstra's Algorithm handle graphs with negative edge weights?
Because it assumes that once a node's shortest distance is found, it won't get shorter. Negative edges can create shorter paths after visiting nodes, breaking this assumption.
Click to reveal answer
advanced
What is the time complexity of Dijkstra's Algorithm when using a binary heap and adjacency list?
O((V + E) log V), where V is the number of vertices and E is the number of edges in the graph.
Click to reveal answer
beginner
Explain the role of the 'distance' array in Dijkstra's Algorithm.
It stores the shortest known distance from the source node to every other node. It updates as the algorithm finds shorter paths.
Click to reveal answer
What type of graph edge weights does Dijkstra's Algorithm require?
ANegative weights allowed
BAny weights including negative cycles
COnly zero weights
DNon-negative weights
✗ Incorrect
Dijkstra's Algorithm only works correctly with non-negative edge weights.
Which data structure helps Dijkstra's Algorithm pick the next closest node efficiently?
APriority queue
BQueue
CLinked list
DStack
✗ Incorrect
A priority queue allows quick access to the node with the smallest tentative distance.
What happens if you run Dijkstra's Algorithm on a graph with negative edge weights?
AIt may produce incorrect results
BIt finds correct shortest paths
CIt runs faster
DIt detects negative cycles automatically
✗ Incorrect
Negative weights can cause incorrect shortest path calculations in Dijkstra's Algorithm.
What does the 'distance' array represent in Dijkstra's Algorithm?
ANumber of edges from source
BShortest known distances from source
CVisited nodes count
DTotal graph weight
✗ Incorrect
The distance array holds the shortest known distances from the source node to each node.
What is the initial distance value for all nodes except the source in Dijkstra's Algorithm?
A0
B-1
CInfinity (or a very large number)
DRandom value
✗ Incorrect
All nodes start with distance set to infinity except the source, which starts at 0.
Describe step-by-step how Dijkstra's Algorithm finds the shortest path from a source node to all other nodes.
Think about how the algorithm updates distances and picks nodes.
You got /5 concepts.
Explain why Dijkstra's Algorithm cannot handle negative edge weights and what could happen if it tries.
Focus on the assumption about final distances and how negative weights break it.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of Dijkstra's Algorithm in graph theory?
easy
A. To find the shortest path from a single source to all other vertices
B. To find the maximum flow in a network
C. To detect cycles in a graph
D. To sort vertices in topological order
Solution
Step 1: Understand the algorithm's goal
Dijkstra's Algorithm is designed to find the shortest paths from one starting point to all other points in a graph with non-negative edge weights.
Step 2: Compare with other graph algorithms
Other options like maximum flow, cycle detection, and topological sorting serve different purposes unrelated to shortest paths.
Final Answer:
To find the shortest path from a single source to all other vertices -> Option A
Quick Check:
Dijkstra's Algorithm = shortest path from single source [OK]
Hint: Remember: Dijkstra finds shortest paths from one start [OK]
Common Mistakes:
Confusing with algorithms for max flow or cycle detection
Thinking it sorts vertices instead of finding paths
Assuming it works with negative weights
2. Which Rust data structure is most suitable to efficiently pick the next vertex with the smallest tentative distance in Dijkstra's Algorithm?
easy
A. HashMap
B. VecDeque
C. BinaryHeap
D. LinkedList
Solution
Step 1: Identify the need for priority selection
Dijkstra's Algorithm requires repeatedly selecting the vertex with the smallest tentative distance efficiently.
Step 2: Choose the appropriate Rust data structure
BinaryHeap is a priority queue in Rust that allows efficient extraction of the smallest or largest element, suitable for this purpose.
Final Answer:
BinaryHeap -> Option C
Quick Check:
Priority queue = BinaryHeap [OK]
Hint: Use BinaryHeap for priority queue in Rust [OK]
Common Mistakes:
Using VecDeque which is a queue, not priority queue
Using HashMap which stores key-value pairs but no priority
Using LinkedList which is inefficient for priority access
3. What will be the output distances array after running Dijkstra's Algorithm from source vertex 0 on this graph represented as adjacency list?
Options show distances from vertex 0 to vertices 0,1,2,3.
medium
A. [0, 4, 1, 6]
B. [0, 3, 1, 4]
C. [0, 2, 1, 3]
D. [0, 1, 2, 5]
Solution
Step 1: Initialize distances and process neighbors
Start at vertex 0 with distance 0. Neighbors: vertex 1 (4), vertex 2 (1). Distances: [0,4,1,∞]
Step 2: Pick vertex with smallest distance and update
Pick vertex 2 (distance 1). Update vertex 1 via 2: 1 + 2 = 3 (less than 4), update vertex 3 via 2: 1 + 5 = 6. Distances: [0,3,1,6]
Step 3: Pick vertex 1 (distance 3) and update
From vertex 1 to 3: 3 + 1 = 4 (less than 6), update vertex 3 distance to 4. Distances: [0,3,1,4]
Final Answer:
[0, 3, 1, 4] -> Option B
Quick Check:
Shortest distances from 0 = [0,3,1,4] [OK]
Hint: Update distances only if new path is shorter [OK]
Common Mistakes:
Not updating vertex 1 distance via vertex 2
Ignoring better path to vertex 3 through vertex 1
Confusing edge weights or directions
4. Identify the bug in this Rust snippet implementing Dijkstra's Algorithm and select the correct fix:
let mut dist = vec![usize::MAX; graph.len()];
dist[source] = 0;
let mut heap = BinaryHeap::new();
heap.push((0, source));
while let Some((cost, u)) = heap.pop() {
if cost > dist[u] { continue; }
for &(v, w) in &graph[u] {
let next = cost + w;
if next < dist[v] {
dist[v] = next;
heap.push((next, v));
}
}
}
medium
A. Initialize dist with 0 instead of usize::MAX
B. Use VecDeque instead of BinaryHeap
C. Remove the if cost > dist[u] check
D. Change heap to store (Reverse<usize>, usize) to get min-heap behavior
Solution
Step 1: Understand Rust's BinaryHeap behavior
Rust's BinaryHeap is a max-heap by default, but Dijkstra needs a min-heap to get the smallest cost first.
Step 2: Fix heap to behave as min-heap
Use std::cmp::Reverse wrapper around cost to invert ordering, so smallest cost is popped first.
Final Answer:
Change heap to store (Reverse<usize>, usize) to get min-heap behavior -> Option D
Quick Check:
BinaryHeap is max-heap by default, use Reverse for min-heap [OK]
Hint: Use Reverse wrapper for min-heap in Rust BinaryHeap [OK]
Common Mistakes:
Ignoring heap ordering and getting wrong shortest paths
Initializing distances incorrectly
Removing important cost check causing wrong updates
5. Given a weighted graph with vertices 0 to 4 and edges: