Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topological Sorting and Least-cost Path Algorithms.

Similar presentations


Presentation on theme: "Topological Sorting and Least-cost Path Algorithms."— Presentation transcript:

1 Topological Sorting and Least-cost Path Algorithms

2 Topological Sort ● Topological sort of a dag G = (V, E) ■ Linear ordering A of all vertices from V ■ If edge (u,v)  E => A[u] < A[v] undershorts pants belt shirt tie jacket shoes socks watch socks, undershorts, pants, shoes, watch, shirt, belt, tie, jacket

3 But this is O( |V| 2 ) |V||V| |V||V|

4

5 Topological Sort ● Uses DFS ● Topological-Sort (G) ■ Call DFS (G) to compute finishing times f[v] for each v ■ Output vertices in decreasing order of finishing time ● Time complexity ■ O (|V| + |E|)

6 Example undershorts pants belt shirt tie jacket shoes socks watch 11, 16 12, 15 6, 7 3, 4 2, 5 1, 8 13, 14 17, 18 9, 10 18-socks, 16-undershorts, 15-pants, 14-shoes, 10-watch, 8-shirt, 7-belt, 5-tie, 4-jacket

7 Correctness ● Theorem ■ Topological-Sort(G) produces a topological sort of a directed acyclic graph G ● Proof: ■ Consider any edge (u, v) ○ Suffices to show f[v] < f[u] ■ Three possible types of edges ○ Tree edge, forward edge, cross edge

8 Single-Source Shortest Path ● Problem: given a weighted directed graph G, find the minimum-cost path from a given source vertex s to every other vertex v ■ “Shortest-path” = minimum weight ■ Weight of path is sum of edges ■ E.g., a road map: what is the shortest path from Millersville to Wrightsville?

9 Dijkstra’s Algorithm ● Cannot have negative edge weights ● Similar to breadth-first search ■ Grow a tree gradually, advancing from vertices taken from a queue (priority queue instead of normal queue) ● Also similar to Prim’s algorithm for MST ■ Use a priority queue keyed on d[v]

10

11 B C DA 10 43 2 15 What will be the total running time?

12 Dijkstra’s Algorithm Dijkstra(G) for each v  V d[v] =  ; d[s] = 0; S =  ; Q = V; while (Q   ) u = ExtractMin(Q); S = S U {u}; for each v  u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); p[v] = u; Relaxation Step Note: this is really a call to Q->DecreaseKey() for a priority queue Q B C DA 10 43 2 15

13 Dijkstra’s Algorithm Dijkstra(G) for each v  V d[v] =  ; d[s] = 0; S =  ; Q = V; while (Q   ) u = ExtractMin(Q); S = S U {u}; for each v  u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); p[v] = u; How many times is ExtractMin() called? How many times is DecreaseKey() called? What will be the total running time? O(E lg V) O(V lg V) O(E lg V + V lg V) = O(E lg V) since E <= V 2

14 Relaxation ● Key operation: relaxation ● Let  (u,v) be cost of shortest path from u to v ■ Maintain upper bound d[v] on  (s,v): Relax(u,v,w) { if (d[v] > d[u]+w) then { d[v]=d[u]+w; p[v] = u; } } } 9 5 2 7 5 2 Relax 6 5 2 6 5 2 u v w uv u v w uv

15 Bellman-Ford Algorithm BellmanFord() for each v  V d[v] =  ; d[s] = 0; for i=1 to |V|-1 { for each edge (u,v)  E Relax(u,v, w(u,v)); } for each edge (u,v)  E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w Initialize d[], which will converge to shortest-path value  Relaxation: Make |V|-1 passes, relaxing each edge Test for solution: have we converged yet? Ie,  negative cycle?

16 Bellman-Ford Algorithm BellmanFord() for each v  V d[v] =  ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v)  E Relax(u,v, w(u,v)); for each edge (u,v)  E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w What will be the running time?

17 Bellman-Ford ● Running time: O(VE) ■ Not so good for large dense graphs ■ But a very practical algorithm in many ways ● Note that order in which edges are processed affects how quickly it converges ● A distributed variant of the Bellman–Ford algorithm is used in distance-vector routing protocolsdistance-vector routing protocols


Download ppt "Topological Sorting and Least-cost Path Algorithms."

Similar presentations


Ads by Google