Академический Документы
Профессиональный Документы
Культура Документы
s 0
x
9
6
4
2
5
y
11
z
Example
1
2
16
3
5
10
4
2
3
14
Example
1
2
16
3
5
10
4
2
3
14
Shortest-Path Variants
Single-source single-destination (1-1): Find the shortest
path from source s to destination v.
Single-source all-destination(1-Many): Find the shortest
path from s to each vertex v.
Single-destination shortest-paths (Many-1): Find a shortest
path to a given destination vertex t from each vertex v.
All-pairs shortest-paths problem (Many-Many): Find a
shortest path from u to v for every pair of vertices u and v.
We talk about directed, weighted graphs
2
2
3
4
0 s
Shortest-Path Variants
No need to consider different solution or algorithm for each variant
(we reduce it into two types)
Shortest-Path Variants
Single-Source Single-Destination (1-1)
Shortest-Path Variants
Single-Source Single-Destination (1-1)
Introduction
Generalization of BFS to handle weighted graphs
Direct Graph G = ( V, E ), edge weight fn ; w : E R
In BFS w(e)=1 for all e E
Weight of path p = v1 v2 vk is
k 1
w( p) w(vi , vi 1 )
i 1
Shortest Path
Shortest Path = Path of minimum weight
(u,v)=
min{(p) : u
Several Properties
Let (1,k) = <v1, ..i, .. j.., .. ,vk > be a shortest path from v1 to vk
Let (i,j) = <vi, ... ,vj > be subpath of (1,k) from vi to vj
for any i, j
Then (I,j) is a shortest path from vi to vj
v0
v1
v2
v3
v4
v5
v6
v7
3- Negative-weight edges
No problem, as long as no negative-weight cycles are
reachable from the source (allowed)
4- Triangle Inequality
Lemma 1: for a given vertex s in V and for every edge (u,v) E,
(s,v) (s,u) + w(u,v)
Proof: shortest path s v is no longer than any other path.
in particular the path that takes the shortest path s v and
then takes cycle (u,v)
u
s
Algorithms to Cover
Dijkstras Algorithm
Shortest Path is DAGs
Initialization
Maintain d[v] for each v in V
d[v] is called shortest-path weight estimate
and it is upper bound on (s,v)
INIT(G, s)
for each v V do
d[v]
[v] NIL
d[s] 0
Upper bound
Parent node
Relaxation
When you find an edge (u,v) then
check this condition and relax d[v] if
possible
RELAX(u, v)
if d[v] > d[u]+w(u,v) then
d[v] d[u]+w(u,v)
[v] u
u
Change d[v]
No chnage
Algorithms to Cover
Dijkstras Algorithm
Shortest Path is DAGs
Like BFS: If all edge weights are equal, then use BFS,
otherwise use this algorithm
v
1
Example
u
10
s
Example
DIJ KSTRA(G, s)
INIT(G, s)
S
> set of discovered nodes
10
QV[G]
while Q do
uEXTRACT-MIN(Q)
s
SS U {u}
for each v in Adj[u] do
5
RELAX(u, v) > May cause
>DECREASE-KEY(Q, v, d[v])
v
1
Example
DIJ KSTRA(G, s)
INIT(G, s)
S
> set of discovered nodes
10
QV[G]
while Q do
s
uEXTRACT-MIN(Q)
SS U {u}
for each v in Adj[u] do
5
RELAX(u, v) > May cause
>DECREASE-KEY(Q, v, d[v])
v
1
Example
DIJ KSTRA(G, s)
INIT(G, s)
S
> set of discovered nodes
10
QV[G]
while Q do
s
uEXTRACT-MIN(Q)
SS U {u}
for each v in Adj[u] do
5
RELAX(u, v) > May cause
>DECREASE-KEY(Q, v, d[v])
v
1
Example
DIJ KSTRA(G, s)
INIT(G, s)
S
> set of discovered nodes
10
QV[G]
while Q do
uEXTRACT-MIN(Q)
s
SS U {u}
for each v in Adj[u] do
5
RELAX(u, v) > May cause
>DECREASE-KEY(Q, v, d[v])
v
1
Example
u
10
s
DIJ KSTRA(G, s)
INIT(G, s)
S
> set of discovered nodes
QV[G]
O(V Log V)
while Q do
uEXTRACT-MIN(Q)
Total in the loop: O(V Log V)
SS U {u}
for each v in Adj[u] do
RELAX(u, v) > May cause
>DECREASE-KEY(Q, v, d[v])
Total in the loop: O(E Log V)
Algorithms to Cover
Dijkstras Algorithm
Shortest Path is DAGs
Example
6
r
s
0
1
u
7
4
3
Example
6
r
s
0
1
u
7
4
3
Example
6
r
s
0
t
2
1
u
7
4
3
Example
6
r
s
0
t
2
1
u
7
4
3
v
6
w
4
Example
6
r
s
0
t
2
1
u
7
4
3
v
5
w
4
Example
6
r
s
0
t
2
1
u
7
4
3
v
5
w
3
Example
6
r
s
0
t
2
1
u
7
4
3
v
5
w
3
Time Complexity: O (V + E)