Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

ADA Hiren

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

SWAMINARAYAN COLLEGE OF ENNG. & TECH.

(DEGREE)

Analysis and Design of algorithms


(3150703)

PREPARED BY:- Patel Hiren


ENROLLMENT NO :- 201150107012

SUBMITTED TO:-
COMPUTER DEPARTMENT
SCET, SAIJ, KALOL
SWAMINARAYAN COLLEGE OF ENNG. & TECH.
(DEGREE)

CERTIFICATE

This Certificate is awarded to Mr. Patel Hiren Daxeshbhai From


SWAMINARAYAN COLLEGE OF ENNG. & TECH. (DEGREE)
having enrollment no:- 201150107012 has completed practical work of
“Analysis and design of algorithms” during the 5th semester on term
2022-23.

INTERNAL GUIDE DATE OF SUBMISSION H.O.D


INDEX

Subject: Analysis and design of algorithm Branch : Computer Engineering

Subject Code: 3150703 Semester: 5th

Sr. Page
PRACTICAL AIM Date Sign
No No.
Implementation and Time analysis of sorting
1 algorithms.
1
Bubble sort, Selection sort, Insertion sort, Merge
sort and Quick sort
Implementation and Time analysis of linear and
2 binary search algorithm. 4

3 Implementation of max-heap sort algorithm 7


Implementation and Time analysis of factorial
4 program using iterative and recursive method 9

Implementation of a knapsack problem using


5 dynamic programming. 12

Implementation of chain matrix multiplication


6 17
using dynamic programming
Implementation of making a change problem
7 using dynamic programming 20

Implementation of a knapsack problem using


8 greedy algorithm 22

Implementation of Graph and Searching (DFS 26


9 and BFS).

10 Implement prim’s algorithm 30

11 Implement kruskal’s algorithm. 34

Implement LCS problem. 37


12

FACULTY: H.O.D:
5TH SEM- C.E (ADA) Enrollement No: 201150107012

PRACTICAL 1

AIM:- Implementation and Time analysis of sorting algorithms.


Bubble sort, Selection sort, Insertion sort, Merge sort and Quick sort.

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

void bubble_sort(int[], int);

void main() {
int arr[30], num, i;

printf("\nEnter no of elements :");


scanf("%d", &num);

printf("\nEnter array elements :");


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

bubble_sort(arr, num);
getch();
}

void bubble_sort(int iarr[], int num) {


int i, j, k, temp;

1|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012

printf("\nUnsorted Data:");
for (k = 0; k < num; k++) {
printf("%5d", iarr[k]);
}

for (i = 1; i < num; i++) {


for (j = 0; j < num - 1; j++) {
if (iarr[j] > iarr[j + 1]) {
temp = iarr[j];
iarr[j] = iarr[j + 1];
iarr[j + 1] = temp;
}
}

printf("\nAfter pass %d : ", i);


for (k = 0; k < num; k++) {
printf("%5d", iarr[k]);
}
}
}

output:

2|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012

Enter no of elements :5
Enter array elements :10 4 55 21 6

Unsorted Data: 10 4 55 21 6
After pass 1 : 4 10 21 6 55
After pass 2 : 4 10 6 21 55
After pass 3 : 4 6 10 21 55
After pass 4 : 4 6 10 21 55

3|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012

PRACTICAL 2

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

#include <stdio.h>

//Selection Sort function to Sort Integer array list


int *selectionSort(int array[],int n)
{
int j,temp,i;

//Iterate start from first element


for (i = 0; i < n; i++)
{
//Iterate and compare till it satisfies condition
for(j = i+1; j < n; j++)
{
if(array[i] > array[j])
{//Swaping operation
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
//return Sorted array
return array;

4|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012

int main()
{
//declaring variables
int array[1000],n,i;

//Number of elements in array form user input


printf("Enter the number of element you want to Sort : ");
scanf("%d",&n);

//code to ask to enter elements from user equal to n


printf("Enter Elements in the list : ");
for(i = 0; i < n; i++)
{
scanf("%d",&array[i]);
}

//calling selectionSort function defined above and gettting


//sorted array in sortArray variable
int *sortArray = selectionSort(array,n);

//print sorted array


printf("Sorted list : ");
for(i = 0; i < n; i++ )
{
printf("%d\t",sortArray[i]);

5|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012

}
}

output:

6|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012

PRACTICAL 3

AIM: Implementation of max-heap sort algorithm.

#include<stdio.h>

int main() {
int i, j, num, temp, arr[20];

printf("Enter total elements: ");


scanf("%d", &num);

printf("Enter %d elements: ", num);


for (i = 0; i < num; i++) {
scanf("%d", &arr[i]);
}

for (i = 1; i < num; i++) {


temp = arr[i];
j = i - 1;
while ((temp < arr[j]) && (j >= 0)) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}

7|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012

printf("After Sorting: ");


for (i = 0; i < num; i++) {
printf("%d", arr[i]);
}

return 0;
}

Output :
Enter total elements: 5 Enter 5 elements: 9 4 1 0 2 After sorting: 0 1 2 4 9
1
2
3

Enter total elements: 5


Enter 5 elements: 9 4 1 0 2
After sorting: 0 1 2 4 9

8|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012

PRACTICAL 4

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

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

void main( )
{
int a[5] = { 11, 2, 9, 13, 57 } ;
int b[5] = { 25, 17, 1, 90, 3 } ;
int c[10] ;
int i, j, k, temp ;

clrscr( ) ;

printf ( "Merge sort.\n" ) ;

printf ( "\nFirst array:\n" ) ;


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

printf ( "\n\nSecond array:\n" ) ;


for ( i = 0 ; i <= 4 ; i++ )
printf ( "%d\t", b[i] ) ;

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

9|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012

{
for ( j = i + 1 ; j <= 4 ; j++ )
{
if ( a[i] > a[j] )
{
temp = a[i] ;
a[i] = a[j] ;
a[j] = temp ;
}
if ( b[i] > b[j] )
{
temp = b[i] ;
b[i] = b[j] ;
b[j] = temp ;
}
}
}

for ( i = j = k = 0 ; i <= 9 ; )
{
if ( a[j] <= b[k] )
c[i++] = a[j++] ;
else
c[i++] = b[k++] ;

if ( j == 5 || k == 5 )
break ;

10 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

for ( ; j <= 4 ; )
c[i++] = a[j++] ;

for ( ; k <= 4 ; )
c[i++] = b[k++] ;

printf ( "\n\nArray after sorting:\n") ;


for ( i = 0 ; i <= 9 ; i++ )
printf ( "%d\t", c[i] ) ;

getch( ) ;
}

11 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

PRACTICAL 5

AIM: Implementation of a knapsack problem using dynamic programming.

# include<stdio.h>

void knapsack(int n, float weight[], float profit[], float capacity) {


float x[20], tp = 0;
int i, j, u;
u = capacity;

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


x[i] = 0.0;

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


if (weight[i] > u)
break;
else {
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i];
}
}

if (i < n)
x[i] = u / weight[i];

tp = tp + (x[i] * profit[i]);

12 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

printf("\nThe result vector is:- ");


for (i = 0; i < n; i++)
printf("%f\t", x[i]);

printf("\nMaximum profit is:- %f", tp);

int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;

printf("\nEnter the no. of objects:- ");


scanf("%d", &num);

printf("\nEnter the wts and profits of each object:- ");


for (i = 0; i < num; i++) {
scanf("%f %f", &weight[i], &profit[i]);
}

printf("\nEnter the capacityacity of knapsack:- ");


scanf("%f", &capacity);

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


ratio[i] = profit[i] / weight[i];

13 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

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


for (j = i + 1; j < num; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;

temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;

temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}

knapsack(num, weight, profit, capacity);


return(0);
}

Output :
Enter the no. of objects:- 7 Enter the wts and profits of each object:- 2 10 3 5 5 15 7 7 1 6 4 18 1
3 Enter the capacity of knapsack:- 15 The result vector is:- 1.000000 1.000000 1.000000
1.000000 1.000000 0.666667 0.000000 Maximum profit is:- 55.333332

14 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Enter the no. of objects:- 7

Enter the wts and profits of each object:-


2 10
35
5 15
77
16
4 18

15 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

13

Enter the capacity of knapsack:- 15

The result vector is:- 1.000000 1.000000 1.000000 1.000000


1.000000 0.666667 0.000000

Maximum profit is:- 55.333332

16 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

PRACTICAL 6

AIM: Implementation of chain matrix multiplication using dynamic programming.

#include <stdio.h>

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

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


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

printf("Enter the number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");

17 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

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


for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of entered matrices:-\n");

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;

18 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

output:

19 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

PRACTICAL 7

AIM: Implementation of making a change problem using dynamic programming

#include <stdio.h>

void coinchange(int coin[], int num, int sum)


{
int i, j;
int *C;
int min;

C = (int*)malloc((sum+1)*sizeof(int));
C[0] = 0;

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


{
min = 999;
for (j = 0; j<num; j++)
{
if (coin[j] <= i)
{
if (min > C[i-coin[j]]+1)
{
min = C[i-coin[j]]+1;
}
}

20 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

}
C[i] = min;
}

printf("minimum coins required %d", C[sum]);


}

int main()
{
int coin[] = {1,2,5};
coinchange(coin, 3 ,9);
}

21 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

PRACTICAL 8

AIM: Implementation of a knapsack problem using greedy algorithm.

#include<stdio.h>

void knapsack(int n, float weight[], float profit[], float capacity) {


float x[20], tp = 0;
int i, j, u;
u = capacity;

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


x[i] = 0.0;

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


if (weight[i] > u)
break;
else {
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i];
}
}

if (i < n)
x[i] = u / weight[i];

22 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

tp = tp + (x[i] * profit[i]);

printf("\nThe result vector is:- ");


for (i = 0; i < n; i++)
printf("%f\t", x[i]);

printf("\nMaximum profit is:- %f", tp);

int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;

printf("\nEnter the no. of objects:- ");


scanf("%d", &num);

printf("\nEnter the wts and profits of each object:- ");


for (i = 0; i < num; i++) {
scanf("%f %f", &weight[i], &profit[i]);
}

printf("\nEnter the capacityacity of knapsack:- ");


scanf("%f", &capacity);

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

23 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

ratio[i] = profit[i] / weight[i];


}

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


for (j = i + 1; j < num; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;

temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;

temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}

knapsack(num, weight, profit, capacity);


return(0);
}

24 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

OUTPUT:
view source
Enter the no. of objects:- 7

Enter the wts and profits of each object:-


2 10
35
5 15
77
16
4 18
13

Enter the capacity of knapsack:- 15

The result vector is:- 1.000000 1.000000 1.000000 1.000000


1.000000 0.666667 0.000000

Maximum profit is:- 55.333332

25 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

PRACTICAL 9

AIM: Implementation of Graph and Searching (DFS and BFS).

#include<stdio.h>
void DFS(int);
int G[10][10],visited[10],n; //n is no of vertices and graph is sorted in array G[10][10]
void main()
{
int i,j;
printf("Enter number of vertices:");

scanf("%d",&n);
//read the adjecency matrix
printf("\nEnter adjecency matrix of the graph:");

for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
//visited is initialized to zero
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
int j;

26 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

printf("\n%d",i);
visited[i]=1;

for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}

27 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

PRACTICAL:9 BFS

include<stdio.h>
#include<conio.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;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++) {
q[i]=0;
visited[i]=0;
}
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]);

28 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

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");
getch();
}

29 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

PRACTICAL 10

AIM: Implement prim’s algorithm.

#include<stdio.h>

#include<conio.h>

int a,b,u,v,n,i,j,ne=1;

int visited[10]={0},min,mincost=0,cost[10][10];

void main()

clrscr();

printf("\nEnter the number of nodes:");

scanf("%d",&n);

printf("\nEnter the adjacency matrix:\n");

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

30 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

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

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

if(cost[i][j]==0)

cost[i][j]=999;

visited[1]=1;

printf("\n");

while(ne < n)

for(i=1,min=999;i<=n;i++)

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

if(cost[i][j]< min)

if(visited[i]!=0)

31 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

min=cost[i][j];

a=u=i;

b=v=j;

if(visited[u]==0 || visited[v]==0)

printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);

mincost+=min;

visited[b]=1;

cost[a][b]=cost[b][a]=999;

printf("\n Minimun cost=%d",mincost);

32 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

getch();

output:

33 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

PRACTICAL 11

AIM: Implement kruskal’s algorithm.

#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()
{
clrscr();
printf("\n\tImplementation of Kruskal's algorithm\n");
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");

34 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

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;
}
}
}
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)

35 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

{
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:

36 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

PRACTICAL 12

AIM: Implement LCS problem.


#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);
37 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

for(i=0;i<=m;i++)
c[i][0]=0;
for(i=0;i<=n;i++)
c[0][i]=0;

//c, u and l denotes cross, upward and downward directions respectively


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

38 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012

{
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:

39 | P a g e

You might also like