a shortest path algorithm. motivation given a connected, positive weighted graph find the length of...

18
A Shortest Path Algorithm A Shortest Path Algorithm

Upload: aleesha-douglas

Post on 27-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

A Shortest Path AlgorithmA Shortest Path Algorithm

Page 2: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

MotivationMotivation

Given a connected, positive weighted graph

Find the length of a shortest path from vertex a to vertex z.

Page 3: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Dijkstra’s Shortest Path AlgorithmDijkstra’s Shortest Path Algorithm

Input: A connected, positive weighted graph,vertices a and z

Output: L(z), the length of a shortest path from a to z

1. Dijkstra(w,a,z,L){2. L(a)=03. for all vertices x ≠a4. L(x)=∞5. T=set of all vertices6. while(z є T){7. choose v є T with minimum L(v)8. T=T-{v}9. for each x є T adjacent to v10. L(x)=min{L(x),L(v)+w(v,x)}11. }12. }

Page 4: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Example 8.4.2Example 8.4.2

Find L(z)

b

ze

f

d

c

a

2

2

1

5

43

4 13

7

2

g6

Page 5: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

InitializationInitialization

b

ze

f

d

c

a

2

2

1

5

43

4 13

7

2

g6

∞0

Page 6: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Iteration 1Iteration 1

b

ze

f

d

c

a

2

2

1

5

43

4 13

7

2

g6

∞0

2

1

Page 7: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Iteration 2Iteration 2

b

ze

f

d

c

a

2

2

1

5

43

4 13

7

2

g6

∞0

2

1

4

6

Page 8: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Iteration 3Iteration 3

b

ze

f

d

c

a

2

2

1

5

43

4 13

7

2

g6

4

6

∞0

2

1

4

6

Page 9: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Iteration 4Iteration 4

b

ze

f

d

c

a

2

2

1

5

43

4 16

7

2

g6

4

4

6

6

∞0

2

1

5

Page 10: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Proof of Dijkstra’s AlgorithmProof of Dijkstra’s Algorithm

Basic Step(i=1):

we set L(a)=0, and L(a) is sure the length of a shortest path from a to a. Inductive step: For an arbitrary step i

Suppose for step k<i, L(v) is the length of a shortest path from a to v.

Next, suppose that at the it step we choose v in T with minimum L(v). We will seek a contradiction that if there is a w whose length is less than L(v) then w is not in T.

By way of contradiction, suppose there is a w with L(w)<L(v), wєT. Then, let P be the shortest path from a to w, and let x be the vertex nearest to a on P that is in T and let u be x’s predecessor. The node u must not be in T (because x was the nearest node to a that was in T). By assumption, L(u) was the length of the shortest path from a to u.

Then, L(x) ≤ L(u)+w(u,x) ≤ length of P < L(v). This is a contradiction. So w is not in T.

According to our assumption, every path from a to v has length at least L(v).

a ux w… …

P

Page 11: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Example 2Example 2

b

z

d

c

a

2

3

1

1

1

2

e2

Find L(z)

Page 12: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

InitializationInitialization

b

z

d

c

a

2

3

1

1

2

e2

∞01

Page 13: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Iteration 1Iteration 1

b

z

d

c

a

2

3

1

1

2

e2

∞0

a,2

a,1

1

Page 14: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Iteration 2Iteration 2

b

z

d

c

a

2

3

1

1

2

e2

∞0

a,2

a,1 d,2

1

Page 15: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Iteration 3Iteration 3

b

z

d

c

a

2

3

1

1

2

e2

d,2

∞0

a,2

a,1

b,5

1

Page 16: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Iteration 4Iteration 4

b

z

d

c

a

2

3

1

1

2

e2

d,2

∞0

a,2

a,1

b,5

1e,4

Page 17: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Iteration 5Iteration 5

b

z

d

c

a

2

3

1

1

2

e2

d,2

0

a,2

a,1

b,5

1e,4

Page 18: A Shortest Path Algorithm. Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z

Theorem 8.4.5Theorem 8.4.5

For input consisting of an n-vertex, simple, connected, weighted graph, Dijkstra’s algorithm has worst-case run time Ѳ(n2).

Proof: The while loop will take Ѳ(n2) worst-case running time.