Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 33

AAD

07-CE-26
PRACTICAL-1
Aim:- Write a recursive and non recursive function to compute n! Compare the space
requirements of no recursive function with those of recursive version.

#include<stdio.h>
#include<conio.h>
unsigned int recr_factorial(int n);
unsigned int iter_factorial(int n);
void main()
{
int n,i;
long fact;
clrscr();
printf(“Enter the number: “);
scanf(“%d”,&n);
if(n==0)
printf(“Factorial of 0 is 1\n”);
else
{
printf(“Factorial of %d Using Recursive Function is %d\n”,n,recr_factorial(n));
printf(“Factorial of %d Using Non-Recursive Function is %d\n”,n,iter_factorial(n));
}
getch();
}

/* Recursive Function*/
unsigned int recr_factorial(int n)
{
return n>=1 ? n * recr_factorial(n-1) : 1;
}

/* Non-Recursive Function*/
unsigned int iter_factorial(int n)
{
int accu = 1;
int i;
for(i = 1; i <= n; i++)
{
accu *= i;
}
return accu;
}

Output:
1
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26

Iteration v/s Recursion


Demerits of recursive algorithms

1. Many programming languages do not support recursion, hence recursive mathematical


function is implemented using iterative methods.
2. A recursive procedure can be called from within or outside itself and to ensure its proper
functioning it has to save in some order the return addresses so that, a return to the proper
location will result when the return to a calling statement is made.
3. The recursive programs needs considerably more storage and will take more time.

Demerits of iterative methods

1. Mathematical functions such as factorial and fibonacci series generation can be easily
implemented using recursion than iteration.
2. In iterative techniques looping of statement is very much necessary.

Recursion is a top down approach to problem solving. It divides the problem into pieces or
selects out one key step, postponing the rest.
Iteration is more of a bottom up approach. It begins with what is known and from this constructs
the solution step by step. The iterative function obviously uses time that is O(n) where as
recursive function has an exponential time complexity.
It is always true that recursion can be replaced by iteration and stacks. It is also true that stack
can be replaced by a recursive program with no stack.

2
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
PRACTICAL-2
Aim:-The array a[0:9]=[4,2,6,7,1,0,9.8.5.3] is to be sorted using insertion sort. Show the
best case, average case and worst case analysis.

#include<stdio.h>
#include<conio.h>
#include<time.h>
void analyze_array(int arr[]);
void selection(int elements[]);
int maxsize=5,best_case[]={1,2,3,4,5},avg_case[]={1,4,2,5,3},worst_case[]={5,4,3,2,1};
void main()
{ int i;
clrscr();
printf("BEST CASE:-\n");
analyze_array(best_case);
printf("\n\nAVERAGE CASE:-\n");
analyze_array(avg_case);
printf("\n\nWORST CASE:-\n");
analyze_array(worst_case);
getch();
}
void analyze_array(int arr[])
{
int i=0;
clock_t start_time,end_time;
printf("\nArray before sorting:\t");
for (i = 0; i < maxsize; i++)
printf("[%i] ", arr[i]);
start_time = clock();
selection(arr);
end_time = clock();
printf("\nArray after sorting:\t");
for (i = 0; i < maxsize; i++)
printf("[%i] ", arr[i]);
printf("\n\nThe time taken: %f Units\n",(end_time - start_time) / (double) CLK_TCK);
}
void selection(int arr[])
{
int i, j;
int min, temp;
3
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
for (i = 0; i < maxsize-1; i++)
{
min = i;
for (j = i+1; j < maxsize; j++)
{
if (arr[j] < arr[min])
{ min = j;
//delay(100);
}
}
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
//delay(100);
}
}

Output:

PRACTICAL-3

4
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
Aim:- Convert an array into link list.

#include <stdio.h>
#define QSIZE 5
typedef struct{
int elem[QSIZE];
int front;
int rear;
int count;
}QType;
void initialize(QType *);
int empty(QType *);
int full(QType *);
int rem(QType *);
void insert(QType *, int);
main()
{
QType Q int code, code1, i, n;
int i1, i2;
initialize(&Q);
printf("initially: is empty?\n %d\n", empty(&Q));
printf("is full?\n %d\n", full(&Q));
printf("Enter 1 to continue, 0 to stop: ");
scanf("%d", &code);
while(code!=0)
{ printf("Enter 2 to insert an item, 3 to remove an item: ");
scanf("%d", &code1);
switch(code1)
{ case 2: printf("Enter an integer to be inserted: ");
scanf("%d", &i1);
insert(&Q, i1);
break;
case 3: i2=rem(&Q);
if(i2==0)
printf("Nothing is removed\n");
else
printf("%d is removed\n", i2);
break;
default: printf("Wrong code\n");
}
printf("Currently in the queue:\n");
if(Q.count==0)
printf("Nothing\n");
else
{
i=Q.front; n=0;
5
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
do{
printf("%d ", Q.elem[i]);
n++;
if(i>=QSIZE-1)
i=0;
else
i++;
}while(n<Q.count);
printf("\n");
}
printf("Enter 1 to continue, 0 to stop: ");
scanf("%d", &code);
}
return 0;
}

void initialize(QType *q)


/* set head counter and tail counter to 0; */
/* set counter of elements in the queue to 0 */
{ q->front=q->rear=0;
q->count=0;
}

int empty(QType * q)
/* This function will determine if the queue is empty by */
/* testing the value of a counter of elements in the queue. */
/* If the queue is empty, this function will print the message */
/* "queue is empty" and return a value of 1. */
/* Otherwise, no message is displayed, and zero is returned. */
{
if (q->count == 0)
{ printf("QUEUE IS EMPTY\n");
return 1;
}
else return 0;
}

int full(QType * q)
/* This function will determine if the queue is full by */
/* testing the value of a counter of elements in the queue.*/
/* If the queue is full, prints "queue is full" and returns*/
/* a value of 1. Otherwise, does not print anything */
/* and returns zero. */
{
if (q->count == QSIZE)
{ printf("QUEUE IS FULL\n");
6
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
return 1;
}
else return 0;
}
int rem(QType *q)
/* This function will return the item currently located */
/* in the head of the queue. Then, the head counter */
/* will be incremented. */
{
int temp;

if (empty(q)) return 0;
temp = q->elem[q->front];
q->front=(q->front+1) % QSIZE;
(q->count)--;
return temp;
}
void insert(QType *q, int item)
/* The integer 'item' is inserted at the tail of the */
/* queue and the tail counter is incremented. */
{
if (full(q)) return;
q->elem[q->rear] = item;
/* printf("in the insert: %d inserted\n", q->elem[q->rear]);*/
q->rear = (q->rear + 1) % QSIZE;
(q->count)++;
return;
}

Output:

7
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26

PRACTICAL-4
Aim:-Write a method to multiply two lower triangular matrices. The result matrix is to be
stored in a two dimensional array.

8
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
#include<stdio.h>
#include<conio.h>
#define ROW 3
#define COL 3
void main()
{
int a[ROW][COL],b[ROW][COL],m[ROW][COL];
int i,j,k,sum=0;

//initialize lower triangular matrices


clrscr();
printf("Enter elements for first lower triangular matrix A\n");
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
if(i>=j)
{
printf("\nEnter the element A[%d][%d]",i,j);
scanf("%d",&a[i][j]);
}
else
{
a[i][j]=0;
}
}

printf("\nEnter elements for second lower triangular matrix B");

for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
if(i>=j)
{
printf("\nEnter the element B[%d][%d] ",i,j);
scanf("%d",&b[i][j]);
}
9
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
else
{
b[i][j]=0;
}
}

//Display both matrices

printf("\nMatrix A is:\n");
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
printf("%5d\t",a[i][j]);
}
printf("\n");
}

printf("\nMatrix B is:\n");

for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
printf("%5d\t",b[i][j]);
}
printf("\n");
}

//performing matrix multiplication

for(i=0;i<ROW;i++)
{

for(j=0;j<COL;j++)
{
sum=0;
for(k=0;k<COL;k++)
10
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
{
sum+=a[i][k]*b[k][j];
}
m[i][j]=sum;
}
}

printf("\nMultiplying matrices A and B yields matrix M\n");


printf("\nMatrix M is:\n");
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
printf("%5d\t",m[i][j]);
}
printf("\n");
}
getch();
}

Output:

11
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26

PRACTICAL-5

12
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
Aim: Write a program to determine whether or not a character string has an unmatched
parenthesis. Use a stack. What is the time complexity of your program? Can we replace the
stack with a queue?

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 50
static struct stack
{
int top;
int numbers[MAXSIZE];
} mystack;

void push(struct stack*,int);


int pop(struct stack*);
int isempty(struct stack*);
void main()
{
char expression[MAXSIZE];
int i = -1;
int number;
clrscr();
mystack.top = -1;
printf("\nEnter the expression >");
scanf("%s",expression);
while(expression[++i] != '\0')
{
switch (expression[i])
{
case '(' : push(&mystack,'(');
break;
case ')' :
if(mystack.numbers[mystack.top]==40)
{
number = pop(&mystack);
break;
}
case '[' : push(&mystack,'[');
break;
case ']' :
if(mystack.numbers[mystack.top]==91)
{
number = pop(&mystack);
break;
}

13
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
case '{' : push(&mystack,'{');
break;
case '}' :
if(mystack.numbers[mystack.top]==123)
{
number = pop(&mystack);
break;
}
}
}
if (!isempty(&mystack))
{
printf("\nExpression has unmatched parenthesis");
}
else
{
printf("\nExpression is okay");
}
getch();
}

void push(struct stack *mystack,int number)


{

if (mystack->top==MAXSIZE-1)
{
printf("\nStack Overflow\n");
exit(1);
}
mystack->numbers[++mystack->top]=number;
}

int pop(struct stack *mystack)


{
if (isempty(mystack))
{
printf("\nStack Underflow\n");
exit(1);
}
mystack->top=(mystack->top)-1;
return(mystack->numbers[mystack->top]);
}

int isempty(struct stack *mystack)


{
return((mystack->top==-1));
14
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
}

Output:

PRACTICAL-6

15
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
Aim: Write a function to perform a preorder traversal of a binary tree represented as an
array. Write a function for inorder, postorder traversal also. Write a function to determine
the height of a linked binary tree.

#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *right, *left;
}*root,*p,*q;

struct node *make(int y)


{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=y;
newnode->right=newnode->left=NULL;
return(newnode);
}

void left(struct node *r,int x)


{
if(r->left!=NULL)
printf("\n Invalid !");
else
r->left=make(x);
}

void right(struct node *r,int x)


{
if(r->right!=NULL)
printf("\n Invalid !");
else
r->right=make(x);
}

void inorder(struct node *r)


{
if(r!=NULL)
16
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
{
inorder(r->left);
printf("\t %d",r->data);
inorder(r->right);
}
}

void preorder(struct node *r)


{
if(r!=NULL)
{
printf("\t %d",r->data);
preorder(r->left);
preorder(r->right);
}
}

void postorder(struct node *r)


{
if(r!=NULL)
{
postorder(r->left);
postorder(r->right);
printf("\t %d",r->data);
}
}

void main()
{
int no;
int choice;
clrscr();
printf("\n Enter the root:");
scanf("%d",& no);
root=make(no);
p=root;
while(1)
{
printf("\n Enter another number:");
scanf("%d",& no);
17
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
if(no==-1)
break;
p=root;
q=root;
while(no!=p->data && q!=NULL)
{
p=q;
if(no<p->data)
q=p->left;
else
q=p->right;
}
if(no<p->data)
{
printf("\n Left branch of %d is %d",p->data,no);
left(p,no);
}
else
{
right(p,no);
printf("\n Right Branch of %d is %d",p->data,no);
}
}
while(1)
{
printf("\n 1.Inorder Traversal \n 2.Preorder Traversal \n 3.Postorder Traversal \n 4.Exit");
printf("\n Enter choice:");
scanf("%d",&choice);
switch(choice)
{
case 1 :inorder(root);
break;
case 2 :preorder(root);
break;
case 3 :postorder(root);
break;
case 4 :exit(0);
default:printf("Error ! Invalid Choice ");
break;
}
18
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
getch();
}

}
Output:

PRACTICAL: 7

Aim:-Write a program that implements change making solution. Program should include a method
to input the purchase amount and the amount given by the customer as well as method to output
the amount of change and a breakdown by denomination. Apply greedy algorithm at the cahier
side that is give less number of if sufficient currency of that denomination available.

19
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
#include<stdio.h>
#include<conio.h>
void main()
{
int c,cur[5],n,num[15],i,temp,j;
clrscr();
printf("\nEnter the no. of currencies:");
scanf("%d",&n);
printf("\nEnter Currencies:") ;
for(i=0;i<n;i++)
{
printf("\nCurrency[%d]:",i+1);
scanf("%d",&cur[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
if(cur[i]>cur[j])
{
temp=cur[i];
cur[i]=cur[j];
cur[j]=temp;
}
}
}
printf("\nEnter the amount u want to get change:");
scanf("%d",&c);
for(i=0;i<n;i++)
{
num[i]=0;
}
for(i=0;i<n;i++)
{
for(num[i]=0;num[i]<100;num[i]++)
{
if((c-cur[i])>=0)
{
c=c-cur[i];
}
20
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
else
break;
}
}
for(i=0;i<n;i++)
{
printf("Number of %d is %d\n",cur[i],num[i]);
}
getch();
}

Output:-

PRACTICAL-8

Aim:-Write a program for 0/1 knapsack problem using this heuristic : Pack the
knapsack in nonincreasing order of profit density.

#include <stdio.h>
#define MAXWEIGHT 100
int n = 3;
int c[10] = {8, 6, 4};
21
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
int v[10] = {16, 10, 7};
int W = 10;
void fill_sack() {
int a[MAXWEIGHT];
int last_added[MAXWEIGHT];
int i, j;
int aux;
for (i = 0; i <= W; ++i)
{
a[i] = 0;
last_added[i] = -1;
}
a[0] = 0;
for (i = 1; i <= W; ++i)
for (j = 0; j < n; ++j)
if ((c[j] <= i) && (a[i] < a[i - c[j]] + v[j]))
{ a[i] = a[i - c[j]] + v[j];
last_added[i] = j; }

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

if (last_added[i] != -1)
{
printf("Weight %d; Benefit: %d; To reach this weight I added object %d (%d$
%dKg) to weight %d.\n", i, a[i], last_added[i] + 1, v[last_added[i]], c[last_added[i]], i -
c[last_added[i]]);
}
else
{
printf("Weight %d; Benefit: 0; Can't reach this exact weight.\n", i);
printf("---\n");
}
aux = W;
while ((aux > 0) && (last_added[aux] != -1))
{
printf("Added object %d (%d$ %dKg). Space left: %d\n", last_added[aux] + 1,
v[last_added[aux]], c[last_added[aux]], aux - c[last_added[aux]]);
aux -= c[last_added[aux]];
}
22
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26

printf("Total value added: %d$\n", a[W]);


}
int main(int argc, char *argv[])
{
fill_sack();
return 0;
}

OutPut :
---
Weight 1; Benefit: 0; Can't reach this exact weight.
---
Weight 2; Benefit: 0; Can't reach this exact weight.
---
Weight 3; Benefit: 0; Can't reach this exact weight.
---
Weight 4; Benefit: 7; To reach this weight I added object 3 (7$ 4Kg) to weight
0.
23
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
Weight 5; Benefit: 7; To reach this weight I added object 3 (7$ 4Kg) to weight
1.
Weight 6; Benefit: 10; To reach this weight I added object 2 (10$ 6Kg) to weight
0.
Weight 7; Benefit: 10; To reach this weight I added object 2 (10$ 6Kg) to weight
1.
Weight 8; Benefit: 16; To reach this weight I added object 1 (16$ 8Kg) to weight
0.
Weight 9; Benefit: 16; To reach this weight I added object 1 (16$ 8Kg) to weight
1.
Weight 10; Benefit: 17; To reach this weight I added object 2 (10$ 6Kg) to weight
4.
Added object 2 (10$ 6Kg). Space left: 4
Added object 3 (7$ 4Kg). Space left: 0
Total value added: 17$

PRACTICAL-9
Aim: Write a program that implement divide and conquer method to find the maximum
and minimum of n elements. Use recursion to implement the divide and conquer scheme.

#include<stdio.h>
int max, min;
int a[100];
void maxmin(int i, int j)
{
int max1, min1, mid;
if(i==j)
24
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
{
max = min = a[i];
}
Else
{
if(i == j-1)
{
if(a[i] <a[j])
{
max = a[j];
min = a[i];
}
Else
{
max = a[i];
min = a[j];
}
}
Else
{
mid = (i+j)/2;
maxmin(i, mid);
max1 = max; min1 = min;
maxmin(mid+1, j);
if(max <max1)
max = max1;
if(min > min1)
min = min1;
}
}
}

void main ()
{
int i, num;
clrscr();
printf ("\n\t\t\tMAXIMUM & MINIMUM\n\n");
printf ("\nEnter the total number of numbers : ");
scanf ("%d",&num);
printf ("Enter the numbers : \n");
for (i=1;i<=num;i++)
{
scanf ("%d",&a[i]);
}
max = a[0];
min = a[0];
25
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
maxmin(1, num);
printf ("Maximum element in an array : %d\n", max);
printf ("Minimum element in an array : %d\n", min);
getch();
}

Output:

PRACTICAL-10
Aim: Implement Kruskal’s algorithm to find shortest path.

#include<stdio.h>
#define INFINITY 999
typedef struct Graph
{
int v1;
int v2;
int cost;
}GR;
GR G[20];
int tot_edges,tot_nodes;
void create();
void spanning_tree();
26
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
int Minimum(int);

void main()
{
printf("\n\t Graph Creation by adjacency matrix ");
create();
spanning_tree();
}
void create()
{
int k;
printf("\n Enter Total number of nodes: ");
scanf("%d",&tot_nodes);
printf("\n Enter Total number of edges: ");
scanf("%d",&tot_edges);
for(k=0;k<tot_edges;k++)
{
printf("\n Enter Edge in (V1 V2)form ");
scanf("%d%d",&G[k].v1,&G[k].v2);
printf("\n Enter Corresponding Cost ");
scanf("%d",&G[k].cost);
}
getch();
}
void spanning_tree()
{
int count,k,v1,v2,i,j,tree[10][10],pos,parent[10];
int sum;
int Find(int v2,int parent[]);
void Union(int i,int j,int parent[]);
count=0;
k=0;
sum=0;
for(i=0;i<tot_nodes;i++)
parent[i]=i;
while(count!=tot_nodes-1)
{
pos=Minimum(tot_edges);//finding the minimum cost edge
if(pos==-1)//Perhaps no node in the graph
break;
v1=G[pos].v1;
v2=G[pos].v2;
i=Find(v1,parent);
j=Find(v2,parent);
if(i!=j)
{
27
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
tree[k][0]=v1;//storing the minimum edge in array tree[]
tree[k][1]=v2;
k++;
count++;
sum+=G[pos].cost;//accumulating the total cost of MST
Union(i,j,parent);
}
G[pos].cost=INFINITY;
}
if(count==tot_nodes-1)
{
printf("\n Spanning tree is...");
printf("\n--------------------------\n");
for(i=0;i<tot_nodes-1;i++)
{
printf("[%d",tree[i][0]);
printf(" - ");
printf("%d",tree[i][1]);
printf("]");
}
printf("\n--------------------------");
printf("\nCost of Spanning Tree is = %d",sum);
}
else
{
printf("There is no Spanning Tree");
}
}
int Minimum(int n)
{
int i,small,pos;
small=INFINITY;
pos=-1;
for(i=0;i<n;i++)
{
if(G[i].cost<small)
{
small=G[i].cost;
pos=i;
}
}
return pos;
}
int Find(int v2,int parent[])
{
while(parent[v2]!=v2)
28
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
{
v2=parent[v2];
}
return v2;
}
void Union(int i,int j,int parent[])
{
if(i<j)
parent[j]=i;
else
parent[i]=j;
}

Output:

29
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26

PRACTICAL-11
Aim: Implement Prim’s algorithm to find shortest path.

# include<stdio.h>
# include<conio.h>
30
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
# define SIZE 20
# define INFINITY 32767

/* This function finds the minimal spanning tree by Prim's Algorithm */

void Prim(int G[][SIZE], int nodes)


{
int select[SIZE], i, j, k;
int min_dist, v1, v2,total=0;

for (i=0 ; i<nodes ; i++) // Initialize the selected vertices list


select[i] = 0;

printf("\n\n The Minimal Spanning Tree Is :\n");


select[0] = 1;
for (k=1 ; k<nodes ; k++)
{
min_dist = INFINITY;
for (i=0 ; i<nodes ; i++) // Select an edge such that one vertex is
{ // selected and other is not and the edge
for (j=0 ; j<nodes ; j++) // has the least weight.
{
if (G[i][j] && ((select[i] && !select[j]) || (!select[i] && select[j])))
{
if (G[i][j] < min_dist)//obtained edge with minimum wt
{
min_dist = G[i][j];
v1 = i;
v2 = j; //picking up those vertices
}
}
}
}
printf("\n Edge (%d %d )and weight = %d",v1,v2,min_dist);
select[v1] = select[v2] = 1;
total =total+min_dist;
}
printf("\n\n\t Total Path Length Is = %d",total);
}

void main()
{
int G[SIZE][SIZE], nodes;
int v1, v2, length, i, j, n;

clrscr();
31
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26
printf("\n\t Prim'S Algorithm\n");

printf("\n Enter Number of Nodes in The Graph ");


scanf("%d",&nodes);
printf("\n Enter Number of Edges in The Graph ");
scanf("%d",&n);

for (i=0 ; i<nodes ; i++) // Initialize the graph


for (j=0 ; j<nodes ; j++)
G[i][j] = 0;
//entering weighted graph
printf("\n Enter edges and weights \n");
for (i=0 ; i<n; i++)
{
printf("\n Enter Edge by V1 and V2 :");
scanf("%d %d",&v1,&v2);
printf("\n Enter corresponding weight :");
scanf("%d",&length);
G[v1][v2] = G[v2][v1] = length;
}
getch();
printf("\n\t");
clrscr();
Prim(G,nodes);
getch();
}

Output:

32
KALOL INSTITUTE OF TECHNOLOGY
AAD
07-CE-26

33
KALOL INSTITUTE OF TECHNOLOGY

You might also like