Title: Single Source Shortest Path
1(No Transcript)
2(No Transcript)
3(No Transcript)
4(No Transcript)
5d
Spacing of C d(p, q)d.
6An Example
K3.
74.4 Single-Source Shortest Paths
- Problem Definition
- Shortest paths and Relaxation
- Dijkstras algorithm (can be viewed as a greedy
algorithm)
8Problem Definition
- Real problem A motorist wishes to find the
shortest possible route from Chicago to
Boston.Given a road map of the United States on
which the distance between each pair of adjacent
intersections is marked, how can we determine
this shortest route? - Formal definition Given a directed graph G(V,
E, W), where each edge has a weight, find a
shortest path from s to v for some interesting
vertices s and v. - ssource
- vdestination.
9B
A
Find a shortest path from station A to station
B. -need serious thinking to get a correct
algorithm.
10Shortest path
- The weight of path pltv0,v1,,vk gt is the sum of
the weights of its constituent edges
The cost of the shortest path from s to v is
denoted as ?(s, v).
11Negative-Weight edges
- Edge weight may be negative.
- negative-weight cycles the total weight in the
cycle (circuit) is negative. - If no negative-weight cycles reachable from the
source s, then for all v ?V, the shortest-path
weight remains well defined,even if it
has a negative value. - If there is a negative-weight cycle on some path
from s to v, we define - .
12a
b
-4
h
i
-1
3
2
4
3
c
d
6
8
3
-8
5
11
5
g
0
s
-3
e
f
3
j
2
7
-6
Figure1 Negative edge weights in a directed
graph.Shown within each vertex is
its shortest-path weight from source s.Because
vertices e and f form a negative-weight cycle
reachable from s,they have shortest-path weights
of - . Because vertex g is reachable from a
vertex whose shortest path is - ,it,too,has
a shortest-path weight of - .Vertices such
as h, i ,and j are not reachable from s,and so
their shortest-path weights are , even
though they lie on a negative-weight cycle.
13Representing shortest paths
- we maintain for each vertex v?V , a predecessor
- v that is the vertex in the
shortest path - right before v.
- With the values of , a backtracking process
can give the shortest path. (We will discuss that
after the algorithm is given)
14- Observation (basic)
- Suppose that a shortest path p from a source s to
a vertex v can be decomposed into - s u v
- for some vertex u and path p. Then, the weight
of a shortest path from s to v is
We do not know what is u for v, but we know u is
in V and we can try all nodes in V in O(n)
time. Also, if u does not exist, the edge (s, v)
is the shortest. Question how to find (s, u),
the first shortest from s to some node?
15Relaxation
- The process of relaxing an edge (u,v) consists of
testing whether we can improve the shortest path
to v found so far by going through u and,if
so,updating dv and v. - RELAX(u,v,w)
- if dvgtduw(u,v)
- then dv duw(u,v) (based on
observation) - v u
16u
v
u
v
2
2
5
9
5
6
RELAX(u,v)
RELAX(u,v)
u
v
u
v
2
2
5
7
5
6
(a)
(b)
Figure2 Relaxation of an edge (u,v).The
shortest-path estimate of each vertex is shown
within the vertex. (a)Because dvgtduw(u,v)
prior to relaxation, the value of dv decreases.
(b)Here, dv duw(u,v) before the
relaxation step,so dv is unchanged by
relaxation.
17Initialization
- For each vertex v ? V, dv denotes an upper
bound on the weight of a shortest path from
source s to v. - dv will be ?(s, v) after the execution of the
algorithm. - initialize dv and ?v as follows .
- INITIALIZE-SINGLE-SOURCE(G,s)
- for each vertex v ? VG
- do dv
- v NIL
- ds 0
18Dijkstras Algorithm
- Dijkstras algorithm assumes that w(e)?0 for each
e in the graph. - maintain a set S of vertices such that
- Every vertex v ?S, dv?(s, v), i.e., the
shortest-path from s to v has been found. (Intial
values Sempty, ds0 and dv?) - (a) select the vertex u?V-S such that
- dumin dxx ?V-S. Set
SS?u - (b) for each node v adjacent to u do
RELAX(u, v, w). - Repeat step (a) and (b) until SV.
19Continue
- DIJKSTRA(G,w,s)
- INITIALIZE-SINGLE-SOURCE(G,s)
- S
- Q VG
- while Q
- do u EXTRACT -MIN(Q)
- S S u
- for each vertex v ? Adju
- do RELAX(u,v,w)
20Implementation
- a priority queue Q stores vertices in V-S, keyed
by their d values. - the graph G is represented by adjacency lists.
21u
v
s
y
x
(a)
22u
v
1
10/s
8
10
9
0
s
3
4
6
2
7
5
5/s
8
2
y
x
(b)
(s,x) is the shortest path using one edge. It is
also the shortest path from s to x.
23u
v
1
14/x
8/x
10
9
0
s
3
4
6
2
7
5
7/x
5/s
2
y
x
(c)
24u
v
1
13/y
8/x
10
9
0
s
3
4
6
2
7
5
7/x
5/s
2
y
x
(d)
25 (e)
26u
v
1
9/u
8/x
10
9
0
s
3
4
6
2
7
5
7/x
5/s
2
y
x
(f)
Backtracking v-u-x-s
27- Theorem Consider the set S at any time in the
algorithms execution. For each v?S, the path Pv
is a shortest s-v path. - Proof We prove it by induction on S.
- If S1, then the theorem holds. (Because ds0
and Ss.) - Suppose that the theorem is true for Sk for
some kgt0. - Now, we grow S to size k1 by adding the node v.
-
28- Proof (continue)
- Now, we grow S to size k1 by adding the node v.
- Let (u, v) be the last edge on our s-v path Pv.
- Consider any other path from P s,,x,y, , v.
(red in the Fig.) - Case 1 y is the first node that is not in S and
x?S. - Since we always select the node with the
smallest value d in the algorithm, we have
dv?dy. - Moreover, the length of each edge is ?0.
- Thus, the length of P?dy?dv.
- That is, the length of any path ?dv.
-
y
x
Case 2 such a y does not exist.
dvduw(u, v)?dxw(x, v). That is, the
length of any path ?dv.
s
u
v
Set S
29The algorithm does not work if there are negative
weight edges in the graph
u
-10
2
v
s
1
S-gtv is shorter than s-gtu, but it is longer than
s-gtu-gtv.
30Time complexity of Dijkstras Algorithm
- Time complexity depends on implementation of the
Queue. - Method 1 Use an array to story the Queue
- EXTRACT -MIN(Q) --takes O(V) time.
- Totally, there are V EXTRACT -MIN(Q)s.
- time for V EXTRACT -MIN(Q)s is O(V2).
- RELAX(u,v,w) --takes O(1) time.
- Totally E RELAX(u, v, w)s are required.
- time for E RELAX(u,v,w)s is O(E).
- Total time required is O(V2E)O(V2)
- Backtracking with ? gives the shortest path in
inverse order. - Method 2 The priority queue is implemented as a
adaptable heap. It takes O(log n) time to do
EXTRACT-MIN(Q). The total running time is
O(Elog n ).
31A problem
Let us design a keyboard for a mechanical hand.
The keyboard has 26 letters A, B, , Z
arranged in one row. The hand is always at the
left end of the row and it comes back to the left
end after pressing a key. Assume that we know the
frequency of every letter. Design the order of
the 26 letters in the row such that the average
length of movement of the mechanical hand is
minimized. Prove that your solution is correct.