The Floyd-Warshall Algorithm
The Floyd-Warshall Algorithm
Problem:
Given a graph G = (V, E), directed or undirected, weighted with edge costs, find the least cost path from u to v for all pairs of vertices (u, v). We assume all weights are non-negative numbers. The cost of a path will be the sum of the costs of all edges in the path.
Subproblem definition:
We let cost[i, j, k] hold the cost of the least cost path between vertex i and vertex j with intermediate nodes chosen from vertices 1, 2,, k.
So, the least cost for the path from i to j will be cost[i, j, n], that is, we have the option of selecting from all the other nodes different from i and j. (Given edge costs) Base case: cost[i, j, 0] = c(i, j).
cost[i, j, 0] is for the path with no intermediate nodes.
In particular, the least cost path may involve node k or it may not Case 1: The least cost path does not go through node k, then cost[i, j, k] = cost[i, j, k-1]. Case 2: The least cost path does go through node k, then cost[i, j, k] = cost[i, k, k-1] + cost[k, j, k-1].
Floyd-Warshall: Pseudocode
for i := 1 to n do for j := 1 to n do cost[i, j] := c[i, j]; // Let c[u, u] := 0 for k := 1 to n do for i := 1 to n do for j := 1 to n do sum = cost[i, k] + cost[k, j]; if(sum < cost[i, j]) then cost[i, j] := sum;
This code derives the least cost value but there is no recovery of the actual path. This is done by remembering the second vertex of the path found so far:
Floyd-Warshall: Pseudocode
for i := 1 to n do for j := 1 to n do cost[i, j] := c[i, j]; next[i, j] := j; // Note! for k := 1 to n do for i := 1 to n do for j := 1 to n do sum := cost[i, k] + cost[k, j]; if(sum < cost[i, j]) then cost[i, j] := sum; next[i, j] := next[i, k]; // Note! // To write out the path from u to v: w := u; write w; while w != v do w := next[w, v]; write w; Note: Running time (n3).
Dijkstras Algorithm
Objective of Dijkstras algorithm:
Dijkstras algorithm finds the least cost paths from a source vertex v to all the other vertices in the graph.
while Q is non-empty do //Note: Greedy! s := vertex such that cost[s] is a minimum; move vertex s from Q to the cloud set C;
// Update the costs
for all t in Q that are in out(s) do cost_via_s := cost[s] + c[s, t]; if(cost_via_s < cost[t]) then cost[t] := cost_via_s;
2 10 20 25
75 30
2
2 10
20 25
25
75 30
0 S
32 43
0 S
32 43
10
35
35
24
11
12
43 24
11
12
Pull S into the cloud and do relaxation 3
2
2 10
20 25
22
75 30
2
2 10
20 25
22
75 30
0 S
32 43
0 S
32 43
10
10
35
35
43 24
11
12
42 24
11
12
Pull Erid Luin into the cloud and do relaxation 3
10
97 22
75 30
2
2 10
20 25
97 22
75 30
10
0 S
32 43
0 S 66
32 43
10
35
35
42 24
11
12
42 24
11
12
53
Pull Isengard into the cloud and do relaxation 3
2
2 10
20 25
97 22
75
2
2 10
20 25
86 22
75 30
10
0 S 65
32 43
10
30 35
0 S 65
32 43
35
42 24
11
12
42 24
11
12
53
3
53
Pull Mt. Doom into the cloud and do relaxation 3
57
56 1
56 1
86 22
75
2
2 10
20 25
86 22
75 30
4 100
0 S 65
32 43
0 S 65
32 43
10
10
30 35
35
42 24
11
12
42 24
11
12
53
3
53 57
Pull Lorien into the cloud and relax 3
57
Pull The
56 1
56 1
86 22
75
4 90 10
2
2
20 25
86 22
75 30
4 90
10
10
0 S 65
32 43
0 S 65
32 43
30
10
35
35
42 24
11
12
53
3
42 24
11
12
53 57
Pull The Iron Hills into the cloud and we are done
56 1
57
56 1
11
Assumptions
Weights are nonnegative. The cost of a spanning tree is the sum of all the weights of all the edges in T. The Minimum Spanning Tree (MST) is the spanning tree with the smallest possible cost.
Typical application: Connect nodes in a computer network using as little wire as possible (MST links).
12
Kruskals Algorithm
// Sort edges in order of increasing weight // so that w[f[1]] <= w[f[2]] <= ... <= w[f[m]]
T := empty set; for i:=1 to m do let u,v be the endpoints of edge f[i] if there is no path between u and v in T then add f[i] to T return T
Proof by induction:
Base case:
For k = 0 the lemma holds trivially.
Induction step:
13
If we remove ek from TG , then TG becomes disconnected and will have two components (call them A and B). Add ek to T*. This creates a cycle in T* involving vertices in both A and B. So, the cycle must contain an edge e different from ek that has one endpoint in A and one in B. Remove edge e, to obtain a new graph T ==> T is a spanning tree.
Thus, we have proved by induction that for every k there exists a MST that contains each of the edges e1, e2, , ek.
14
Prims Algorithm
Main idea:
Start from an arbitrary single vertex s and gradually grow a tree.
We maintain a set of connected vertices S.
S := {s}; T := empty set; while S <> V do e := (u,v) such that u is in S, v is not in S and w(e) is smallest possible; add v to S; add e to T; return T;
15
Proof by induction:
Base case:
For k = 0 the lemma holds trivially.
Induction step:
Let S be the set of finished vertices after k 1 steps of the algorithm. Add ek to T*. This will create a cycle in T*. The cycle must contain an edge e different from ek that has one endpoint in S and one not in S. Remove edge e and denote the new graph by T . T is a spanning tree.
16
Thus, we have proved by induction that for every k there exists a MST that contains each of the edges e1, e2, , ek.
We do the same set of operations with the cost as in Dijkstra's algorithm: (initialize a structure, decrease values m times, select the minimum n - 1 times). Therefore we get O(n2) time when we implement cost with an array, and O((n + m) log n) when we implement it with a heap.
17
while S<>V do v := vertex which is not in S and has the smallest cost[v]; e := (v, other[v]); add v to S; add e to T;
// Update data structure
for each x not in S if w(v,x) < cost[x] then cost[x] := w(v,x); other[x] := v; return T;
18
Given nodes u and v, we want to choose a route between nodes u and v with the highest reliability.
The reliability of a route is a product of the reliabilities of all its links.
Problem #2
Bridges in Graphs:
Suppose we have a computer network with many links.
We assume the network is currently connected so as to enable communication between any two nodes of the network.
19
Problem #3
The Greyhound bus problem:
Suppose we are given a bus schedule with information for several buses. A bus is characterized by four attributes:
the from-city, the to-city, departure time, arrival time.
Find buses going from city F to city T taking the fastest trip?
Take into account travel and wait times between bus arrivals and depatures..
First, we eliminate an idea that leads to an inadequate solution: Use a graph that has nodes representing cities. Label each edge with the travel time between cities. Now go for the least cost path.
BUT: there is no accounting for wait times! Also, travel times between two cities may vary during the day.
20
Problems #4
The RootBear Problem:
Suppose we have a narrow canyon with perpendicular walls on either side of a forest.
We assume a north wall and a south wall.
Viewed from above we see the A&W RootBear attempting to get through the canyon.
We assume trees are represented by points. We assume the bear is a circle of given diameter d. We are given a list of coordinates for the trees.
Find an algorithm that determines whether the bear can get through the forest.
** * * * * * * * * *
Solution to Problem #1
Reliable network routing:
Suppose we have a computer network with many links. Every link has an assigned reliability.
The reliability is a probability between 0 and 1 that the link will operate correctly.
Given nodes u and v, we want to choose a route between nodes u and v with the highest reliability.
The reliability of a route is a product of the reliabilities of all its links.
21
The route will correspond to a path in the graph. Can we make this look like a shortest path problem? Yes: Since reliability is computed as a product, we will want to change the weights so that an edge is assigned the logarithm of the probability.
Then we sum logs to work with products of probabilities.
To get the best reliability path we want the highest probability of operation which we can derive by finding the least weight path if the assigned weights are negative logarithms of the probability values.
Then we are able to use Dijkstras algorithm.
Solution to Problem #2
Bridges in Graphs:
Suppose we have a computer network with many links.
network is currently connected so as to enable communication between any two nodes of the network.
22
A different approach:
We view both the network nodes and network links as nodes in our graph representation. We connect a link-vertex to a node-vertex if the network link has an endpoint in the network node. Then, a link is critical (i.e. a bridge) if and only if the corresponding link-vertex is an articulation point.
Solution to Problem #3
The Greyhound bus problem:
Suppose we are given a bus schedule with information for several buses. A bus is characterized by four attributes:
the from-city, the to-city, departure time, arrival time.
Find buses going from city F to city T with the fastest trip?
Take into account travel and wait times between arrival and departure times..
First, lets eliminate an idea leading to an inadequate solution: Use a graph that has nodes representing cities. Label each edge with the travel time between cities. Now go for the least cost path.
BUT: there is no accounting for wait times! Also, travel times between two cities may vary during the day.
23
Another approach:
Use a graph in which each vertex is a bus. There will be an edge between busses x and y if and only if: x.to_city = y.from_city and y.departure_time > x.arrival_time. Our time cost for an edge will be: waiting time + travel time on bus y = (y.departure_time x.arrival_time) + (y.arrival_time y.departure_time) = y.arrival_time x.arrival_time.
24
We need two special vertices for the origin and destination cities. There is an edge from origin to bus x if and only if x.from_city = origin. Time cost of this edge is x.arrival_time - x.departure_time. There is an edge from bus y to the destination if and only if y.to_city = destination. The time cost of this edge is 0. We now have a shortest path problem:
Note: the shortest trip is via Toronto with time 6:25 hours.
Solution to Problem #4
The RootBear Problem:
Suppose we have a canyon with perpendicular walls on either side of a forest.
We assume a north wall and a south wall.
Viewed from above we see the A&W RootBear attempting to get through the canyon.
We assume trees are represented by points. We assume the bear is a circle of given diameter d. We are given a list of coordinates for the trees.
Find an algorithm that determines whether the bear can get through the forest.
** * * * * * * * * *
25
Conclusion
Graphs are a very important formalism in computer science. Efficient algorithms are available for many important problems:
exploration, shortest paths, minimum spanning trees, cut links, colouring, etc.
If we formulate a problem as a graph problem, chances are that an efficient non-trivial algorithm for solving the problem is already known. Some problems have a natural graph formulation.
For others we need to choose a less intuitive graph formulation.
Some problems that do not seem to be graph problems at all can be formulated as such.
26