DAA Lab Manual19-20
DAA Lab Manual19-20
LAB MANUAL
Subject Code:KCS-553
III Year Vth Semester(CSE)
Session-2022
Prepared By
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
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
if(index >= n)
{
return 0;
}
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);
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
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);
}}
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;
int arr[size];
printf("Enter the elements of array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
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);
}
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;
return 0;
}
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");
int main() {
int i, j;
nqueen(0);
return (0);
}
Output :
Enter the no. of queens:- 4
. . . Q
Q . . .
. . 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
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
27