Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shortest path algorithm. Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges.

Similar presentations


Presentation on theme: "Shortest path algorithm. Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges."— Presentation transcript:

1 Shortest path algorithm

2 Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges however. 4 Examples include physical distances between nodes as well as measurements of ‘cost differences’

3 Map of fly times between various cities (in hours) 2 6 4 3 41 5 2 a b i f e c g d h

4 A more complicated network 9 7 2 3 2 1 012 34 8 4 1

5 DAM representation 9 7 2 3 2 1 012 34 8 4 0123401234 0 1 2 3 4 0 8 - 9 4 - 0 1 - - - 2 0 3 - - - 2 0 7 - - 1 - 0 1

6 Shortest path 4 The shortest path between any two vertices is the one with the smallest sum of the weighted edges 4 Example: what is the shortest path between vertex 0 and vertex 1?

7 Shortest path problem 9 7 2 3 2 1 012 34 8 4 Although it might seem like the shortest path is the most direct route... 1

8 Shortest path problem 9 7 2 3 2 1 012 34 8 4 1

9 9 7 2 3 2 1 012 34 8 4 1

10 9 7 2 3 2 1 012 34 8 4 The shortest path is really quite convoluted at times. 1

11 Shortest path solution 4 Attributed to Edsger Dijkstra 4 Solves the problem and also determines the shortest path between a given vertex and ALL other vertices! 4 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.

12 How it works 4 If a vertex v is in S the shortest path from v0 to v involves only vertices in S 4 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. 4 Clear as mud?

13 Shortest path problem 9 7 2 3 2 1 012 34 8 4 1

14 Step 1 4 Initially, S contains only v0 4 W contains the weights of the single edge (it is the first row of the adjacency matrix) 4 Example: find the shortest path from vertex 0 to vertex 4 (i.e. find the shortest path from vertex 0 to all other vertices)

15 Initial setup W 0 1 2 3 4 0 S 8 9 4 999 0 The approach will be to find a vertex that is not in S and then check it against W to see if there are any places where its addition to a path will shorten the distance. NOTE: 999 indicates no distance is defined

16 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 any shorter path from V0 to V4 going through any other vertex because just getting from V0 to that vertex is longer than from V0 to V4. (We are assuming that edges cannot have negative weights).

17 Shortest path problem 9 7 2 3 2 1 012 34 8 4 1

18 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 distance from V0 to one other vertex, V4. 4

19 Updating W W 0 1 2 3 4 0 S 8 9 4 999 0 Now see if we can update W by going from V0 to V4 to one of the other vertices (V1, V2, V3). See next slides for comparisons. 4

20 V0 to V4 to V1 = 999 9 7 2 3 2 1 012 34 8 4 1

21 V0 to V4 to V2 = 5 9 7 2 3 2 1 012 34 8 4 1

22 V0 to V4 to V3 = 999 9 7 2 3 2 1 012 34 8 4 1

23 Updating W W 0 1 2 3 4 0 S 8 9 4 5 0 So, we update W for V2 4

24 Second iteration W 0 1 2 3 4 0 S 8 9 4 5 0 Once we have evaluated one vertex we go looking for another. V0 and V4 are already in S so now we look for the shortest path from V0 to another vertex. The shortest this time is V2. (through v4) 4

25 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 distance from V0 to one other vertex, V2. 42

26 Updating W W 0 1 2 3 4 0 S 8 9 4 5 0 Now see if we can update W by going from V0 to V4 to V2 to one of the other vertices (V1, V3). See next slides for comparisons. 42

27 V0 to V4 to V2 to V1 = 7 9 7 2 3 2 1 012 34 8 4 1

28 V0 to V4 to V2 to V3 = 8 9 7 2 3 2 1 012 34 8 4 1

29 Updating W W 0 1 2 3 4 0 S 7 8 4 5 0 Update W to reflect the shorter paths to each of v1 and v3. 42

30 Third Iteration W 0 1 2 3 4 0 S 7 8 4 5 0 From S we know that shortest paths from V0 to V4 and V2 have already been figured out. Now we look for the shortest distance from V0 to either V1 or V3. 42

31 Adding to S W 0 1 2 3 4 0 S 7 8 4 5 0 It is shorter from V0 to V1 so we add V1 to S 421

32 Update W W 0 1 2 3 4 0 S 7 8 4 5 0 Then we must recompute the distance to V3 through V1 and compare it to what is already stored in W. 421

33 V0 to V4 to V2 to V1 to V3 = 999 9 7 2 3 2 1 012 34 8 4 1

34 Fourth Iteration W 0 1 2 3 4 0 S 7 8 4 5 0 Only one vertex remains, so add it to S. 4213

35 In conclusion W 0 1 2 3 4 7 8 4 5 0So, what we have got here is an array W containing the shortest paths from V0 to every other vertex in the network.

36 Pseudocode for shortest path 1. Create a set S that contains only vertex 0 2. N = number of vertices in G (the graph) 3. For v = 0 through N-1 3.1 W[v] = A[0][v] 4. For (step = 2 through N) 4.1 Find smallest W[v] with v not in S 4.2 Add v to S 4.3 For all vertices u not in S 4.3.1 If W[u] > W[v] + A[v][u] 4.3.1.1 W[u] = W[v] + A[v][u]

37 Invariant for Steps 2 - N Invariant: For v not in S, W[v] is the smallest weight of all paths from 0 to v that pass through only 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 shortest path from 0 to v lies entirely in S.


Download ppt "Shortest path algorithm. Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges."

Similar presentations


Ads by Google