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

DAA Lab Manual19-20

Uploaded by

Suraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

DAA Lab Manual19-20

Uploaded by

Suraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 27

DESIGN AND ANALYSIS OF ALGORTHIM

LAB MANUAL

Subject Code:KCS-553
III Year Vth Semester(CSE)
Session-2022

Prepared By

Mr. Mohd Kaleem Mr. Amit Kr. Srivastava


Assistant Professor,CSE Assistant Professor,CSE

Department of Computer Science & Engineering

SHAMBHUNATH INSTITUTE OF ENGINEERING & TECHNOLOGY


Prayagraj
SHAMBHUNATH INSTITUTE OF ENGINEERING & TECHNOLOGY
Prayagraj
COMPUTER SCIENCE AND ENGINEERING

Design and analysis of Algorithm LAB(KCS-553)

Course Outcome(CO) Bloom’s Knowledge Level(KL)

CO 1 Implement algorithm to solve problems by iterative approach. K2, K4

CO 2 Implement algorithm to solve problems by divide and conquer K3, K5


approach
CO 3 Implement algorithm to solve problems by Greedy algorithm K4, K5
approach.
Implement algorithm to solve problems by Dynamic K4, K5
CO 4 programming, backtracking,
branch and bound approach.
CO 5 Implement algorithm to solve problems by branch and bound K3, K4
approach.
Design and analysis of Algorithm LAB SYLLABUS (KCS-
553)

DETAILED SYLLABUS
1. Write a program in c using gcc complier using iterative approach
a) Simple Linear Search
b) Recursive Linear Search

2. Write a program in c using gcc complier using divide and conquer approach
a) Simple Binary Search
b) Recursive Binary Search
3. Write a program in c using gcc complier for Selection sort using recursive

4. Write a program in c using gcc complier for Insertion sort using recursive

5. Write a program in c using gcc complier for Merge sort using recursive using divide and conquer
approach

6. Write a program in c using gcc complier for Quick sort using recursive u sing divide and conquer
approach

7. Write a program in c using gcc complier Fractional Knapsack Problem using Greedy Solution

8. Write a program in c using gcc complier Strassen’s Matrix Multiplication .using divide and conquer
approach

9. Write a program in c using gcc complier Implement N Queen Problem using Backtracking

10. Implement Travelling Sales Person problem using Branch and bound method

PROGRAM -1
Object: Write a program in c using gcc complier
a)Simple Linear Search
b) Recursive Linear Search

Code: a) Simple Linear Search


#include <stdio.h>
#include<time.h>
int main()
{
clock_t t;
t=clock();

int array[100], search, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

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


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

printf("Enter a number to search\n");


scanf("%d", &search);

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


{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
t=clock()-t;
double time_taken=((double)t/CLOCKS_PER_SEC);
printf("%f",time_taken);

return 0;
}

INPUT &OUTPUT:
Input: 5
Enter the array elements
1
2
3
4
5
Enter a number to search
4
Output:4 is present at location 4.
0.000271

Code: b) Recursive Linear Search


#include <stdio.h>
#include<time.h>
int RecursiveLS(int arr[], int value, int index, int n)
{
int pos = 0;

if(index >= n)
{
return 0;
}

else if (arr[index] == value)


{
pos = index + 1;
return pos;
}

else
{
return RecursiveLS(arr, value, index+1, n);
}
return pos;
}

int main()
{
clock_t t;
t=clock();
int n, value, pos, m = 0, arr[100];
printf("Enter the total elements in the array ");
scanf("%d", &n);

printf("Enter the array elements\n");


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

printf("Enter the element to search ");


scanf("%d", &value);

pos = RecursiveLS(arr, value, 0, n);


if (pos != 0)
{
printf("Element found at pos %d ", pos);
}
else
{
printf("Element not found");
}

double time_taken=((double)t/CLOCKS_PER_SEC);
printf("%f",time_taken);
return 0;
}
INPUT:
Enter the total elements in the array 5
Enter the array elements
1
2
3
4
5
Enter the element to search 4
OUTPUT:
Element found at pos 4
0.000536

PROGRAM-2
Object: Write a program in c using gcc complier
a) Simple Binary Search
b) Recursive Binary Search

Code: a) Simple Binary Search


#include <stdio.h>
#include<time.h>
int main()
{
clock_t t;
t=clock();
int i, low, high, mid, n, key, array[100];
printf("Enter number of elements");
scanf("%d",&n);
printf("Enter %d integer", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to find");
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+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
t=clock()-t;
double time_taken=((double)t/CLOCKS_PER_SEC);
printf("%f",time_taken);
return 0;
}
INPUT
Enter number of element5
Enter 5 integer1
2
3
4
5
OUTPUT
Enter value to find4
4 found at location 4
0.000608
Code: b) Recursive Binary Search
#include <stdio.h>
#include<time.h>
void binary_search(int [], int, int, int);
int main()
{
int key, size, i;
int list[25];
clock_t t;
t=clock();
printf("Enter size of a list: ");
scanf("%d", &size);
printf("Enter elements\n");
for(i = 0; i < size; i++)
{
scanf("%d",&list[i]);
}
printf("\n");
printf("Enter key to search\n");
scanf("%d", &key);
binary_search(list, 0, size, key);
t=clock()-t;
double time_taken=((double)t/CLOCKS_PER_SEC);
printf("%f",time_taken);
}
void binary_search(int list[], int lo, int hi, int key)
{
int mid;
if (lo > hi)
{
printf("Key not found\n");
return;
}
mid = (lo + hi) / 2;
if (list[mid] == key)
{
printf("Key found\n");
}
else if (list[mid] > key)
{
binary_search(list, lo, mid - 1, key);
}
else if (list[mid] < key)
{
binary_search(list, mid + 1, hi, key);
}}
INPUT: Enter size of a list: 5
Enter elements
1
2
3
4
5
OUTPUT: Enter key to search
4
Key found
0.000538
PROGRAM- 3
Object: Write a program in c using gcc complier for Selection sort using recursive
Code:
#include <stdio.h>
#include<time.h>
void selection(int [], int, int, int, int);
int main()
{
int list[30], size, temp, i, j;
clock_t t;
t=clock();
printf("Enter the size of the list: ");
scanf("%d", &size);
printf("Enter the elements in list:\n");
for (i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
selection(list, 0, 0, size, 1);
printf("The sorted list in ascending order is\n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
t=clock()-t;
double time_taken=((double)t/CLOCKS_PER_SEC);
printf("%f",time_taken);

return 0;
}
void selection(int list[], int i, int j, int size, int flag)
{
int temp;

if (i < size - 1)
{
if (flag)
{
j = i + 1;
}
if (j < size)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
selection(list, i, j + 1, size, 0);
}
selection(list, i + 1, 0, size, 1);
}}

INPUT: Enter the size of the list: 5


Enter the elements in list:
1
45
32
67
89
OUTPUT:
The sorted list in ascending order is
1 32 45 67 89
0.000281
PROGRAM- 4
Object: Write a program in c using gcc complier for Insertion sort using recursive
Code: #include<stdio.h>
#include<time.h>
void InsertionSort(int arr[], int n)
{
if(n<=1)
return;
InsertionSort(arr, n-1);
int temp = arr[n-1];
int in = n-2;
while(in>=0 && arr[in]>temp)
{
arr[in+1] = arr[in];
in--;
}
arr[in+1] = temp;
}
int main()
{
int size;
clock_t t;
t=clock();

printf("Enter the size of the array:");


scanf("%d",&size);
int arr[size],i;
printf("Enter the element of the array:");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);

printf("Before Sorting Array Element are: ");


for(i=0;i<size;i++)
printf("%d ",arr[i]);
InsertionSort(arr, size);
printf("\nAfter Sorting Array Element are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
t=clock()-t;
double time_taken=((double)t/CLOCKS_PER_SEC);

printf("%f",time_taken);
}
INPUT
Enter the size of the array:5
Enter the element of the array:1
2
3
4
2
OUTPUT
Before Sorting Array Element are: 1 2 3 4 2
After Sorting Array Element are: 1 2 2 3 4
0.000280
PROGRAM- 5
Object: Write a program in c using gcc complier for Insertion sort using recursive
Code:
#include <stdio.h>
#include <stdlib.h>
#include<time.h>

// merge function
void Merge(int arr[], int left, int mid, int right)
{
int i, j, k;
int size1 = mid - left + 1;
int size2 = right - mid;

// created temporary array


int Left[size1], Right[size2];

// copying the data from arr to temporary array


for (i = 0; i < size1; i++)
Left[i] = arr[left + i];

for (j = 0; j < size2; j++)


Right[j] = arr[mid + 1 + j];

// merging of the array


i = 0; // intital index of first subarray
j = 0; // inital index of second subarray
k = left; // initial index of parent array
while (i < size1 && j < size2)
{
if (Left[i] <= Right[j])
{
arr[k] = Left[i];
i++;
}
else
{
arr[k] = Right[j];
j++;
}
k++;
}

// copying the elements from Left[], if any


while (i < size1)
{
arr[k] = Left[i];
i++;
k++;
}

// copying the elements from Right[], if any


while (j < size2)
{
arr[k] = Right[j];
j++;
k++;
}
}

//merge sort function


void Merge_Sort(int arr[], int left, int right)
{
if (left < right)
{

int mid = left + (right - left) / 2;

// recursive calling of merge_sort


Merge_Sort(arr, left, mid);
Merge_Sort(arr, mid + 1, right);

Merge(arr, left, mid, right);


}
}
// driver code
int main()
{
int size;
clock_t t;
t=clock();
printf("Enter the size: ");
scanf("%d", &size);

int arr[size];
printf("Enter the elements of array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}

Merge_Sort(arr, 0, size - 1);

printf("The sorted array is: ");


for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
t=clock()-t;
double time_taken=((double)t/CLOCKS_PER_SEC);
printf("%f",time_taken);
return 0;
}
INPUT:
Enter the size: 5
Enter the elements of array: 67
34
56
23
2
OUTPUT:
The sorted array is: 2 23 34 56 67
0.000241
PROGRAM- 6
Object: Write a program in c using gcc complier for Quick sort using recursive
Code: #include <stdio.h>
#include<time.h>
void quicksort (int [], int, int);
int main()
{
int list[100];
int value, i;
clock_t t;
t=clock();
printf("Enter The Number Of Elements To Sort: ");
scanf("%d", &value);
printf("\nEnter The Elements To Sort: \n");
for (i = 0; i < value; i++)
{
printf("\nEnter The [ %d ] Element: ",i+1);
scanf("%d", &list[i]);
}
quicksort(list, 0, value - 1);
printf("\n Implemented Quick Sort: \n\n");
for (i = 0; i < value; i++)
{
printf("%d ", list[i]);
}
printf("\n");
t=clock()-t;
double time_taken=((double)t/CLOCKS_PER_SEC);
printf("%f",time_taken);
return 0;
}
void quicksort(int list[], int low, int high)
{
int pivot, i, j, temp;
if (low < high)
{
pivot = low;
i = low;
j = high;
while (i < j)
{
while (list[i] <= list[pivot] && i <= high)
{
i++;
}
while (list[j] > list[pivot] && j >= low)
{
j--;
}
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
} }
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, low, j - 1);
quicksort(list, j + 1, high);
}}
INPUT:
Enter The Number Of Elements To Sort: 5
Enter The Elements To Sort:
Enter The [ 1 ] Element: 34
Enter The [ 2 ] Element: 22
Enter The [ 3 ] Element: 11
Enter The [ 4 ] Element: 34
Enter The [ 5 ] Element: 2
Implemented Quick Sort:
2 11 22 34 34
0.000374
PROGRAM- 7
Object: Write a program in c using gcc complier Fractional Knapsack Problem using Greedy
Solution
Code: #include <stdio.h>
int 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)
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);
}

INPUT: Enter the capacity of knapsack:


50
Enter the number of items:
3
Enter the weight and value of 3 item:
Weight[0]: 10
Value[0]: 60
Weight[1]: 20
Value[1]: 100
Weight[2]: 30
Value[2]: 120
OUTPUT: Added object 1 (60 Rs., 10Kg) completely in the bag. Space left: 40.
Added object 2 (100 Rs., 20Kg) completely in the bag. Space left: 20.
Added 66% (120 Rs., 30Kg) of object 3 in the bag.
Filled the bag with objects worth 240.00 Rs.

PROGRAM- 8
Object: Write a program in c using gcc complier Strassen’s Matrix Multiplication .using divide
and conquer approach
Code:
#include<stdio.h>
int main(){
int a[2][2], b[2][2], c[2][2], i, j;
int m1, m2, m3, m4 , m5, m6, m7;

printf("Enter the 4 elements of first matrix: ");


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

printf("Enter the 4 elements of second matrix: ");


for(i = 0; i < 2; i++)
for(j = 0;j < 2; j++)
scanf("%d", &b[i][j]);

printf("\nThe first matrix is\n");


for(i = 0; i < 2; i++){
printf("\n");
for(j = 0; j < 2; j++)
printf("%d\t", a[i][j]);
}

printf("\nThe second matrix is\n");


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

m1= (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]);


m2= (a[1][0] + a[1][1]) * b[0][0];
m3= a[0][0] * (b[0][1] - b[1][1]);
m4= a[1][1] * (b[1][0] - b[0][0]);
m5= (a[0][0] + a[0][1]) * b[1][1];
m6= (a[1][0] - a[0][0]) * (b[0][0]+b[0][1]);
m7= (a[0][1] - a[1][1]) * (b[1][0]+b[1][1]);

c[0][0] = m1 + m4- m5 + m7;


c[0][1] = m3 + m5;
c[1][0] = m2 + m4;
c[1][1] = m1 - m2 + m3 + m6;

printf("\nAfter multiplication using Strassen's algorithm \n");


for(i = 0; i < 2 ; i++){
printf("\n");
for(j = 0;j < 2; j++)
printf("%d\t", c[i][j]);
}

return 0;
}

INPUT: Enter the 4 elements of first matrix:


12
34
Enter the 4 elements of second matrix:
56
78
The first matrix is

1 2
3 4
The second matrix is
5 6
7 8
OUTPUT: After multiplication using Strassen's algorithm

19 22
43 50

PROGRAM- 9
Object: Write a program in c using gcc complier Strassen’s Matrix Multiplication .using divide
and conquer approach
CODE: #include<stdio.h>
#include<math.h>

char a[10][10];
int n;

void printmatrix() {
int i, j;
printf("\n");

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


for (j = 0; j < n; j++)
printf("%c\t", a[i][j]);
printf("\n\n");
}
}

int getmarkedcol(int row) {


int i;
for (i = 0; i < n; i++)
if (a[row][i] == 'Q') {
return (i);
break;
}
}

int feasible(int row, int col) {


int i, tcol;
for (i = 0; i < n; i++) {
tcol = getmarkedcol(i);
if (col == tcol || abs(row - i) == abs(col - tcol))
return 0;
}
return 1;
}

void nqueen(int row) {


int i, j;
if (row < n) {
for (i = 0; i < n; i++) {
if (feasible(row, i)) {
a[row][i] = 'Q';
nqueen(row + 1);
a[row][i] = '.';
}
}
} else {
printf("\nThe solution is:- ");
printmatrix();
}
}

int main() {
int i, j;

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


scanf("%d", &n);

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


for (j = 0; j < n; j++)
a[i][j] = '.';

nqueen(0);
return (0);
}
Output :
Enter the no. of queens:- 4

The solution is:-


. Q . .

. . . Q

Q . . .

. . Q .

The solution is:-


. . Q .

Q . . .

. . . Q

. Q .

PROGRAM- 10
Object: Implement Travelling Sales Person problem using Branch and bound method
CODE:
#include<stdio.h>
#include<conio.h>
int a[10][10], visited[10], n, cost = 0;
void get() {
int i, j;
printf("Enter No. of Cities: ");
scanf("%d", &n);
printf("\nEnter Cost Matrix: \n");
for (i = 0; i < n; i++) {
printf("\n Enter Elements of Row# : %d\n", i + 1);
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
visited[i] = 0;
}
printf("\n\nThe cost list is:\n\n");
for (i = 0; i < n; i++) {
printf("\n\n");
for (j = 0; j < n; j++)
printf("\t % d", a[i][j]);
}
}
void mincost(int city) {
int i, ncity;
visited[city] = 1;
printf("%d –>", city + 1);
ncity = least(city);
if (ncity == 999) {
ncity = 0;
printf("%d", ncity + 1);
cost += a[city][ncity];
return;
}
mincost(ncity);
}
int least(int c) {
int i, nc = 999;
int min = 999, kmin;
for (i = 0; i < n; i++) {
if ((a[c][i] != 0) && (visited[i] == 0))
if (a[c][i] < min) {
min = a[i][0] + a[c][i];
kmin = a[c][i];
nc = i;
}
}
if (min != 999)
cost += kmin;
return nc;
}
void put() {
printf("\n\nMinimum cost:");
printf("%d", cost);
}
void main() {
get();
printf("\n\nThe Path is:\n\n");
mincost(0);
put();
}
INPUT
Enter No. of Cities: 6
Enter Cost Matrix:
99 10 15 20 99 8
5 99 9 10 8 99
6 13 99 12 99 5
8 8 9 99 6 99
99 10 99 6 99 99
10 99 5 99 99 99

Enter Elements of Row# : 1


Enter Elements of Row# : 2
Enter Elements of Row# : 3
Enter Elements of Row# : 4
Enter Elements of Row# : 5
Enter Elements of Row# : 6
OUTPUT
The Path is:

1 –>6 –>3 –>4 –>5 –>2 –>1

Minimum cost:46
ATTAINMENT OF PROGRAM OUTCOMES & PROGRAM
SPECIFIC OUTCOMES

Program Specific
Program
Exp.No. Outcomes Attained
Experiment Outcomes
Attained
1 Write a program in c using gcc complier using iterative
approach
a) Simple Linear Search
b)Recursive Linear Search

2 Write a program in c using gcc complier using divide and


conquer approach
a)Simple Binary Search
b) Recursive Binary Search

3 Write a program in c using gcc complier for Selection sort


using recursive
4 Write a program in c using gcc complier for Insertion sort using
recursive
5 Write a program in c using gcc complier for Merge sort using
recursive using divide and conquer approach
6 Write a program in c using gcc complier for Quick sort using
recursive using divide and conquer approach
7 Write a program in c using gcc complier Fractional Knapsack
Problem using Greedy Solution
8 Write a program in c using gcc complier Strassen’s Matrix
Multiplication .using divide and conquer approach
9 Write a program in c using gcc complier Implement N Queen
Problem using Backtracking
10 Implement Travelling Sales Person problem using Branch and
bound method

27

You might also like