Graph Algorithms, 5 - PowerPoint PPT Presentation

About This Presentation
Title:

Graph Algorithms, 5

Description:

Let Vk={1,2,...k}. Sk(i,j) = length of the shortest path ... Sk(i,j) = length of the shortest path between i and j whose interior vertices are all in Vk. ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 37
Provided by: csMon
Category:

less

Transcript and Presenter's Notes

Title: Graph Algorithms, 5


1
Graph Algorithms, 5
  • Binhai Zhu
  • Computer Science Department, Montana State
    University

2
All-Pair Shortest Paths Algorithm
Given a weighted, directed graph D(V,E,w) of n
vertices, we want to compute the shortest paths
from u to v for all vertex pairs u,v e V. This
is one of the most important problems in computer
science. For example, it has applications in
transportation networks.
3
All-Pair Shortest Paths Algorithm
Given a weighted, directed graph D(V,E,w) of n
vertices, we want to compute the shortest paths
from u to v for all vertex pairs u,v e V. Using
what we have learnt, how do we solve the problem?
4
All-Pair Shortest Paths Algorithm
Given a weighted, directed graph D(V,E,w) of n
vertices, we want to compute the shortest paths
from u to v for all vertex pairs u,v e V. Using
what we have learnt, how do we solve the
problem? Run Dijkstra at each vertex! It takes
O(n Elog V)O(n3log n) time.
5
All-Pair Shortest Paths Algorithm
Given a weighted, directed graph D(V,E,w) of n
vertices, we want to compute the shortest paths
from u to v for all vertex pairs u,v e V. Using
what we have learnt, how do we solve the
problem? Run Dijkstra at each vertex! It takes
O(n Elog V)O(n3log n) time. Can we do
better?
6
Dynamic Programming Idea
Input weighted, directed graph D(V,E,w) of n
vertices, say V1,2,,n. Let P(i,j) be the
shortest path between i and j.
7
Dynamic Programming Idea
Input weighted, directed graph D(V,E,w) of n
vertices, say V1,2,,n. Let P(i,j) be the
shortest path between i and j.
j
i
8
Dynamic Programming Idea
Input weighted, directed graph D(V,E,w) of n
vertices, say V1,2,,n. Let P(i,j) be the
shortest path between i and j. DP-related
Question How can we decompose the problem into
some simpler ones?
j
i
9
Dynamic Programming Idea
Input weighted, directed graph D(V,E,w) of n
vertices, say V1,2,,n. Let P(i,j) be the
shortest path between i and j. DP-related
Question How can we decompose the problem into
some simpler ones? A It should go through some
vertex k.
j
i
10
Dynamic Programming Idea
Input weighted, directed graph D(V,E,w) of n
vertices, say V1,2,,n. Let P(i,j) be the
shortest path between i and j. DP-related
Question How can we decompose the problem into
some simpler ones? A It should go through some
vertex k.
j
k
i
11
Dynamic Programming Idea
Let P(i,j) be the shortest path between i and
j. Let Vk1,2,k. Sk(i,j) length of the
shortest path between i and j whose interior
vertices are all in Vk.
j
k
i
12
Dynamic Programming Idea
Let P(i,j) be the shortest path between i and
j. Let Vk1,2,k. Sk(i,j) length of the
shortest path between i and j whose interior
vertices are all in Vk. What is the length of the
shortest path between i and j?
j
k
i
13
Dynamic Programming Idea
Let P(i,j) be the shortest path between i and
j. Let Vk1,2,k. Sk(i,j) length of the
shortest path between i and j whose interior
vertices are all in Vk. What is the length of the
shortest path between i and j? It is Sn(i,j).
j
k
i
14
Dynamic Programming Idea
Let Vk1,2,k. Sk(i,j) length of the shortest
path between i and j whose interior vertices are
all in Vk. If k is on the shortest path from i to
j, Sk(i,j)
j
k
i
15
Dynamic Programming Idea
Let Vk1,2,k. Sk(i,j) length of the shortest
path between i and j whose interior vertices are
all in Vk. If k is on the shortest path from i to
j, Sk(i,j) Sk-1(i,k)
Sk-1(k,j)
j
k
i
16
Dynamic Programming Idea
  • Let Vk1,2,k.
  • Sk(i,j) length of the shortest path between i
    and j whose interior vertices are all in Vk.
  • If k is on the shortest path from i to j,
  • Sk(i,j) Sk-1(i,k) Sk-1(k,j).
  • If k is not on the shortest path from i to j
  • Sk(i,j) Sk-1(i,j).

j
k
i
17
Dynamic Programming Formula
  • Let Vk1,2,k.
  • Ski,j length of the shortest path between i
    and j whose interior vertices are all in Vk.
  • S0i,j w(i,j).
  • Ski,j min Sk-1i,j, Sk-1i,k Sk-1k,j
    .

j
k
i
18
Dynamic Programming Algorithm
Floyd(W1n,1n,S1n,1n) For i 1 to n For
j 1 to n if lti,jgt in E then Si,j ?
Wi,j else Si,j ? 8 For k 1 to n For i
1 to n For j 1 to n if Si,j gt
Si,k Sk,j then Si,j ?
Si,k Sk,j
19
A detailed example
j1
2
5
i1

4
2
1
2
1
6
S0W
4
3
3
2
1
2
4
5
5
3
20
A detailed example
j1
2
5
i1
0
4
8
8

3
4
2
1
2
0
6
8
8
2
1
6
S0W
8
8
8
0
1
4
3
3
2
8
1
0
4
2
3
2
8
8
1
8
4
0
5
5
3
21
A detailed example
j1
2
5
i1
0
4
8
8

3
4
2
1
2
0
6
8
8
2
1
6
8
8
8
S0W
0
1
4
3
3
2
8
1
0
4
2
3
2
8
8
1
8
4
0
5
5
3
i1
0
4
8
8
3
2
0
6
8
8
2
8
S1
0
1
0
4
2
3
0
8
8
1
8
5
22
A detailed example
j1
2
5
i1
0
4
8
8

3
4
2
1
2
0
6
8
8
2
1
6
8
8
8
S0W
0
1
4
3
3
2
8
1
0
4
2
3
2
8
8
1
8
4
0
5
5
3
i1
0
4
8
8
3
2
0
6
8
8
2
8
4
5
S1
0
1
0
4
2
3
8
0
8
8
1
8
5
23
A detailed example
j1
2
5
i1
0
4
8
8

3
4
2
1
2
0
6
8
8
2
1
6
8
8
8
S0W
0
1
4
3
3
2
8
1
0
4
2
3
2
8
8
1
8
4
0
5
5
3
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
24
How do we retrieve the actual path?
  • We maintain Pki,j as follows
  • P0i,j 0 for all i,j in V
  • Pki,j Pk-1i,j, if Ski,j Sk-1i,j
  • Pki,j k, if Ski,j ? Sk-1i,j

25
How do we retrieve the actual path?
  • We maintain Pki,j as follows
  • P0i,j 0 for all i,j in V
  • Pki,j Pk-1i,j, if Ski,j Sk-1i,j
  • Pki,j k, if Ski,j ? Sk-1i,j
  • To reconstruct the shortest path i,j in Pn-,-
  • If Pni,j0, edge lti,jgt is the shortest path.
  • If Pni,jk, then k is an interior vertex on the
    path, other interior vertices can be obtained by
    checking Pni,k and Pnk,j.

26
Floyds Algorithm (with matrix P)
Floyd(W1n,1n,S1n,1n,P1n,1n) For i 1
to n For j 1 to n Pi,j ? 0 if
lti,jgt in E then Si,j ? Wi,j else Si,j ?
8 For k 1 to n For i 1 to n For j
1 to n if Si,j gt Si,k Sk,j
then Si,j ? Si,k Sk,j
Pi,j ? k
27
Dynamic Programming (It is all about filling
tables)
Sk (k1,2,3,4,5)
i
k
j
i
j
k
28
Dynamic Programming (It is all about filling
tables)
Sk (k1,2,3,4,5)
i
k
j
i
j
k
Where is the final shortest path length from node
3 to 5?
29
Dynamic Programming (It is all about filling
tables)
Sk (k1,2,3,4,5)
i
k
j
i
j
k
Where is the final shortest path length from node
3 to 5?
30
A detailed example
j1
2
5
i1
0
4
0
2
0

4
2
1
2
0
5
0
5
5
1
6
2
1
1
P5
0
0
4
3
3
2
1
0
3
0
0
3
2
3
3
0
3
4
0
5
5
3
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
31
A detailed example
j1
2
5
i1
0
4
0
2
0

4
2
1
2
0
5
0
5
5
1
6
2
1
1
P5
0
0
4
3
3
2
1
0
3
0
0
3
2
3
3
0
3
4
0
5
5
3
How do we retrieve the shortest path from 2 to 5?
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
32
A detailed example
j1
2
5
i1
0
4
0
2
0

4
2
1
2
0
5
0
5
5
1
6
2
1
1
P5
0
0
4
3
3
2
1
0
3
0
0
3
2
3
3
0
3
4
0
5
5
3
How do we retrieve the shortest path from 3 to 5?
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
33
A detailed example
j1
2
5
i1
0
4
0
2
0

4
2
1
2
0
5
0
5
5
1
6
2
1
1
P5
0
0
4
3
3
2
1
0
3
0
0
3
2
3
3
0
3
4
0
5
5
3
How do we retrieve the shortest path from 3 to
5? P3,52, so 325
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
34
A detailed example
j1
2
5
i1
0
4
0
2
0

4
2
1
2
0
5
0
5
5
1
6
2
1
1
P5
0
0
4
3
3
2
1
0
3
0
0
3
2
3
3
0
3
4
0
5
5
3
How do we retrieve the shortest path from 3 to
5? P3,52, so 325 P3,21,so
3125 P3,1P2,5P1,20 So 3?1?2?5.
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
35
Transitive Closure of a directed graph D
  • The transitive closure of a directed graph
    D(V,E) is a graph D(V,E), where
  • E(i,j) there is a path from vertex i to
    vertex j in graph D.

36
Transitive Closure of a directed graph D
  • The transitive closure of a directed graph
    D(V,E) is a graph D(V,E), where
  • E(i,j) there is a path from vertex i to
    vertex j in graph D.
  • Q How can we compute D?
Write a Comment
User Comments (0)
About PowerShow.com