0% found this document useful (0 votes)
280 views

Data Structure and Algorithm Lab Manual

This document contains information about a Data Structures with Algorithms Lab course offered at Brindavan College of Engineering. It provides details such as the academic year, semester, course title, code, duration and name and contact of the faculty member. It also lists 10 programming problems related to data structures and algorithms that students will implement as part of the course.

Uploaded by

Aman Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
280 views

Data Structure and Algorithm Lab Manual

This document contains information about a Data Structures with Algorithms Lab course offered at Brindavan College of Engineering. It provides details such as the academic year, semester, course title, code, duration and name and contact of the faculty member. It also lists 10 programming problems related to data structures and algorithms that students will implement as part of the course.

Uploaded by

Aman Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Brindavan College of Engineering

Dwarakanagar, Bagalur Main Road, Yelahanka

Department of MCA
Data Structures with Algorithms Lab (20MCA16)

1. GENERAL INFORMATION:

Academic Year: 2020-2021 Semester: I

Title Code Duration

Data Structures with Algorithms Lab 20MCA16 4 HRS

2. FACULTY DETAILS:
DETAILS

Name Ms.Manisha Kumari

Department MCA

E-mail address [email protected]


Data Structures with Algorithms Lab 20MCA16

Program List

1.Write a C program to Implement the following searching techniques


a. Linear Search b. Binary Search.

2. Write a C program to implement the following sorting algorithms


using user defined functions:
a. Bubble sort (Ascending order)
b. Selection sort (Descending order).

3. Write a C Program implement STACK with the following operations


a. Push an Element on to Stack
b. Pop an Element from Stack

4. Implement a Program in C for converting an Infix Expression to


Postfix Expression.

5. Implement a Program in C for evaluating a Postfix Expression.

6. Write a C program to simulate the working of a singly linked list


providing the following operations: a. Display & Insert
b. Delete from the beginning/end
c. Delete a given element

7. Obtain the Topological ordering of vertices in a given graph with


the help of a c programming.

8. Check whether a given graph is connected or not using DFS


method using C programming.

9. From a given vertex in a weighted connected graph, find shortest


paths to other vertices Using Dijkstra's algorithm (C programming)

10. Find Minimum Cost Spanning Tree of a given undirected graph


using Kruskal's algorithm ( C programming)
Data Structures with Algorithms Lab 20MCA16

Program 1
1.Write a C program to Implement the following searching techniques

a. Linear Search b. Binary Search.

a. Linear Search

What is a Linear Search?

A linear search, also known as a sequential search, is a method of


finding an element within a list. It checks each element of the list
sequentially until a match is found or the whole list has been searched.

#include<stdio.h>
int main ()
{
int a[20],i,x,n;
clrscr();
printf("How many elements?\n");
scanf("%d",&n);
printf("Enter %d elements:\n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("Enter element to search:\n");
scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;

if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
getch();
return 0;
}
Data Structures with Algorithms Lab 20MCA16

OUTPUT

What is a Binary Search?


• A Binary Search is a sorting algorithm, that is used to search an element
• in a sorted array.
• A binary search technique works only on a sorted array, so an array
must be sorted to apply binary search on the array.
• It is a searching technique that is better than the linear search
technique as the number of iterations decreases in the binary search.

1.b.Binary Search
#include<stdio.h>
#include<conio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
clrscr();
printf("Enter number of elements \n");
scanf("%d",&n);
printf("Enter %d integers in ascending order\n", n);
for(i = 0; i < n; i++)
{
scanf("%d",&array[i]);
}
printf("Enter value to find\n");
Data Structures with Algorithms Lab 20MCA16

scanf("%d", &key);
low = 0; high =n - 1;
mid = (low+high)/2;
while (low <= high)
{
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key)
{
printf("%d found at location %d\n", key, mid);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list\n", key);
getch();
return 0;
}

OUTPUT
Data Structures with Algorithms Lab 20MCA16

Program 2

2. Write a C program to implement the following sorting algorithms


using user defined functions:
a. Bubble sort (Ascending order)
b. Selection sort (Descending order).

#include<stdio.h>
#include<conio.h>

void bubble(int a[], int n)


{
int i, j, temp;
for(i=0; i<n;i++)
{
for(j=0; j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

void selection(int a[], int n)


{
int i, j, temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
Data Structures with Algorithms Lab 20MCA16

}
}
}

void main()
{
int a[20],n,i, opt;
clrscr();
printf("Enter the size of the array:");
scanf("%d",&n);
for(;;)
{
printf("\n ***** Sorting *****\n");
printf("\n 1. Selection Sort (Descending Order) \n 2. Bubble Sort
(Ascending Order) \n 3. Exit ");
printf("\n Enter your
option:"); scanf("%d",&opt);
switch(opt)
{
case 1: printf("\nEnter array
elements:\n"); for(i=0;i<n;i++)
scanf("%d",&a[i]);
selection(a, n);
printf("\nElements in Descending order:\n");
for(i=0;i<n;i++)
printf("%d \t", a[i]);
break;
case 2: printf("\nEnter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble(a, n);
printf("\nElements in ascending order:\n");
for(i=0;i<n;i++)
printf("%d \t", a[i]);
break;
case 3:
default: exit(0);
}
}
}
Data Structures with Algorithms Lab 20MCA16

OUTPUT

Enter the size of the array : 3

***** Sorting *****


1. Selection Sort (Descending Order)
2. Bubble Sort (Ascending Order)
3. Exit
Enter your option: 1
Enter array elements: 55 98 74 1 7
Elements in Descending order : 98 74 55 7 1

***** Sorting *****


1. Selection Sort (Descending Order)
2. Bubble Sort (Ascending Order)
3. Exit
Enter your option:2
Enter array elements: 55 77 99 5 4
Elements in ascending order : 4 5 55 77 99

***** Sorting *****


1. Selection Sort (Descending Order)
2. Bubble Sort (Ascending Order)
3. Exit

Enter your option:3


Data Structures with Algorithms Lab 20MCA16

Program 3

3. Write a C Program implement STACK with the following operations


a. Push an Element on to Stack
b. Pop an Element from Stack

Algorithm:
Data Structure: An Array with TOP as the pointer.
• Start
• Initialize the array of 10 elements and name it as stack
• Initialize other variables like top in the beginning of the program
• Provide the choice to the users for the different operations on stack like
Push(insert),Pop(delete),Display and Exit
• If the choice= push then call the function push()
• If the choice= pop then call the function pop()
• If the choice= display then call the function display()
• If the choice= exit then exit from the program
• End

Function push()
• Check for the overflow condition of the stack
• if (TOP>= SIZE) then Print “Stack is full”
• If not overflow, increment the value of top
• Get the element to be inserted onto the stack from the user
• Assign it as the topmost value, stack[top]
Function pop ()
• Check for the underflow(empty) condition of the stack
• if TOP<0 Print “Stack is Empty”
• If not empty, Output the element to be deleted from the stack
• Decrement the value of top.
Function display()
• Display all the elements of the stack
Data Structures with Algorithms Lab 20MCA16

#include<stdio.h>
int stack[10],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=10]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING
ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t
4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
Data Structures with Algorithms Lab 20MCA16

printf("\n\t EXIT POINT ");


break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
Data Structures with Algorithms Lab 20MCA16

{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}

OUTPUT

Enter the size of STACKmax[100]:5


STACK OPERATIONS USING ARRAY
1.PUSH
2.POP
3.DISPLAY
4.EXIT

Enter the choice:1


Enter a value to be pushed:6

Enter the choice :1


Enter a value to be pushed: 2

Enter the choice :1


Enter a value to be pushed:4

Enter the choice :1


Enter a value to be pushed:1

Enter the choice :1


Enter a value to be pushed:5

Enter the choice :1


Data Structures with Algorithms Lab 20MCA16

Stack is overflow.

Enter the choice : 3

The elements in the stack


5
1
4
2
6
Press next choice

Enter the choice : 2


The popped element is 5

Enter the choice: 3

The elements in the stack


1
4
2
6

Enter the choice : 2


The popped element is 1

Enter the choice : 2


The popped element is 4

Enter the choice : 2


The popped element is 2

Enter the choice : 2


The popped element is 6

Enter the choice : 2


Stack is underflow
Data Structures with Algorithms Lab 20MCA16

Enter the choice : 3


Stack is Empty

Enter the choice: 6


Please Enter a valid choice (1/2/3/4)

Enter the choice: 4


Data Structures with Algorithms Lab 20MCA16

Program 4

4.Implement a Program in C for converting an Infix


Expression to Postfix Expression

#include<stdio.h>
#include<conio.h>
#include<string.h>

char infix[50],postfix[50],stack[25];
int top=-1;
void convert_fun();
int precedence(char);
void push(char);
char pop();

void main()
{
clrscr();
printf("Enter the infix expression\n");
scanf("%s",infix);
convert_fun();
printf("\n\t THE INFIX EXPRESSION :%s",infix);
printf("\n\t THE POSTFIX EXPRESSION:%s",postfix);
getch();
}
void convert_fun()
{
int i=0,j=0;
char c,tmp;
push('#');
Data Structures with Algorithms Lab 20MCA16

while(i<strlen(infix))
{
c=infix[i++];
switch(c)
{
case '(' :push(c);
break;
case ')' :tmp=pop();
while(tmp!='(')
{
postfix[j++]=tmp;
tmp=pop();
}
break;
case'+':
case'-':
case'*':
case'/':
case'^':
while(precedence(stack[top])>=precedence(c))
{
tmp=pop();
postfix[j++]=tmp;
}
push(c);
break;
default: postfix[j++]=c;
break;
}
}
while(top>0)
Data Structures with Algorithms Lab 20MCA16

{
tmp=pop();
postfix[j++]=tmp;
}
}
void push(char c)
{
stack[++top]=c;
}

char pop()
{
return(stack[top--]);
}
int precedence(char c)
{
switch(c)
{
case'^':return (3);
case'*':
case'/': return(2);
case '+':
case'-':return(1);
case'(':
case')':return(0);
case'#':return(-1);
}
}
Data Structures with Algorithms Lab 20MCA16

OUTPUT

1.Enter the infix expression a+b


The infix expression is a+b
The postfix expression is ab+

2.Enter the infix expression A + B * C + D


The infix expression is A + B * C + D
The postfix expression is A B C * + D +
Data Structures with Algorithms Lab 20MCA16

Program 5

5. Implement a Program in C for evaluating an Postfix


Expression

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
int top,s[100];
int cal(char sym,int op1,int op2)
{
switch(sym)
{
case'+' : return(op1+op2);
case'-' : return(op1-op2);
case'*' : return(op1*op2);
case'/' : return(op1/op2);
case'^' : return(pow(op1,op2));
}
return 0;
}
void main()
{
int i,op1,op2;
char pf[100],sym;
clrscr();
top=-1;
printf("enter the postfix expression:");
Data Structures with Algorithms Lab 20MCA16

gets(pf);
for(i=0;i<strlen(pf);i++)
{
sym=pf[i];
if(isdigit (sym))
{
s[++top]=sym-'0';
}
else
{
op2=s[top--];
op1=s[top--];
s[++top]=cal(sym,op1,op2);
}
}
printf("\n value of %s is %d",pf,s[top--]);
getch();
}

OUTPUT

1.Enter the postfix expression: 3 4 * 2 5 * +


Value of 3 4 * 2 5 * + is 22

2. Enter the postfix expression: 2^3*5


Value of 2^3*5 is 22
Data Structures with Algorithms Lab 20MCA16

Program 6

6.Write a C program to simulate the working of a singly


linked list providing the following operations:
a. Display & Insert
b. Delete from the beginning/end
c. Delete a given element

#include <stdio.h>
#include<stdlib.h>

void display();
void insert();
void delete_begin();
void delete_end();
void delete_pos();

struct node
{
int info;
struct node *next;
};

struct node *start=NULL;

int main()
{
int choice;
clrscr();
while(1){
Data Structures with Algorithms Lab 20MCA16

printf("\n MENU\n");
printf(" 1.Insert\n");
printf(" 2.Display\n");
printf(" 3.Delete from beginning\n");
printf(" 4.Delete from the end\n");
printf(" 5.Delete from specified position\n");
printf(" 6.Exit\n");
printf("--------------------------------------\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{

case 1: insert();
break;
case 2: display();
break;
case 3: delete_begin();
break;
case 4: delete_end();
break;
case 5: delete_pos();
break;
case 6: exit(0);
break;
default: printf("\n Wrong Choice:\n");
break;
}
}
}
Data Structures with Algorithms Lab 20MCA16

void insert()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}

void display()
{
struct node *ptr;
if(start==NULL)
{
printf("\nList is empty:\n");
return;
}
Data Structures with Algorithms Lab 20MCA16

else
{
ptr=start;
printf("\nThe List elements are:\n");
while(ptr!=NULL)
{
printf("%d\t",ptr->info );
ptr=ptr->next ;
}
}
}
void delete_begin()
{
struct node *ptr;
if(ptr==NULL)
{
printf("\nList is Empty:\n");
return;
}
else
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is :%d\t",ptr->info);
free(ptr);
}
}
void delete_end()
{
struct node *temp,*ptr;
if(start==NULL)
Data Structures with Algorithms Lab 20MCA16

{
printf("\nList is Empty:");
exit(0);
}
else if(start->next ==NULL)
{
ptr=start;
start=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
}
void delete_pos()
{
int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nThe List is Empty:\n");
Data Structures with Algorithms Lab 20MCA16

exit(0);
}
else
{
printf("\nEnter the position of the node to be
deleted:\t");
scanf("%d",&pos);
if(pos==0)
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is:%d\t",ptr->info
);
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++) { temp=ptr; ptr=ptr->next ;
if(ptr==NULL)
{
printf("\nPosition not Found:\n");
return;
}
}
temp->next =ptr->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
}
}
Data Structures with Algorithms Lab 20MCA16

OUTPUT

MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit

Enter your choice: 1


Enter the data value for the node: 2

MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Enter your choice: 1
Enter the data value for the node: 3

MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
Data Structures with Algorithms Lab 20MCA16

6.Exit
Enter your choice: 1
Enter the data value for the node: 2

MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Enter your choice: 1
Enter the data value for the node: 5

MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Enter your choice: 1
Enter the data value for the node: 6

MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Data Structures with Algorithms Lab 20MCA16

Enter your choice: 2

The list elements are:


6 5 2 3 2

MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Enter your choice: 3
The deleted element is
:6

MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Enter your choice: 4
The deleted element is
:2

MENU
1.Insert
2.Display
3.Delete from beginning
Data Structures with Algorithms Lab 20MCA16

4.Delete from the end


5.Delete from specified position
6.Exit
Enter your choice: 5
Enter the position of the node to be deleted : 1
The deleted element is : 5

Enter your choice: 5


Enter the position of the node to be deleted : 5
Position not found

MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Enter your choice: 6
Data Structures with Algorithms Lab 20MCA16

Program 7

7. Obtain the Topological ordering of vertices in a given


graph with the help of a c programming.

#include<stdio.h>
#include<conio.h>
void main()
{
int n, count =0, am[10][10], indeg[10], flag[10], i, j, k;
clrscr();
printf("Enter number of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
indeg[i]=0;
flag[i]=0;
}
printf("\nEnter adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&am[i][j]);
printf("\nMatrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",am[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
Data Structures with Algorithms Lab 20MCA16

for(j=0;j<n;j++)
indeg[i] += am[j][i];
printf("\nThe topological ordering is:\n");
while(count<n)
{
for(k=0;k<n;k++)
if((indeg[k]==0) && (flag[k]==0))
{
printf("%d\n",k);
flag[k]=1;
count++;
for(i=0;i<n;i++)
if(am[k][i]==1)
indeg[i]--;
}
}
getch();
}
OUTPUT:
Enter number of vertices: 6
Enter adjacency matrix:
011000
000100
000010
001001
000000
000010
The topological ordering is:
013524
Data Structures with Algorithms Lab 20MCA16

Program 8

8. Check whether a given graph is connected or not using


DFS method using C programming.

#include<stdio.h>
#include<conio.h>
void dfs(int n, int a[10][10], int u, int visited[])
{
int v;
visited[u]=1;
for(v=0;v<n;v++)
if((a[u][v]==1)&& (visited[v]==0))
dfs(n,a,v,visited);
}
void main()
{
int n, i, j, a[10][10], visited[10],flag, connected;
clrscr();
printf("Enter number of vertices:");
scanf("%d",&n);
printf("\nEnter adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
connected=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
visited[j]=0;
dfs(n,a, i, visited);
flag=0;
Data Structures with Algorithms Lab 20MCA16

for(j=0;j<n;j++)
if(visited[ j]==0)
flag=1;
if(flag==0)
connected=1;
}
if(connected==1)
printf("\nGraph is connected");
else
printf("\nGraph is not connected");
getch();
}

OUTPUT
1. Enter number of vertices: 6
Enter adjacency matrix:
010100
101100
010100
111000
000001
000010
Graph is not connected

2: Enter number of vertices: 3


Enter adjacency matrix:
011
101
110
Graph is connected
Data Structures with Algorithms Lab 20MCA16

Program 9

9. From a given vertex in a weighted connected graph, find


shortest paths to other vertices Using Dijkstra's algorithm
(C programming)

#include<stdio.h>
#include<conio.h>
void dijkstra(int n, int v, int cost[10][10],int dist[10])
{
int count, u, i, w, visited[10], min;
for(i=0;i<n;i++)
{
visited[i]=0;
dist[i]=cost[v][i];
}
visited[v]=1;
dist[v]=1;
count=2;
while(count<=n)
{
min=999;
for(w=0;w<n;w++)
if((dist[w]<min) && (visited[w]!=1))
{
min=dist[w];
u=w;
}
visited[u]=1;
count++;
for(w=0;w<n;w++)
if((dist[u]+cost[u][w]<dist[w]) && (visited[w]!=1))
Data Structures with Algorithms Lab 20MCA16

dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n, v, cost[10][10], dist[10], i, j;
clrscr();
printf("Enter number of vertices:");
scanf("%d",&n);
printf("\nEnter cost matrix (for infinity, enter 999):\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
printf("\nEnter source vertex:");
scanf("%d",&v);
dijkstra(n,v,cost,dist);
printf("\nShortest path from \n");
for(i=0;i<n;i++)
if(i!=v)
printf("\n%d -> %d = %d", v, i, dist[i]);
getch();
}
Data Structures with Algorithms Lab 20MCA16

OUTPUT

Enter number of vertices:7


Enter cost matrix (for infinity, enter 999):
0 2 999 3 999 999 999
2 0 9 999 1 4 999
999 9 0 999 999 3 999
3 999 999 0 5 999 7
999 1 999 5 0 999 4
999 4 3 999 999 0 6
999 999 999 7 4 6 0
Enter source vertex: 0
Shortest path from
0 -> 1 = 2
0 -> 2 = 9
0 -> 3 = 3
0 -> 4 = 3
0 -> 5 = 6
0 -> 6 = 7
Data Structures with Algorithms Lab 20MCA16

Program 10

10. Find Minimum Cost Spanning Tree of a given


undirected graph using Kruskal's algorithm ( C
programming)

#include<stdio.h>
#include<conio.h>
void main()
{
int n, v, u,cost[10][10], parent[10]={0}, i, j;
int count=1, mincost=0, min, a, b;
clrscr();
printf("Enter number of vertices:");
scanf("%d",&n);
printf("\nEnter cost matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
while(count<n)
{
min=999;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
{
Data Structures with Algorithms Lab 20MCA16

min=cost[i][j];
a=u=i;
b=v=j;
}
while(parent[u])
u=parent[u];
while(parent[v])
v=parent[v];
if(u!=v)
{
count++;
printf("\nEdge(%d, %d) = %d", a, b, min);
mincost+=min;
parent[v]=u;
}
cost[a][b]=cost[b][a]=999;
}
printf("\nMinimum cost = %d", mincost);
getch();
}
Data Structures with Algorithms Lab 20MCA16

OUTPUT

Enter number of vertices: 7

Enter cost matrix (for infinity, enter 999)

0 2 999 3 999 999 999


2 0 9 999 1 4 999
999 9 0 999 999 3 999
3 999 999 0 5 999 7
999 1 999 5 0 999 4
999 4 3 999 999 0 6
999 999 999 7 4 6 0

Edge(2, 5) = 1
Edge(1, 2) = 2
Edge(1, 4) = 3
Edge(3, 6) = 3
Edge(2, 6) = 4
Edge(5, 7) = 4

Minimum cost = 17

You might also like