Skip to main content

Sample C Program To Implement Minimum Spanning Tree Using Functions, Pointers & Structures.


STEP 1: Start the program.
STEP 2: Assign MAX a constant value of 20.
STEP 3: Declare a structure edge with structure variable *front.
STEP 4: Define the functions and variables required in the program globally.
STEP 5: Call the function create_graph() inside the main function.

STEP 6: Call the function make_tree().
STEP 7: Set a for loop using i.
for(i = 1; i <= count; i++)
STEP 8: Print the values of tree[i].u and tree[i].v
STEP 9: Stop the program.


STEP 1: Declare the local variables.
STEP 2: Read the number of nodes n.
STEP 3: Calculate the value of max_edge as
max_edge = n * (n - 1) / 2.
STEP 4: Set a for loop using i.
for(i = 0; i <= max_edge; i++)
STEP 5: Read the values of origin and destiny.

STEP 6: Check whether origin and destiny are equal to 0.
STEP 7: If so exit the loop using break statement
STEP 8: Read the weight of the current edge wt.
STEP 9: Check the following condition.
if(origin > n || destin > n || origin <= 0 || destin <= 0)
STEP 10: If any of the condition is true, print “invalid edge!” and decrement the value of i by 1.

STEP 11: Else call the function insert_pque(origin, destin, wt).
STEP 12: Check whether i is less than n - 1, if so then exit with an error message.


STEP 1: Declare the local variables.
STEP 2: Initialize the variable tmp, node1, node2.
STEP 3: Assign the values for node1, node2.
STEP 4: Calculate and print the values of root_n1 and root_n2.
STEP 5: If the two roots are not equal, call the function insert_tree.

STEP 6: Assign the value of root_n1 to father [root_n2].

FUNCTION OF INSERT_TREE (int i, int j, int wt )

STEP 1: Declare the local variables.
STEP 2: Increment the value of count.
STEP 3: Assign values to tree[count].u, tree[count].v, tree[count].weight.

FUNCTION OF INSERT_PQUE (int i, int j, int wt )

STEP 1: Declare the local variables
STEP 2: Allocate the memory space of size, struct edge for tmp.
STEP 3: Assign values to tmp -> u, tmp -> v, tmp -> weight.
STEP 4: Check for the following condition.
if(front == NULL || tmp -> weight < front -> weight)
STEP 5: If any of the conditions are true assign the value of front to tmp -> link and assign tmp to

STEP 6: Else set a while loop and declare following.
q = q -> link;
tmp -> link = q -> link;
q -> link = tmp;
if(q ->> link == NULL)
tmp -> link = NULL;


STEP 1: Declare the local variable.
STEP 2: Assign the value of front to temp.
STEP 3: Print the values of processed edge and return the value of tmp.


#define MAX 20

struct edge
int u;
int v;
int weight;
struct edge *link;

*front = NULL;
int father[MAX];
struct edge tree[MAX];
int n;
int wt_tree = 0;
int count = 0;

void make_tree();
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;


printf(" Edge to be included in spanning tree are: \n ");

for(i = 1; i <= count; i++)
printf(" %d -> ", tree[i].u);
printf(" %d \n ", tree[i].v);

printf(" Weight of this minimum spanning tree is: %d \n ", wt_tree);



int i, wt, max_edge, origin, destin;
printf(" Enter no. of nodes: ");
scanf(" %d ", &n);
max_edge = n * (n - 1) / 2;

for(i = 0; i <= max_edge; i++)
printf(" Enter edge %d (0 0 to quit): ", i);
scanf(" %d %d ", &origin, &destin);

if((origin == 0) && (destin == 0))
printf(" Enter weight for this edge: ");
scanf(" %d ", &wt) ;

if(origin > n || destin > n || origin <= 0|| destin <= 0)
printf(" Invalid edge! ");
i- -;
else insert_pque(origin, destin, wt);

if(i < n - 1)
printf(" Spanning tree is not possible.");

return 0;


void make_tree()

struct edge *tmp;
int node1, node2, root_n1, root_n2;

while(count < n - 1)
tmp = del_pque();
node1 = tmp -> u;
node2 = tmp -> v;
printf(" n1 =%d ", node1);
printf(" n2 = %d ", node2);
while(node1 > 0)
root_n1 = node1;
node1 = father[node1];
while(node2 > 0)
root_n2 = node2;
node2 = father[node2];
printf(" Rootn1 = %d \n ", root_n1);
printf(" Rootn2 = %d \n ", root_n2);

if(root_n1 != root_n2)
insert_tree(tmp -> u, tmp -> v, tmp -> weight);
wt_tree = wt_tree + tmp -> weight;
father[root_n2] = root_n1;


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

printf(" This Edge inserted in the spanning tree\n ");
tree[count].u = i;
tree[count].v = j;
tree[count].weight = wt;


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

struct edge *tmp, *q;
tmp = (struct edge *)malloc ( sizeof( struct edge ) ) ;
tmp -> u = i;
tmp -> v = j;
tmp -> weight = wt;

if(front == NULL || tmp -> weight < front -> weight)
tmp -> link = front;
front = tmp;
q = front;
while(q -> link != NULL && q -> link -> weight <= tmp -> weight)
q = q -> link;

tmp -> link = q -> link;
q -> link = tmp;
if(q -> link == NULL)
tmp -> link = NULL;


struct edge *del_pque()

struct edge *tmp;
tmp = front;
printf(" Edge Processed is %d -> %d %d \n ",tmp -> u, tmp -> v, tmp -> weight);
front = front -> link;
return tmp;



If you liked the post/article or if you think this post/article helped you in any way, then please show your appreciation by filling up at least one form from the options given below:

-- Click Here To Win Rewards Each Time When You Access Internet.

-- Click Here To Add The Best Online Dictionary Resources... Right To Your Browser.

-- Click Here To Instantly Check All Your Webmail Accounts... Right From Your Browser.

-- Click Here To Get Up To 40% Off On Your Favorite Books.

-- Click Here To Register In AllSchoolStuff - Helping Parents Buy School & Learning Products.

-- Click Here To Apply To Multiple Colleges With A Single Application.

-- Click Here To Register In SwarnIQ - A Social Reading Experience.

-- Click Here To Win Rewards, Cash, Shopping Vouchers & Exciting Gifts For Completing Surveys.

-- Click Here To Submit Your Resume - TimesJobs (Freshers & Experienced)

-- Click Here To Submit Your resume - Shine (Freshers & Experienced)


Popular posts from this blog

Tell Me Something About Yourself - Interview Answers.

Try to introduce some of your most important employment-oriented skills as well as your education and accomplishments to the interviewer. Answer to this question is very important because it positions you for the rest of the interview. That's why this statement is often called the "Positioning Statement".

One should take the opportunity to show his/her communication skills by speaking clearly and concisely in an organized manner. Since there is no right or wrong answer for this question hence it is important to appear friendly.


1) I am a person with strong interpersonal skills and have the ability to get along well with people. I enjoy challenges and looking for creative solutions to problems.

2) Besides the details given in my resume, I believe in character values, vision and action. I am quick in learning from mistakes. I am confident that the various tests that you have conducted will corroborate my competencies aptitude and right attitude for the…

100 C Programs Frequently Asked in Technical Interview.

Why Should We Hire You? - Interview Answers

The interviewer asks this question just to find out how your skills, experience and knowledge can add value to the job.Well to answer this question you need to learn what they are looking for and then tie them with your strengths. This ensures that you are a viable fit for the position.

The more detail you give the stronger your answers will be. Try to summarize all your accomplishments and relate what makes you unique.

The question can also be

"Why should we hire you over other candidates?"
"Why should we hire you over the others waiting to be interviewed?"

Your answers can be:

1) You should hire me because I’m the best person for the job. I realize that there are likely other candidates who also have the ability to do this job. Yet I bring an additional quality that makes me the best person for the job - my passion for excellence. I am passionately committed to producing truly class results.

2) I believe I’m the perfect fit for this position. I’m very detai…