Professional Documents
Culture Documents
Topic Overview
Defi nitions and Representation Minimum Spanning Tree: Prim's Algorithm Single-Source Shortest Paths: Dijkstra's Algorithm All-Pairs Shortest Paths Transitive Closure Connected Components Algorithms for Sparse Graphs
An undirected graph G is a pair (V; E), where V is a nite set of points called vertices and E is a nite set of edges An edge e 2 E is an unordered pair (u; v), where u; v 2 V .In a directed graph, the edge e is an ordered pair (u; v). An edge (u; v) is incident from vertex u and is incident to vertex v. A path from a vertex v to a vertex u is a sequence hv0; v1; v2; : : : ; vki of vertices where v0 = v, vk = u, and (vi; vi+1) 2 E for i = 0; 1; : : : ; k 1. The length of a path is de ned as the number of edges in the path.
De nitions and Representation e 4 5 3 2 2 1 (a) (a) An undirected graph and (b) a directed graph. 1 (b) 6 3 6 4 5 f
An undirected graph is connected if every pair of vertices is connected by a path. A forest is an acyclic graph, and a tree is a connected acyclic graph. A graph that has weights associated with each edge is called a weighted graph.
Graphs can be represented by their adjacency matrix or an edge (or vertex) list. Adjacency matrices have a value ai;j = 1 if nodes i and j share an edge; 0 otherwise. In case of a weighted graph, ai;j = wi;j , the weight of the edge. The adjacency list representation of a graph G = (V; E) consists of an array Adj[1::jV j] of lists. Each list Adj[v] is a list of all vertices adjacent to v. For a grapn with n nodes, adjacency matrices take T heta(n2) space and adjacency list takes (jEj) space.
A spanning tree of an undirected graph G is a subgraph of G that is a tree containing all the vertices of G. In a weighted graph, the weight of a subgraph is the sum of the weights of the edges in the subgraph. A minimum spanning tree (MST) for a weighted undirected graph is a spanning tree with minimum weight.
Prim's algorithm for nding an MST is a greedy algorithm. Start by selecting an arbitrary vertex, include it into the current MST. Grow the current MST by inserting into it the vertex closest to one of the vertices already in current MST.
a 1 5 3
3 f d[ ]
a e
b f
1 0 5 1 1 1 a 0 1 3 11 3 5 1 b 1 0 5 1 11 2 1 c 3 5 0 1 4 d 11 2 0 1 e 11 1 4 0 5 f 2 111 5 0 c a b d e f d[ ] 1 0 2 14 1 a 0 1 3 11 3 b 1 0 5 1 11 2 1 c 3 5 0 1 1 4 3 e
c 1 d 2
5 selected b
f 3
4 d 11 2 0 1 e 11 1 4 0 5 f 2 111 5 0 c a b d e c 1 2 d a 3 f 3 5 1 2 d 4 e 4 e 1 5
d[ ]
1 0 2 1 4 3
a 0 1 3 11 3 b 1 0 5 1 11 c 3 5 0 21 1 4 d 11 2 0 1 e 11 1 4 0 5 f 2 111 5 0 c a b d e f d[ ] 1 0 2 11 3 a 0 1 3 11 3 b 1 0 5 1 11 2 1 c 3 5 0 1 d 11 2 04 1 11 1 4 0 5 e 2 111 5 0 f
(d )
F in a l m in im u m spanning tree 1 b 5
c 1
if edge (r; v) exists set d[v] := w(r; v); else set d[v] := 1; while VT 6= V do begin nd a vertex u such that d[u] := minfd[v]jv 2 (V VT := VT [ fug; VT ) do d[v] := minfd[v]; w(u; v)g; endwhile end PRIM MST for all v 2 (V VT )g;
The algorithm works in n outer iterations it is hard to execute these iterations concurrently. The inner loop is relatively easy to parallelize. Let p be the number of processes, and let n be the number of vertices. The adjacency matrix is partitioned in a 1-D block fashion, with distance vector d partitioned accordingly. In each step, a processor selects the locally closest node. followed by a global reduction to select globally closest node. This node is inserted into MST, and the choice broadcast to all processors. Each processor updates its part of the d vector locally.
n A
(b)
Processors
p-1
The partitioning of the distance array d and the adjacency matrix A among p processes.
VT )g;
B E 2
1 F
2 3 1 2 H 1
G
I 1
3 C 1
D 2
B B B B B B B B B
B
A =
B
0 2 3 1 0 111 11
0
1 111
111 1111
11111
1111 1111111
@ 0
B B B B B B B B B
B
A =
B
1111111 0 2 3 1 0 11 4 0 11
111 1111
1111
1111 1111111
1111111
0
B B B B
A =B
0 2 3 4 5 3 1 0 111 1 11 1 0 3
0 1 2
111 1 3 4 3 C
3
B
B
B B B B B
111 1111
1
0
11
3
1 1111
C
1111
1111 1111111
3
1
2 3
0 1 0
2
1 1
C C
C
C C
@ 1111111 0
B B B B
1 0 A 5 6 3 4 3 4
2 3
0 2 3 4 5 3 1 0 11 4 1 0 1 2 11 1 0 3
111 1111
0
5 1
3 1
1
C
C
C
8
A =
B
B
B
B B B B B
1111
1111 1111111
3 0
1
1111
2 3
0 1 0
2
1
C
C C
C
C
@ 1111111
0 A
PDF to Word