Parallel Computation: Minimum Spanning Tree and Prim's Algorithm - Prof. Edgar Gabriel, Study notes of Computer Science

A part of the cosc 4397 – parallel computation course notes by edgar gabriel. It covers the topic of minimum spanning trees and prim's algorithm for parallel computation. Explanations, examples, and code snippets for both sequential and parallel implementations.

Typology: Study notes

Pre 2010

Uploaded on 08/17/2009

koofers-user-5nl
koofers-user-5nl 🇺🇸

6 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COSC 4397 – Parallel Computation
Edgar Gabriel
COSC 4397
Parallel Computation
Graph Algorithms (II)
Edgar Gabriel
Spring 2006
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Parallel Computation: Minimum Spanning Tree and Prim's Algorithm - Prof. Edgar Gabriel and more Study notes Computer Science in PDF only on Docsity!

COSC 4397 – Parallel ComputationEdgar Gabriel

COSC 4397

Parallel ComputationGraph Algorithms (II)

Edgar GabrielSpring 2006

2

COSC 4397 – Parallel ComputationEdgar Gabriel

Minimum Spawning Tree

•^

Minimum Spawning Tree:– Spawning tree: undirected Graph G’ being a

subgraph of G containing all vertices

– Minimum spawning tree: spawning tree with

minimum weight 1

(^35)

8

4

4 4

3

4 4

4

COSC 4397 – Parallel ComputationEdgar Gabriel

Prim’s Algorithm (II)

• V

: vector containing the vertices already added to theT spawning tree

• (V-V

):T

set of vertices, which have not yet been added

to the spawning tree.

• d:

distance vector, e.g.

d[i]

contains the minimum

weight of vertex

i

to

any

vertex in the spawning tree

5

COSC 4397 – Parallel ComputationEdgar Gabriel

Example (I)

(^51)

3

4

4 2 0 1

3

1

        

        

∞ ∞ ∞ ∞ ∞

∞ ∞ ∞ ∞ ∞

0 5

2

5 0 4 1

4 0 2 1

1 2 0 5 3

1 5 0 1

3

3 1 0

Arbitrary starting point r=

(^51)

3

4

4 2 0 1

3

1

[^

]∞

=^

d

[^

] 1

= T

V -1:= undefined

node considered in the following search, sinceVertex is already in the spawning tree

7

COSC 4397 – Parallel ComputationEdgar Gabriel

Example (III)

(^51)

3

4

4 2 0 1

3

1

[^

] 1

= T

V

[^

] 3

= d

(^51)

3

4

4 2 0 1

3

1

[^

] 1

= T

V

[^

] 3

= d

8

COSC 4397 – Parallel ComputationEdgar Gabriel

Example (IV)

(^51)

3

4

4 2 0 1

3

1

[^

] 5

= T

V

[^

] 3

= d

10

COSC 4397 – Parallel ComputationEdgar Gabriel

Sequential implementation (II)

int find_u ( int vt[N], int vtcount, int d[N] ){

int i, j, found;int current_min=MY_INF, current_minloc=-1;for ( i=0; i<N; i++ ) {

for (found = 0, j=0; j<vtcount; j++ ) {

if (i==vt[j]) found=1; } if (found) continue;if ( d[i] < current_min) {

current_minloc = i;current_min = d[i]; } } return current_minloc; }

11

COSC 4397 – Parallel ComputationEdgar Gabriel

Sequential implementation (III)

void update_d ( int d[N], int vt[N], int vtcount,

int N, int u, int a[N][N] )

int i, j, found;for ( i=0; i<N; i++ ) {

for ( found=0, j=0; j<vtcount; j++ ) {

if (i==vt[j]) found=1; } if (found) continue;d[i] = min (d[i], a[u][i]); } return; }

13

COSC 4397 – Parallel ComputationEdgar Gabriel

Parallel Algorithm 1 (II)

int find_u ( int vt[N], int vtcount, int d ){

int min[2], gmin[2], i, rank;MPI_Comm_rank ( MPI_COMM_WORLD, &rank );min[0] = d;min[1] = rank;for ( i=0; i < vtcount; i++ ) {

if ( vt[i] == rank ) {

min[0] = MY_INF;break; } } MPI_Allreduce ( min, gmin, 1, MPI_2INT, MPI_MINLOC,

MPI_COMM_WORLD);

return gmin[1];}

14

MPI_MINLOC and MPI_MAXLOC (I)^ COSC 4397 – Parallel ComputationEdgar Gabriel

•^

Operators for reduction operations returning theminimum/maximum value and the process owning theminimum and maximum value

•^

Special MPI data types have to be used^ – MPI_2INT:

array of two integers.

  • Element zero contains min/max value– Element one contains the rank of the process owningminimal/maximal value

– MPI_FLOAT_INT

structure consisting of a float

and an int^ struct {

float

val;

int

rank;

16

COSC 4397 – Parallel ComputationEdgar Gabriel

Parallel Algorithm 1 (III)

void update_d ( int *d, int *vt, int vtcount, int u,

int a[N] )

int i, rank;MPI_Comm_rank ( MPI_COMM_WORLD, &rank);for ( i=0; i < vtcount; i++ ) {

if ( vt[i] == rank ) {

return; } } d = min (d, a[u]);return; }

17

COSC 4397 – Parallel ComputationEdgar Gabriel

Parallel Algorithm 2 (I)

•^

Each process owns a certain number of columns of theadjacency matrix

•^

Each process owns the according elements of thedistance vector

• V

is replicated on each processT

d A

19

COSC 4397 – Parallel ComputationEdgar Gabriel

Parallel Algorithm 2 (III)

void update_d ( int *d, int *vt, int vtcount, int u,

int a[N] )

int i, j, rank, found;for ( i=0; i<nx; i++ ) {

for ( found=0, j=0; j<vtcount; j++ ) {

if ((

cxnx+i*

)==vt[j]) {

found=1; } } if (found) continue;d[i] = my_min (d[i], a[u][i]); } return; }