05 - Single Source Shortest Path (Solution)

05 - Single Source Shortest Path (Solution) - The Hong Kong...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The Hong Kong University of Science & Technology COMP 271: Design and Analysis of Algorithms Fall 2006 Tutorial 6: Shortest Path Below is the suggested solution to the questions. Question 1 : Give a simple example of a directed graph with negative-weight edge(s) for which Dijkstra’s algorithm produces incorrect answers. Why does the correctness proof of Dijkstra’s algorithm not go through when negativeweight edges are allowed? Solution: The trick is to get Dijkstra’s algorithm to finalize a vertex before its true minimum distance is known. Consider the following digraph. Original Weighted Digraph s 4 w 2 u -3 1 v Step 1: s 0 4 2 4 w u 2 -3 1 v i Step 2: s 0 4 2 4 w u 2 -3 1 v 3 Step 3: s 0 4 2 4 w u 2 -3 1 v 3 Step 4: s 0 4 2 4 w u 1 -3 WRONG! 1 v 3 1 • Step 1: When s is processed, d[u] = 2 and d[w] = 4. • Step 2: We process u first, setting d[v ] = 3. • Step 3: Next, we process v , which changes nothing. • Step 4: Finally, we process w, setting d[u] = 1. We observe that the final value of d[v ] = 3, but the value is not the actual shortest path length from s to v . There is another path < s, w, u, v > of length 2 (since 4-3+1=2) which is even shorter. Therefore the result produced by Dijkstra’s algorithm is not correct. The proof fails at the point it assumes that all edge weights are nonnegative (see below). The proof says δ (s, y ) ≤ δ (s, u), which need not hold if some edges have negative weights. [ Part of the Dijkstra’s correctness proof ] Suppose y appears midway on the path from s to u, and all subsequent edges have non-negative weights, we have δ (s, y ) ≤ δ (s, u), and thus d[y ] = δ (s, y ) ≤ δ (s, u) < d[u]. Question 2 : Show each step in executing Dijkstra’s algorithm on the following weighted digraph, where s is the source vertex. Use dashed-arrows on edges to indicate predecessor pointers, numeric label in each vertex to represent its current shortest path estimate and black colored nodes to show those vertices that we know the true distance. 3 s 5 1 4 7 3 2 3 5 2 8 2 Note: The first step has been done for you. ( i denote +∞ ) 2 Step 1 (Given) 3 3 s 0 5 5 1 4 i 2 8 7 3 6 1 5 2 8 i 3 13 2 7 3 i 3 i 2 i 2 5 i Step 2 3 3 0 5 5 1 4 i 2 8 7 3 6 1 5 2 8 9 3 13 2 7 3 i 3 i 2 10 2 5 i Step 3 3 s 0 5 3 4 10 2 5 i s Step 4 3 3 0 5 4 10 2 5 i 3 Step 5 3 s 0 5 5 1 3 4 6 2 8 7 3 9 3 12 2 10 2 5 i s Step 6 3 3 0 5 5 1 4 6 2 8 7 3 9 3 12 2 10 2 5 15 Step 7 3 s 0 5 5 1 3 4 6 2 8 7 3 9 3 12 2 10 2 5 s 14 Step 8 3 3 0 5 5 1 4 6 2 8 7 3 9 3 12 10 2 5 14 2 4 ...
View Full Document

Ask a homework question - tutors are online