Professional Documents
Culture Documents
Graph Algorithms: Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar
Graph Algorithms: Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar
• Transitive Closure
• Connected Components
f
3 6 3 4
2
1 1
(a) (b)
0 1 0 0 0
1 0 1 0 1
2 3
A= 0 1 0 0 1
0 0 0 0 1
0 1 1 1 0
4 5
2 1 3 5
2 3
3 2 5
4 5
4 5
5 2 3 4
8 2 2
a 3 a b c d e f
(d) Final minimum
d[] 1 0 2 1 1 3
spanning tree 1 f
3
a 0 1 3 ∞∞ 3
b 5 b 1 0 5 1 ∞∞
5
c c 3 5 0 2 1 ∞
1
2
PSfrag
1
replacements d
e
∞ 1 2 0 4 ∞
∞∞ 1 4 0 5
4 e f 2 ∞∞∞ 5 0
d
Prim’s minimum spanning tree algorithm.
Minimum Spanning Tree: Prim’s Algorithm
• This node is inserted into MST, and the choice broadcast to all
processors.
A n (b)
PSfrag replacements
Processors 0 1 i p-1
• It grows this set based on the node closest to source using one
of the nodes in the current shortest path set.
Single-Source Shortest Paths: Dijkstra’s Algorithm
0 1 0 1
0 2 3 ∞ ∞ ∞ ∞ ∞ ∞ 0 2 3 4 5 3 ∞ ∞ ∞
B
B ∞ 0 ∞ ∞ ∞ 1 ∞ ∞ ∞ C
C
B
B ∞ 0 ∞ ∞ ∞ 1 3 4 3 C
C
B
B ∞ ∞ 0 1 2 ∞ ∞ ∞ ∞ C
C
B
B ∞ ∞ 0 1 2 ∞ 3 ∞ ∞ C
C
∞ ∞ ∞ 0 ∞ ∞ 2 ∞ ∞ ∞ ∞ ∞ 0 3 ∞ 2 3 ∞
B C B C
1 2
B C B C
A =B
B ∞ ∞ ∞ ∞ 0 ∞ ∞ ∞ ∞
C
C A =B
B ∞ ∞ ∞ ∞ 0 ∞ ∞ ∞ ∞
C
C
∞ ∞ ∞ ∞ ∞ 0 2 3 2 ∞ ∞ ∞ ∞ 3 0 2 3 2
B C B C
B C B C
∞ ∞ ∞ ∞ 1 ∞ 0 1 ∞ ∞ ∞ ∞ ∞ 1 ∞ 0 1 ∞
B C B C
B C B C
∞ ∞ ∞ ∞ ∞ ∞ ∞ 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 0 ∞
B C B C
@ A @ A
∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 0
0 1 0 1
0 2 3 4 5 3 5 6 5 0 2 3 4 5 3 5 6 5
B
B ∞ 0 ∞ ∞ 4 1 3 4 3 C
C
B
B ∞ 0 ∞ ∞ 4 1 3 4 3 C
C
B
B ∞ ∞ 0 1 2 ∞ 3 4 ∞ C
C
B
B ∞ ∞ 0 1 2 ∞ 3 4 ∞ C
C
∞ ∞ ∞ 0 3 ∞ 2 3 ∞ ∞ ∞ ∞ 0 3 ∞ 2 3 ∞
B C B C
4
A8 =
B C B C
A =B
B ∞ ∞ ∞ ∞ 0 ∞ ∞ ∞ ∞
C
C
B
B ∞ ∞ ∞ ∞ 0 ∞ ∞ ∞ ∞
C
C
∞ ∞ ∞ ∞ 3 0 2 3 2 ∞ ∞ ∞ ∞ 3 0 2 3 2
B C B C
B C B C
∞ ∞ ∞ ∞ 1 ∞ 0 1 ∞ ∞ ∞ ∞ ∞ 1 ∞ 0 1 ∞
B C B C
B C B C
∞ ∞ ∞ ∞ ∞ ∞ ∞ 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 0 ∞
B C B C
@ A @ A
∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 0
Matrix-Multiplication Based Algorithm
• Complexity is O(n3).
Dijkstra’s Algorithm: Parallel Formulation
computation
z }| {
3 communication
n z }| {
TP = Θ + Θ(n log p). (1)
p
(k)
• If vertex vk is not in the shortest path from vi to vj , then pi,j is the
(k−1)
same as pi,j .
(k) (k)
• If f vk is in pi,j , then we can break pi,j into two paths – one from
vi to vk and one from vk to vj . Each of these paths uses vertices
from {v1, v2, . . . , vk−1}.
Floyd’s Algorithm
(
(k)
w(vin
, vj ) o if k = 0
di,j = (k−1) (k−1) (k−1) (2)
min di,j , di,k + dk,j if k ≥ 1
This equation must be computed for each pair of nodes and for
k = 1, n. The serial complexity is O(n3).
Floyd’s Algorithm
Floyd’s all-pairs shortest paths algorithm. This program computes the all-pairs
shortest paths of the graph G = (V, E) with adjacency matrix A.
Floyd’s Algorithm: Parallel Formulation Using 2-D Block
Mapping
√ √
• Matrix D (k) is divided into p blocks of size (n/ p) × (n/ p).
√
• In general, during the k th iteration, each of the p processes
√
containing part of the k th row send it to the p − 1 processes in
the same column.
√
• Similarly, each of the p processes containing part of the k th
√
column sends it to the p − 1 processes in the same row.
Floyd’s Algorithm: Parallel Formulation Using 2-D Block
Mapping
n
√
p
n
√
p (1,1) (1,2)
(2,1)
(i − 1) √np + 1, (j − 1) √np + 1
i √np , j √np
(a) (b)
√ √
(a) Matrix D (k) distributed by 2-D block mapping into p × p
subblocks, and (b) the subblock of D (k) assigned to process Pi,j .
Floyd’s Algorithm: Parallel Formulation Using 2-D Block
Mapping
k column k column
(k−1)
dk,r
k row
(k−1)
dl,k
(k)
dl,r PSfrag replacements
(a) (b)
Floyd’s parallel formulation using the 2-D block mapping. P∗,j denotes all the
processes in the j th column, and Pi,∗ denotes all the processes in the ith row.
The matrix D (0) is the adjacency matrix.
Floyd’s Algorithm: Parallel Formulation Using 2-D Block
Mapping
computation
z }| { z communication
}| {
n3 n2
TP = Θ + Θ √ log p .
p p
Floyd’s Algorithm: Parallel Formulation Using 2-D Block
Mapping
t+1
t+2
t+3
t+4
t+5
1 2 3 4 5 6 7 8 9 10
Processors
• Similarly, elements of the first column are sent from process P i,j
to process Pi,j+1.
√
• Each such step takes time Θ(n/ p).
√
• After Θ( p) steps, process P√p,√p gets the relevant elements of
the first row and first column in time Θ(n).
computation
z }| {
3 communication
n z }| {
TP = Θ + Θ(n).
p
Maximum Number
of Processes Corresponding Isoefficiency
for E = Θ(1) Parallel Run Time Function
Dijkstra source-partitioned Θ(n) Θ(n2 ) Θ(p3)
Dijkstra source-parallel Θ(n2/ log n) Θ(n log n) Θ((p log p)1.5 )
Floyd 1-D block Θ(n/ log n) Θ(n2 log n) Θ((p log p)3 )
Floyd 2-D block Θ(n2/ log2 n) Θ(n log2 n) Θ(p1.5 log3 p)
Floyd pipelined 2-D block Θ(n2) Θ(n) Θ(p1.5)
Transitive Closure
2 3 5 7 8
Perform DFS on the graph to get a forest – eac tree in the forest
corresponds to a separate connected component.
1 4 6 9
12
10
2 3 5 11
(a)
1 4 6 9
12
10
2 3 5 11
(b)
1 7 1 0 1 1 1 0 0 0
2 1 0 1 0 0 0 0 Processor 1
3 1 1 0 1 1 0 0
2 4 6 4 1 0 1 0 1 0 0
5 0 0 1 1 0 0 0
6 0 0 0 0 0 0 1 Processor 2
3 5 7 0 0 0 0 0 1 0
(a) (b)
1 7 1 7
2 4 6 2 4 6
3 5 3 5
(c) (d)
1 7 1 7
2 4 6 2 4 6
3 5 3 5
(e) (f)
(a) (b)
(c)
(a) (b)
c
h
11
15
0 1
(b) After the 2nd random number assignment (c) Final maximal independent set
ments 7 2 2
(3) h:4, f:6, i:inf 0 1 4 7 3 6 10 4 ∞
• For this reason, we use multiple queues, one for each processor.
Each processor builds its priority queue only using its own
vertices.