40 likes | 137 Views
All-Pairs SPs on DG. Run Dijkstra;s algorithm for each vertex or Use DP: Floyd-Warshall O(n 3 ) algorithm. The structure of SP: Intermediate vertices of P={v 1 ,v 2 ,…,v k }: {v 2 ,v 3 ,…,v k-1 } {1,2,…,k-1} notation for vertices.
E N D
All-Pairs SPs on DG • Run Dijkstra;s algorithm for each vertex or • Use DP: Floyd-Warshall O(n3) algorithm. • The structure of SP: • Intermediate vertices of P={v1,v2,…,vk}: • {v2,v3,…,vk-1} • {1,2,…,k-1} notation for vertices. • For any i,j of V consider all paths with intermediate vertices in {1,2,…,k}. • Let P be min-weight path found this way.
All-Pairs SP (cont.) • Relation between P and SP(i,j) with intermediate vertices in {1,2,…,k-1} • K not in P: same • K in P: break P: • Recursive solution: dij(k) is weight of SP(i,j) with interm. vertices in {1,2,…,k}. • dij(k)=wij, if k=0; • dij(k)=min{dij(k-1), dik(k-1)+dkj(k-1)} if k >=1. • Matrix D(n)=dij(n) gives final solution.
SP Bottom-Up Computation • W: wij= 0, if i=j; weight of edge (i,j) if exists; “infinity”, otherwise. D(0)=W; For k=1 to n do for i=1 to n do for j=1 to n do dij(k)=min{dij(k-1),dik(k-1)+dkj(k-1)}. Return D(n).
Constructing a SP • Compute predecessor matrix P(n) together with D(n). • Pij(k): predecesor of j on SP(i,j) with intermediate vertices in {1,2,…,k}. • Pij(0)= nil, if i=j or no edge (i,j). i, if edge (i,j). • K >=1: ikj same predecessor as kj with intermediate vertices in {1,2,…,k-1}. • Pij(k)=Pij(k-1), if dij(k-1) <= dik(k-1)+dkj(k-1) • Pij(k)=Pkj(k-1), if dij(k-1) > dik(k-1)+dkj(k-1)