Sunteți pe pagina 1din 77

# Graph Algorithms

## Shortest path problems

Graph Algorithms
Shortest path problems

Graph Algorithms
Shortest path problems

Graph Algorithms
Single-Source Shortest Paths
Given graph (directed or undirected) G = (V,E) with
weight function w: E R and a vertex sV,
find for all vertices vV the minimum possible weight for
path from s to v.
We will discuss two general case algorithms:
Dijkstra's (positive edge weights only)
Bellman-Ford (positive end negative edge weights)
If all edge weights are equal (let's say 1), the problem
is solved by BFS in (V+E) time.

Graph Algorithms
Dijkstras Algorithm - Relax
Relax(vertex u, vertex v, weight w)
if d[v] > d[u] + w(u,v) then
d[v] d[u] + w(u,v)
p[v] u

Graph Algorithms
Dijkstras Algorithm - Idea

Graph Algorithms
Dijkstras Algorithm - SSSP-Dijkstra
SSSP-Dijkstra(graph (G,w), vertex s)
InitializeSingleSource(G, s)
S
Q V[G]
while Q 0 do
u ExtractMin(Q)
S S {u}
Relax(u,v,w)
InitializeSingleSource(graph G, vertex s)
for v V[G] do
d[v]
p[v] 0
d[s] 0

Graph Algorithms
Dijkstras Algorithm - Example

1
10
2

4
7

5
2

Graph Algorithms
Dijkstras Algorithm - Example

10
2

0
5

Graph Algorithms
Dijkstras Algorithm - Example

10

10
2

5
5

Graph Algorithms
Dijkstras Algorithm - Example

10

10
2

5
5

Graph Algorithms
Dijkstras Algorithm - Example

10
2

14

5
5

Graph Algorithms
Dijkstras Algorithm - Example

10
2

14

5
5

Graph Algorithms
Dijkstras Algorithm - Example

10
2

13

5
5

Graph Algorithms
Dijkstras Algorithm - Example

10
2

13

5
5

Graph Algorithms
Dijkstras Algorithm - Example

10
2

5
5

Graph Algorithms
Dijkstras Algorithm - Example

10
2

5
5

## Graph AlgorithmsSSSP-Dijkstra(graph (G,w), vertex s)

Dijkstras
Algorithm Complexity

## executed (V) times

(E) times in total

(V)

(1) ?

InitializeSingleSource(G, s)
S
Q V[G]
while Q 0 do
u ExtractMin(Q)
S S {u}
Relax(u,v,w)
InitializeSingleSource(graph G, vertex s)
for v V[G] do
d[v]
p[v] 0
d[s] 0
Relax(vertex u, vertex v, weight w)
if d[v] > d[u] + w(u,v) then
d[v] d[u] + w(u,v)
p[v] u

Graph Algorithms
Dijkstras Algorithm - Complexity
InitializeSingleSource

TI(V,E) = (V)

Relax

TR(V,E) = (1)?

SSSP-Dijkstra
T(V,E) = TI(V,E) + (V) + V (log V) + E TR(V,E) =
= (V) + (V) + V (log V) + E (1) = (E + V log V)

Graph Algorithms
Dijkstras Algorithm - Complexity

Graph Algorithms
Dijkstras Algorithm - Correctness

Graph Algorithms
Dijkstras Algorithm - negative weights?

Graph Algorithms
Bellman-Ford Algorithm - negative
cycles?

Graph Algorithms
Bellman-Ford Algorithm - Idea

Graph Algorithms
Bellman-Ford Algorithm - SSSPBellmanFord
SSSP-BellmanFord(graph (G,w), vertex s)
InitializeSingleSource(G, s)
for i 1 to |V[G] 1| do
for (u,v) E[G] do
Relax(u,v,w)
for (u,v) E[G] do
if d[v] > d[u] + w(u,v) then
return false
return true

Graph Algorithms
Bellman-Ford Algorithm - Example
-2
5

6
-3

7
9

-4

Graph Algorithms
Bellman-Ford Algorithm - Example
-2
6

-3

0
7

-4

Graph Algorithms
Bellman-Ford Algorithm - Example
-2
6

6
-3

7
7

-4

Graph Algorithms
Bellman-Ford Algorithm - Example
-2
6

6
-3

7
7

-4
2

Graph Algorithms
Bellman-Ford Algorithm - Example
-2
6

2
-3

7
7

-4
2

Graph Algorithms
Bellman-Ford Algorithm - Example
-2
6

2
-3

7
7

-4

7
-2

Graph Algorithms
Bellman-Ford Algorithm - Complexity

(E)
(E)

## SSSP-BellmanFord(graph (G,w), vertex s)

InitializeSingleSource(G, s)
for i 1 to |V[G] 1| do
for (u,v) E[G] do
Relax(u,v,w)
(1)
for (u,v) E[G] do
if d[v] > d[u] + w(u,v) then
return false
return true

Graph Algorithms
Bellman-Ford Algorithm - Complexity
InitializeSingleSource

TI(V,E) = (V)

Relax

TR(V,E) = (1)?

SSSP-BellmanFord
T(V,E) = TI(V,E) + V E TR(V,E) + E =
= (V) + V E (1) + E =
= (V E)

Graph Algorithms
Bellman-Ford Algorithm - Correctness

Graph Algorithms
Bellman-Ford Algorithm - Correctness

Graph Algorithms
Bellman-Ford Algorithm - Correctness

Graph Algorithms
Bellman-Ford Algorithm - Correctness

Graph Algorithms
Shortest Paths in DAGs - SSSP-DAG
SSSP-DAG(graph (G,w), vertex s)
topologically sort vertices of G
InitializeSingleSource(G, s)
for each vertex u taken in topologically sorted order do
for each vertex v Adj[u] do
Relax(u,v,w)

Graph Algorithms
Shortest Paths in DAGs - Example

6
5

2
3

1
7

-1

-2
4

Graph Algorithms
Shortest Paths in DAGs - Example

0
3

1
7

-1

-2
2

Graph Algorithms
Shortest Paths in DAGs - Example

0
3

1
7

-1

-2
2

Graph Algorithms
Shortest Paths in DAGs - Example

0
3

1
7

-1

-2
2

Graph Algorithms
Shortest Paths in DAGs - Example

0
3

1
7

-1

-2
2

Graph Algorithms
Shortest Paths in DAGs - Example

0
3

1
7

-1

6
4

-2
2

Graph Algorithms
Shortest Paths in DAGs - Example

0
3

1
7

-1

6
4

-2
2

Graph Algorithms
Shortest Paths in DAGs - Example

0
3

1
7

-1

5
4

-2
2

Graph Algorithms
Shortest Paths in DAGs - Example

0
3

1
7

-1

5
4

-2
2

Graph Algorithms
Shortest Paths in DAGs - Example

0
3

1
7

-1

5
4

-2
2

Graph Algorithms
Shortest Paths in DAGs - Example

0
3

1
7

-1

5
4

-2
2

Graph Algorithms
Shortest Paths in DAGs - Complexity
SSSP-DAG(graph (G,w), vertex s)
topologically sort vertices of G
InitializeSingleSource(G, s)
for each vertex u taken in topologically sorted order do
for each vertex v Adj[u] do
Relax(u,v,w)

## T(V,E) = (V + E) + (V) + (V) + E (1) = (V + E)

Graph Algorithms
Application of SSSP - currency
conversion

Graph Algorithms
Application of SSSP - currency
conversion

Graph Algorithms
Application of SSSP - currency
conversion

Graph Algorithms
Application of SSSP - constraint
satisfaction

Graph Algorithms
Application of SSSP - constraint
satisfaction

Graph Algorithms
Application of SSSP - constraint
satisfaction

Graph Algorithms
Application of SSSP - constraint
satisfaction

Graph Algorithms
Application of SSSP - constraint
satisfaction

Graph Algorithms
Application of SSSP - constraint
satisfaction

Graph Algorithms
All-Pairs Shortest Paths
Given graph (directed or undirected) G = (V,E) with
weight function w: E R find for all pairs of vertices
u,v V the minimum possible weight for path from u to v.

Graph Algorithms
Floyd-Warshall Algorithm - Idea

Graph Algorithms
Floyd-Warshall Algorithm - Idea

Graph Algorithms
Floyd-Warshall Algorithm - Idea
ds,t(i) the shortest path from s to t containing only vertices
v1, ..., vi
ds,t(0) = w(s,t)
w(s,t)
ds,t(k) =

## min{ds,t(k-1), ds,k(k-1) + dk,t(k-1)} if k > 0

if k = 0

Graph Algorithms
Floyd-Warshall Algorithm - Algorithm
FloydWarshall(matrix W, integer n)
for k 1 to n do
for i 1 to n do
for j 1 to n do
dij(k) min(dij(k-1), dik(k-1) + dkj(k-1))
return D(n)

Graph Algorithms
Floyd-Warshall Algorithm - Example
W
3

-5
4

-4
1 7
0
-5 0
6 0
0

8
2

-4

3
0
4
2

Graph Algorithms
Floyd-Warshall Algorithm - Example
(0)

D(0)

3
0
4
2

-4
1 7
0
-5 0
6 0

0
0
0

0
0
0

0
0

0
0

Graph Algorithms
Floyd-Warshall Algorithm - Example
(1)

D(1)

3
0
4
2 5

-4
1 7
0
-5 0 -2
6 0

0
0
0
1

0
0
0

0
0

0
0

1
0

Graph Algorithms
Floyd-Warshall Algorithm - Example
(2)

D(2)

3
0
4
2 5

4
1
0 5
-5 0
6

-4
7
11
-2
0

0
0
0
1

0
0
0

2
0
2
0
0

0
0
2
1
0

Graph Algorithms
Floyd-Warshall Algorithm - Example
(3)

D(3)

3
0
4
2 -1

4
1
0 5
-5 0
6

-4
7
11
-2
0

0
0
0
3

0
0
0

2
0
2
0
0

0
0
2
1
0

Graph Algorithms
Floyd-Warshall Algorithm - Example
(4)

D(4)

0
3
7
2
8

3
0
4
-1
5

-1
-4
0
-5
1

4
1
5
0
6

-4
-1
3
-2
0

0
4
4
0
4

0
0
0
3
3

4
4
0
0
4

2
0
2
0
0

0
1
1
1
0

Graph Algorithms
Floyd-Warshall Algorithm - Example
(5)

D(5)

0
3
7
2
8

3
0
4
-1
5

-1
-4
0
-5
1

2
1
5
0
6

-4
-1
3
-2
0

0
4
4
0
4

0
0
0
3
3

4
4
0
0
4

5
0
2
0
0

0
1
1
1
0

Graph Algorithms

## Floyd-Warshall Algorithm - Extracting the

shortest paths

Graph Algorithms
Floyd-Warshall Algorithm - Complexity
FloydWarshall(matrix W, integer n)
for k 1 to n do
for i 1 to n do
for j 1 to n do
dij(k) min(dij(k-1), dik(k-1) + dkj(k-1))
return D(n)
3 for cycles, each executed exactly n times

## T(V,E) = (n3) = (V3)

Graph Algorithms
All-Pairs Shortest Paths -Johnson's
algorithm

Graph Algorithms
All-Pairs Shortest Paths - Reweighting

Graph Algorithms
All-Pairs Shortest Paths - Reweighting

Graph Algorithms
All-Pairs Shortest Paths - Reweighting