To find the shortest path from a single starting point (source) to all other points (vertices) 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 vertex with the smallest tentative distance?
A priority queue (often implemented as a min-heap) is used to quickly pick the vertex 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 vertex's shortest distance is found, it won't change. Negative edges can create shorter paths later, breaking this assumption.
Click to reveal answer
advanced
What is the time complexity of Dijkstra's Algorithm using a priority queue 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 relaxation step in Dijkstra's Algorithm.
Relaxation means checking if the current known distance to a neighbor can be improved by going through the current vertex. If yes, update the neighbor's distance.
Click to reveal answer
What type of graph edge weights does Dijkstra's Algorithm require?
AAny weights including negative cycles
BNegative weights allowed
COnly zero weights
DNon-negative weights
✗ Incorrect
Dijkstra's Algorithm works only with non-negative edge weights to guarantee correct shortest paths.
Which data structure helps Dijkstra's Algorithm efficiently pick the next vertex to process?
APriority Queue
BQueue
CStack
DLinked List
✗ Incorrect
A priority queue allows selecting the vertex with the smallest tentative distance quickly.
What happens during the relaxation step in Dijkstra's Algorithm?
ADistances to neighbors are updated if a shorter path is found
BVertices are removed from the graph
CEdges are reversed
DAll distances are reset to infinity
✗ Incorrect
Relaxation updates the shortest known distances to neighbors if a better path is found.
What is the initial distance to the source vertex in Dijkstra's Algorithm?
AInfinity
BZero
CNegative infinity
DOne
✗ Incorrect
The source vertex distance is set to zero because the distance from the source to itself is zero.
If a graph has 5 vertices and 10 edges, what is the approximate time complexity of Dijkstra's Algorithm using a priority queue?
AO(5^2)
BO(10 log 5)
CO((5 + 10) log 5)
DO(5 + 10)
✗ Incorrect
The time complexity is O((V + E) log V), so here it is O((5 + 10) log 5).
Describe step-by-step how Dijkstra's Algorithm finds the shortest paths from a source vertex.
Think about initialization, selection, relaxation, and termination.
You got /4 concepts.
Explain why Dijkstra's Algorithm does not work correctly with negative edge weights.
Consider what happens if a shorter path appears later due to negative edges.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of Dijkstra's Algorithm in graph theory?
easy
A. To sort vertices in topological order
B. To find the maximum flow in a network
C. To detect cycles in a graph
D. To find the shortest path from a single source to all other vertices
Solution
Step 1: Understand the algorithm's goal
Dijkstra's Algorithm is designed to find the shortest distance from one starting point (source) to every other point in a graph.
Step 2: Compare with other graph algorithms
Other algorithms like max flow or cycle detection serve different purposes, so they do not match Dijkstra's goal.
Final Answer:
To find the shortest path from a single source to all other vertices -> Option D
Quick Check:
Shortest path = find minimum distances [OK]
Hint: Dijkstra finds shortest paths from one start point [OK]
Common Mistakes:
Confusing with max flow algorithms
Thinking it detects cycles
Assuming it sorts vertices
2. Which Python data structure is commonly used to efficiently select the next vertex with the smallest tentative distance in Dijkstra's Algorithm?
easy
A. List
B. Heap (Priority Queue)
C. Queue
D. Set
Solution
Step 1: Identify the need for efficient minimum extraction
Dijkstra's Algorithm repeatedly picks the vertex with the smallest tentative distance, so an efficient way to get the minimum is needed.
Step 2: Choose the right data structure
A heap or priority queue allows quick extraction of the smallest element, unlike lists or queues which are slower for this task.
Final Answer:
Heap (Priority Queue) -> Option B
Quick Check:
Priority queue = efficient min extraction [OK]
Hint: Use a heap to get smallest distance vertex fast [OK]
Common Mistakes:
Using a simple list which is slow
Using a queue which is FIFO, not min-based
Using a set which has no order
3. What will be the output distances array after running Dijkstra's Algorithm from source vertex 0 on this graph represented by adjacency list with weights?
Step 3: Pick vertex with smallest distance (1), update neighbors
From vertex 1, update vertex 3: min(6, 3+1=4) = 4.
Step 4: Final distances
Distances array is [0, 3, 1, 4].
Final Answer:
[0, 3, 1, 4] -> Option C
Quick Check:
Distances updated stepwise = [0, 3, 1, 4] [OK]
Hint: Update neighbors with min distance each step [OK]
Common Mistakes:
Not updating vertex 1 distance from vertex 2
Ignoring better path to vertex 3
Mixing up edge weights
4. Consider this Dijkstra's Algorithm snippet in Python:
def dijkstra(graph, source):
dist = {v: float('inf') for v in graph}
dist[source] = 0
visited = set()
while len(visited) < len(graph):
u = min((v for v in graph if v not in visited), key=lambda x: dist[x])
visited.add(u)
for v, w in graph[u]:
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
return dist
What is the main issue with it?
medium
A. The algorithm does not use a priority queue for efficiency
B. The visited set is not updated properly
C. The algorithm does not handle unreachable vertices
D. The algorithm does not update distances correctly
Solution
Step 1: Analyze the selection of next vertex
The code uses min() over all unvisited vertices to find the smallest distance vertex, which is correct but inefficient.
Step 2: Identify efficiency problem
Without a priority queue, each min() call takes O(V) time, making the algorithm slower but still correct logically.
Step 3: Check for correctness vs efficiency
Though inefficient, the code updates distances and visited set correctly, so the main issue is lack of priority queue for efficiency.
Final Answer:
The algorithm does not use a priority queue for efficiency -> Option A
Quick Check:
Missing priority queue = inefficient but correct [OK]
Hint: Dijkstra needs priority queue for speed, else slow [OK]
Common Mistakes:
Thinking visited set is not updated
Assuming distance updates are wrong
Confusing unreachable vertex handling
5. Given a weighted graph with negative edge weights but no negative cycles, why can't Dijkstra's Algorithm be used to find shortest paths from a source vertex?
hard
A. Because Dijkstra's Algorithm assumes all edge weights are non-negative
B. Because Dijkstra's Algorithm cannot handle graphs with cycles
C. Because Dijkstra's Algorithm requires the graph to be directed acyclic
D. Because Dijkstra's Algorithm only works on unweighted graphs
Solution
Step 1: Understand Dijkstra's assumption on edge weights
Dijkstra's Algorithm assumes all edge weights are zero or positive to guarantee correctness.
Step 2: Explain why negative weights break the algorithm
Negative weights can cause the algorithm to miss shorter paths found later, as it greedily picks the smallest tentative distance.
Step 3: Clarify other options
The algorithm can handle cycles if no negative cycles exist, and it works on weighted graphs, not just unweighted.
Final Answer:
Because Dijkstra's Algorithm assumes all edge weights are non-negative -> Option A