Shortest Path · Algorithms
22 questions · asked in 17 of 27 years (2000–2026)
MCQ 21 · NAT 11-mark 12 · 2-mark 10
Questions 11–15 of 22
What is the minimum possible weight of a path P from vertex 1 to vertex 2 in this graph such that P contains at most 3 edges?
Answer: (B)
All paths from 1 to 2 with at most 3 edges:
- 1 edge: 1-2 = 12
- 2 edges: 1-0-2 = 1+8 = 9, 1-3-2 = 4+7 = 11, 1-4-2 = 9+3 = 12
- 3 edges: 1-0-3-2 = 1+1+7 = 9, 1-0-4-2 = 1+4+3 = 8, 1-3-0-2 = 4+1+8 = 13, 1-3-4-2 = 4+2+3 = 9, 1-4-0-2 = 9+4+8 = 21, 1-4-3-2 = 9+2+7 = 18
The minimum is 8. (The cheapest path overall, 1-0-3-4-2 = 1+1+2+3 = 7, has 4 edges, so it is not allowed.)
Answer: (B)
P. Always finds a negative weighted cycle, if one exists.
Q. Finds whether any negative weighted cycle is reachable from the source.
Answer: (B)
Single-source shortest paths, time .
In each pass, relax every edge once. At most passes are needed, so the cost is . One more pass is then made to verify the result: if any distance still improves, a negative cycle is reachable from the source.
Example from the figure, source A. The edges are relaxed in the order in every pass: , , , , , .
Each pair of lines in the table gives the distances and the predecessors. Rows i=0, 1, 2 are the passes. Row i=3 is the verification pass: the values stay unchanged. This is because the only cycle here, , weighs , so there is no negative cycle.
If a negative cycle were reachable from the source, some value would still drop in the verification pass, so Q is true.
Now say the cycle is not reachable from the source. Its vertices stay at from the beginning to the end, because relaxing an edge that starts at gives nothing. Other vertices that are not reachable also stay at . So the algorithm cannot tell such a cycle apart from those vertices, and it misses it. So P is false.
Answer: (B)
Dijkstra's single source shortest path algorithm when run from vertex a in the above graph, computes the correct shortest path distance to
Answer: (D)
1. Take (0): .
2. Take (1): , .
3. Take (-2): .
4. Take (0): .
5. Take (3): , .
6. Take (-2): the edge to gives , which is not better than .
7. Take (3): the edges to and give 4 and 5, no improvement.
8. Take (6): the edges to and give 8, no improvement.
Final distances: . No vertex is improved after it is finalised, so these are the true shortest distances (they can be checked by hand, and the graph has no negative cycle).
Dijkstra is not safe for negative edges in general. Here it works because no later vertex offers a shorter route to an already finalised one. So it is correct for all vertices.
Answer: (D)
Answer: (C)
Going from to by a shortest path and then taking the edge is one path from to , so , that is . So .
Option (D) is not always true: is possible (if the shortest path to goes through ). Options (A) and (B) are not always true either: can be larger than 12, for example 100, if is reached by another path of weight 65.
Answer: (C)
Answer: (D)
- BFS from visits vertices in order of their number of edges from , so it gives all shortest paths in time.
- Dijkstra with a heap takes .
- Warshall's algorithm finds all pairs and takes .
- DFS does not give shortest paths. It may reach a vertex by a long path first.
BFS is the fastest of these.
Answer: (D)
Keep practising