Graph Algorithm
Graph Algorithm
John Mellor-Crummey
2
Terminology
• Graph G = (V,E)
—V is a finite set of points called vertices
—E is a finite set of edges
• Undirected graph
—edge e ∈ E
– unordered pair (u,v), where u,v ∈ V
• Directed graph
—edge (u,v) ∈ E
– incident from vertex u
– incident to vertex v
4
More Terminology
5
Adjacency Matrix for Graph G = (V,E)
Adjacency matrix
representation
Adjacency list
representation
Undirected graph
7
Minimum Spanning Tree
8
Minimum Spanning Tree
9
Computing a Minimum Spanning Tree
Prim's sequential algorithm
// add u to T
// recompute d[:] now
// that u is in T
10
Minimum Spanning Tree: Prim's Algorithm
• Parallelization prospects
—outer loop (|V| iterations): hard to parallelize
– adding 2 vertices to tree concurrently is problematic
—inner loop: relatively easy to parallelize
– consider which vertex is closest to MST in parallel
12
Parallel Formulation of Prim's Algorithm
Data partitioning
– partition adjacency matrix in a 1-D block fashion (blocks of columns)
– partition distance vector d accordingly
distance array
adjacency matrix
• In each step,
—each process first identifies the locally closest node
—perform a global reduction to select globally closest node
– minloc reduction: node id and distance
—leader inserts node into MST
—broadcasts choice to all processes
—each process updates its part of d vector locally based on choice
14
Parallel Formulation of Prim's Algorithm
15
16
Single-Source Shortest Paths
17
Computing Single-Source Shortest Paths
Dijkstra’s sequential algorithm
// add u to T
// recompute L[:]
// now that u is in T
18
Parallel Formulation of Dijkstra's Algorithm
19
All-Pairs Shortest Paths
20
All-Pairs Shortest Path
21
All-Pairs Shortest Path
22
All-Pairs Shortest Path Dijkstra's Algorithm
• Analysis
—requires no interprocess communication
– provided adjacency matrix is replicated at all processes
—parallel run time: Θ(n2)
• Algorithm is cost optimal
—asymptotically same # of ops in parallel as in sequential version
• However: can only use n processors (one per source)
23
All-Pairs Shortest Path Dijkstra's Algorithm
• Connectivity matrix of G
—matrix A* = (ai*,j)
– ai*,j = 1 if there is a path from vi to vj or i = j,
– ai*,j = ∞ otherwise.
• Computing A*
—use all-pairs shortest paths algorithm on the connectivity matrix
—resulting matrix A*: ai,j = 1 if di,j < ∞ or i = j
25
Connected Components
26
Connected Components
27
Connected Components Parallel Formulation
28
Connected Components Parallel Formulation
1. Partition adjacency
Graph G matrix of the graph G
into two parts
29
Connected Components Parallel Formulation
30
Connected Components Parallel Formulation
31
Connected Components
Θ(nα(n) log p)
32
Algorithms for Sparse Graphs
33
References
34