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

ece 242
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

ECE242 L31: Shortest Path Algorithms November 30, 2009

ECE 242 Data Structures

Lecture 31

Shortest Path Algorithms

ECE242 L31: Shortest Path Algorithms November 30, 2009

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
slide-2
SLIDE 2

ECE242 L31: Shortest Path Algorithms November 30, 2009

Shortest Path Problem

° Weight: cost, distance, travel time, hop …

1 3 4 10 5 7 2 5 6 u v

ECE242 L31: Shortest Path Algorithms November 30, 2009

Example – Dijkstra Algorithm °Greedy Algorithm – find shortest path from V0 °Assume all weight of edge >0

1 2 3 4 5 10 2 3 1 2 7 4 9 6 source

node from node V0 to other nodes V1 10 V2 5 V3 ∞ V4 ∞ best

slide-3
SLIDE 3

ECE242 L31: Shortest Path Algorithms November 30, 2009

Example – Dijkstra Algorithm °step 1: find the shortest path from node 0

  • node 2 is selected

1 2 3 4 5 10 2 3 1 2 7 4 9 6 source

node from node V0 to other nodes V1 10 V2 5 V3 ∞ V4 ∞ best V2

ECE242 L31: Shortest Path Algorithms November 30, 2009

Example – Dijkstra Algorithm °step 2: recalculate the path to all other nodes

  • find the shortest path to node 0. Node 4 is selected

1 2 3 4 5 10 2 3 1 2 7 4 9 6 source

node from node V0 to other nodes V1 10 8 V2 5 5 V3 ∞ 14 V4 ∞ 7 best V2 V4

slide-4
SLIDE 4

ECE242 L31: Shortest Path Algorithms November 30, 2009

Example – Dijkstra Algorithm °step 3: recalculate the path to all other nodes

  • find the shortest path to node 3. node 1 is selected

1 2 3 4 5 10 2 3 1 2 7 4 9 6 source

node from node V0 to other nodes V1 10 8 8 V2 5 5 5 V3 ∞ 14 13 V4 ∞ 7 7 best V2 V4 V1

ECE242 L31: Shortest Path Algorithms November 30, 2009

Example – Dijkstra Algorithm °step 3: recalculate the path to all other nodes

  • find the shortest path to node 3. node 3 is selected

1 2 3 4 5 10 2 3 1 2 7 4 9 6 source

node from node V0 to other nodes V1 10 8 8 8 V2 5 5 5 5 V3 ∞ 14 13 9 V4 ∞ 7 7 7 best V2 V4 V1 V3

slide-5
SLIDE 5

ECE242 L31: Shortest Path Algorithms November 30, 2009

Example – Dijkstra Algorithm °Now we get all shortest paths to each node

1 2 3 4 5 10 2 3 1 2 7 4 9 6 source

node from node V0 to other nodes V1 10 8 (0,2) 8 (0,2) 8 V2 5 (0,2) 5 5 5 V3 ∞ 14 (0,2,3) 13

(0,2,4,3)

9 (0,2,1,3) V4 ∞ 7 (0,2,4) 7 7 best V2 V4 V1 V3

ECE242 L31: Shortest Path Algorithms November 30, 2009

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 }

slide-6
SLIDE 6

ECE242 L31: Shortest Path Algorithms November 30, 2009

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.

ECE242 L31: Shortest Path Algorithms November 30, 2009

Complexity For Dijkstra’s Algorithm °For N node graph, E edges

  • Initialization: O(N)
  • While Loop: N iteration, then O(N*N) = O(N2)

°For source u, shortest distance from u to any node

  • v. All pair shortest path algorithm run O(N3) time.
slide-7
SLIDE 7

ECE242 L31: Shortest Path Algorithms November 30, 2009

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.

ECE242 L31: Shortest Path Algorithms November 30, 2009

Dijkstra Algorithm in Java (Part 1)

slide-8
SLIDE 8

ECE242 L31: Shortest Path Algorithms November 30, 2009

Dijkstra Algorithm in Java (Part 2)

ECE242 L31: Shortest Path Algorithms November 30, 2009

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 (v3)

  • 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

slide-9
SLIDE 9

ECE242 L31: Shortest Path Algorithms November 30, 2009

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.

ECE242 L31: Shortest Path Algorithms November 30, 2009

Floyd Warshall Algorithm

slide-10
SLIDE 10

ECE242 L31: Shortest Path Algorithms November 30, 2009

Traveling Salesman Find the shortest path that visits every vertex once, and only once.

1.5 2 5 1.3 199 3 6 0.5 3 1 4

1 2 3 4 5 6 7 8

ECE242 L31: Shortest Path Algorithms November 30, 2009

Traveling Salesman Find the shortest path that visits every vertex once, and only once.

1.5 2 5 1.3 199 3 6 0.5 3 4 1.5 2 5 1.3 199 3 6 0.5 3 1

1 2 3 4 5 6 7 8

slide-11
SLIDE 11

ECE242 L31: Shortest Path Algorithms November 30, 2009

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.

ECE242 L31: Shortest Path Algorithms November 30, 2009

Summary ° Shortest path solutions find lowest cost path from a vertex to other vertices ° Dijkstra’s algorithm effective in location shortest paths

  • O(V2) complexity

° Floyd Warshall Algorithm

  • Locate shortest paths from all vertices to all other vertices

° Very useful algorithms