Graph ( II). Shortest path, Minimum spanning tree. GGuy 19-3-2011. Review. DFS (Depth-First-Search) Graph traverse Topological sort BFS (Breadth-First-Search) F inding shortest path. Shortest Path Problem. In a weighted graph G We want to find a path from s to t ,
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
More Related Content
Presentation Transcript
Graph (II) Shortest path, Minimum spanning tree GGuy 19-3-2011
Algorithms Single source? All pairs? Negative weight edge? Negative cycles?
Dijkstra’s algorithm for-each v, d[v] ← ∞ Q.Insert(s,0) while not Q.Empty() do (u, w) = Q.ExtractMin() if (visited[u]) continue; d[u] ← w for-each v where (u, v) in E Q.Insert(v, d[u] + weightuv)
Dijkstra’s algorithm Implement Q using binary heap (priority queue) At most E edges in the heap Time complexity: O(E log (E)) = O(E log (V)) Space complexity: O(V+E)
Bellman Ford for-each v, d[v] ← ∞ d[s] ← 0 Do V-1 times for each edge (u,v) if (d[u] + weightuv< d[v]) d[v] ← d[u] + weightuv
Bellman Ford’s algorithm Assume the shortest path from s->t is P0 P1 P2 … Pm-1 where m < |V| and s is fixed In the 1st iteration, d[P1] is shortest In the 2nd iteration, d[P2] is shortest … In the m-1 iteration, d[Pm-1] is shortest
Bellman Ford’s algorithm After |V|-1 iterations, If there exists edge (u,v) where d[u] + weightuv< d[v] Negative cycle!
Floyd Warshall’s algorithm for all pairs(i,j), d[i][j] ← ∞ for all edges(u,v), d[u][v] ← weightuv for k=1 to V for i=1 to V for j=1 to V d[i][j] = min(d[i][j], d[i][k]+d[k][j])
Floyd Warshall’s algorithm Assume the shortest path from s->t is P0 P1 P2 … Pm-1 for any s,t For example, 5->3->1->2->4->6 i=1: 5->1->6 i=2: 5->1->2->6 i=3: 5->3->1->2->6 i=4: 5->3->1->2->4->6 i=5: 5->3->1->2->4->6 i=6: 5->3->1->2->4->6 We won’t miss any shortest path!
Tree What is a tree? G is connected and acyclic G is connected and |E| = |V| - 1 G is acyclic and |E| = |V| - 1 For any pairs in G, there is a unique path
Kruskal’s algorithm T← empty set of edges Sort the edges in increasing order For each edge e (in increasing order) if T + e does not contain a cycle add e to T