Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
30 views35 pages

Prashant DAA File

Uploaded by

Prashant Dixit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
30 views35 pages

Prashant DAA File

Uploaded by

Prashant Dixit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 35
DRONACHARYA GROUP OF INSTITUTIONS DESIGN AND ANALYSIS OF ALGORITHM LAB FILE NAME — Prashant Dixit CLASS — G1-B UNIVERSITY ROLL NO — 2102300100119 COLLEGE ROLL NO - 16125 DESIGN AND ANALYSIS OF ALGORITHMS LAB (KCS 553) List of Experiments SrNo Name Of Experiments Dates of Experiments 1 | Program for Recursive Binary & Linear Search. 2 | Program for Heap Sort & Quick Sort, 3 | Program for Merge Sort. 4 | Program for Selection Sort. 5 | Program for Insertion Sort. 6 | Knapsack Problem using Greedy Solution 7 | Perform Travelling Salesman Problem 8 _ | Find Minimum Spanning Tree using Kruskal's Algorithm 9 | Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s algorithm, 10 | Implement, the 0/1 Knapsack problem using (a) Dynamic Programming method (b) Greedy method. PROGRAM-1 Q4 - PROGRAM FOR RECURSIVE BINARY AND LINEAR SEARCH. include int binarySearch(int arr{], int |, intr, int x) { if(r>= I) int mid = 1+ (r=1) /2; if (arr{mid] == x) return mid; if (arr{mid] > x) { return binarySearch(arr, |, mid - 1, x); } return binarySearch(arr, mid + 1, r, x); d return -1; } J/ driver code int main(void) { _ intarel] ={2,3, 4,10, 40}; int size = sizeof(arr) / sizeof(arr{0)); // elementtobe searched intx = 10; int index = binarySearch(arr, 0, size -1, x); if (index == -1) { printf("Element is not present in array"); else { printf("Element is present at index %d'", index); } return 0; (edi igh - 0 x Hinclude int linearSearch(int arr(], int size, int key) { if (size == 0) { return -1; } if (arr{size - 1} == key) { return size - 1; } return linearSearch(arr, size - 1, key); } // Driver code int main() int arrfl 5, 15, 6, 9, 4}; intkey=4; int index = linearSearch(arr, sizeof(arr) / sizeof(int), key}; if (index. 1{ printf("Key not found in the array.\n"); } else { printf("The element %6d is found at %d index of the " “given array \n", key, index); } return 0; Q.2~- PROGRAM FOR HEAP SORT. include void swap(int *a, int *b) { int temp = *a; tas tb; *“b=temp; } void heapify(int arr} int n, int i) { intlarges intleft=2*1+1; intright = 2* i+ 2; if left arrllargest]) largest = left; if (right < n && arr{right] > arr[largest]) largest = right; if (largest != i) { swap(8arr(i], arrllargest)); heapify(arr, n, largest); } void heapSort{intarr{], int n) { for (inti=n /2- 4; 1 >= 0;§- heapify(arr, n, i); PROGRAM-2 for (inti=m-11>= 0;i-) { swap(8arr[0], &arr[il); heapify(arr, i, 0); } } void printArray(int arr[], int n) { for (inti=0;i void merge(intarr{],intp, int q, intr) { intnd=q-p+4; intn2=r-q; int Und], M[n2]; for (inti= 0; i< m4; i++) Ui] = arr{p + i]; for (int} = 0; j void swap(int *a, int *b) { int temp = *a; *a=tb; *b=temp; } void selectionSort{int array{], int size) { for(int step = 0; step < size- 1; step++){ int min_idx = step; for (int i= step + 1; i< size; i++) { if (arrayli] void printArray(int array[], int size) { for int i= 0;1< size; i++) { printf ("sed ", arraylil); } printf("\n"); } void insertionSort{int array], int size) { for (int step = 1; step < size; step+t) { int key = array[step]; intj=step-1; while (key = 0) { arraylj + 1] = arraylj); oi } arraylj + 1)= key; } } int main() { int datal] = (9, 5, 1, 4, 3}; int size = sizeof(data) / sizeof(data(0]); insertionSort(data, size); printf("Sorted array in ascending order:\n"); printArray(data, size);} PROGRAM-6 Q6 - PROGRAM FOR QUICK SORT include void swap(int *a, int *b) { int int partition(int arrayl], int low, int high) { int pivot = arrayihigh]; inti = (low - 1); for(int j= low; < high; j++) { if (arrayli] <= pivot) { itt; swap(8arrayli], &array(j)); } } swap(8array[i+ 1], &arrayfhigh]); return (i+ 1); } void quickSort(intarray[], int low, int high) { if (low < high) { int pi= partition(array, low, high); quickSort(array, low, pi- 1); quicksort(array, pi+ 1, high); } } void printArray(intarrayl], int size) { for (int i= 0;i < size; ++i) { printf ("96d ", arraylil); ) printf("\n"); } int main) { int datal] = (8, 7, 2, 1, 0, 9, 6}; int n = sizeof(data) /sizeof(datal0}); printf("Unsorted Array\n"); printArray(data, n); quickSort(data, 0, n - 1); printf("Sorted array in ascending order:\n"); printArray(data, n);} I PROGRAM-7 Q7- KNAPSACK PROBLEM USING GREEDY SOLUTION. include void main() { int capacity, no_items, cur_weight, item; int used[10]; float total_profit inti, int weight{10]; int value[10]; printf("Enter the capacity of knapsack:\n"); scanf("%6d", &capacity); printf(""Enter the number of items: scanf("%d", &ino_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("ged", &value[i)); } for i=0;i 0) { for (i= 0; i (float) valuefitem] /weightfitem)))) item =i; used[item] = 1; cur_weight = weightlitem]; total_profit += value[item); if (cur_weight >= 0) printf("Added object 96d (%d Rs., %dKg) completely in the bag. Space left: %d.\n", item + 1, valuefitem], 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, valuelitem], 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 Q8 - FIND MINIMUM SPANNING TREE USING KRUSKAL’S ALGORITHM. Hinclude include const int inf = 999999; intk,a, b, u,v, n, ne= 3; int mincost = 0; int cost{3][3] = {0, 10, 20},{22, 0,15},{16, 18, 0}; int pl9I = {0}; int applyfindyint i) { while( pli] != 0) iplils return i } int applyunion(int int) { if(it=i) { pil return 1; } return 0; y int main() n=3; inti, i; for (inti=0;1< 1; i++) { for (int j= 0; j %d\n", a, b); mincost +=min_val; } cost[a][b] = cost{b] [a] = 999; ne++; } printf(" Minimum cost = %d”,mincost); return 0; PROGRAM-9 Q9-- PROGRAM FOR KNAPSACK PROBLEM USING DYANAMIC PROGRAMMING. include int max(inta, int b) {return (a>b) ? a:b; } int knapSack(int W, int wt], int all], int n) { // Base Case if (n==0|| W==0) return 0; if (wt[n - 1] > W) return knapSack(W, wt, val, n- 1); else return max( valfn -4] + knapSack(W-wt[n- 1], wt, val,n- 1), knapSack(W, wt, val, n -1)); } // Driver code int main() { int profit(] = { 60, 100, 120 }; int weight!] = { 10, 20, 30}; int W = 50; int n = sizeof(profit) / sizeof(profit(0}); printf("96d", knapSack(W, weight, profit, n)}; return 0; } Mdniningaccre PROGRAM-10 Q10- FIND MINIMUM SPANNING TREE USING PRIM’S ALGORITHM. include include Hinclude define V5 int minkey(int key[], bool mstSet[]) { // initialize min value int min = INT_MAX, min_index; for (int v= 0; v< V; v++) if (mstSet[v] == false && key[v] < min) min = key[v], min_index = v; return min_index; } int printMST(int parent], int graphVJIV]) { printf("Edge \tWeight\n"); for(int i= 1;i int matrix{25][25], visited_cities[ 10), limit, cost = 0; int tsp(int ¢) { int count, nearest_city = 999; int minimum = 999, temp; for(count = 0; count < limit; count++) { if((matrix{c][count] != 0) &8 (visited_cities{count] == 0)) { if{matrix{c][count] < minimum) { minimum = matrix[count]{0] + matrix{c][count]; } temp= matrix{c][count]; nearest_city = count; } } if{minimum '= 999) { cost = cost + temp; } return nearest_city; , void minimum_cost{int city) { int nearest_city; visited_cities{city] = 1; printf("s6d", city + 1); nearest_city=tsp(city); if(nearest_city == 999) { nearest_city = 0; printf("sod", nearest_city + 1); cost = cost + matrix{city][nearest_city); return; } minimum_cost(nearest_city); } int main() { inti, i: printf("Enter Total Number of Cities:\t"); scanf("géd'", &limit); printf("\nEnter Cost Matrix\n"); for{i= 0; < limit; i++) { printf("\nEnter %d Elements in Row[%d]\n", limit, i+ 1); for{j =0;j < limit; j++) { scanf("s6d", &matrixlilj)); } visited_cities[i] = 0; } printf("\nEntered Cost Matrix\n"); Fori= 0; i< limit; i++) { printf("\n"); for(j = 0; < limit; j++) { printf("96d ", matrifilj)); ) } printf("\n\nPath\t"); minimum _cost(0); printf("\n\nMinimum Cost:\t"}; printf("96d\n", cost); return 0; ,

You might also like