 # 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.

## Presentation on theme: "1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping."— Presentation transcript:

1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping edges to real valued weights. The weight of a path p = (v, v,..., v ) is  w(v, v ). 12k i=1 k-1 ii+1 The weight of the path along the red edges is 1 + 6 + 1 + 4 = 12. Single source shortest path problem: given a vertex s, for every vertex v  V find a shortest path from s to v.

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. It also maintains, for each vertex v not in S, an upper bound d[v] on the weight of a shortest path from source s to v. Dijkstra’s algorithm solves this problem efficiently for the case in which all weights are nonnegative (as in the example graph). The algorithm repeatedly selects the vertex u  V – S with minimum bound d[u], inserts u into S, and relaxes all edges leaving u (determines if passing through u makes it “faster” to get to a vertex adjacent to u).

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. s Set S = { } initially. 1 234 5 6 0 v d[v]d[v] Also initialize a queue with all the vertices and their upper bounds. Suppose vertex 1 is the source.

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s v 234 5 6 1 0 S = (here we keep the bound with the vertex in S) Remove from the queue the vertex u  V – S with minimum bound d[u], insert u into S,... d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 234 5 6 10 1 5 v Suppose vertex 1 is the source.... and relax the edges from this vertex. 1 0 d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 234 5 6 10 1 5 v Suppose vertex 1 is the source.... reordering the queue according to the new upper bounds. 1 0 d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 234 5 6 10 1 5 v Suppose vertex 1 is the source. Repeat... 1 0 d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 24 5 6 10 5 v Suppose vertex 1 is the source.... remove from queue and put in S 1 0 3 1 d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 24 5 6 9 4 2 v Suppose vertex 1 is the source.... relax the edges... 3 1 1 0 d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 24 5 6 9 4 2 v Suppose vertex 1 is the source.... and reorder the queue... 3 1 1 0 d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 24 5 6 9 4 2 v Suppose vertex 1 is the source. 3 1 1 0 Repeat... d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 24 5 6 9 4 2 v Suppose vertex 1 is the source. 3 1 1 0... remove from queue and put in S d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 24 5 6 9 4 2 4 v Suppose vertex 1 is the source. 3 1 1 0... relax the edges... d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 2 4 5 6 9 4 2 4 v Suppose vertex 1 is the source. 3 1 1 0... and reorder the queue... d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 2 4 5 6 9 4 2 4 v Suppose vertex 1 is the source. 3 1 1 0 Repeat... d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 2 5 4 6 9 2 4 4 v Suppose vertex 1 is the source. 3 1 1 0... remove from queue and put in S d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 2 5 4 6 9 2 4 4 v Suppose vertex 1 is the source. 3 1 1 0... relax the edges (no change in this case)... d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 2 5 4 6 9 2 4 4 v Suppose vertex 1 is the source. 3 1 1 0... and reorder the queue (no change in this case)... d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 2 5 4 6 9 2 4 4 v Suppose vertex 1 is the source. 3 1 1 0 Repeat... d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 2 5 4 6 9 2 4 4 v Suppose vertex 1 is the source. 3 1 1 0... remove from queue and put in S d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 2 5 4 6 9 2 4 4 v Suppose vertex 1 is the source. 3 1 1 0... relax the edges (no change in this case)... d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 2 5 4 6 9 2 4 4 v Suppose vertex 1 is the source. 3 1 1 0 Repeat... d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = 2 5 4 6 92 4 4 v Suppose vertex 1 is the source. 3 1 1 0 Done! d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. s S = Suppose vertex 1 is the source. The result is the bottom row which contains the length of the shortest path from s to the vertex above it. v 2 5 4 6 92 4 4 3 1 1 0 d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = To compute the corresponding paths, we augment the data structure with an additional attribute, p[v], which is the vertex that precedes v in a shortest path. v 2 5 4 6 92 4 4 3 1 1 0 3 1 3 3 5 p[v]p[v] d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = Using the predecessor attribute, p[v], it’s easy to construct the path to v, working backwards from v to s. v 2 5 4 6 92 4 4 3 1 1 0 3 1 3 3 5p[v]p[v] d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = E.g., v = 6. v 2 5 4 6 92 4 4 3 1 1 0 3 1 3 3 5p[v]p[v] d[v]d[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 s S = E.g., v = 6. p = 5, v 2 5 4 6 92 4 4 3 1 1 0 3 1 3 3 5 d[v]d[v] p[v]p[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 3 1 2 6 1 1 8 s S = E.g., v = 6. p = 5, p = 3, v 2 5 4 6 92 4 4 3 1 1 0 3 1 3 3 5 d[v]d[v] p[v]p[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 3 1 2 6 1 1 8 s S = E.g., v = 6. p = 5, p = 3, p = 1. v 2 5 4 6 92 4 4 3 1 1 0 3 1 3 3 5 d[v]d[v] p[v]p[v]

Graph Algorithms: Shortest Path 1 2 3 4 6 5 10 1 5 4 3 3 1 2 6 1 1 8 s S = E.g., v = 6. p = 5, p = 3, p = 1. v 2 5 4 6 92 4 4 3 1 1 0 3 1 3 3 5 Path: 1, 3, 5, 6. Weight: 4. d[v]d[v] p[v]p[v]

Graph Algorithms: Shortest Path PseudoCode Notes: - G is a directed, weighted graph with nonnegative edge weights - Edges of G are represented using an Adjacency List - Q is a Priority Queue. Current distance upper bound (d) is key value in Q DIJKSTRA(G,w,s) INITIALIZE-SINGLE-SOURCE(G,s) S 0 Q V[G] while Q = 0 do u EXTRACT-MIN(Q) S S U {u} for each vertex v in AdjList[u] do RELAX(u,v,w)

Priority queue T T Total time Unsorted Array O(|V|) O(1) O(V 2 + E) = O(V 2 ) Binary heap O(log |V|) O(log |V|) O((V+E) log |V|) = O(E log |V|) EXTRACT-MIN DECREASE-KEY Graph Algorithms: Shortest Path Computing time analysis: look at different implementations of the priority queue, with different costs for the queue operations. EXTRACT-MIN executed how many times? EXTRACT-MIN executed |V| times. DECREASE-KEY is called by RELAX DECREASE-KEY executed how many times? DECREASE-KEY executed total of |E| times. Total time = |V| T + |E| T EXTRACT-MIN DECREASE-KEY

Download ppt "1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping."

Similar presentations