Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shortest Paths.

Similar presentations


Presentation on theme: "Shortest Paths."— Presentation transcript:

1 Shortest Paths

2 Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances, costs, etc. Example: A vertex represents an airport and stores the three-letter airport code An edge represents a flight route between two airports and stores the mileage of the route ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 Los Angeles Chicago Providence San Francisco Honolulu Dallas Miami New York

3 Shortest Paths The problem: Weight of the path P
sum of the weights of all the edges on the path P Shortest path path with smallest weight The problem: Given a weighted graph and two vertices u and v, we want to find a path of minimum total weight (shortest path) between u and v.

4 Shortest Path Properties
Property 1: A sub-path of a shortest path is itself a shortest path Property 2: There is a tree of shortest paths from a start vertex to all the other vertices Example: Tree of shortest paths from Providence ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1205 Providence Los Angeles Chicago San Francisco Honolulu Dallas Miami New York

5 The Distance of a Shortest Path
Case 1: The graph may have negative edges but no negative cycles. The shortest distance from s to t can be computed. A B s t -3 1 8 d(s,t)=6 Case 2: The graph contains negative weight cycles, and a path from s to t includes an edge on a negative weight cycle. The shortest path distance is -. 1 s 1 8 t d(s,t)=-  A B -3

6 Shortest Path Algorithms
Dijkstra’s algorithm does NOT allow negative edges. Uses a greedy heuristic. undirected/directed graph Bellman-Ford’s and Floyd’s algorithms work correctly for any graph and can detect negative cycles. Must be directed graph if negative edges are included.

7 Shortest Path Algorithms
Dijkstra’s algorithm does NOT allow negative edges. Uses a greedy heuristic. undirected/directed graph Bellman-Ford’s and Floyd’s algorithms work correctly for any graph and can detect negative cycles. Must be directed graph if negative edges are included. We will study Dijkstra’s algorithm only. The others are beyond the scope of this course

8 Dijkstra’s Algorithm The distance of a vertex v from a vertex s is the length of a shortest path between s and v Dijkstra’s algorithm (pronounced dyke-stra) computes the distances of all the vertices from a given start vertex s Assumptions: the graph is connected the edge weights are nonnegative

9 Dijkstra’s Algorithm d(u)
We grow a “cloud” of vertices, beginning with s and eventually covering all the vertices We store with each vertex u a label d(u) representing the distance of u from s in the subgraph consisting of the cloud and its adjacent vertices At each step We add to the cloud the vertex u outside the cloud with the smallest distance label, d(u) We update the labels d(.) of the vertices adjacent to u s s d(u) 8 A 4 8 A 4 2 2 2 8 7 2 1 4 8 7 1 3 B C D B C D 9 3 9 5 3 11 2 2 E F 5 E F 5

10 Dijkstra’s Algorithm - Edge Relaxation
d(u) = 50 Consider an edge e = (u,z) such that u is the vertex most recently added to the cloud z is not in the cloud The relaxation of edge e updates distance d(z) as follows: d(z) := min{d(z),d(u) + weight(e)} 10 d(z) = 75 e u s z d(u) = 50 10 d(z) = 60 u e s z

11 Example C B A E D F 4 2 8 7 1 5 3 9 11

12 Example C B A E D F 3 2 7 5 8 4 1 9

13 Dijkstra’s Algorithm A priority queue stores the vertices outside the cloud (Key: distance, Element: vertex) Algorithm ShortestPath(G, v) Input A weighted graph G with nonnegative edge weights, and a start vertex v of G Output a label D[u] for each u of G , so that D[u] is the length of a shortest path from v to u in G Initialize D[v] := 0 , and D[u] :=  for every vertex u ≠ v Let a priority queue Q contain all the vertices of G using the D label as a key while Q is not empty do {pull a new vertex u into the cloud} u := Q.removeMin() for each vertex z adjacent to u such that z is in Q do {perform the relaxation on edge (u,z)} if D[u] + w((u,z)) < D[z] then D[z] := D[u] + w((u,z)) change to D[z] the key of vertex z in Q return the label D[u] of each vertex u

14 Analysis of Dijkstra’s Algorithm
Inserting all the vertices in Q with their initial key value can be done in O(n log n) At each iteration of the while loop, we spend O(log n) time to remove vertex u from Q and O(degree (v) log n) time to perform the relaxation procedure on each edge //note that it takes log n to update D[z] in the heap The overall running time for the entire while loop is O(m log n) remember Sv degree(v) = 2m Dijkstra’s algorithm runs in O((n + m) log n) time provided the graph is represented by the adjacency list structure // the use of adjacency list allows us to step through the vertices adjacent to u during the relaxation step in time proportional to their number

15 Why It Doesn’t Work for Negative-Weight Edges
Dijkstra’s algorithm is based on the idea that it adds vertices by increasing distance. If a node with a negative incident edge were to be added late to the cloud, it could mess up distances for vertices already in the cloud. A 8 4 6 7 5 4 B 7 C 1 D -8 5 9 2 5 E F C’s true distance is 1, but it is already in the cloud with d(C)=5!


Download ppt "Shortest Paths."

Similar presentations


Ads by Google