Graph Algorithms PowerPoint PPT Presentation

presentation player overlay
1 / 31
About This Presentation
Transcript and Presenter's Notes

Title: Graph Algorithms


1
Graph Algorithms
  • Graph theory plays an important role in computer
    science
  • Used to model many problems
  • Example ) model a network topology as a graph

2
Weighted Adjacency Matrix (1)
G (V, E, w)
1
1
2
3
1
3
1
5
2
A
2
3
3
3
Weighted 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

4
Graph 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)

5
Minimum 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
6
Prims 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

7
G 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
8
Select 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
9
Step 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
10
a
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
11
Step 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
12
a
b
c
d
a
a
1
3
b
A
5
b
c
c
d
1
2
d
D
minDc,Aa,c min2,32
13
Step 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
14
Final MST
a
b
c
d
a
a
1
3
b
A
5
b
c
c
d
1
2
d
D
The minimum weight 4.
15
Time 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)

16
Parallel 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
17
Select 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
18
Update 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
19
Step 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

20
processor 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
21
Finding 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
22
Finding 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
23
Finding 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
24
processor 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
25
Step 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
26
processor 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
27
Step 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
28
processor 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
29
Time 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
30
Speedup and Efficiency
  • Speedup
  • Efficiency

31
Cost-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.
Write a Comment
User Comments (0)
About PowerShow.com