+ All Categories
Home > Documents > DAA MINIPROJECT

DAA MINIPROJECT

Date post: 14-Jul-2015
Category:
Upload: vivek-singh
View: 124 times
Download: 1 times
Share this document with a friend
21
 Session – 2011-12 B.tech V semester (C.S.) Prim’s and kruskal Algorithm Submitted by:- Vivek kumar Uni. Sr N.-0903210130 Submitted to:- Mr. Neeraj Kumar Professor (CS & E Department) Submitted on:- 20-Nov-2011
Transcript

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 1/21

Session – 2011-12

B.tech V semester (C.S.)

Prim’s and kruskal Algorithm

Submitted by:-

Vivek kumar 

Uni. Sr N.-0903210130

Submitted to:-

Mr. Neeraj Kumar 

Professor 

(CS & E Department)

Submitted on:-

20-Nov-2011

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 2/21

Prim’s Algorithm:-

Prim's algorithm is a greedy algorithm that finds a minimum spanning

tree for a connected weighted undirected graph.

Theory & concept:-

Prim’s algorithm was developed in 1930 by Czech mathematician Vojtech

Jarník and later independently by computer scientist Robert C. Prim in 1957 and

rediscovered by Edsger Dijkstra in 1959. Therefore it is also sometimes called the

DJP algorithm, The Jarnik algorithm, or the Prim–Jarnik algorithm.

Prim's algorithm is a greedy algorithm that finds a minimum spanning

tree for a connected weighted undirected graph.

Prim’s algorithm continuously increases the size of a Minimum spanning tree, oneedge at a time, starting with a tree consisting of a single vertex, until it spans all

vertices. Input: A non-empty connected weighted graph with vertices V and edges E .

Initialize: V new = { x}, where x is an arbitrary node (starting point) from V , E new ={}

Repeat until V new = V :

Choose an edge (u, v) with minimal weight such that u is in V new and v is not.

if there are multiple edges with the same weight, any of them may be picked.

Add v to V new, and (u, v) to E new

Output: V new and E new describe a minimal spanning tree.

Algorithm & Its Analysis:-

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 3/21

MST-PRIM(V, E,w, r )

• Q ← { }

• for each u in V

•   do key[u] ← ∞

• π[u] ← NIL

• INSERT(Q, u)

• DECREASE-KEY(Q, r, 0)

• key[r ] ← 0

• while Q is not empty

•   do u ← EXTRACT-MIN(Q)

•   for each v in Adj[u]

•   do if v in Q and w(u, v) < key[v]

•   then π[v] ← u

• DECREASE-KEY(Q, v, w(u, v))

Prim’s Algorith Example : -

This is our original weighted graph. The numbers near the edges indicate

their weight. Now will find MST using Prim’s Algorithm :-

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 4/21

Solution :-

Vertex D has been arbitrarily chosen as a starting point. Vertices A, B, E

and F are connected to D through a single edge. A is the vertex nearest

to D and will be chosen as the second vertex along with the edge AD.

The next vertex chosen is the vertex nearest to either D or A. B is 9

away from D and 7 away from A, E is 15, and F is 6. F is the smallest

distance away, so we highlight the vertex F and the arc DF.

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 5/21

The algorithm carries on as above. Vertex B, which is 7 away from A, is

highlighted.

In this case, we can choose between C, E, and G. C is 8 away from B, E

is 7 away from B, and G is 11 away from F. E is nearest, so we highlight

the vertex E and the arc BE.

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 6/21

Here, the only vertices available are C and G. C is 5 away from E, and G

is 9 away from E. C is chosen, so it is highlighted along with the arc EC.

Vertex G is the only remaining vertex. It is 11 away from F, and 9 away

from E. E is nearer, so we highlight it and the arc EG.

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 7/21

 Now all the vertices have been selected and the minimum spanning tree

is shown in green. In this case, it has weight 39.

U Edge(u,v) V \ U

{ } {A,B,C,D,E,F,G}

{D}

(D,A) = 5 V(D,B) = 9

(D,E) = 15

(D,F) = 6

{A,B,C,E,F,G}

{A,D}

(D,B) = 9

(D,E) = 15

(D,F) = 6 V

(A,B) = 7

{B,C,E,F,G}

{A,D,F}

(D,B) = 9

(D,E) = 15

(A,B) = 7 V

(F,E) = 8

(F,G) = 11

{B,C,E,G}

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 8/21

{A,B,D,F}

(B,C) = 8

(B,E) = 7 V

(D,B) = 9

cycle

(D,E) = 15(F,E) = 8

(F,G) = 11

{C,E,G}

{A,B,D,E,F}

(B,C) = 8

(D,B) = 9

cycle

(D,E) = 15

cycle

(E,C) = 5 V

(E,G) = 9

(F,E) = 8

cycle

(F,G) = 11

{C,G}

{A,B,C,D,E,F}

(B,C) = 8

cycle

(D,B) = 9

cycle

(D,E) = 15cycle

(E,G) = 9 V

(F,E) = 8

cycle

(F,G) = 11

{G}

{A,B,C,D,E,F,G}

(B,C) = 8

cycle

(D,B) = 9

cycle

(D,E) = 15

cycle

(F,E) = 8

cycle

(F,G) = 11

cycle

{}

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 9/21

In this problem cost of MST is:- 39

Analysis of Prim’s algorithm :-

The performance of Prim's algorithm depends on how the priority queueis implemented:

• Suppose Q is a binary heap.

Initialize Q and first for loop: O(V lg V)Decrease key of r : O(lg V)while loop: |V| EXTRACT-MIN calls O(V lg V)

≤ |E| DECREASE-KEY calls implies → that O(E lg V)

Total: O(E lg V)

 

• Suppose we could do DECREASE-KEY in O(1) amortized time.

Then ≤ |E| DECREASE-KEY calls take O(E) time altogether →total time becomes O(V lg V + E).

• When a Q is implemented as a binary heap:-

The total time for prim's algorithm using a binary heap is O(|V| lg|V| + |E| lg |V|).

• When Q is implemented as a Fibonacci heap:-

The total time for prim's algorithm using a Fibonacci heap

to implement the priority queue Q is O(|V| lg |V| + |E|).

• When graph G = (V, E) is sparse:- |E| = Θ(|V|)

From above description, it can be seen that both versions of prim'salgorithm will have the running time O(|V| lg |V|). Therefore, the

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 10/21

Fibonacci-heap implementation will not make prim's algorithmasymptotically faster for sparse graph.

• When graph G is dense, i.e., |E| = Θ(|V|)

From above description, we can see that prim's algorithm with the binary heap will take |V|2 lg |V| time whereas prim's algorithm withFibonacci-heap will take |V|2. Therefore, the Fibonacci-heapimplementation does not make prim''s algorithm asymptoticallyfaster for dense graphs.

Comparing the time complexities of the two versions of prim'salgorithms. We can see that |E| is greater than |V|, the prim''s

algorithm with the Fibonacci-heap implementation will beasymptotically faster than the one with the binary-heapimplementation.

Algorithm Implementation “C” Program :-

#include<stdio.h>

#include<conio.h>

#define INF 1000

int vertex[10];

int wght[10][10];

int new_wght[10][10];

int closed[10];

int n;

int inclose(int i,int n1)

{

/*chk for the ith vertex presence in closed*/int j;

for(j=0;j<=n1;j++)

if(closed[j]==i)

return 1;

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 11/21

return 0;

}

void buildtree()

{int i=0,j,count=0;

int min,k,v1=0,v2=0;

closed[0]=0;

while(count<n-1)

{

min=INF;

for(i=0;i<=count;i++)for(j=0;j<n;j++)

if(wght[closed[i]][j]<min && !inclose(j,count))

{

min=wght[closed[i]][j];

v1=closed[i];

v2=j;

}

new_wght[v1][v2]=new_wght[v2][v1]=min;

count++;

closed[count]=v2;

 printf("\nScan : %d %d---------%d wght = %d\n",count,v1+1,v2+1,min);

getch();

}

}

void main()

{

int i,j,ed,sum=0;

clrscr();

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 12/21

 printf("\n\n\tPRIM'S ALGORITHM TO FIND SPANNINGTREE\n\n");

 printf("\n\tEnter the No. of Nodes : ");

scanf("%d",&n);

for(i=0;i<n;i++)

{

vertex[i]=i+1;

for(j=0;j<n;j++)

{

wght[i][j]=INF;

new_wght[i][j]=INF;

}

}

 printf("\n\nGetting Weight.\n");

 printf("\n\tEnter 0 if path doesn't exist between {v1,v2} else enter thewght\n");

for(i=0;i<n;i++)

for(j=i+1;j<n;j++)

{

 printf("\n\t%d -------- %d : ",vertex[i],vertex[j]);

scanf("%d",&ed);

if(ed>=1)

wght[i][j]=wght[j][i]=ed;

}

getch();

clrscr();

 printf("\n\n\t\tNODES CURRENTLY ADDED TO SPANNINGTREE\n\n");

 buildtree();

 printf("\n\n\t\tMINIMUM SPANNING TREE\n\n");

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 13/21

 printf("\n\t\tLIST OF EDGES\n\n");

for(i=0;i<n;i++)

for(j=i+1;j<n;j++)

if(new_wght[i][j]!=INF){

 printf("\n\t\t%d ------ %d = %d ",vertex[i],vertex[j],new_wght[i][j]);

sum+=new_wght[i][j];

}

 printf("\n\n\t Total Weight : %d ",sum);

getch();

}

Output :-

PRIM'S ALGORITHM TO FIND SPANNING TREE

Enter the No. of Nodes : 7

Getting Weight.

Enter 0 if path doesn't exist between {v1,v2} else enter the wght1 -------- 2 : 7

1 -------- 3 : 0

1 -------- 4 : 5

1 -------- 5 : 0

1 -------- 6 : 0

1 -------- 7 : 0

2 -------- 3 : 82 -------- 4 : 9

2 -------- 5 : 7

2 -------- 6 : 0

2 -------- 7 : 0

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 14/21

3 -------- 4 : 0

3 -------- 5 : 5

3 -------- 6 : 0

3 -------- 7 : 04 -------- 5 : 15

4 -------- 6 : 6

4 -------- 7 : 0

5 -------- 6 : 8

5 -------- 7 : 9

6 -------- 7 : 11

NODES CURRENTLY ADDED TO SPANNING TREEScan : 1 1---------4 wght = 5

Scan : 2 4---------6 wght = 6

Scan : 3 1---------2 wght = 7

Scan : 4 2---------5 wght = 7

Scan : 5 5---------3 wght = 5

Scan : 6 5---------7 wght = 9

MINIMUM SPANNING TREE

LIST OF EDGES

1 ------ 2 = 7

1 ------ 4 = 5

2 ------ 5 = 7

3 ------ 5 = 5

4 ------ 6 = 6

5 ------ 7 = 9

Total Weight : 39

Kruskal ’s Algorithm :-

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 15/21

Kruskal's algorithm is a greedy algorithm that finds a minimum spanningtree fora connectedweighted graph. This means it finds a subset of the edges thatforms a tree that includes every vertex, where the total weightof all theedges in the tree is minimized.

It finds a safe edge to add to the growing forest by finding, of all edges thatconnect any two tree in the forest ,an edge (u,v).

kruskal Algorithm & its Analysis:-

MST-KRUSKAL(G, w)•  A ← Ø• for each vertex v V [G]• do MAKE-SET(v)• sort the edges of  E into nondecreasing order by weight w• for each edge (u, v) E , taken in nondecreasing order by weight• do if FIND-SET(u) ≠ FIND-SET(v)• then A  A U {(u, v)}• UNION(u, v)•  return A

kruskal’s Algorith Example :-This is our original weightedgraph. The numbers near the edges indicate their weight.

Solution :-• AD and CE are the shortest arcs, with length 5, and AD has been

arbitrarily chosen, so it is highlighted.

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 16/21

• CE is now the shortest arc that does not form a cycle, with length5, so it is highlighted as the second arc.

• The next arc, DF with length 6, is highlighted using much thesame method.

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 17/21

• The next-shortest arcs are AB and BE, both with length 7. AB ischosen arbitrarily, and is highlighted. The arc BD has beenhighlighted in red, because there already exists a path (in green)

 between B and D, so it would form a cycle (ABD) if it werechosen.

• The process continues to highlight the next-smallest arc, BE withlength 7. Many more arcs are highlighted in red at this stage: BC

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 18/21

 because it would form the loop BCE, DE because it would formthe loop DEBA, and FE because it would form FEBAD.

• Finally, the process finishes with the arc EG of length 9, and theminimum spanning tree is found.

Analysis :-

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 19/21

Initialize the set A: O(1)

First for loop: |V| MAKE-SETs

Sort E: O(E lg E)

Second for loop: O(E) FIND-SETs and UNIONs

Assuming the implementation of disjoint-set data structure, that usesunion by rank and path compression: O((V + E) α(V)) + O(E lg E)

• Since G is connected,

|E| ≥ |V| − 1⇒ O(E α(V)) + O(E lg E).

α(|V|) = O(lg V) = O(lg E).

Therefore, Total time is O(E lg E).

• |E| ≤ |V|2 ⇒lg |E| = O(2 lg V) = O(lg V).

Therefore, O(E lg V) time. (If edges are already sorted, O(E α(V)),which is almost linear.)

Algorithm Implementation “C” Program :-

#include<stdio.h>#define INF 1000char vertex[10];int wght[10][10];int span_wght[10][10];

int source;struct Sort{int v1,v2;int weight;}que[20];int n,ed,f,r;

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 20/21

int cycle(int s,int d){int j,k;if(source==d)

return 1;for(j=0;j<n;j++)if(span_wght[d][j]!=INF && s!=j){if(cycle(d,j))return 1;}return 0;}void build_tree(){int i,j,w,k,count=0;for(count=0;count<n;f++){i=que[f].v1;

 j=que[f].v2;w=que[f].weight;span_wght[i][j]=span_wght[j][i]=w;source=i;k=cycle(i,j);if(k)span_wght[i][j]=span_wght[j][i]=INF;elsecount++;}}void swap(int *i,int *j){

int t;t=*i;*i=*j;*j=t;}void main(){

5/12/2018 DAA MINIPROJECT - slidepdf.com

http://slidepdf.com/reader/full/daa-miniproject 21/21

int i,j,k=0,temp;int sum=0;clrscr();

 printf("\n\n\tKRUSKAL'S ALGORITHM TO FIND SPANNING

TREE\n\n"); printf("\n\tEnter the No. of Nodes : ");scanf("%d",&n);for(i=0;i<n;i++){

 printf("\n\tEnter %d value : ",i+1);fflush(stdin);scanf("%c",&vertex[i]);for(j=0;j<n;j++){


Recommended