Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm 09 B: Graph Algorithms II CS1102S: Data Structures and Algorithms Martin Henz March 19, 2010 Generated on Thursday 18 th March, 2010, 00:21 CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 1
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm 1 Review: Graphs, Shortest Path 2 Unweighted Shortest Paths 3 Dijkstra’s Algorithm 4 Correctness and Complexity of Dijkstra’s Algorithm CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 2
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm 1 Review: Graphs, Shortest Path 2 Unweighted Shortest Paths 3 Dijkstra’s Algorithm 4 Correctness and Complexity of Dijkstra’s Algorithm CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 3
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Graph, Vertices, Edges Graph A graph G = ( V , E ) consists of a set of vertices , V , and a set of edges , E . CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 4
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Graph, Vertices, Edges Graph A graph G = ( V , E ) consists of a set of vertices , V , and a set of edges , E . Edge Each edge is a pair ( v , w ) , where v , w ∈ V . CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 5
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Graph, Vertices, Edges Graph A graph G = ( V , E ) consists of a set of vertices , V , and a set of edges , E . Edge Each edge is a pair ( v , w ) , where v , w ∈ V . Directed graph If the pairs are ordered, then the graph is directed . CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 6
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Graph, Vertices, Edges Graph A graph G = ( V , E ) consists of a set of vertices , V , and a set of edges , E . Edge Each edge is a pair ( v , w ) , where v , w ∈ V . Directed graph If the pairs are ordered, then the graph is directed . Weight Sometimes the edges have a third component, knows as either a weight or a cost . Such graphs are called weighted graphs . CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 7
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Paths Path A path in a graph is a sequence of vertices w 1 , w 2 , w 3 , . . . , w N such that ( w i , w i + 1 ) ∈ E for 1 ≤ i < N . It is said to lead from w 1 ot w N . CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 8
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm The Shortest Path Problem Input Weighted graph: associated with each edge ( v i , v j ) is a cost c i , j to traverse the edge. CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 9
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm The Shortest Path Problem Input Weighted graph: associated with each edge ( v i , v j ) is a cost c i , j to traverse the edge. Weighted path length Cost of path v 1 v 2 · · · v N is � N − 1 i = 1 c i , i + 1 . CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 10
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Single-Source Shortest-Path Problem Problem Given as input a weighted graph, G = ( V , E ) , and a distinguished vertex, s , find the shortest weighted path from s to every other vertex in G . CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 11
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Example CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 12
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Example Shortest path from v 1 to v 6 CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 13
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Example Shortest path from v 1 to v 6 has a cost of 6 and goes from v 1 to v 4 to v 7 to v 6 . CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 14
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Unweighted Shortest Paths: Example Find the shortest path from v 3 to all other vertices CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 15
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Idea Level-order traversal Start with s (distance 0) and proceed in phases currDist, each time going through all vertices. If vertex is “known” and has distance currDist, set the distance of its neighbors to currDist + 1. CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 16
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Implementation CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 17
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Inefficiency Careless loop In each phase, we go through all vertices. We can remember the “known” vertices in a data structure. CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 18
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Inefficiency Careless loop In each phase, we go through all vertices. We can remember the “known” vertices in a data structure. Suitable data structure Queue: will contain the vertices in order of increasing distance CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 19
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Implementation CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 20
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Dijkstra’s Algorithm: Idea Idea Treat nodes in the order of shortest distance CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 21
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Dijkstra’s Algorithm: Idea Idea Treat nodes in the order of shortest distance CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 22
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Example Initial configuration: CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 23
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Example After v 1 is declared known: CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 24
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Example After v 4 is declared known: CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 25
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Example After v 2 is declared known: CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 26
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Example After v 5 and then v 3 are declared known: CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 27
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Example After v 7 is declared known: CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 28
Review: Graphs, Shortest Path Unweighted Shortest Paths Dijkstra’s Algorithm Correctness and Complexity of Dijkstra’s Algorithm Example After v 6 is declared known: CS1102S: Data Structures and Algorithms 09 B: Graph Algorithms II 29
Recommend
More recommend