Title: Graph Algorithms
1Graph Algorithms
- Graph theory plays an important role in computer
science - Used to model many problems
- Example ) model a network topology as a graph
2Weighted Adjacency Matrix (1)
G (V, E, w)
1
1
2
3
1
3
1
5
2
A
2
3
3
3Weighted Adjacency Matrix (2)
- The space required to store a graph with n
vertices is O(n2) - Useful for dense graphs
- Dense graphs
- example complete graph
- Sparse graphs
- example ring
4Graph algorithms I will cover
- Discuss algorithms for dense graphs
- Minimum spanning tree Prims algorithm
- Single-source shortest paths Dijkstras
algorithm - All-pairs shortest paths
- Dijkstras algorithm
- Floyds algorithm (if time permits)
5Minimum Spanning Tree
a
- Spanning tree of G a subgraph of G that is a
tree containing all vertices of G - Minimum spanning tree (MST) of G
- G a weighted undirected graph
- A spanning tree with the minimum weight
- Applications of MST
- the minimum length of cable necessary to connect
computers
1
3
5
b
c
1
2
d
The minimum weight 4
6Prims algorithm (serial)
- A greedy algorithm
- select a starting vertex (root)
- choose the next vertex and edge that are
guaranteed to be a MST - continue until all vertices have been selected
7G original graph
A the weighted adjacency matrix for G
a
b
c
d
a
a
1
3
b
5
A
b
c
c
1
d
2
d
8Select the root
Dv the least weight from any vertex to v in
MST during its construction
a
b
c
d
a
a
1
3
b
A
5
b
c
c
update
root
d
1
2
d
D
9Step 1 select the next vertex and edge
a
b
c
d
a
a
1
3
b
A
5
b
c
c
d
1
2
d
D
Pick a minimum weighted edge in D, which is not
in a MST ?minDa,Dc,Ddmin1,5,11
10a
b
c
d
a
a
1
3
b
A
5
b
c
c
d
1
2
d
2
D
minDa,Ad,a min1, 81
minDc,Ad,c min5,22
11Step 2 select the next vertex and edge
a
b
c
d
a
a
1
3
b
A
5
b
c
c
d
1
2
d
D
Pick a minimum weighted edge in D, which is not
in a MST ? minDa,Dc1
12a
b
c
d
a
a
1
3
b
A
5
b
c
c
d
1
2
d
D
minDc,Aa,c min2,32
13Step 3 select the next vertex and edge
a
b
c
d
a
a
1
3
b
A
5
b
c
c
d
1
2
d
D
Pick a minimum weighted edge in D, which is not
in a MST ? minDc2
14Final MST
a
b
c
d
a
a
1
3
b
A
5
b
c
c
d
1
2
d
D
The minimum weight 4.
15Time complexity of Prims algorithm (serial)
- The number of steps n-1
- Finding a minimum weighted edge in the array D in
each step O(n) - The total complexity ?(n2)
16Parallel Formulation of Prims algorithm
a
b
c
d
- Partition the adjacency matrix A and the distance
array D among p processors - Each processor handles n/p vertices.
- Example) n 4, p 2, n/p4/2 2
a
a
b
b
c
c
d
d
processor 0
processor 1
17Select the root
a
b
c
d
a
a
a
b
b
1
3
c
c
5
b
c
d
d
1
2
d
Processor 0 broadcasts the root to all other
processors
processor 0
processor 1
18Update the array D
a
b
c
d
a
a
a
b
b
1
3
c
c
5
b
update
c
d
d
root
1
2
d
processor 0
processor 1
19Step 1 select the next vertex and edge
- Find the minimum in D, which is not in MST by
using All-to-one reduction - Store it (the next vertex) in processor 0
- Processor 0 broadcasts the next vertex to all
other processors by using one-to-all broadcast
20processor 0
processor 1
a
b
c
d
a
min11
min5,11
1
3
one-to-all reduction
5
Processor 0 picks a vertex d
b
c
Processor 0 broadcasts d
1
2
d
all-to-one broadcast
Pick a minimum weighted edge in D, which is not
in a MST
21Finding a minimum all-to-one reduction
1
4,7,1,2,3
3
2
4
3,5,2,8,6
8,7,3,9,5
9,6,8,4,7
22Finding a minimum all-to-one reduction
1
min4,7,1,2,31
3
2
4
min3,5,2,8,62
min8,7,3,9,53
min9,6,8,4,74
23Finding a minimum all-to-one reduction
1
min4,7,1,2,31
2
4
3
3
2
4
min3,5,2,8,62
min8,7,3,9,53
min9,6,8,4,74
1
Again, on find min1,2,3,4 1
24processor 0
processor 1
a
b
c
d
a
a
a
b
b
1
3
c
c
5
b
d
d
c
1
update
2
2
d
minDa,Ad,a min1, 81
minDc,Ad,c min5,22
25Step 2 select the next vertex and edge
processor 0
processor 1
a
b
c
d
a
min11
min22
1
3
5
one-to-all reduction
b
Processor 0 picks a vertex a
c
Processor 0 broadcasts a
1
2
d
all-to-one broadcast
26processor 0
processor 1
a
b
c
d
a
a
a
1
b
b
3
c
c
5
b
c
d
d
1
2
d
minDc,Aa,c min2,32
27Step 3 select the next vertex and edge
processor 0
processor 1
a
b
c
d
a
1
min ?
min22
3
5
b
one-to-all reduction
c
Processor 0 picks a vertex c
1
Processor 0 broadcasts c
2
d
all-to-one broadcast
28processor 0
processor 1
Final MST
a
b
c
d
a
a
a
1
b
b
3
c
c
5
b
c
d
d
1
2
d
29Time complexity of Prims algorithm (parallel)
- The number of steps ?(n)
- Finding a minimum weighted edge in the array D in
each step - The total complexity
computation
communication
30Speedup and Efficiency
31Cost-optimal
- For a cost-optical parallel formulation,
- the cost of solving MST in parallel grows at he
same rate as does the cost of the serial
algorithm. -
-
-
-