Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shortest paths and transitive closure Data structure 2002/12/4.

Similar presentations


Presentation on theme: "Shortest paths and transitive closure Data structure 2002/12/4."— Presentation transcript:

1 Shortest paths and transitive closure Data structure 2002/12/4

2 Is there a path? How short it can be?  Single source/ All destinations –Nonnegative edge costs –General weights  All-pairs shortest path  Transitive closure

3 Single source all destinations Dijkstra’s algorithm  A spanning tree again  For nonnegative edge costs (Why?)  Start from a vertex v, greedy method  dist[w]: the shortest length to w through S v u w length( ) dist[w] dist[u]

4 shortest() Void shortestpath(int v, int cost[][MAX_VERTICES], int dist [], int n, int found[]) { int i,u,w; for (i=0;i

5 An example San Francisco Los Angeles Denver Chicago Boston New York Miami New Orleans

6 Single source all destinations BellmanFord algorithm  For general weights  Path has at most n-1 edges, otherwise…  dist k [u]: from v to u, has at most k edges 

7 BellmanFord() Void BellmanFord(int n, int v) { int i,k; for (i =0;idist[i]+length[i][u]) dist[u]=dist[i]+length[i][u]; } O(n^3)

8 All-Pairs shortest paths  執行 n 次 single source all destinations algo.  Dynamic programming strategy – 利用 recursive formula 來表示 – 好好地 implement recursive formula, 用 table 輔助  A k [i][j] ≡ shortest length from i to j through no intermediate vertex greater than k  A -1 [i][j] = length[i][j]  A k [i][j] = min{A k-1 [i][j], A k-1 [i][k]+ A k-1 [k][j], k≥0

9 AllLengths() Void AllLength(int n) { int i,j,k; for(i=0;i

10 Transitive closure  Definition: transitive closure matrix, A + –A + [i][j] = 1, if there’s a path of length > 0 from i to j –A + [i][j] = 0, otherwise  Definition: reflexive transitive closure matrix, A* –A*[i][j] = 1, if there’s a path of length >= 0 from i to j –A*[i][j] = 0, otherwise  O(n^3), by AllLengths()  O(n^2), an undirected graph, by connected check


Download ppt "Shortest paths and transitive closure Data structure 2002/12/4."

Similar presentations


Ads by Google