Ayush Ada

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 34

TY CE 2 AyushPatel 200410107100

SARDAR VALLABHBHAI PATEL INSTITUTE OF


TECHNOLOGY, VASAD

Computer Engineering Department


Subject: Contributor personality
development program Subject
Code: 3150004 TY SEMESTER- 5

NAME: Ayush
Patel
EN.NO:20041010
7100
CLASS: TY-CE-2
BATCH:A

Academic Year 2022-23(Odd)


TY CE 2 AyushPatel 200410107100

PRACTICAL LIST

Practical Practical
No. Name
Implementation and Time analysis of sorting algorithms.
1 Bubble sort, Selection sort, Insertion sort, Merge sort and
Quicksort
Implementation and Time analysis of linear and binary
2
search algorithm.
3 Implementation of max-heap sort algorithm
Implementation and Time analysis of factorial program
4
using iterative and recursive method
Implementation of a knapsack problem using
5
dynamic programming.
Implementation of chain matrix multiplication using dynamic
6
programming.
Implementation of making a change problem using dynamic
7
programming
8 Implementation of a knapsack problem using greedy algorithm
9 Implementation of Graph and Searching (DFS and BFS).
10 Implement prim’s algorithm
11 Implement Kruskal’s algorithm.
12 Implement LCS problem.
TY CE 2 AyushPatel 200410107100

Practical 1
Aim: Implementation and Time analysis of sorting algorithms.

Selection Sort :
PROGRAM CODE:
#include<stdio.h
> int main()
{

int i,j,count,temp,number[10];
printf("Enter the numbers of
elements:"); scanf("%d",&count);
printf("Enter %d elements:",count);

for(i=0;i<count;i++)
scanf("%d",&number[i])
;

for(i=0;i<count;i++)
{
for(j=i+1;j<count;j++)
{
If(number[i]>number[j]
){ temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}

printf("Sorted Elements:");
TY CE 2 AyushPatel 200410107100

for(i=0;i<count;i++) printf("\t
%d",number[i]
);
return 0;

OUTPUT:

 Insertion Sort :

PROGRAM CODE:

#include<stdio.h
> int main()
{

int i, j, n, k, a[25];
printf("Enter the No. of Elements :
"); scanf("%d",&n);
printf("Enter %d elements: ",n);

for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=1;i<n;i++)
{
TY CE 2 AyushPatel 200410107100

k=a[i];
j=i-1;

while((k<a[j])&&(j>=0))
{
a[j+1]=a[j]
; j=j-1;
}
a[j+1]=k;
}

printf("Order of Sorted elements:


"); for(i=0;i<n;i++)
printf(" %d",a[i]);

return 0;
}

OUTPUT:
TY CE 2 AyushPatel 200410107100

PRATICAL 2
AIM: Implementation and Time analysis of linear and binary
search algorithm.

LINEAR

SEARCH: PROGRAM

CODE:

#include<stdio.h

>

#include<conio.h

> void main()

int a[20],i,size,item,pos,flag=0;

printf("\n Enter the size of an array:

"); scanf("%d",&size);

printf("\n Enter the elements of the array: ")

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

scanf("%d",&a[i]);

printf("\n Enter the element to be searched:

"); scanf("%d",&item);

for(i=0;i<size;i++)
TY CE 2 AyushPatel 200410107100

if(item==a[i])

pos=i;

flag=1;

break;

if(flag==1)

printf("\n The element is in the list and its position

is: %d",pos+1); else

printf("\n The element is not found");

getch();

OUTPUT :
TY CE 2 AyushPatel 200410107100

BINARY

SEARCH: PROGRAM

CODE:

#include
<stdio.h> int
main()
{
int c, low, high, middle, n, search, array[100];

printf("Enter number of elements in the


array\n"); scanf("%d", &n);

printf("Enter %d integers\n", n);

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


scanf("%d",
&array[c]);

printf("Enter value to
find\n"); scanf("%d",
&search);

low = 0;
high = n - 1;
middle = (low+high)/2;

while (low <= high) {


if (array[middle] <
search) low = middle +
1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search,
middle+1); break;
}
else
high = middle - 1;

middle = (low + high)/2;


}
TY CE 2 AyushPatel 200410107100

if (low > high)


TY CE 2 AyushPatel 200410107100

printf("Element Not Found! %d isn't present in the list.\n", search);

return 0;
}

OUTPUT:
TY CE 2 AyushPatel 200410107100

PRACTICAL 3
Aim: Implementation of max-heap sort algorithm

PROGRAM CODE:
#include<stdio.h

> void create(int

[]);

void down_adjust(int

[],int); void main()

int heap[30],n,i,last,temp;

printf("Enter no. of

elements:"); scanf("%d",&n);

printf("\nEnter elements:");

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

scanf("%d",&heap[i])

; heap[0]=n;

create(heap);

while(heap[0] > 1) //swap heap[1] and

heap[last] last=heap[0];

temp=heap[1];

heap[1]=heap[last]

;
TY CE 2 AyushPatel 200410107100

heap[last]=temp;

heap[0];

down_adjust(heap,1)

printf("\nArray after

sorting:\n"); for(i=1;i<=n;i++)

printf("%d ",heap[i]);

void create(int heap[])

int i,n;

n=heap[0]; //no. of

elements for(i=n/2;i>=1;i--)

down_adjust(heap,i);

void down_adjust(int heap[],int i)

int j,temp,n,flag=1;

n=heap[0];

while(2*i<=n && flag==1)


TY CE 2 AyushPatel 200410107100

j=2*i; //j points to left child

if(j+1<=n && heap[j+1] >

heap[j]) j=j+1;

if(heap[i] >

heap[j])

flag=0;

else

temp=heap[i];

heap[i]=heap[j];

heap[j]=temp;

i=j;

OUTPUT:
TY CE 2 AyushPatel 200410107100

PRACTICAL 4
AIM: Implementation and Time analysis of factorial program using iterative
and recursive method.

Factorial using recursion:

PROGRAM CODE:

#include<stdio.h
> int fact(int);
void main()
{

int n,f;
printf("Enter the
number");
scanf("%d",&n);
f=fact(n);
printf("factorial=%d",
f);
}
int fact(int n)
{
if(n==0)
return 1;
else
if(n==1)
return 1;
else
return n*fact(n-1);
}
OUTPUT:
TY CE 2 AyushPatel 200410107100

Factorial using

iteration: PROGRAM CODE:

#include<stdio.h
> int main()
{

int i,fact=1,number;
printf("Enter the
number:");
scanf("%d",&number);
for(i=1;i<=number;i++)
{

fact=fact*i;
}
printf("Factorial of %d
is: %d",number,fact); return 0;
}

OUTPUT:
TY CE 2 AyushPatel 200410107100

PRACTICAL 5
AIM: Implementation of a knapsack problem using dynamic

programming. PROGRAM CODE:

#include<stdio.h>

int max(int x, int y) { return (x > y)? x :

y; } int knapSack(int W, int wt[], int val[],

int n)

int i, w;

int K[n+1][W+1];

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

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

if (i==0 || w==0)

K[i][w] = 0;

else if (wt[i-1] <= w)

K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-

1][w]); else

K[i][w] = K[i-1][w];

}
TY CE 2 AyushPatel 200410107100

return K[n][W];

int main()

int i, n, val[20], wt[20], W;

printf("Enter number of

items:"); scanf("%d", &n);

printf("Enter value and weight of

items:\n"); for(i = 0;i < n; ++i){

scanf("%d%d", &val[i], &wt[i]);

printf("Enter size of

knapsack:"); scanf("%d", &W);

printf("%d", knapSack(W, wt, val,

n)); return 0;

}
TY CE 2 AyushPatel 200410107100

OUTPUT:
TY CE 2 AyushPatel 200410107100

PRACTICAL 6
AIM: Implementation of chain matrix multiplication using dynamic

programming. PROGRAM CODE:

#include <stdio.h>
#include<limits.h>
#define INFY
999999999 long int
m[20][20];
int s[20][20];
int p[20],i,j,n;

void print_optimal(int i,int j)


{
if (i == j)
printf(" A%d
",i); else
{
printf("( ");
print_optimal(i, s[i][j]);
print_optimal(s[i][j] + 1,
j); printf(" )");
}
}

void matmultiply(void)
{
long int q;
int k;
for(i=n;i>0;i--)
{
for(j=i;j<=n;j++)
{
if(i==j)
m[i][j]=0;
else
TY CE 2 AyushPatel 200410107100

{
for(k=i;k<j;k++)
{
q=m[i][k]+m[k+1][j]+p[i-
1]*p[k]*p[j]; if(q<m[i][j])
{
m[i][j]=q;
s[i][j]=k;
}
}
}
}
}
}

int MatrixChainOrder(int p[], int i, int j)


{
if(i == j)
return 0;
int k;
int min =
INT_MAX; int
count;

for(k = i; k <j; k++)


{
count = MatrixChainOrder(p, i, k) +
MatrixChainOrder(p, k+1, j) + p[i-
1]*p[k]*p[j];

if(count < min)


min = count;
}
return min;
}

void main()
{
TY CE 2 AyushPatel 200410107100

int k;
printf("Enter the no. of elements:
"); scanf("%d",&n);

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

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

+)
{
m[i][i]=0;
m[i][j]=INFY;
s[i][j]=0;
}

printf("\nEnter the dimensions: \n");

for(k=0;k<=n;k++)
{
printf("P%d: ",k);
scanf("%d",&p[k]);
}

matmultiply();

printf("\nCost Matrix
M:\n"); for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
printf("m[%d][%d]: %ld\n",i,j,m[i][j]);

i=1,j=n;
printf("\nMultiplication Sequence :
"); print_optimal(i,j);
printf("\nMinimum number of multiplications
is : %d ", MatrixChainOrder(p, 1, n));

}
TY CE 2 AyushPatel 200410107100

OUTPUT:
TY CE 2 AyushPatel 200410107100

PRACTICAL 7

AIM: Implementation of making a change problem using dynamic

programming. PROGRAM CODE:

#include<stdio.h>
int count(int S[],int m,int n)
{

if(n==0)
return 1;
if(n<0)
return 0;
if(m<=0 &&
n>=1)
return 0;

return count(S,m-1,n)+count(S,m,n-S[m-1]);
}

int main()
{
int i,j;
int arr[]={4,1,2};
int m=sizeof(arr)/sizeof(arr[0]);
printf("The solution
is:""%d",count(arr,m,8)); getchar();
return 0;
}

OUTPUT:
TY CE 2 AyushPatel 200410107100

PRACTICAL 8
AIM: Implementation of a knapsack problem using greedy

algorithm PROGRAM CODE:

#include
<stdio.h> void
main()
{
int capacity, no_items, cur_weight,
item; int used[10];
float total_profit;
int i;
int
weight[10];
int value[10];
printf("Enter the capacity of knapsack:\
n"); scanf("%d", &capacity);
printf("Enter the number of items:\
n"); scanf("%d", &no_items);
printf("Enter the weight and value of %d item:\n",
no_items); for (i = 0; i < no_items; i++)
{
printf("Weight[%d]:\t", i);
scanf("%d", &weight[i]);
printf("Value[%d]:\t", i);
scanf("%d", &value[i]);
}
for (i = 0; i < no_items;
++i) used[i] = 0;
cur_weight =
capacity; while
(cur_weight > 0)
{
item = -1;
for (i = 0; i < no_items; ++i)
TY CE 2 AyushPatel 200410107100

if ((used[i] == 0) &&
((item == -1) || ((float) value[i] / weight[i] > (float)
value[item] / weight[item])))
item = i;
used[item] = 1;
cur_weight -=
weight[item]; total_profit
+= value[item]; if
(cur_weight >= 0)
printf("Added object %d (%d Rs., %dKg) completely in the bag.
Space left: %d.\n", item + 1, value[item], weight[item],
cur_weight);
else
{
int item_percent = (int) ((1 + (float) cur_weight / weight[item]) *
100); printf("Added %d%% (%d Rs., %dKg) of object %d in the
bag.\n", item_percent, value[item], weight[item], item + 1);
total_profit -= value[item];
total_profit += (1 + (float)cur_weight / weight[item]) * value[item];
}
}
printf("Filled the bag with objects worth %.2f Rs.\n", total_profit);
}

OUTPUT:
TY CE 2 AyushPatel 200410107100

PRACTICAL 9:

AIM: Implementation of Graph and Searching (DFS and

BFS). PROGRAM CODE FOR DFS:

#include<stdio.h>
int G[10][10],visited[10],n;

void main()
{
int i,j;
printf("Enter number of
vertices:"); scanf("%d",&n);
printf("\nEnter adjenceny matrix of the
graph:"); for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j])
;
for(i=0;i<n;i+
+)
visited[i]=0;
DFS(0);
}

void DFS(int i)
{
int j; printf("\n
%d",i)
; visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==
1) DFS(j);
}
TY CE 2 AyushPatel 200410107100

OUTPUT:

PROGRAM CODE FOR BFS:

#include<stdio.h>
int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;

void bfs(int v) {
for(i = 1; i <= n; i++)
if(a[v][i]
&& !visited[i]) q[++r]
= i;
if(f <= r)
{ visited[q[f]] =
1;
bfs(q[f++]);
}
}

void main()
{ int v;
printf("\n Enter the number of
vertices:"); scanf("%d", &n);

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


{ q[i] = 0;
visited[i] = 0;
TY CE 2 AyushPatel 200410107100

printf("\n Enter graph data in matrix


form:\n"); for(i=1; i<=n; i++) {
for(j=1;j<=n;j++)
{ scanf("%d",
&a[i][j]);
}
}

printf("\n Enter the starting


vertex:"); scanf("%d", &v);
bfs(v);
printf("\n The node which are reachable are:\n");

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


{ if(visited[i])
printf("%d\t", i);
else {
printf("\n Bfs is not possible. Not all nodes are
reachable"); break;
}
}
}

OUTPUT:
TY CE 2 AyushPatel 200410107100

PRACTICAL 10
AIM: Implement prim’s algorithm

PROGRAM CODE:

#include<stdio.h
> int main()
{
int cost[10]
[10],visited[10]={0},i,j,n,no_e=1,min,a,b,min_cost=0;
printf("Enter number of nodes ");
scanf("%d",&n);
printf("Enter cost in form of adjacency 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]=1000;
}
}

visited[1]=1;
while(no_e<n)
{
min=1000;

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
if(visited[i]!=0)
{
min=cost[i][j]
; a=i;
b=j;
TY CE 2 AyushPatel 200410107100

}
}
}
}

if(visited[b]==0)
{
printf("\n%d to %d cost=%d",a,b,min);
min_cost=min_cost+min;
no_e++;
}
visited[b]=1;

cost[a][b]=cost[b][a]=1000;
}
printf("\nminimum weight is%d",min_cost);
return 0;
}

OUTPUT:
TY CE 2 AyushPatel 200410107100

PRACTICAL 11
AIM: Implement
Kruskal’s

algorithm. PROGRAM CODE:


#include<stdio.h
>
#include<conio.h
>
#include<stdlib.
h>
int i,j,k,a,b,u,v,n,ne=1;
int
min,mincost=0,cost[9][9],parent[9];
int find(int);
int
uni(int,int);
void main()
{

printf("\nEnter the no. of


vertices:"); scanf("%d",&n);
printf("\nEnter the cost adjacency
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;
}
}
printf("The edges of Minimum Cost Spanning Tree
are\n"); while(ne < n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j <= n;j++)
{
if(cost[i][j] < min)
{
min=cost[i][j]
; a=u=i;
b=v=j;
}
}
TY CE 2 AyushPatel 200410107100

}
u=find(u);
v=find(v);
if(uni(u,v)
)
{
printf("%d edge (%d,%d)
=%d\n",ne++,a,b,min); mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost
= %d\n",mincost); getch();
}
int find(int i)
{
while(parent[i]
) i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}

OUTPUT:
TY CE 2 AyushPatel 200410107100

PRACTICAL 12:
AIM: Implement LCS

problem. PROGRAM CODE:

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

int i,j,m,n,c[20][20];
char x[20],y[20],b[20][20];

void print(int i,int j)


{
if(i==0 || j==0)
return; if(b[i]
[j]=='c')
{
print(i-1,j-1);
printf("%c",x[i-
1]);
}
else if(b[i][j]=='u')
print(i-1,j);
else
print(i,j-1);
}

void lcs(
{
m=strlen(x);
n=strlen(y);
for(i=0;i<=m;i+
+)
c[i][0]=0;
for(i=0;i<=n;i++)
c[0][i]=0;

for(i=1;i<=m;i+
+)
for(j=1;j<=n;j++)
{
if(x[i-1]==y[j-1])
{
c[i][j]=c[i-1][j-1]+1;
TY CE 2 AyushPatel 200410107100

b[i][j]='c';
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-
1][j];
} b[i][j]='u';
else
{

c[i][j]=c[i][j-
} 1]; b[i][j]='l';
}
}
int main()
{
printf("Enter 1st
sequence:"); scanf("%s",x);
printf("Enter 2nd
sequence:"); scanf("%s",y);
printf("\nThe Longest Common Subsequence is
"); lcs();
print(m,n);
return 0;
}

OUTPUT:

You might also like