All Pairs Shortest Paths - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

All Pairs Shortest Paths

Description:

application of single source shortest path algorithms. direct methods to solve the problem: ... table of distances between all pairs of cities for a road atlas ... – PowerPoint PPT presentation

Number of Views:105
Avg rating:3.0/5.0
Slides: 43
Provided by: mariekewe
Category:

less

Transcript and Presenter's Notes

Title: All Pairs Shortest Paths


1
All Pairs Shortest Paths
2
outline
  • application of single source shortest path
    algorithms
  • direct methods to solve the problem
  • matrix multiplication
  • Floyds algorithm
  • transitive closure (Warshalls algorithm)

3
motivation
  • computer network
  • aircraft network (e.g. flying time, fares)
  • railroad network
  • table of distances between all pairs of cities
    for a road atlas

4
single source shortest path algorithms
  • if edges are non-negative
  • running the Dijkstra algorithm n-times, once for
    each vertex as the source
  • running time O(nm lg n)
  • (using binary-heap)

5
single source shortest path algorithms
  • negative-weight edges
  • Bellman-Ford algorithm
  • running time O(n2 m)

6
data structure
  • adjacency matrix
  • c E ? ? as n x n matrix C
  • 0 if i j,
  • cij c(i,j) if i ? j and (i,j) ? E,
  • 8 if i ? j and (i,j) ? E

7
matrix multiplication (idea)
  • dij(m) minimum weight of any path
  • from i to j
  • that contains at most m edges

8
matrix multiplication (idea)
dik(m-1)
k
p
ckj
i
j
dij(m-1)
v
9
matrix multiplication (idea)
  • dij(m) min (dij(m-1), min1kn dik(m-1)
    ckj)
  • look at all possible predecessors k of j and
    compare

10
matrix multiplication (structure)
  • dij(1) cij
  • dij(m) min (dij(m-1), min1kn dik(m-1)
    ckj)
  • min1kn dik(m-1) ckj

11
matrix multiplication (structure)
  • Compute a series of matrices
  • D(1), D(2), , D(n-1)
  • D(m) D(m-1) ? C
  • final matrix D(n-1) contains the final
    shortest-path weights

12
matrix multiplication (pseudo-code)
  • EXTEND-SHORTEST-PATHS (D,C )
  • n ? rows D
  • let D' (d'ij ) be an n x n matrix
  • for i ? 1 to n
  • do for j ? 1 to n
  • do d'ij ? 8
  • for k ? 1 to n
  • do d'ij ? min (d'ij , dik ckj)
  • return D'

13
matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
14
matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
15
matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
16
matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
17
matrix multiplication (example)
  • ?
  • 1
  • d14(2) (0 3 8 ? ?4) ? ?
  • 0
  • 6
  • min (?, 4, ?, ?, 2)
  • 2

18
matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
19
matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
20
relation to matrix multiplication
  • C A ? B
  • cij Sk1n aik ? bkj
  • compare
  • D(m) D(m-1) ? C
  • ? dij(m) min1kn dik(m-1) ckj

21
matrix multiplication
  • compute the sequence of n-1 matrices
  • D(1) D(0) ? C C,
  • D(2) D(1) ? C C2,
  • D(3) D(2) ? C C3,
  • D(n-1) D(n-2) ? C Cn-1

22
matrix multiplication (pseudo-code)
  • ALL-PAIRS-SHORTEST-PATHS (C )
  • n ? rows C
  • D (1) ? C
  • for m ? 2 to n - 1
  • do D (m) ? EXTEND-SHORTEST-PATHS
  • (D (m-1),C )
  • return D (n-1)
  • negative cycles dii lt 0

23
matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
24
matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
25
matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
26
matrix multiplication (running time)
  • O(n4)
  • Improving the running time
  • compute not all D(m) matrices
  • interested only in D(n-1), which is D(m) for all
    integers m n-1

27
improving the running time
  • compute the sequence
  • D(1) C,
  • D(2) C2 C ? C,
  • D(4) C4 C2 ? C2,
  • D(8) C8 C4 ? C4
  • ...
  • D(2 ? ?lg(n-1)?) C2? ?lg(n-1)?
  • C2 ? ( ?lg(n-1)? -1) ? C2 ?( ?lg(n-1)? -1)
  • so we need only ?lg(n-1)? matrix products
  • O(n3 lg n)

28
Floyds algorithm
1
2
.
i
j
k-1
k
k1
.
n
29
Floyds algorithm
  • dij(0) cij
  • (no intermediate vertices at all)
  • dij(k) min(dij(k-1), dik(k-1) dkj(k-1)) if
    k 1
  • result D(n) (dij(n)) d(i,j)
  • (because all intermediate vertices are in the
    set 1, 2, , n)

30
Floyds algorithm (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
31
Floyds algorithm (example)
2
2
?
3
3
3
8
2
4
4
1
?4
?
5
5
4 ? 3 ? 5
32
Floyds algorithm (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
33
Floyds algorithm (example)
1
1
?
3
3
3
4
4
4
2
1
5
5
5
7
4 ? 5 ? 2 3 ? 4 ? 1 ? 5 ? 4 3 ? 5 ?
34
Floyds algorithm (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
35
Floyds algorithm (pseudo-code)
  • FLOYD (C )
  • n ? rows C
  • D (0) ? C
  • for k ? 1 to n
  • do for i ? 1 to n
  • do for j ? 1 to n
  • dij(k) ? min (dij(k-1), dik(k-1) dkj(k-1))
  • return D (n)
  • running time O(n3)

36
transitive closure (the problem)
  • find out whether there is a path from i to j
    and compute
  • G (V,E),
  • where E (i,j) there is a path from i to
    j in G

a
c
b
i
j
37
transitive closure (the problem)
  • find out whether there is a path from i to j
    and compute
  • G (V,E),
  • where E (i,j) there is a path from i to
    j in G

a
c
b
i
j
38
transitive closure
  • one way
  • set cij 1 and
  • run the Floyd algorithm
  • running time O(n3)

39
transitive closure
  • another way
  • substitute and min by AND and OR
  • in Floyds algorithm
  • running time O(n3)

40
transitive closure (pseudo-code)
  • TRANSITIVE-CLOSURE (G )
  • n ? ?V G ?
  • for i ? 1 to n
  • do for j ? 1 to n
  • do if i j OR (i,j ) ? E G
  • then tij(0) ? 1
  • else tij(0) ? 0
  • for k ? 1 to n
  • do for i ? 1 to n
  • do for j ? 1 to n
  • do tij(k) ? tij(k-1) OR (tik(k-1) AND
    tkj(k-1) )
  • return T (n)

41
table of running times
42
Floyds algorithm
k
dik(k-1)
dkj(k-1)
i
dij(k-1)
j
Write a Comment
User Comments (0)
About PowerShow.com