L16 Graph Part05

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Lecture 16

Graph-Based Algorithms

CSE373: Design
and Analysis of
Shortest Path Problems

Modeling problems as graph problems:
• Road map is a weighted graph:
vertices = cities
edges = road segments between cities
edge weights = road distances
• Goal: find a shortest path between two vertices (cities)
Shortest Path Problems

What is shortest path ?
§ shortest length between two vertices for an unweighted
graph:
§ smallest cost between two vertices for a weighted graph:
B 210 B

A A
450
60 190

C unweighted C weighted
graph graph
200 130
D D
E E
Shortest Path Problems

Input:
t x
6
• Directed graph G = (V, E) 3 9
3
4
• Weight function w : E → R s 0
2 1
2 7
k 5 3

Weight of path pw(=p )v0,
v1,
w( v.i  1., .vi,) vk 5 11
i 1
6
y z


Shortest-path weightp from u to v:

δ(u, v) = min w(p) : u v if there exists a path from u to


v
Variants of Shortest Paths

Single-source shortest path
• Given G = (V, E), find a shortest path from a given source vertex s to
each vertex v  V

Single-destination shortest path
• Find a shortest path to a given destination vertex t from each vertex v
• Reverse the direction of each edge  single-source

Single-pair shortest path
• Find a shortest path from u to v for given vertices u and v
• Solve the single-source problem

All-pairs shortest-paths
• Find a shortest path from u to v for every pair of vertices u and v
Shortest-Path Representation
For each vertex v  V:

d[v] = δ(s, v): a shortest-path estimate
• Initially, d[v]=∞
• Reduces as algorithms progress

[v] = predecessor of v on a shortest path from s
• If no predecessor, [v] = NIL
•  induces a tree—shortest-path tree

Shortest paths & shortest path trees are not unique
Initialization
Alg.: INITIALIZE-SINGLE-SOURCE(V, s)
1. for each v  V
2. do d[v] ← 
3. [v] ← NIL
4. d[s] ← 0

•.
All the shortest-paths algorithms start with
INITIALIZE-SINGLE-SOURCE
Relaxation

Relaxing an edge (u, v) = testing whether we can
improve the shortest path to v found so far by going
through u
If d[v] > d[u] + w(u, v)
we can improve the shortest path to v
s s
 updateud[v] and [v]
v u v
2 2
5 9 5 6

RELAX(u, v, w) RELAX(u, v, w)

u v u v
2 2
5 7 5 6

After relaxation: d[v]  d[u] + w(u, v)


RELAX(u, v, w)
1. if d[v] > d[u] + w(u, v)
2. then d[v] ← d[u] + w(u, v)
3. [v] ← u

•.
All the single-source shortest-paths algorithms
• start by calling INIT-SINGLE-SOURCE
• then relax edges
•.
The algorithms differ in the order and how many
times they relax each edge
Dijkstra’s Algorithm

Single-source shortest path problem:
• No negative-weight edges: w(u, v) > 0  (u, v)  E


Maintains two sets of vertices:
• S = vertices whose final shortest-path weights have
already been determined

• Q = vertices in V – S: min-priority queue



Keys in Q are estimates of shortest-path weights (d[v])


Repeatedly select a vertex u  V – S, with the
minimum shortest-path estimate d[v]
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) t 1 x
 
9
2. S←  10
2 3 4 6
s 0
3. Q ← V[G] 5 7
 
2
4. while Q   y z
t 1 x
5. do u ← EXTRACT-MIN(Q) 10
 
10 9

6. S ← S  {u} s 0
2 3 4 6

5 7
7. for each vertex v  Adj[u] 5 
2
y z
8. do RELAX(u, v, w)
Example
t 1 x t 1 x
10
8 14 8 1413
10 9 10 9
2 3 4 6 2 3 4 6
s 0 s 0
5 7 5 7
5 7 5
2
7
2
y z y z

t x t 1 x
1
8 13 9 8 9
10 9 10 9

2 4 2 3 4 6
s 0 3 6 s 0

7 5 7
5
5 7 5 7
2 2
y z y z
Dijkstra’s Pseudo Code

Graph G, weight function w, root s

relaxing
edges
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V,(V)
s)
2. S← 
O(V) build min-heap
3. Q ← V[G]
Executed O(V) times
4. while Q   O(lgV)
5. do u ← EXTRACT-MIN(Q)
6. S ← S  {u}
O(E) times; O(lgV)
7. for each vertex v  Adj[u]
8. do RELAX(u, v, w)
Dijkstra’s Running Time

Q T(Extract T(Decrease- Total


-Min) Key)
array O(V) O(1) O(V 2)
binary heap O(lg V) O(lg V) O(E lg V)
Negative-Weight Edges

What if we have negative-weight edges?

a b
-4 h i
3 -1 2
3 4  
c 6 d g
5 8
s 0 5 11 - -8 3
-3
y 
2 3 7
- - j
e -6 f
Negative-Weight Edges

s  a: only one path
(s, a) = w(s, a) = 3


s  b: only one path
(s, b) = w(s, a) + w(a, b) = -1
a b
-4 h i
3 -1 2
3 4  
c 6 d g
5 8
s 0 5 11 - -8 3
-3
y 
2 3 7
- - j
e -6 f
Negative-Weight Edges

s  c: infinitely many paths
s, c, s, c, d, c, s, c, d, c, d, c


cycle c, d, c has positive weight (6 - 3 = 3)

s, c is shortest path with weight (s, b) = w(s, c) = 5

a b
-4 h i
3 -1 2
3 4  
c 6 d g
5 8
s 0 5 11 - -8 3
-3
y 
2 3 7
- - j
e -6 f
Negative-Weight Edges

s  e: infinitely many paths:
• s, e, s, e, f, e, s, e, f, e, f, e
• cycle e, f, e has negative weight: 3 + (- 6) = -3
• many paths from s to e with arbitrarily large negative
weights
• (s, e) = a-   no shortest
b
path exists between s and e
-4 h i
• Similarly:3 (s, f) = --1, 4(s, g) = - 2 h, i, j not
3 
c 6 d g reachable
5 8
s 0 5 11 - 3
from s
-8
-3
y 
2 3 7
- - j
e -6 f
(s, h) = (s, i) = (s, j) = 
Negative-Weight Edges

Negative-weight edges may form negative-weight
cycles


If such cycles are reachable from the source: (s, v)
is not properly defined
a b
-4 h i
3 -1 2
3 4  
c 6 d g
5 8
s 0 5 11 - -8 3
-3
y 
2 3 7
- - j
e -6 f
Cycles

Can shortest paths contain cycles?

Negative-weight cycles No!

Positive-weight cycles: No!
• By removing the cycle we can get a shorter path

We will assume that when we are finding shortest
paths, the paths will have no cycles
Bellman-Ford Algorithm

Single-source shortest paths problem
• Computes d[v] and [v] for all v  V

Allows negative edge weights

Returns:
• TRUE if no negative-weight cycles are reachable from the
source s
• FALSE otherwise  no solution exists

Idea:
• Traverse all the edges |V – 1| times, every time performing
a relaxation step of each edge
BELLMAN-FORD(V, E, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s)
2. for i ← 1 to |V| - 1
3. do for each edge (u, v)  E
4. do RELAX(u, v, w)
5. for each edge (u, v)  E
6. do if d[v] > d[u] + w(u, v)
7. then return FALSE
8. return TRUE
Example
t 5 x
 
6 -2
-3
8 7
s 0
-4
7 2
 
9
y z

E: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
Example
t 5 x t x
Pass 1 5
6
  Pass 2
6 4
11

6 -2 6 -2
-3 -3
8 7 8
s 0 7
s 0
-4 -4
7 2 7 2
7
  7 2

9 9
y z y z
Pass 3 t 5 x Pass 4 t 5 x
2
6 4
11
 2
6 4
11

6 -2 6 -2
-3 -3
8 7 8 7
s 0 s 0
-4 -4
7 2 7 2
7 2
 7 2
-2

9 9
y z y z

E: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
Detecting Negative Cycles

for each edge (u, v)  E

do if d[v] > d[u] + w(u, v)

then return FALSE

return TRUE
s b s b s b
2 2 2
0  0
-3 2
 -6
-3 -1
2

-8 3 -8 3 -8 3
 5
 5
2
c c c

Observe edge (s, b):


d[b] = -1, d[s] + w(s, b) = -4
 d[b] > d[s] + w(s, b)
BELLMAN-FORD(V, E, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) (V)
2. for i ← 1 to |V| - 1 O(V)

3. do for each edge (u, v)  E O(E) O(VE)

4. do RELAX(u, v, w)
O(E)
5. for each edge (u, v)  E
6. do if d[v] > d[u] + w(u, v)
7. then return FALSE
8. return TRUE

Running time: O(VE)


Single-Source Shortest Paths in DAGs

Given a weighted DAG: G = (V, E) – solve the
shortest path problem

Idea:
• Topologically sort the vertices of the graph
• Relax the edges according to the order given by the
topological sort

for each vertex, we relax each edge that starts from that vertex


Are shortest-paths well defined in a DAG?
• Yes, (negative-weight) cycles cannot exist
DAG-SHORTEST-PATHS(G, w, s)
1. topologically sort the vertices of G (V+E)

2. INITIALIZE-SINGLE-SOURCE(V, s) (V)

3. for each vertex u, taken in topologically (V)

sorted order (V+E)


(E)
4. do for each vertex v  Adj[u]
5. do RELAX(u, v, w)

Running time: (V+E)


Example
6 1
r s t x y z
5 2 7 -1 -2
 0    
3 4 2
6 1
r s t x y z
5 2 7 -1 -2
 0    
3 4 2

6 1
r s t x y z
5 2 7 -1 -2
 0 2
 6
  
3 4 2
Example
6 1
r s t x y z
5 2 7 -1 -2
 0 2 6 6
 4

3 4 2

6 1
r s t x y z
5 2 7 -1 -2
 0 2 6 6
5 4

3 4 2

6 1
r s t x y z
5 2 7 -1 -2
 0 2 6 5 4
3

3 4 2
Example

6 1
r s t x y z
5 2 7 -1 -2
 0 2 6 5 3
3 4 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy