Docsity

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

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; }