ece 242
play

ECE 242 Data Structures Lecture 31 Shortest Path Algorithms - PDF document

ECE 242 Data Structures Lecture 31 Shortest Path Algorithms November 30, 2009 ECE242 L31: Shortest Path Algorithms Single Source Shortest Path Problem Single source shortest path problem Find the shortest path to all other nodes from a


  1. ECE 242 Data Structures Lecture 31 Shortest Path Algorithms November 30, 2009 ECE242 L31: Shortest Path Algorithms Single Source Shortest Path Problem ° Single source shortest path problem • Find the shortest path to all other nodes from a specific node ° Dijkstra’s shortest path algorithm • Find shortest path greedily by updating distance to all other nodes ° Possible applications • Vacation travel • Network connectivity • Digital circuit analysis November 30, 2009 ECE242 L31: Shortest Path Algorithms

  2. Shortest Path Problem ° Weight: cost, distance, travel time, hop … u 10 4 3 1 5 5 2 v 7 6 November 30, 2009 ECE242 L31: Shortest Path Algorithms Example – Dijkstra Algorithm ° Greedy Algorithm – find shortest path from V0 ° Assume all weight of edge >0 node from node V 0 to other nodes 1 1 3 10 V 1 10 2 3 9 4 6 0 V 2 5 source 7 5 V 3 ∞ 2 4 2 V 4 ∞ best November 30, 2009 ECE242 L31: Shortest Path Algorithms

  3. Example – Dijkstra Algorithm ° step 1: find the shortest path from node 0 • node 2 is selected node from node V 0 to other nodes 1 1 3 10 V 1 10 2 3 9 4 6 0 V 2 5 source 7 5 V 3 ∞ 2 4 2 V 4 ∞ best V 2 November 30, 2009 ECE242 L31: Shortest Path Algorithms Example – Dijkstra Algorithm ° step 2: recalculate the path to all other nodes • find the shortest path to node 0. Node 4 is selected node from node V 0 to other nodes 1 1 3 10 V 1 10 8 2 3 9 4 6 0 V 2 5 5 source 7 5 V 3 ∞ 14 2 4 2 V 4 7 ∞ best V 2 V 4 November 30, 2009 ECE242 L31: Shortest Path Algorithms

  4. Example – Dijkstra Algorithm ° step 3: recalculate the path to all other nodes • find the shortest path to node 3. node 1 is selected node from node V 0 to other nodes 1 1 3 10 V 1 10 8 8 2 3 9 4 6 0 V 2 5 5 5 source 7 5 V 3 ∞ 14 13 2 4 2 V 4 ∞ 7 7 best V 2 V 4 V 1 November 30, 2009 ECE242 L31: Shortest Path Algorithms Example – Dijkstra Algorithm ° step 3: recalculate the path to all other nodes • find the shortest path to node 3. node 3 is selected node from node V 0 to other nodes 1 1 3 10 V 1 10 8 8 8 2 3 9 4 6 0 V 2 5 5 5 5 source 7 5 V 3 ∞ 14 13 9 2 4 2 V 4 ∞ 7 7 7 best V 2 V 4 V 1 V 3 November 30, 2009 ECE242 L31: Shortest Path Algorithms

  5. Example – Dijkstra Algorithm ° Now we get all shortest paths to each node node from node V 0 to other nodes 8 8 V 1 10 8 1 (0,2) (0,2) 1 3 10 5 V 2 5 5 5 2 3 (0,2) 9 4 6 0 14 9 13 source V 3 ∞ 7 5 (0,2,3) (0,2,4,3) (0,2,1,3) 2 4 7 2 V 4 ∞ 7 7 (0,2,4) best V 2 V 4 V 1 V 3 November 30, 2009 ECE242 L31: Shortest Path Algorithms Dijkstra Algorithm Mark source node selected Initialize all distance to Infinite, source node distance to 0. Make source node the current node. While (there is unselected node) { Expand on current node Update distance for neighbors of current node Find an unselected node with smallest distance, and make it current node and mark this node selected } November 30, 2009 ECE242 L31: Shortest Path Algorithms

  6. Pseudo-code For Dijkstra’s Algorithm T = {A} For all vertices if v is adjacent to A D(v) = w(A,v) else D(v) = ∞ ∞ ∞ ∞ Find u not in T such that D(u) is a minimum Add u to T for v not in T and v adjacent to u D(v) = min[D(v), D(u) + w(u,v)] Where: A = source node, T = termination set D(v) = current shortest distance to v w(i,j) = weight of edge connecting i and j * update D(v) if the path to v via u is shorter than the previous shortest path. November 30, 2009 ECE242 L31: Shortest Path Algorithms Complexity For Dijkstra’s Algorithm ° For N node graph, E edges - Initialization: O(N) While Loop: N iteration, then O(N*N) = O(N 2 ) - ° For source u, shortest distance from u to any node v. All pair shortest path algorithm run O(N 3 ) time. November 30, 2009 ECE242 L31: Shortest Path Algorithms

  7. Shortest Paths ° Dijkstra's Single-Source Algorithm • Maintains an array containing the distance to each vertex. • Initially, the distance to the source vertex is 0 and all other distances are infinite. November 30, 2009 ECE242 L31: Shortest Path Algorithms Dijkstra Algorithm in Java (Part 1) November 30, 2009 ECE242 L31: Shortest Path Algorithms

  8. Dijkstra Algorithm in Java (Part 2) November 30, 2009 ECE242 L31: Shortest Path Algorithms Shortest paths between all nodes ° Floyd-Warshall All-Pairs Algorithm Find the shortest path between each pair of vertices • - Could just run Dijkstra's algorithm once from each vertex, using time in � ( v 3 ) • Floyd-Warshall all-pairs takes time in this order, but it is somewhat simpler. - Uses dynamic programming. - Result[i] [j] is the shortest known distance from vertex i to vertex j. Not responsible for Floyd Warshall algorithm on final exam November 30, 2009 ECE242 L31: Shortest Path Algorithms

  9. Floyd Warshall Shortest Path ° When possible intermediate points have been considered the distances are correct. • Two possibilities for each pair of vertices i and j: - The shortest path using vertices 0 through 5 as intermediate points does not involve vertex 5 (It was already correct). - The shortest path using vertices 0 through 5 as intermediate points does involve vertex 5. - Must be result[i][5] + result[5][j]. Neither of these two subpaths can have vertex 5 as an intermediate point, because it is an endpoint. November 30, 2009 ECE242 L31: Shortest Path Algorithms Floyd Warshall Algorithm November 30, 2009 ECE242 L31: Shortest Path Algorithms

  10. Traveling Salesman Find the shortest path that visits every vertex once, and only once. 1 1.5 3 2 7 4 2 5 6 0.5 1 3 3 5 8 1.3 4 199 6 November 30, 2009 ECE242 L31: Shortest Path Algorithms Traveling Salesman Find the shortest path that visits every vertex once, and only once. 1 1.5 1.5 3 3 2 7 4 2 2 5 5 6 6 0.5 0.5 1 3 3 3 5 8 1.3 1.3 4 199 199 6 November 30, 2009 ECE242 L31: Shortest Path Algorithms

  11. Traveling Salesman ° There is no known polynomial-time algorithm for an arbitrarily weighted graph. ° This problem is part of a class of problems known as “NP-complete”. ° Showing that TSP is easy or hard would lead to fame and fortune. November 30, 2009 ECE242 L31: Shortest Path Algorithms Summary ° Shortest path solutions find lowest cost path from a vertex to other vertices ° Dijkstra’s algorithm effective in location shortest paths • O(V 2 ) complexity ° Floyd Warshall Algorithm • Locate shortest paths from all vertices to all other vertices ° Very useful algorithms November 30, 2009 ECE242 L31: Shortest Path Algorithms

Recommend


More recommend