shortest path algorithm. introduction 4 the graphs we have seen so far have edges that are...

37
Shortest path algorithm

Post on 21-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

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

Shortest path algorithm

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

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’

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

Map of fly times between various cities (in hours)

2

6

4

34 1

5

2

ab

i

f

e

c

g

d

h

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

A more complicated network

9

7

2

32 1

0 1 2

3 4

8

4

1

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

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

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

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?

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

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

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

Shortest path problem

9

7

2

32 1

0 1 2

3 4

8

4

1

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

Shortest path problem

9

7

2

32 1

0 1 2

3 4

8

4

1

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

Shortest path problem

9

7

2

32 1

0 1 2

3 4

8

4

The shortestpath is reallyquite convolutedat times.

1

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

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.

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

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?

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

Shortest path problem

9

7

2

32 1

0 1 2

3 4

8

4

1

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

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)

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

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

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

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).

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

Shortest path problem

9

7

2

32 1

0 1 2

3 4

8

4

1

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

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

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

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

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

V0 to V4 to V1 = 999

9

7

2

32 1

0 1 2

3 4

8

4

1

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

V0 to V4 to V2 = 5

9

7

2

32 1

0 1 2

3 4

8

4

1

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

V0 to V4 to V3 = 999

9

7

2

32 1

0 1 2

3 4

8

4

1

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

Updating W

W

0

1

2

3

4

0

S

8

9

4

5

0

So, we update W for V2

4

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

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

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

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

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

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

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

V0 to V4 to V2 to V1 = 7

9

7

2

32 1

0 1 2

3 4

8

4

1

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

V0 to V4 to V2 to V3 = 8

9

7

2

32 1

0 1 2

3 4

8

4

1

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

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

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

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

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

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

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

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

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

V0 to V4 to V2 to V1 to V3 = 999

9

7

2

32 1

0 1 2

3 4

8

4

1

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

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

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

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.

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

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]

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

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.