Download presentation
Presentation is loading. Please wait.
1
CS330 Discussion 6
2
Shortest path In lecture, the following problem was discussed:
Input: A graph πΊ(π,πΈ), with edge weights π π , and π , π‘βπ Output: The shortest path (length) from π to π‘ in πΊ and Dijkstraβs algorithm was found to be the optimal algorithm. If instead we want to find the shortest path from π to all vertices, we can still use Dijkstraβs. However, if we want to find the shortest path from all vertices to all vertices, the problem becomes much more difficult.
3
All pairsβ shortest path (APSP)
The problem is defined as follows: Input: A graph πΊ(π,πΈ), with edge weights π π Output: For all π , π‘ pairs, the shortest path (length) from π to π‘ in πΊ To simplify the algorithm, we will also require that no negative cycles exist in the graph. To solve this, weβll use dynamic programming.
4
Breaking down the problem
Suppose we know the shortest paths from all πβπ to all πβπ, which donβt use π as an intermediate vertex. That is, if the shortest path from π to π uses π we donβt know that path, but if the second shortest path doesnβt use π, we know that path. We also know the shortest path from any vertex to π and from π to any vertex, since in neither case is π is an intermediate vertex.
5
Breaking down the problem
Suppose we know the shortest paths from all πβπ to all πβπ, which donβt use π as an intermediate vertex. Thereβs two cases to consider for each π, π pair: π is on the shortest path from π to π. What is the shortest π, π path length here? π isnβt on the shortest path from π to π. What is the shortest π, π path length here? Once we know these two path lengths, which do we pick?
6
Generalization Letβs generalize the previous problem β suppose we know all shortest paths for all π, π pairs using only intermediate vertices in π, a subset of π, and we want to know all the shortest paths using only intermediate vertices in πβͺ{π}. Again, there are two cases: The shortest path using only πβͺ π contains π. What is the shortest π, π path length using only πβͺ π in this case? The shortest path using only πβͺ{π} doesnβt contain π. What is the shortest π, π path length using only πβͺ π in this case?
7
Recursive formulation
Let us define an arbitrary order on the vertices, i.e. let π= {1, 2, 3, β¦π}. Let π(π, π, π) be the shortest path length from π to π using only intermediate vertices {1, 2, 3, β¦π}. Then: If π is on the shortest path, π π, π, π =π π, π, πβ1 +π(π, π, πβ1) Else, π π, π, π =π(π, π, πβ1) Equivalently, π π, π, π = min π π,π,πβ1 , π π, π, πβ1 +π π, π, πβ1 In what order should we solve the subproblems? (i.e. how should we iterate over π,π,π?
8
Base cases For the base cases β if we canβt use any intermediate vertices, then what is the shortest path from π to π? Whatβs the shortest path from a vertex to itself?
9
First version def APSP(G): SP = 3-D array indexed 1 to n, 1 to n, 0 to n For each vertex i: SP[i][i][0] = 0 For each edge (i, j): SP[i][j][0] = e(i,j) For k from 1 to n: For i from 1 to n: For j from 1 to n: SP[i][j][k] = min(SP[i][j][k-1], SP[i][k][k-1]+SP[k][j][k-1]) Return SP[1:n][1:n][n]
10
Analysis Our DP table is clearly size π( π 3 ), and each update takes π(1) time, so the runtime is simply π( π 3 ). This runtime is pretty good β there are various improvements, but they come with nuances. However, we can very easily improve on the spatial complexity.
11
Improving the algorithm
One nice property of this algorithm: If we compute in increasing π order, once we compute π(π, π,π), we donβt need to store π(π, π, πβ 1). We wonβt use π(π, π, πβ1) to compute any π π’, π£, π+1 , π π’, π£, π+2 β¦ because the difference in the third argument is 2 or greater. We wonβt use π(π, π, πβ1) to compute any π(π’, π£, π) unless π=π or π=π, in which case π(π,π,π) and π(π, π, πβ1) are the same. Thus, at any stage in the algorithm, we only need to store π(π, π, π) for the most recent π value.
12
Second version def APSP(G): SP = 2-D array indexed 1 to n, 1 to n For each vertex i: SP[i][i] = 0 For each edge (i, j): SP[i][j] = e(i,j) For k from 1 to n: For i from 1 to n: For j from 1 to n: SP[i][j]= min(SP[i][j], SP[i][k]+SP[k][j]) Return SP
13
Improvements Now, we only require π( π 2 ) space at no cost to correctness or runtime. This is the Floyd-Warshall algorithm. The algorithm was developed in 1962, and until 2014, all improvements on it required the graph to hold specific properties.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.