Professional Documents
Culture Documents
YOYO1
YOYO1
PRACTICAL -1
Aim: Implementation and Time analysis of linear and binary search algorithm.
Solution (Linear Search):
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int arr[10],i,j;
printf("Enter the numbers in array :");
for(j=0;j<10;j++)
{
scanf("%d",&arr[j]);
}
printf("Enter numbers you want to search in array:");
scanf("%d",&i);
for(j=0;j<10;j++)
{
if(arr[j]==i)
{
printf("Number is : %d At : %d (postion)",arr[j],j+1);
}
}
return 0;
}
Page 1 of 30
ENROLLMENT NUMBER : IU2041220025
Page 2 of 30
ENROLLMENT NUMBER : IU2041220025
Page 3 of 30
ENROLLMENT NUMBER : IU2041220025
Page 4 of 30
ENROLLMENT NUMBER : IU2041220025
PRACTICAL -2
Aim: Implementation and Time analysis of sorting algorithms: Bubble sort, Selection sort,
and Quicksort.
Solution (Bubble Sort):
#include <stdio.h>
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
Page 6 of 30
ENROLLMENT NUMBER : IU2041220025
Page 7 of 30
ENROLLMENT NUMBER : IU2041220025
Page 8 of 30
ENROLLMENT NUMBER : IU2041220025
Page 9 of 30
ENROLLMENT NUMBER : IU2041220025
Page 10 of 30
ENROLLMENT NUMBER : IU2041220025
Page 11 of 30
ENROLLMENT NUMBER : IU2041220025
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
int main(){
scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
Page 13 of 30
ENROLLMENT NUMBER : IU2041220025
PRACTICAL -3
Aim: Implementation and Time analysis of factorial program using iterative and recursive
methods.
Solution (Iterative Method):
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++)
{
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
Page 15 of 30
ENROLLMENT NUMBER : IU2041220025
Page 16 of 30
ENROLLMENT NUMBER : IU2041220025
PRACTICAL -4
Aim: Implement Prim’s algorithm.
Solution:
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define V 5
return min_index;
printf("Edge \tWeight\n");
int parent[V];
int key[V];
bool mstSet[V];
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
mstSet[u] = true;
Page 18 of 30
ENROLLMENT NUMBER : IU2041220025
printMST(parent, graph);
int main()
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
printf("Matrix\n");
printf("{ 0, 2, 0, 6, 0 }\n");
printf("{ 2, 0, 3, 8, 5 }\n");
printf("{ 0, 3, 0, 0, 7 }\n");
printf("{ 6, 8, 0, 0, 9 }\n");
printf("{ 0, 5, 7, 9, 0 }\n\n");
primMST(graph);
return 0;
Output:
PRACTICAL -5
Aim: Implement Kruskal’s algorithm.
Solution:
#include <stdio.h>
#define MAX 30
typedef struct edge {
int u, v, w;
} edge;
typedef struct edge_list {
edge data[MAX];
int n;
} edge_list;
edge_list elist;
int Graph[MAX][MAX], n;
edge_list spanlist;
void kruskalAlgo();
int find(int belongs[], int vertexno);
void applyUnion(int belongs[], int c1, int c2);
void sort();
void print();
// Applying Krushkal Algo
void kruskalAlgo() {
int belongs[MAX], i, j, cno1, cno2;
elist.n = 0;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++) {
if (Graph[i][j] != 0) {
elist.data[elist.n].u = i;
elist.data[elist.n].v = j;
elist.data[elist.n].w = Graph[i][j];
elist.n++;
}
}
sort();
for (i = 0; i < n; i++)
belongs[i] = i;
spanlist.n = 0;
for (i = 0; i < elist.n; i++) {
cno1 = find(belongs, elist.data[i].u);
cno2 = find(belongs, elist.data[i].v);
if (cno1 != cno2) {
spanlist.data[spanlist.n] = elist.data[i];
spanlist.n = spanlist.n + 1;
applyUnion(belongs, cno1, cno2);
}
}
}
int find(int belongs[], int vertexno) {
return (belongs[vertexno]);
}
void applyUnion(int belongs[], int c1, int c2) {
int i;
Page 20 of 30
ENROLLMENT NUMBER : IU2041220025
Graph[1][0] = 4;
Graph[1][1] = 0;
Graph[1][2] = 2;
Graph[1][3] = 0;
Graph[1][4] = 0;
Graph[1][5] = 0;
Graph[1][6] = 0;
Graph[2][0] = 4;
Graph[2][1] = 2;
Graph[2][2] = 0;
Graph[2][3] = 3;
Graph[2][4] = 4;
Graph[2][5] = 0;
Graph[2][6] = 0;
Graph[3][0] = 0;
Graph[3][1] = 0;
Page 21 of 30
ENROLLMENT NUMBER: IU0000000
Graph[3][2] = 3;
Graph[3][3] = 0;
Graph[3][4] = 3;
Graph[3][5] = 0;
Graph[3][6] = 0;
Graph[4][0] = 0;
Graph[4][1] = 0;
Graph[4][2] = 4;
Graph[4][3] = 3;
Graph[4][4] = 0;
Graph[4][5] = 0;
Graph[4][6] = 0;
Graph[5][0] = 0;
Graph[5][1] = 0;
Graph[5][2] = 2;
Graph[5][3] = 0;
Graph[5][4] = 3;
Graph[5][5] = 0;
Graph[5][6] = 0;
kruskalAlgo();
print();
}
Output:
PRACTICAL -6
Aim: Implementation of a knapsack problem using dynamic programming.
Solution:
#include<stdio.h> int max(int a, int b)
{ return (a > b)? a : b;
}
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]; }
} 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;
}
Output:
PRACTICAL -7
Aim: Implementation of matrix chain multiplication using dynamic programming.
Solution:
#include<stdio.h> #include<limits.h>
int MatrixChainMultiplication(int p[], int n)
{ int m[n][n];
int i, j, k, L, q; for (i=1;
i<n; i++) m[i][i] = 0;
int main()
{
int n,i;
printf("Enter number of matrices :- \n");
scanf("%d",&n);
n++; int arr[n];
printf("\nEnter dimensions :- \n"); for(i=0;i<n;i++)
{ printf("Enter d%d :- ",i);
scanf("%d",&arr[i]);
}
int size = sizeof(arr)/sizeof(arr[0]);
printf("Minimum number of multiplications is :- %d ",
MatrixChainMultiplication(arr, size)); return 0;
}
Output:
PRACTICAL -8
Aim: Implementation of making a change problem using dynamic programming.
Solution:
#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[] = {2, 5, 8};
int m = sizeof(arr)/sizeof(arr[0]);
printf("%d \n", count(arr, m, 10));
return 0;
}
Output:
PRACTICAL -9
Aim: Implementation of Graph and Searching (DFS and BFS).
Solution (DFS):
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int i, visited[MAX];
char vertex[MAX];
int graph[MAX][MAX] = {{0,1,0,1,0},
{1,0,1,0,0},
{0,1,0,0,0},
{1,0,0,0,1},
{0,0,0,1,0}};
void dfs(int currentVertex)
{
printf("%c ",vertex[currentVertex]);
visited[currentVertex] = 1;
for(i=0; i<MAX; i++ )
{
if(visited[i] == 0)
{
//Recursive calling dfs() i.e implementing stack
dfs(i);
}
}
}
int main()
{
vertex[0] = 'A';
vertex[1] = 'B';
vertex[2] = 'C';
vertex[3] = 'D';
vertex[4] = 'E';
dfs(0);
return 0;
}
Output (DFS):
Page 26 of 30
ENROLLMENT NUMBER: IU0000000
Solution (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;
}
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;
}
}
}
Page 27 of 30
ENROLLMENT NUMBER: IU0000000
Output (BFS):
Page 28 of 30
ENROLLMENT NUMBER: IU0000000
PRACTICAL -10
Aim: Implement LCS problem.
Solution:
#include <stdio.h>
#include <string.h>
int i, j, m, n, LCS_table[20][20];
char S1[20] = "ACADB", S2[20] = "CBDA", b[20][20];
void lcsAlgo() {
m = strlen(S1);
n = strlen(S2);
int i = m, j = n;
while (i > 0 && j > 0) {
if (S1[i - 1] == S2[j - 1]) {
lcsAlgo[index - 1] = S1[i - 1];
i--;
j--;
index--;
}
else if (LCS_table[i - 1][j] > LCS_table[i][j - 1])
i--;
else
j--;
}
// Printing the sub sequences
printf("S1 : %s \nS2 : %s \n", S1, S2);
printf("LCS: %s", lcsAlgo);
Page 29 of 30
ENROLLMENT NUMBER: IU0000000
}
int main() {
lcsAlgo();
printf("\n");
}
Output :