Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.

Similar presentations


Presentation on theme: "1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted."— Presentation transcript:

1 1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted path length of v 1, v 2, …, v N : applications: –fastest way to get from one place to another –cheapest way to fly/send data from one city to another., where c i,j is the cost of edge (v i, v j )

2 2 Unweighted Shortest Path Special case of the weighted problem: all weights are 1. Solution: breadth-first search. Similar to level-order traversal for trees. v3v3 v6v6 v1v1 v2v2 v4v4 v7v7 v5v5 v0v0 s

3 3 void Graph::unweighted (Vertex s) { Queue q(NUM_VERTICES); Vertex v, w; q.enqueue(s); s.dist = 0; while (!q.isEmpty()) { v = q.dequeue(); for each w adjacent to v if (w.dist == INFINITY) { w.dist = v.dist + 1; w.path = v; q.enqueue(w); } each edge examined at most once each vertex enqueued at most once total running time: O( )

4 4 Weighted Shortest Path no negative weight edges. Dijkstra’s algorithm: uses similar ideas as the unweighted case. algorithms: do what seems to be best at every decision point. S “known” s V - S “unknown” v

5 5 Example v3v3 v6v6 v1v1 v2v2 v4v4 v5v5 v0v0 s 1 2 2 2 1 11 53 5 6 10

6 6 void Graph::dijkstra(Vertex s) { Vertex v,w; s.dist = 0; while (true) { v = smallest unknown dist. vert; if (v == NOT_A_VERTEX) break; v.known = true; for each w adjacent to v if (!w.known) if (v.dist + cvw < w.dist) { decrease(w.dist to v.dist + cvw); w.path = v; } while loop executes |V| times each edge examined once

7 7 Analysis How long does it take to find the smallest unknown distance? –simple scan using an array: O(|V|) –binary heap: O(log |V|) Total running time: –simple scan: O(|V| 2 + |E|) –binary heap: O(|V| log |V| + |E| log |V|) = O(|E| log |V|)

8 8 Proof of Correctness By induction on |S|, at end of loop: 1.For all v  S, v.dist is equal to the length of the shortest path from s to v. 2.For all v  V – S, v.dist is the length of the shortest path from s to v that lies wholly within S, except v itself. S “known” s V - S “unknown” G=(V,E) v

9 9 The proof Basis step. |S| = 1. The shortest path from s to itself has length 0. A path from s to v  V – S wholly within S except for v consists of a single edge (s, v). v.dist = cvw in the algorithm. Ind. step. Suppose v is chosen. If v.dist is not the length of a shortest path from s to v, then there must be a shorter path P. The path P must contain some vertex other than v that is not in S. (By ind. hyp. #2.)

10 10 Induction step, cont’d Let x be the first such vertex in P. But then x.dist < v.dist (why?), and so x should have been chosen, not v. Contradiction. Therefore, P must not exist, and v.dist is the length of the shortest path from s to v. #2 is true because of the decrease() operation. (why?) s x v S P

11 11 Negative Edge Costs Dijkstra’s algorithm fails. Idea: Get rid of the idea of known and unknown vertices. –Place s on the queue. –Dequeue a vertex and update distances of adjacent vertices, and place an adjacent vertex in the queue if its distance was lowered. s x v S P

12 12 Acyclic Graphs Idea: We do not need to choose a vertex with minimum cost from an unknown set. Instead, choose the vertices in topological order. Running time: O(|V| + |E|) s 2 4 2 5 3 1 1 4 2 -8 3 1


Download ppt "1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted."

Similar presentations


Ads by Google