308-203A Introduction to Computing II Lecture 16: Dijkstra - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

308-203A Introduction to Computing II Lecture 16: Dijkstra

Description:

The Travelling Salesman Problem: find a shortest ... A greedy algorithm does not work for the Travelling. Salesman Problem: a. b. c. d. e. 10 ... – PowerPoint PPT presentation

Number of Views:121
Avg rating:3.0/5.0
Slides: 28
Provided by: alfre6
Category:

less

Transcript and Presenter's Notes

Title: 308-203A Introduction to Computing II Lecture 16: Dijkstra


1
308-203AIntroduction to Computing IILecture
16 DijkstrasAlgorithm
Fall Session 2000
2
Graphs with Weighted Edges
Graphs frequently come with extra satellite
data for the vertices or edges. A common
varient is assigning each edge some
positive weight.
Graph G (V, E) weight E ? ? (i.e. a
mapping from edges to real numbers)
3
Example
The paradigmatic example is a map of
highways labelled with distances
Roberval
101
Val-dOr
Chicoutimi
259
318
211
531
Rivière-du-Loup
206
Québec
Trois-Rivières
130
142
158
Montréal
147
Sherbrooke
4
Weight of a Path Problems
For a path, P P ( v1, v2, vi ) The weight
of the path is WP ? weight(vi, v(i1))
5
Shortest-Path Problems
There is a general class of problems on
such graphs involving finding paths of minimal
weight. Some are easy to solve and some are
hard.
6
A Hard Problem
The Travelling Salesman Problem find a
shortest path through a connected graph which
visits all of the vertices exactly once.
This problem is NP-Complete, which means there
is no computationally tractable
solution (according to decades of empirical
evidence, NP-Complete problems require
exponential time to solve)
7
A Not-So-Hard Problem
The Single-Source Shortest Path Problem Given a
vertex, v, find the shortest path from v to every
vertex reachable from v. A nice solution is
Dijkstras algorithm, which is a kind of
algorithm known as a Greedy algorithm.
8
Greedy algorithms
A Greedy algorithm is an algorithm based on
the principle of always taking the choice which
looks best in the short-run. In most cases, this
does not actually yield an optimal long-term
solution (or even a good one)
9
A Greedy Algorithm that Works
Say youre on a long drive. Clearly, the way to
minimize the number of gas stops is to stop at a
gas station only when you know youll run out of
gas before you see another one. This is greedy
because given the choice to stop or not to stop,
you always choose the one which appears to
minimize the total number of stops.
10
A Flawed Greedy Algorithm
A greedy algorithm does not work for the
Travelling Salesman Problem
Assume we start from a
b
c
1
10
a
5
8
3
d
14
e
11
A Flawed Greedy Algorithm
Best greedy choices gives the path P (a,
b, c, d, e)
Weight 30
b
c
1
10
a
5
8
3
d
14
e
12
A Flawed Greedy Algorithm
But I can find a better path P (a, b, e, c,
d)
Weight 26
b
c
1
10
a
5
8
3
d
14
e
13
Dijkstras Algorithm
For the Single-Source Shortest Path Problem,
the Greedy approach actually does work.
Idea 1) Label all vertices with a best-known
distance so far 2) Initialize distance to 0
for the starting vertex and ? for all others
14
Dijkstras Algorithm
Idea (continued) 3) Maintain a list, A, of
unexplored vertices, which is initialized
to all vertices 4) Choose vertices from A to
explore by a greedy strategy always take
the one with minimum distance 5) When you
explore a vertex, update the best known
distance to its neighbors and remove it from A.
15
Dijkstra (pseudocode)
for each v ? V, distance(v) ? distance(
startVertex ) 0 A V // Initialize B
Ø for j 1 to ( V - 1 ) // Grow set B
u Extract-Min( A ) // Greedy choice
B B ? u for each v ? Neighbors( u
) // Update distances Relax(v, u)
16
Helper Routine
Updates are performed by checking whether
u yields a better way to get to v
Relax(Vertex v, Vertex u) newWeight
distance( u ) weight( u, v ) if
(newWeight lt distance( v ) ) distance( v )
newWeight
17
Example
Initialize starting vertex to 0 and all others to
?
5
?
?
0
a
b
c
7
2
1
3
d
e
f
?
?
3
?
8
A a, b, c, d, e, f
B ?
18
Example
Remove minimum element from A and call Relax()
5
5
?
0
a
b
c
7
2
1
3
d
e
f
3
?
3
?
8
A b, c, d, e, f
B a
19
Example
Remove minimum element from A and call Relax()
5
5
?
0
a
b
c
7
2
1
3
d
e
f
3
?
3
?
8
A b, c, d, e, f
B a
20
Example
Now d is the minimum in A
5
5
?
0
a
b
c
7
2
1
3
d
e
f
3
8
3
?
11
8 3 11
A b, c, e, f
B a, d
21
Example
Now b is the minimum in A We find we now have
a better path to e
5
5
?
0
a
b
c
7
2
52 7
1
3
d
e
f
3
8
3
?
7
A c, e, f
B a, b, d
22
Example
Now e is the minimum in A
5
5
7 7 14
0
a
b
c
7
2
1
3
d
e
f
3
8
3
7
7 3 10
A c, f
B a, b, d, e
23
Example
Now f is the minimum in A
5
5
11
0
a
b
c
10 1 11
7
2
1
3
d
e
f
3
8
3
7
10
A f
B a, b, c, d, e
24
Example
Done!
5
5
11
0
a
b
c
7
2
1
3
d
e
f
3
8
3
7
10
A f
B a, b, c, d, e
25
Running time
Like for Depth and Breadth-First Searches, we
execute a body of code once per vertex, and that
code looks only at the neighbors of the vertex it
is run for ? O( V E )
26
Proof of Correctness
Since Greedy Algorithms dont always give
optimal solutions, we must furnish a proof that,
in the particular case of Dijkstras algorithm,
one actually does compute the shortest possible
paths
27
Any questions?
Write a Comment
User Comments (0)
About PowerShow.com