All Pairs Shortest Paths Eric Price UT Austin CS 331, Spring 2020 Coronavirus Edition CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Talk Outline APSP 1 Problems 2 CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
All Pairs Shortest Paths Given a graph G , find shortest s � t path distance for all s , t ∈ V . CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
All Pairs Shortest Paths Given a graph G , find shortest s � t path distance for all s , t ∈ V . Approaches: CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
All Pairs Shortest Paths Given a graph G , find shortest s � t path distance for all s , t ∈ V . Approaches: ◮ Bellman-Ford for all s : O ( V 2 E ) CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
All Pairs Shortest Paths Given a graph G , find shortest s � t path distance for all s , t ∈ V . Approaches: ◮ Bellman-Ford for all s : O ( V 2 E ) ◮ Dijkstra for all s : O ( VE + V 2 log V ) if nonnegative weights CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
All Pairs Shortest Paths Given a graph G , find shortest s � t path distance for all s , t ∈ V . Approaches: ◮ Bellman-Ford for all s : O ( V 2 E ) ◮ Dijkstra for all s : O ( VE + V 2 log V ) if nonnegative weights ◮ Floyd-Warshall: O ( V 3 ) CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
All Pairs Shortest Paths Given a graph G , find shortest s � t path distance for all s , t ∈ V . Approaches: ◮ Bellman-Ford for all s : O ( V 2 E ) ◮ Dijkstra for all s : O ( VE + V 2 log V ) if nonnegative weights ◮ Floyd-Warshall: O ( V 3 ) ◮ Johnson: O ( VE + V 2 log V ) in general CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Floyd-Warshall 1: function FloydWarshall ( A ) ⊲ A [ u → v ] is cost of u → v edge D ← A ⊲ (or ∞ if no edge) 2: for w ∈ V do 3: for u ∈ V do 4: for v ∈ V do 5: D [ u , v ] ← min( D [ u , v ] , D [ u , w ] + D [ w , v ]) 6: return D 7: CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Floyd-Warshall 1: function FloydWarshall ( A ) ⊲ A [ u → v ] is cost of u → v edge D ← A ⊲ (or ∞ if no edge) 2: for w ∈ V do 3: for u ∈ V do 4: for v ∈ V do 5: D [ u , v ] ← min( D [ u , v ] , D [ u , w ] + D [ w , v ]) 6: return D 7: Takes an adjacency matrix, returns a distance matrix CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Floyd-Warshall 1: function FloydWarshall ( A ) ⊲ A [ u → v ] is cost of u → v edge D ← A ⊲ (or ∞ if no edge) 2: for w ∈ V do 3: for u ∈ V do 4: for v ∈ V do 5: D [ u , v ] ← min( D [ u , v ] , D [ u , w ] + D [ w , v ]) 6: return D 7: Takes an adjacency matrix, returns a distance matrix O ( n 3 ) time CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Floyd-Warshall 1: function FloydWarshall ( A ) ⊲ A [ u → v ] is cost of u → v edge D ← A ⊲ (or ∞ if no edge) 2: for w ∈ V do 3: for u ∈ V do 4: for v ∈ V do 5: D [ u , v ] ← min( D [ u , v ] , D [ u , w ] + D [ w , v ]) 6: return D 7: Takes an adjacency matrix, returns a distance matrix O ( n 3 ) time Lemma Let P = ( s , u 1 , . . . , u k , t ) be a shortest s → t path. After visiting w / ∈ { s , t } , P \ { w } is also a shortest s � t path in D. CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Floyd-Warshall 1: function FloydWarshall ( A ) ⊲ A [ u → v ] is cost of u → v edge D ← A ⊲ (or ∞ if no edge) 2: for w ∈ V do 3: for u ∈ V do 4: for v ∈ V do 5: D [ u , v ] ← min( D [ u , v ] , D [ u , w ] + D [ w , v ]) 6: return D 7: Takes an adjacency matrix, returns a distance matrix O ( n 3 ) time Lemma Let P = ( s , u 1 , . . . , u k , t ) be a shortest s → t path. After visiting w / ∈ { s , t } , P \ { w } is also a shortest s � t path in D. Q: negative edges? CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Floyd-Warshall 1: function FloydWarshall ( A ) ⊲ A [ u → v ] is cost of u → v edge D ← A ⊲ (or ∞ if no edge) 2: for w ∈ V do 3: for u ∈ V do 4: for v ∈ V do 5: D [ u , v ] ← min( D [ u , v ] , D [ u , w ] + D [ w , v ]) 6: return D 7: Takes an adjacency matrix, returns a distance matrix O ( n 3 ) time Lemma Let P = ( s , u 1 , . . . , u k , t ) be a shortest s → t path. After visiting w / ∈ { s , t } , P \ { w } is also a shortest s � t path in D. Q: negative edges? OK CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Floyd-Warshall 1: function FloydWarshall ( A ) ⊲ A [ u → v ] is cost of u → v edge D ← A ⊲ (or ∞ if no edge) 2: for w ∈ V do 3: for u ∈ V do 4: for v ∈ V do 5: D [ u , v ] ← min( D [ u , v ] , D [ u , w ] + D [ w , v ]) 6: return D 7: Takes an adjacency matrix, returns a distance matrix O ( n 3 ) time Lemma Let P = ( s , u 1 , . . . , u k , t ) be a shortest s → t path. After visiting w / ∈ { s , t } , P \ { w } is also a shortest s � t path in D. Q: negative edges? OK Negative cycles? CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Floyd-Warshall 1: function FloydWarshall ( A ) ⊲ A [ u → v ] is cost of u → v edge D ← A ⊲ (or ∞ if no edge) 2: for w ∈ V do 3: for u ∈ V do 4: for v ∈ V do 5: D [ u , v ] ← min( D [ u , v ] , D [ u , w ] + D [ w , v ]) 6: return D 7: Takes an adjacency matrix, returns a distance matrix O ( n 3 ) time Lemma Let P = ( s , u 1 , . . . , u k , t ) be a shortest s → t path. After visiting w / ∈ { s , t } , P \ { w } is also a shortest s � t path in D. Q: negative edges? OK Negative cycles? Check if diagonal < 0 CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Floyd-Warshall 1: function FloydWarshall ( A ) ⊲ A [ u → v ] is cost of u → v edge D ← A ⊲ (or ∞ if no edge) 2: for w ∈ V do 3: for u ∈ V do 4: for v ∈ V do 5: D [ u , v ] ← min( D [ u , v ] , D [ u , w ] + D [ w , v ]) 6: return D 7: Takes an adjacency matrix, returns a distance matrix O ( n 3 ) time Lemma Let P = ( s , u 1 , . . . , u k , t ) be a shortest s → t path. After visiting w / ∈ { s , t } , P \ { w } is also a shortest s � t path in D. Q: negative edges? OK Negative cycles? Check if diagonal < 0 CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Johnson’s algorithm Recall: CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Johnson’s algorithm Recall: ◮ Dijkstra would be great if we had nonnegative edges CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Johnson’s algorithm Recall: ◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R , the graph with weights w ′ ( u → v ) := w ( u → v ) − h ( u ) + h ( v ) . has shortest path distances D ′ [ s , t ] = D [ s , t ] + h ( t ) − h ( s ) . CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Johnson’s algorithm Recall: ◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R , the graph with weights w ′ ( u → v ) := w ( u → v ) − h ( u ) + h ( v ) . has shortest path distances D ′ [ s , t ] = D [ s , t ] + h ( t ) − h ( s ) . Idea: in O ( VE + V 2 log V ), CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Johnson’s algorithm Recall: ◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R , the graph with weights w ′ ( u → v ) := w ( u → v ) − h ( u ) + h ( v ) . has shortest path distances D ′ [ s , t ] = D [ s , t ] + h ( t ) − h ( s ) . Idea: in O ( VE + V 2 log V ), Compute a single h so w ′ ( u → v ) ≥ 0 for all h . 1 CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Johnson’s algorithm Recall: ◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R , the graph with weights w ′ ( u → v ) := w ( u → v ) − h ( u ) + h ( v ) . has shortest path distances D ′ [ s , t ] = D [ s , t ] + h ( t ) − h ( s ) . Idea: in O ( VE + V 2 log V ), Compute a single h so w ′ ( u → v ) ≥ 0 for all h . ( h consistent) 1 CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Johnson’s algorithm Recall: ◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R , the graph with weights w ′ ( u → v ) := w ( u → v ) − h ( u ) + h ( v ) . has shortest path distances D ′ [ s , t ] = D [ s , t ] + h ( t ) − h ( s ) . Idea: in O ( VE + V 2 log V ), Compute a single h so w ′ ( u → v ) ≥ 0 for all h . ( h consistent part 2) 1 CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Johnson’s algorithm Recall: ◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R , the graph with weights w ′ ( u → v ) := w ( u → v ) − h ( u ) + h ( v ) . has shortest path distances D ′ [ s , t ] = D [ s , t ] + h ( t ) − h ( s ) . Idea: in O ( VE + V 2 log V ), Compute a single h so w ′ ( u → v ) ≥ 0 for all h . ( h consistent part 2) 1 Compute D ′ for every source s using Dijkstra on w ′ . 2 CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10
Recommend
More recommend