0% found this document useful (0 votes)
95 views7 pages

WAP in C To Implement Minimal Spanning Tree by (A) Kruskal's Algorithm

This C program implements Kruskal's algorithm to find the minimum spanning tree of a graph using a priority queue. It takes in a weighted undirected graph as input, inserts the edges into a priority queue sorted by weight. It then removes edges one by one and inserts them into the spanning tree if they do not form a cycle. The edges of the final minimum spanning tree are printed as output.

Uploaded by

Ankur Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views7 pages

WAP in C To Implement Minimal Spanning Tree by (A) Kruskal's Algorithm

This C program implements Kruskal's algorithm to find the minimum spanning tree of a graph using a priority queue. It takes in a weighted undirected graph as input, inserts the edges into a priority queue sorted by weight. It then removes edges one by one and inserts them into the spanning tree if they do not form a cycle. The edges of the final minimum spanning tree are printed as output.

Uploaded by

Ankur Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

(9)WAP in C to implement minimal spanning Tree

by
(a) Kruskal’s Algorithm
#include<stdio.h>

#include<conio.h>

#include<iostream.h>

#include<process.h>

#include<alloc.h>

#define MAX 10

struct edge

int u;

int v;

int weight;

struct edge *link;

}*front=NULL;

struct edge tree[MAX];

int father[MAX];

int n,wt_tree=0,count=0;

void create_graph();

void maketree();

//void display();

void insert_tree(int i,int j,int wt);


void insert_pque(int i,int j,int wt);

struct edge *del_pque();

void main()

int i,j;

clrscr();

create_graph();

maketree();

cout<<endl;

cout<<"Edges to be include in spanning tree are :"<<endl;

for(i=1;i<=count;i++)

cout<<tree[i].u<<"->";

cout<<tree[i].v;

cout<<"\t";

getch();

void create_graph()

int i,max_edge,origin,destin,wt;

cout<<"Enter the node";

cin>>n;
max_edge=n*(n-1)/2;

for(i=1;i<=max_edge;i++)

cout<<"enetr edge (0 0 to quit) "<<i;

cin>>origin>>destin;

if((origin==0)&&(destin==0))

break;

cout<<"enter weight for this edge";

cin>>wt;

if(origin>n||destin>n||origin<=0||destin<=0)

cout<<"Invalid edge";

i--;

else

insert_pque(origin,destin,wt);

if(i<n-1)

cout<<"Spanning tree is not possible";

exit(1);

}
void maketree()

struct edge *temp;

int node1,node2,root_n1,root_n2;

while(count<n-1)

temp=del_pque();

node1=temp->u;

node2=temp->v;

cout<<"n1="<<node1;

cout<<"n2="<<node2;

while(node1>0)

root_n1=node1;

node1=father[node1];

while(node2>0)

root_n2=node2;

node2=father[node2];

cout<<"Rootn1 "<<root_n1;

cout<<"Rootn2 "<<root_n2;
if(root_n1!=root_n2)

insert_tree(temp->u,temp->v,temp->weight);

wt_tree=wt_tree+temp->weight;

father[root_n2]=root_n1;

void insert_tree(int i,int j,int wt)

cout<<"This Edge Inserted In the spanning tree";

count++;

tree[count].u=i;

tree[count].v=j;

tree[count].weight=wt;

void insert_pque(int i,int j,int wt)

struct edge *temp,*q;

temp=(struct edge *)malloc(sizeof(struct edge));

temp->u=i;

temp->v=j;

temp->weight=wt;
if(front==NULL||temp->weight<front->weight)

temp->link=front;

front=temp;

else

q=front;

while(q->link!=NULL && q->link->weight<=temp->weight)

q=q->link;

temp->link=q->link;

q->link=temp;

if(q->link==NULL)

temp->link=NULL;

struct edge *del_pque()

struct edge *temp;

temp=front;

cout<<endl;

cout<<"Edge Processed is "<<temp->u<<"->"<<temp->v<<" "<<temp-


>weight;

cout<<endl;
front=front->link;

return temp;

You might also like