1 / 40

Algorithms

Algorithms. Bellman-Ford Algorithm Directed-Acyclic Graphs All Pairs-Shortest Path Algorithm. Why does Dijkstra’s greedy algorithm work?. Because we know that when we add a node u to the set S, the value d is the length of the shortest path from s to u.

strom
Download Presentation

Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithms Bellman-Ford Algorithm Directed-Acyclic Graphs All Pairs-Shortest Path Algorithm

  2. Why does Dijkstra’s greedy algorithm work? • Because we know that when we add a node u to the set S, the value d is the length of the shortest path from s to u. • But, this only works if the edges of the graph are nonnegative.

  3. a 10 7 b c -4 Would Dikjstra’s Algorithm work with this graph?

  4. a 6 7 b c -14 What is the length of the shortest path from a to c in this graph?

  5. Bellman-Ford Algorithm • The Bellman-Ford algorithm can be used to solve the general single source shortest path problem • Negative weights are allowed • The algorithm detects negative cycles and returns false if the graph contains one.

  6. BELLMAN-FORD(G,w,s) 1 INITIALIZE-SINGLE-SOURCE(G,s) 2 for i  1 to |V[G]| -1 3 do for each edge (u,v)  E[G] 4 do RELAX(u,v,w) 5 for each edge (u,v)  E[G] 6 do if d[v] > d[u] + w(u,v) 7 then return false 8 return true

  7. When there are no cycles of negative length, what is maximum number of edges in a shortest path when the graph has |V[G]| vertices and |E[G]| edges?

  8. Dynamic Programming Formulation • The following recurrence shows how the Bellman Ford algorithm computes the d values for paths of length k.

  9. a 10 Processing order 1 2 3 (a,c) (b,a) (c,b) (c,d) (d,b) 7 b c -4 -5 9 d

  10. Complexity of the Bellman Ford Algorithm • Time complexity: • Performance can be improved by • stopping adding a test to the loop to see if any d values were updated on the previous iteration, or • maintain a queue of vertices whose d value changed on the previous iteration-only process these on the next iteration

  11. Single-source shortest paths in directed acyclic graphs • Topological sorting is the key to efficient algorithms for many DAG applications. • A topological sort of a dag is a linear ordering of all of its vertices such that if G contains an edge (u,v), then u appears before v in the ordering.

  12. e g d f b c a a d c b e f g

  13. DAG-SHORTEST-PATHS(G,w,s) 1 topologically sort the vertices of G 2 INITIALIZE-SINGLE-SOURCE(G,s) 3 for each vertex u taken in topological order 4 do for each vertex v  Adj[u] 5 do RELAX(u,v,w)

  14. Topological Sorting TOPOLOGICAL-SORT(G) 1 call DFS(G) to compute finishing times f[v] for each vertex v 2 as each vertex is finished, insert it onto the front of a linked list 3 return the linked list of vertices

  15. Depth-first search • Goal: search all edges in the graph one time • Strategy: Search deeper in the graph whenever possible • Edges are explored out of the most recently discovered vertex v that still has unexplored edges leaving it. • Backtrack when a dead end is encountered

  16. Predecessor subgraph • The predecessor subgraph of a depth first search • forms a depth-first forest • composed of depth-first trees • The edges in E are called tree edges

  17. Vertex coloring scheme • All vertices are initially white • A vertex is colored gray when it is discovered • A vertex is colored black when it is finished (all vertices adjacent to the vertex have been examined completely)

  18. Time Stamps • Each vertex v has two time-stamps • d[v] records when v is first discovered (and grayed) • f[v] records when the search finishes examining its adjacency list (and is blackened) • For every vertex u • d[u] < f[u]

  19. Color and Time Stamp Summary • Vertex u is • white before d[u] • gray between d[u] and f[u] • black after f[u] • Time is a global variable in the pseudocode

  20. DFS(G) 1 for each vertex u  V[G] 2 do color[u] white 3 [u] nil 4 time 0 5 do for each vertex u  Adj[u] 6 do if color[u] = white 7 then DFS-VISIT(u)

  21. DFS-VISIT(u) 1 color[u] gray 2 d[u] time time time +1 3 for each vertex v  Adj[u] 4 do if color[v] = white 5 then [v] u 6 DFS-VISIT(v) 7 color[u] black 8 f[u] time time time +1

  22. b e 1/ c a g f d

  23. b 2/ e 1/ c a g f d

  24. b 2/ e 3/ 1/ c a g f d

  25. b 2/ e 3/ 4/ 1/ c a g f d

  26. b 2/ e 3/ 4/5 1/ c a g f d

  27. b 2/ e 3/6 4/5 1/ c a g f d

  28. b 2/7 e 3/6 4/5 1/ c a g f d

  29. b 2/7 e 3/6 8/ 4/5 1/ c a g f d

  30. b 2/7 e 3/6 8/9 4/5 1/ c a g f d

  31. b 2/7 e 3/6 8/9 4/5 1/ c a g 10/ f d

  32. b 2/7 e 3/6 8/9 4/5 1/ c a g 10/ 11/ f d

  33. b 2/7 e 3/6 8/9 4/5 1/ c a g 11/12 10/ f d

  34. b 2/7 e 3/6 8/9 4/5 1/ c a g 11/12 10/13 f d

  35. b 2/7 e 3/6 8/9 4/5 1/14 c a g 11/12 10/13 f d

  36. Running time of DFS • lines 1-3 of DFS • lines 5-7 of DFS • lines 2-6 of DFS-VISIT

  37. Running Time of Topological Sort • DFS • Insertion in linked list

  38. 8 b 7 e 6 4 7 7 10 2 9 5 14 c 6 a g 1 3 12 13 9 f d

  39. Running Time for DAG-SHORTEST-PATHS • Topological sort • Initialize-single source • 3-5 each edge examined one time

More Related