Finding the shortest p paths between one vertex to the other g p vertices in the graph
0 of 120
1
0
0
2
0
3
. Finding the shortest paths between any pair of vertices 3.The SSSP problem is about… The SSSP problem is about
1. Finding the shortest path between a pair of vertices in the graph 2.

Let s move on the next topic Let’s move on the next topic
ALL‐PAIRS SHORTEST PATHS
.

4. 5.
0
4
0
5
. (each clicker can select up to 3 times) I have not heard about this APSP problem or its solution before solution before I know this problem and its four liner Floyd Warshall s four liner Floyd Warshall’s solution I used it for PS4 :O I used it for PS7 :O I know how Floyd I know how Floyd Warshall’s algorithm works.What is your knowledge level about APSP now? b t APSP ?
1.
3. not just how to code that 0 0 0 four lines…
0 of 120
1 2 3
2.

Motivating Problem 2 g
Analyzing the average number of clicks to browse WWW
• In year 2000. only 19 clicks are necessary to move from any page on the WWW to any other page :O
– That is. then the average path length between arbitrary pairs of vertices i th i f ti in the graph is 19 h i 19 – For example. 12 = 1 20 = 1. 02 = 1 0 0 . the average path length between arbitrary pair of vertices in this graph below is: pair of vertices in this graph below is:
• • • • 01 = 1.333
1 2
. 10 = 2. if the pages on the web are viewed as vertices in a graph. 21 = 2 Average = (1+1+2+1+1+2) / 6 = 8 / 6 = 1.

Motivating Problem 3 g
Finding the best meeting point
• Given a weighted graph that model a city and the travelling time between various places in that city
– Find the best meeting point for two persons (there are lots of such queries). one is currently in A and the other is in B – For example. they meet  • After 12 units of time. they meet  A
0
10
1
1
7
25 8
2
4
B
3
2
. the best meeting point between two persons currently in A = 0 and B = 3 is at vertex 2
• Bj t B just need 1 unit of time to walk from 32 and then wait for A d 1 it f ti t lk f 32 d th it f A • A needs 12 units of time to walk from 02 After units of time.

we can only solve the APSP problem for small graph. j++) // then j D[i][j] = Math. D[i][k] + D[k][j]).
• O(V3) i ) since we have three nested loops! h th t dl !
– Apparently. i++) // before i for (int j = 0. k++) // remember. if we only given a short amount of time. it contains the weight of path(i. k first for (int i = 0. j) After Floyd Warshall’s i i h i h f h(i j) – It is usually a nice algorithm for the pre‐processing part 
for (int k = 0.Floyd Warshall s Sneak Preview Floyd Warshall’s – Sneak Preview
• W We use an Adjacency Matrix: D[|V|][|V|] Adj M i
– Originally D[i][j] contains the weight of edge(i. j < V. i < V. j)  O(1) – Af Fl d W h ll’ stop.min(D[i][j]. k < V. as none of the only solve the APSP problem for small graph as none of the APSP solution shown here runs better than O(V3)…
.

Preprocessing + (Lots of) Queries Preprocessing + (Lots of) Queries
• This is another problem solving technique • Preprocess the data once (can be a costly operation) • But then future queries can be (much) faster by working on the processed data by working on the processed data • Example with APSP problem:
– Once we have pre processed the APSP information with Once we have pre‐processed the APSP information with O(V3) Floyd Warshall’s algorithm… – Future queries that asks “what is the shortest path weight Future queries that asks what is the shortest path weight between vertex i and j” can now be answered in O(1)…
.

j. iteratively add k by one until k = V ‐ 1
k j i
. we only use direct edges only)
– Then..Floyd Warshall s Basic Idea (1) Floyd Warshall’s – Basic Idea (1)
• Assume that the vertices are labeled as [0 . k]
– How Robert Floyd and Stephen Warshall managed to arrive at this How Robert Floyd and Stephen Warshall managed to arrive at this formulation is beyond this lecture…
• Initially k = ‐1 (or to say. V ‐ 1]. • Now let sp(i. k) denotes the shortest path between vertex i and vertex j with the restriction that the vertices on the shortest path t j ith th t i ti th t th ti th h t t th (excluding i and j) can only consist of vertices from [0 ..

. ‐> p[i][j] ‐> j – Initially. i.Variant 1 – Print the Actual SP (1) Variant 1 Print the Actual SP (1)
• We have learned to use array/Vector p (predecessor/ parent) to record the BFS/DFS/SP Spanning Tree
– But now. i ‐> . then D[i][j] = D[i][k] + D[k][j] and p[i][j] = p[k][j]  this will be the last vertex before j in the shortest path th h t t th
.e. we are dealing with all‐pairs of paths :O
• Solution: Use predecessor matrix p
– let p be a 2D predecessor matrix. p[i][j] = i for all pairs of i and j – If D[i][k] + D[k][j] < D[i][j]. where p[i][j] is the last vertex before j on a shortest path from i to j..

min(D[i][j].Variant 3 – Variant 3 Minimax/Maximin (1)
• The minimax problem is a problem of finding the minimum of maximum edge weight along all possible paths from vertex i to vertex j (maximin is the reverse)
– For a single path from i to j. we pick the maximum edge weight along this path – Then.max(D[i][k]. INF otherwise p per normal // the three nested loops as p D[i][j] = Math. Math. for all possible paths from i to j.
. a modification of Floyd Warshall’s
// Initially: D[i][i] = 0 // D[i][j] = weight of edge(i. D[k][j])). we pick the one with the minimum max‐edge‐weight th i i d i ht
• Solution: Again. j) exist.

Java Implementations Java Implementations
• Let’s see: FloydWarshallDemo.java • Let’s see how easy to change the basic form of Floyd Warshall’s algorithm to its variants • Note: That Java file is not written in OOP fashion… Note: That Java file is not written in OOP fashion…
– Please re‐factor it if you need to use it for other projects!
.