A Shortest Path Algorithm - PowerPoint PPT Presentation

About This Presentation
Title:

A Shortest Path Algorithm

Description:

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 ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 19
Provided by: winlabRu
Category:

less

Transcript and Presenter's Notes

Title: A Shortest Path Algorithm


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

3
Dijkstras 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
  • Dijkstra(w,a,z,L)
  • L(a)0
  • for all vertices x ?a
  • L(x)8
  • Tset of all vertices
  • while(z ? T)
  • choose v ? T with minimum L(v)
  • TT-v
  • for each x ? T adjacent to v
  • L(x)minL(x),L(v)w(v,x)

4
Example 8.4.2
b
c
2
1
2
4
3
2
z
e
d
a
4
3
7
1
6
f
g
5
  • Find L(z)

5
Initialization
8
8
b
c
2
1
2
4
3
2
8
8
z
e
d
a
8
0
4
3
7
1
6
f
g
5
8
8
1.L(a)0
2.L(x)8,x?a
3.Ta,b,c,d,e,f,g,z
6
Iteration 1
2
8
8
b
c
2
1
2
4
3
2
8
8
z
e
d
a
8
0
4
3
7
1
6
f
g
5
8
8
1
1.L(a)minL(x),x?T
2.Tb,c,d,e,f,g,z
3.L(b)2,L(f)1
7
Iteration 2
8
2
b
c
2
1
2
4
3
2
8
8
4
z
e
d
a
8
0
4
3
7
1
6
f
g
5
8
6
1
1.L(f)minL(x),x?T
2.Tb,c,d,e,g,z
3.L(d)4,L(g)6
8
Iteration 3
8
2
4
b
c
2
1
2
4
3
2
4
8
6
z
e
d
a
8
0
4
3
7
1
6
f
g
5
6
1
1.L(b)minL(x),x?T
2.Tc,d,e,g,z
3.L(c)4,L(e)6,L(d)4
9
Iteration 4
4
2
b
c
2
1
2
4
6
2
4
6
z
e
d
a
8
5
0
4
3
7
1
6
f
g
5
6
1
1.L(c)L(d)minL(x),x?T,begin with c first
2.Td,e,g,z
3.L(e)6,L(z)5

We can continue, but L(z) will not change in this
example.
10
Proof of Dijkstras Algorithm
  • Basic Step(i1)
  • 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 klti, 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)ltL(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 xs
    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 lt
    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).

11
Example 2
b
c
3
2
2
z
a
1
1
2
d
e
1
  • Find L(z)

12
Initialization
8
8
b
c
3
2
2
1
z
a
8
0
1
2
d
e
1
8
8
1.L(a)0
2.L(x)8,x?a
3.Ta,b,c,d,e,z
13
Iteration 1
a,2
8
8
b
c
3
2
2
1
z
a
8
0
1
2
d
e
1
8
8
a,1
1.L(a)minL(x),x?T
2.Tb,c,d,e,z
3.L(b)2,L(d)1
14
Iteration 2
8
a,2
b
c
3
2
2
1
z
a
8
0
1
2
d
e
1
8
d,2
a,1
1.L(d)minL(x),x?T
2.Tb,c,e,z
3.L(e)2
15
Iteration 3
8
a,2
b,5
b
c
3
2
2
1
z
a
8
0
1
2
d
e
1
d,2
a,1
1.L(b)minL(x),x?T,(a,2)lt(d,2)
2.Tc,e,z
3.L(c)5,L(e)2
16
Iteration 4
a,2
b,5
b
c
3
2
2
1
z
a
8
0
e,4
1
2
d
e
1
d,2
a,1
1.L(e)minL(x),x?T
2.Tc,z
3.L(z)4
17
Iteration 5
a,2
b,5
b
c
3
2
2
1
z
a
0
e,4
1
2
d
e
1
d,2
a,1
1.L(z)minL(x),x?T
2.Tc
3.L(c)5.Stop.
18
Theorem 8.4.5
  • For input consisting of an n-vertex, simple,
    connected, weighted graph, Dijkstras algorithm
    has worst-case run time ?(n2).
  • Proof The while loop will take ?(n2) worst-case
    running time.
Write a Comment
User Comments (0)
About PowerShow.com