View
216
Download
0
Tags:
Embed Size (px)
Citation preview
Shortest path algorithm
Introduction
The graphs we have seen so far have edges that are unweighted.
Many graph situations involve weighted edges however.
Examples include physical distances between nodes as well as measurements of ‘cost differences’
Map of fly times between various cities (in hours)
2
6
4
34 1
5
2
ab
i
f
e
c
g
d
h
A more complicated network
9
7
2
32 1
0 1 2
3 4
8
4
1
DAM representation
9
7
2
32 1
0 1 2
3 4
8
4
01234
0 1 2 3 4
0 8 - 9 4- 0 1 - -- 2 0 3 -- - 2 0 7- - 1 - 0
1
Shortest path
The shortest path between any two vertices is the one with the smallest sum of the weighted edges
Example: what is the shortest path between vertex 0 and vertex 1?
Shortest path problem
9
7
2
32 1
0 1 2
3 4
8
4
Although itmight seem like the shortestpath is the mostdirect route...
1
Shortest path problem
9
7
2
32 1
0 1 2
3 4
8
4
1
Shortest path problem
9
7
2
32 1
0 1 2
3 4
8
4
1
Shortest path problem
9
7
2
32 1
0 1 2
3 4
8
4
The shortestpath is reallyquite convolutedat times.
1
Shortest path solution
Attributed to Edsger Dijkstra Solves the problem and also determines
the shortest path between a given vertex and ALL other vertices!
It uses a set S of selected vertices and an array W of weights such that W[v] is the weight of the shortest path (so far) from vertex 0 to vertex v that passes through all of the vertices in S.
How it works
If a vertex v is in S the shortest path from v0 to v involves only vertices in S
If v is not in S then v is the only vertex along the path that is not in S (in other words the path ends with an edge from some vertex in S to v.
Clear as mud?
Shortest path problem
9
7
2
32 1
0 1 2
3 4
8
4
1
Step 1
Initially, S contains only v0 W contains the weights of the
single edge (it is the first row of the adjacency matrix)
Example: find the shortest path from vertex 0 to vertex 4 (i.e. find the shortest path from vertex 0 to all other vertices)
Initial setup
W
0
1
2
3
4
0
S
8
9
4
999
0
The approach will be to finda vertex that is not in S and thencheck it against W to see if thereare any places where its additionto a path will shorten the distance.NOTE: 999 indicates no distance is defined
Finding a shortest path
W
0
1
2
3
4
0
S
8
9
4
999
0
V4 is the shortest distance of any vertex from V0. Also note that there CANNOT be anyshorter path from V0 to V4 going throughany other vertex because just getting fromV0 to that vertex is longer than from V0 to V4.(We are assuming that edges cannot have negativeweights).
Shortest path problem
9
7
2
32 1
0 1 2
3 4
8
4
1
Adding to S
W
0
1
2
3
4
0
S
8
9
4
999
0
So, add vertex V4 to S.We now know the shortest distancefrom V0 to one other vertex, V4.
4
Updating W
W
0
1
2
3
4
0
S
8
9
4
999
0
Now see if we can update W bygoing from V0 to V4 to one of the other vertices (V1, V2, V3).See next slides for comparisons.
4
V0 to V4 to V1 = 999
9
7
2
32 1
0 1 2
3 4
8
4
1
V0 to V4 to V2 = 5
9
7
2
32 1
0 1 2
3 4
8
4
1
V0 to V4 to V3 = 999
9
7
2
32 1
0 1 2
3 4
8
4
1
Updating W
W
0
1
2
3
4
0
S
8
9
4
5
0
So, we update W for V2
4
Second iteration
W
0
1
2
3
4
0
S
8
9
4
5
0
Once we have evaluated one vertexwe go looking for another.V0 and V4 are already in S so now welook for the shortest path from V0to another vertex.The shortest this time is V2. (through v4)
4
Adding to S
W
0
1
2
3
4
0
S
8
9
4
5
0
So, add vertex V2 to S.We now know the shortest distancefrom V0 to one other vertex, V2.
4 2
Updating W
W
0
1
2
3
4
0
S
8
9
4
5
0
Now see if we can update W bygoing from V0 to V4 to V2 to one of the other vertices (V1, V3).See next slides for comparisons.
4 2
V0 to V4 to V2 to V1 = 7
9
7
2
32 1
0 1 2
3 4
8
4
1
V0 to V4 to V2 to V3 = 8
9
7
2
32 1
0 1 2
3 4
8
4
1
Updating W
W
0
1
2
3
4
0
S
7
8
4
5
0
Update W to reflect the shorter pathsto each of v1 and v3.
4 2
Third Iteration
W
0
1
2
3
4
0
S
7
8
4
5
0
From S we know that shortest pathsfrom V0 to V4 and V2 have alreadybeen figured out.Now we look for the shortest distancefrom V0 to either V1 or V3.
4 2
Adding to S
W
0
1
2
3
4
0
S
7
8
4
5
0
It is shorter from V0 to V1 so weadd V1 to S
4 2 1
Update W
W
0
1
2
3
4
0
S
7
8
4
5
0
Then we must recompute the distanceto V3 through V1 and compare it towhat is already stored in W.
4 2 1
V0 to V4 to V2 to V1 to V3 = 999
9
7
2
32 1
0 1 2
3 4
8
4
1
Fourth Iteration
W
0
1
2
3
4
0
S
7
8
4
5
0
Only one vertex remains, so add it to S.
4 2 1 3
In conclusion
W
0
1
2
3
4
7
8
4
5
0So, what we have got here is anarray W containing the shortestpaths from V0 to every othervertex in the network.
Pseudocode for shortest path
1. Create a set S that contains only vertex 02. N = number of vertices in G (the graph)3. For v = 0 through N-13.1 W[v] = A[0][v]4. For (step = 2 through N)4.1 Find smallest W[v] with v not in S4.2 Add v to S4.3 For all vertices u not in S4.3.1 If W[u] > W[v] + A[v][u]4.3.1.1 W[u] = W[v] + A[v][u]
Invariant for Steps 2 - N
Invariant: For v not in S, W[v] is the smallestweight of all paths from 0 to v that pass throughonly vertices in S before reaching v. For v in S,W[v] is the smallest weight of all paths from 0 to v(including paths outside of S), and the shortestpath from 0 to v lies entirely in S.