0% found this document useful (0 votes)
9 views35 pages

DM Chapter10 6 2

FDG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views35 pages

DM Chapter10 6 2

FDG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

Section 10.

Shortest-Path Problems
Section Summary
Introduction
A Shortest-Path Algorithm
The Traveling Salesperson Problem
Introduction
consider how an airline system can be modeled. We
set up the basic graph model by representing cities
by vertices and flights by edges.

Problems involving distances can be


modeled by assigning distances between
cities to the edges.
Introduction
Problems involving flight time can be
modeled by assigning flight times to
edges.
Introduction
Problems involving fares can be
modeled by assigning fares to the
edges.
Introduction
Graphs that have a number assigned to
each edge are called weighted graphs.

To be more specific, let the length of a


path in a weighted graph be the sum of
the weights of the edges of this path.
A Shortest-Path Algorithm
There are several different algorithms
that find a shortest path between two
vertices in a weighted graph. greedy algorithm
贪心算法

We will present a greedy algorithm


discovered by the Dutch mathematician Edsger
Dijkstra in 1959.
The version we will describe solves this
problem in undirected weighted graphs where
all the weights are positive.
Edsger Dijkstra 艾兹赫尔 · 戴克斯特拉 , 荷兰计算
机科学家,曾获得 1972 年图灵奖
A Shortest-Path Algorithm
Example What is the length of a shortest
path between a and z in the weighted graph
shown below?
b 3 c
4 2
a 3 z
1
2
3
d e
A Shortest-Path Algorithm
Solution Although a shortest path is easily
found by inspection, we will develop some
ideas useful in understanding Dijkstra’s
algorithm.
We will solve this problem by finding the
length of a shortest path from a to
successive vertices, until z is reached.

To be
A Shortest-Path Algorithm

The only paths b 3 c


starting at a that 4 2
contain no vertex
other than a are 0 a 3 z
formed by adding an 1
2
edge that has a as
3
one endpoint. These d e
paths have only one
edge.

To be
A Shortest-Path Algorithm

b 3 c
They are a, b of
4 2
length 4 and a, d of
length 2. 0 a 3 z
It follows that d is the
1
closest vertex to a, 2
and the shortest path 3
from a to d has length
d e
2.

To be
A Shortest-Path Algorithm
We can find the c
b 3
second closest vertex
by examining all 4 2
paths that begin with 0
a 3 z
the shortest path
1
from a to a vertex in 2
the set {a, d}, 3
followed by an edge d e
that has one endpoint
in {a, d} and its other
endpoint not in this
set. To be
A Shortest-Path Algorithm

There are two such b 3 c


paths to consider, a, d, 4 2
e of length 5 and a, b
of length 4. 0 a 3 z
Hence, the second 1
closest vertex to a is b 2
and the shortest path 3
d e
from a to b has length
4.

To be
A Shortest-Path Algorithm
To find the third closest
vertex to a, we need b 3 c
examine only the paths 4 2
that begin with the
shortest path from a to 0 a 3 z
a vertex in the set {a, 1
2
d, b}, followed by an
3
edge that has one d e
endpoint in the set {a,
d, b} and its other
endpoint not in this set.

To be
A Shortest-Path Algorithm
There are three such
paths, a, b, c of length b 3 c
7, a, b, e of length 7, 4 2
and a, d, e of length 5.
Because the 0 a 3 z
shortest of these paths 1
2
is a, d, e, the third
3
closest vertex to a is e d e
and the length of the
shortest path from a to
e is 5.

To be
A Shortest-Path Algorithm
To find the fourth
closest vertex to a, we
need examine only the b 3 c
paths that begin with 4 2
the shortest path from a
to a vertex in the set {a,0 a 3 z
d, b, e}, followed by an 1
2
edge that has one 3
endpoint in the set {a, d e
d, b, e} and its other
endpoint not in this set.

To be
A Shortest-Path Algorithm
There are two such
paths, a, b, c of length b 3 c
7 and a, d, e, z of 4 2
length 6. Because the
0 a 3 z
shorter of these paths
is a, d, e, z, the fourth 1
2
closest vertex to a is z 3
and the length of the d e
shortest path from a to
z is 6.
A Shortest-Path Algorithm
We will now consider the general problem of
finding the length of a shortest path between a
and z in an undirected connected simple weighted
graph.

Dijkstra’s algorithm proceeds by finding the


length of a shortest path from a to a first vertex,
the length of a shortest path from a to a second
vertex, and so on, until the length of a shortest
path from a to z is found.
A Shortest-Path Algorithm
As a side benefit, this algorithm is easily
extended to find the length of the shortest
path from a to all other vertices of the
graph, and not just to z.

We now give the details of Dijkstra’s


algorithm.
A Shortest-Path Algorithm
A Shortest-Path Algorithm
A Shortest-Path Algorithm
A Shortest-Path Algorithm
THEOREM 1 Dijkstra’s algorithm finds the length of a
shortest path between two vertices in a connected
simple undirected weighted graph.
Take as the inductive hypothesis the following
assertion: At the kth iteration
(i ) the label of every vertex v in S is the length
of a shortest path from a to this vertex, and
(ii ) the label of every vertex not in S is the
length of a shortest path from a to this vertex
that
contains only (besides the vertex itself)
vertices in S.
A Shortest-Path Algorithm
Example: Use Dijkstra’s algorithm to find the
length of a shortest path between the
vertices a and z in the weighted graph
displayed in Figure (a).5
b d
4 6
8
a 1 2 z
2 3
10
c e
(a)
A Shortest-Path Algorithm
b 5 d
∞ ∞
4 6
8
a 0 1 2 ∞ z
2 3
10
c ∞ ∞ e
(a)
A Shortest-Path Algorithm
[a]
b 5 d
4 ∞
4 6
8
a 0 1 2 ∞ z
2 3
10
c 2 ∞ e
[a] (b)
A Shortest-Path Algorithm
[a, c] [a, c]
b 5 d
3
4 10

4 6
8
a 0 1 2 ∞ z
2 3
10
c 2 12
∞ e
[a] (c) [a, c]
(b)
A Shortest-Path Algorithm
[a, c] [a, c,
b 5
8 d
b]
10
3
4 6
8
a 0 1 2 ∞ z
2 3
10
c 2 12 e
[a] (d) [a, c]
(c)
A Shortest-Path Algorithm
[a, c] [a, c,
b 5 b] d
3 8
4 6
8
a 0 1 2 14
∞ z
[a, c, b,
2 3 d]
10
c 2 10
12 e
[a] (e) [a, c, b,
(d)
d]
A Shortest-Path Algorithm
[a, c] [a, c,
b 5 b] d
3 8
4 6
8
a 0 1 2 14
13 z
[a, c, b, d, e]
2 3
10
c 2 10 e
[a] (e)
(f) [a, c, b,
d]
A Shortest-Path Algorithm
[a, c] [a, c,
b 5 b] d
3 8
4 6
8
a 0 1 2 14 z
[a, c, b, d, e]
2 3
10
c 2 10 e
[a] (g) [a, c, b,
d]
The Traveling Salesperson Proble
Consider the following problem: A traveling
salesperson wants to visit each of n cities exactly
once and return to his starting point. In which
order should he visit these cities to travel the
minimum total distance?
The Traveling Salesperson
Problem
The most straightforward way to solve an
instance of the traveling salesperson problem
is to examine all possible Hamilton circuits
and select one of minimum total length.

How many circuits


do we have to
examine to solve the
problem if there are This grows extremely rapidl
n vertices in the
graph?
The Traveling Salesperson
Problem
However, no algorithm with polynomial worst-
case time complexity is known for solving this
problem.
Furthermore, if a polynomial worst-case time
complexity algorithm were discovered for the
traveling salesperson problem, many other difficult
problems would also be solvable using polynomial
worst-case time complexity algorithms.
The Traveling Salesperson
Problem
A practical approach to the traveling
salesperson problem when there are many
vertices to visit is to use an approximation
algorithm.
These are algorithms that do not necessarily
produce the exact solution to the problem but
instead are guaranteed to produce a solution
that is close to an exact solution.

You might also like