Graph-Based Algorithms: CSE373: Design and Analysis of Algorithms

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

Graph-Based Algorithms

CSE373: Design and Analysis of Algorithms


All-Pairs Shortest Paths
2
Given: 3 4

Directed graph G = (V, E) 1


8
3
2
Weight func on w : E → R 1
-4 7 -5

Compute: 5
6
4

The shortest paths between all pairs of vertices in a graph


Representation of the result: an n × n matrix of shortest-path
distances δ(u, v)
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) (V)
2. S← 
3. Q ← V[G] O(V) build min-heap
4. while Q   Executed O(V) times
5. do u ← EXTRACT-MIN(Q) O(lgV)
6. S ← S  {u}
7. for each vertex v  Adj[u]
8. do RELAX(u, v, w) O(E) times; O(lgV)
Running time: O(VlgV + ElgV) = O(ElgV)
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)
5. for each edge (u, v)  E O(E)
6. do if d[v] > d[u] + w(u, v)
7. then return FALSE
8. return TRUE

Running time: O(VE)


All-Pairs Shortest Paths - Solutions
Run BELLMAN-FORD once from each vertex:
O(V2E), which is O(V4) if the graph is dense (E = (V2))

If no negative-weight edges, could run Dijkstra’s algorithm


once from each vertex:
O(VElgV) with binary heap, O(V3lgV) if the graph is dense

We can solve the problem in O(V3), with no elaborate data


structures
All-Pairs Shortest Paths
Assume the graph (G) is given as adjacency matrix of
weights 2
W = (wij), n x n matrix, |V| = n 3 4
Vertices numbered 1 to n 8
1 3
0 if i = j 2
1
-4 7 -5
wij = weight of (i, j) if i  j , (i, j)  E
5 4
∞ if i  j , (i, j)  E 6

Output the result in an n x n matrix


D = (dij), where dij = δ(i, j)
Solve the problem using dynamic programming
Optimal Substructure of a Shortest Path
All subpaths of a shortest path are shortest paths
Let p: a shortest path p from vertex i to j that contains at
most m edges
at most m edges
If i = j

11
w(p) = 0 and p has no edges j
i
p’ k
If i  j: p = i kj
p’ has at most m-1 edges at most m - 1 edges
p’ is a shortest path
δ(i, j) = δ(i, k) + wkj
Recursive Solution
lij(m) = weight of shortest path i j that contains at most m
edges
at most m edges
m = 0: lij(0) = 0 if i = j

11
 if i  j i
j
k

m  1: lij(m) = min { lij(m-1) , min {lik(m-1) + wkj} }


1kn
= min {lik(m-1) + wkj}
1kn

Shortest path from i to j with at most m – 1 edges


Shortest path from i to j containing at most m edges, considering
all possible predecessors (k) of j
Computing the Shortest Paths
m = 1: lij(1) = wij L(1) = W
The path between i and j is restricted to 1 edge

Given W = (wij), compute: L(1), L(2), …, L(n-1), where


L(m) = (lij(m))
L(n-1) contains the actual shortest-path weights
Given L(m-1) and W  compute L(m)
Extend the shortest paths computed so far by one more edge

If the graph has no negative cycles: all simple shortest paths contain
at most n - 1 edges
δ(i, j) = lij(n-1) and lij(n) = lij(n+1). . . = lij(n-1)
Extending the Shortest Path
lij(m) = min {lik(m-1) + wkj}
1kn
k
j j

i * = i
k

L(m-1) nxn W L(m)

Replace: min  + Computing L(m) looks like


+   matrix multiplication
EXTEND-SHORTEST-PATHS(L, W)
1. n = L.rows
2. let L’ = (lij’) be a new n × n matrix
3. for i = 1 to n
4. for j = 1 to n
5. lij’ = ∞
6. for k = 1 to n
7. lij’ = min(lij’, lik + wkj)
8. return L’
Running time: (n3)
SLOW-ALL-PAIRS-SHORTEST-PATHS(W)
1. n = L.rows

2. L(1) = W

3. for m = 2 to n - 1

4. L(m) = EXTEND-SHORTEST-PATHS (L(m - 1), W)

5. return L(n - 1)

Running time: (n4)


Example lij(m) = min {lik(m-1) + wkj}
1kn

2 L(m-1) = L(1) W

3 4 0 3 8  -4 0 3 8  -4
8
1 3  0  1 7  0  1 7
2
-4
1
-5
 4 0    4 0  
7
5 4 2  -5 0  2  -5 0 
6
   6 0    6 0

0 3 8 2 -4
3 0 -4 1 7
L(m) = L(2)
 4 0 5 11 … and so on until L(4)
2 -1 -5 0 -2
8  1 6 0
Improving Running Time
No need to compute all L(m) matrices
If no negative-weight cycles exist:
L(m) = L(n - 1) for all m  n – 1
We can compute L(n-1) by computing the sequence:
L(1) = W L(2) = W2 = W  W
L(4) = W4 = W2  W2 L(8) = W8 = W4  W4 …

 2 x  n 1
n 1 2  lg( n1) 
L W
FASTER-ALL-PAIRS-SHORTEST-PATHS(W)
1. n = W.rows
2. L(1) = W
3. m = 1
4. while m < n – 1
5. let L(2m) be a new n × n matrix
6. L(2m) = EXTEND-SHORTEST-PATHS(L(m), L(m))
7. m = 2*m
8. return L(m)

OK to overshoot: products don’t change after L(n - 1)


Running Time: (n3lg n)
The Floyd-Warshall Algorithm
2
3 4

Given: 1
8
3
2
Directed, weighted graph G = (V, E) -4
1
-5
7
Negative-weight edges may be present 5 4
6
No negative-weight cycles could be present in the graph
Compute:
The shortest paths between all pairs of vertices in a graph
The Structure of a Shortest Path
For any pair of vertices i, j  V, consider all paths from i
to j whose intermediate vertices are all drawn from
a subset {1, 2, …, k}
Find p, a minimum-weight path from these paths

p1
pu j
i

pt

No vertex on these paths has index > k


The Structure of a Shortest Path
k
i j
k is not an intermediate vertex of path p
Shortest path from i to j with intermediate vertices from {1, 2, …, k} is a
shortest path from i to j with intermediate vertices from {1, 2, …, k - 1}

k is an intermediate vertex of path p


p1 is a shortest path from i to k p1 
k p2
j
p2 is a shortest path from k to j i

k is not intermediary vertex of p1, p2


p1 and p2 are shortest paths from i to k with vertices from {1, 2, …, k - 1}
A Recursive Solution (cont.)
dij(k) = the weight of a shortest path from vertex i to vertex
j with all intermediary vertices drawn from {1, 2, …, k}
k=0
dij(k) = wij
A Recursive Solution (cont.)
dij(k) = the weight of a shortest path from vertex i to vertex
j with all intermediary vertices drawn from {1, 2, …, k}
k1
Case 1: k is not an intermediate vertex of path p
dij(k) = dij(k-1)
k
i j
A Recursive Solution (cont.)
dij(k) = the weight of a shortest path from vertex i to vertex
j with all intermediary vertices drawn from {1, 2, …, k}
k1
Case 2: k is an intermediate vertex of path p
dij(k) = dik(k-1) + dkj(k-1)


k
j
i
Computing the Shortest Path Weights
dij(k) = wij if k = 0
min {dij(k-1) , dik(k-1) + dkj(k-1) } if k  1
The final solution: D(n) = (dij(n)):
dij(n) = (i, j)  i, j  V
j j

+ (k, j)

i i
(i, k)
D(k-1) D(k)
The Floyd-Warshall algorithm
1. n = W.rows
2. D(0) = W
3. for k = 1 to n
4. let D(k) = dij(k) be a new n × n matrix
5. for i = 1 to n
6. for j = 1 to n
7. dij(k) = min(dij(k-1), dik(k-1) + dkj(k-1))
8. return D(n)

Running Time: O(n3)


Computing predecessor matrix
How do we compute the predecessor matrix?

Initialization:

Updating:
Example dij(k) = min {dij(k-1) , dik(k-1) + dkj(k-1) }
D(0) = W D(1)
2 1 2 3 4 5 1 2 3 4 5
3 4 1 0 3 8  -4 1 0 3 8  -4
8
1 3 2  0  1 7 2  0  1 7
2
-4
1
-5
3  4 0   3  4 0  
7
5 4 4 2  -5 0  4 2 5 -5 0 -2
6
D(2) 5    6 0 5    6 0
1 2 3 4 5 D(3) D(4)
1 0 3 8 4 -4 0 3 8 4 -4 0 3 -1 4 -4
2  0  1 7  0  1 7 3 0 -4 1 -1
3  4 0 5 11  4 0 5 11 7 4 0 5 3
4 2 5 -5 0 -2 2 -1 -5 0 -2 2 -1 -5 0 -2
5    6 0    6 0 8 5 1 6 0
Example dij(k) = min {dij(k-1) , dik(k-1) + dkj(k-1) }
D(5) P(5)
2 1 2 3 4 5 1 2 3 4 5
3 4 1 0 1 -3 2 -4 1 - 3 4 5 1
8
1
2
3 2 3 0 -4 1 -1 2 4 - 4 2 1
-4 7
1
-5
3 7 4 0 5 3 3 4 3 - 2 1
5 4 4 2 -1 -5 0 -2 4 4 3 4 - 1
6
5 8 5 1 6 0 5 4 3 4 5 -

Source: 5, Destination: 1
Shortest path: 8
Path: 5 …1 : 5…4…1: 5->4…1: 5->4->1

Source: 1, Destination: 3
Shortest path: -3
Path: 1 …3 : 1…4…3: 1…5…4…3: 1->5->4->3

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