You are on page 1of 35

LAB MANUAL

DESIGN AND ANALYSIS OF ALGORITHMS

SCHOOL OF COMPUTER SCIENCE & ENGINEERING

BAHRA UNIVERSITY SHIMLA HILLS

Page | 1

Table of contents 1.Wap in c to implement binary search algorithm. ............................................................................................ 3 2.Wap in c to implement linear search algorithm. ............................................................................................. 4 3.Wap in c to implement quick sort algorithm. .................................................................................................. 5 4.Wap in c to implement selection sort algorithm. ............................................................................................ 7 5. Wap in c to implement merge sort algorithm. ................................................................................................ 8 6. Wap in c to implement knapsack problem. .................................................................................................. 10 7. Wap in c to implement travelling salesman problem. .................................................................................. 14 8. Wap in c to implement graph coloring. ......................................................................................................... 16 9. Wap in c to implement prims algorithm. .................................................................................................... 18 10. Wap in c to implement kruskals algorithm. ............................................................................................... 22 11. Wap in to implement dijakstra algorithm. .................................................................................................. 25 12. Wap in c to implement 15 puzzle problem............................................................................................... 28

Page | 2

1. wap in c to implement binary search algorithm.


#include <stdio.h> int main () { int c, first, last, middle, n, search, array [100]; printf (enter number of elements\n); scanf (%d, &n); printf (enter %d integers\n, n); for (c=0; c<n; c++) scanf (%d, array*c+); printf (enter value to find \n); scanf (%d, &search); first=0; last=n-1; middle= (first+last)/2; while (first<=last) { if (array [middle] <search) first= middle+1; else if (array [middle] ==search) { printf (%d found at location %d \n, search, middle+1); break; ) else last = middle-1; middle= (first+last)/2; } if (first>last)
Page | 3

printf (not found! %d is not present in the list.\n, search); return0; }

2. wap in c to implement linear search algorithm.


#include <stdio.> int main () { int array [100], search, c, number; printf (enter the number of element in array\n); scanf (%d, &number); printf (enter %numbers\n, number); for (c= 0; < number; c++) scanf (%d, &array*c+); printf (enter the number to search \n); scanf (%d, &search); for (c=0; c< number; c++) { if (array[c] ==search) /*if required element found */ { printf (%d is present at location %d.\n, search, c+1); break; } } if (c==number) printf (%d is not present in array .\n, search); return0; }

Page | 4

3. wap in c to implement quick sort algorithm.


#include<stdio.h> int split (int*, int, int); void main () { int arr[10]={11,2,9,13,57,25,17,1,90,3}; int i; void quicksort (int*, int, int) quicksort (arr, 0, 9); printf (\narray after sorting; \n); for (i =0; i<=9; i++); printf 9%d\t, arr*i+); } void quicksort (int a [], int lower, int upper) { int i; if (upper>lower) { i =split (a, lower, upper); quicksort (a, lower, i-1); quicksort (a, i+1, upper); } } int split (int a [], int lower, int upper) { int i, p, q, t; p= lower+1; q=upper; i=a [lower];

Page | 5

while (q>=p) { while (a[p] <i) p++; while (a[q]>i) q--; if (p>q) { t=a[p]; a[q] =a[q]; a[q] =t; } } t=a [lower]; a [lower] =a[q]; a[q] =t; return; }

Page | 6

4 wap in c to implement selection sort algorithm.


#include <stdio.h> int main () { int array [100], n, c, d, position, swap; printf (enter number of element\n); scanf (%d, &n); printf (enter %d integers\n, n); for(c=0; c<n; c++) scanf (%d, &array*c+); for (c=0; c< (n-1); c++) { position =c; for (d=c+1; d<n; d++) { if (array [position]> array[d]) position=d; } if position1=c) { swap=array[c]; array[c] =array [position]; array [position] =swap; } }

printf (sorted list in ascending order: \n); for(c=0; c<n; c++) printf (%d\n, array*c+); return (0);}
Page | 7

5. wap in c to implement merge sort algorithm.


#include<stdio.h> #include<coino.h> void main (void) { int array [100], n, i=0 clrscr (); printf (enter the element to be sorted :); scanf (%d, &n); printf (\nenter the elements to be sorted: \n); for (i=0; i<n; i++) { printf (\t array *%d+ =, i); scanf (%d, &array*i+); } printf (\n before mergesort ;); // array before mergesort for (i=0; i<n; i++) { printf (%4d, array *i+); } printf (\n); mergesort (array, 0, n-1); printf (\n); mergsort (array, 0, n-1); printf (\n after mergsort :); // array after mergsort for (i=0; i <n; i++); { printf (%d4d, array *i+);
Page | 8

} printf (\n); getch (); } void mergsort (int arr [], int i, int h) { int i=0 int length = h-1+1; int pivot =0; int merge 1= 0; int merge2 =0; int temp [100]; if (i==h) return; pivot = (i+h)/2; mergesort (arr, i, pivot); mergesort (arr, pivot +1, h); for (i=0; i < length; i++) { temp [1] = arr [1+i]; } merg1 =0; merg2=pivot-1+1; for (i=0; i< length; i++ { if (merge2<=h-1) { if (merg1<=pivot-1) {
Page | 9

if (temp [merge1]>temp [merge2]) { arr [i+1] =temp [merge2++1]; } else { arr [i+1] = temp [merge1++]; } } else { arr [i+1] =temp [merge2++]; } } else { arr [i+1] =temp [merge1++]; } } }

6. wap in c to implement knapsack problem.


Page | 10

#include<stdio.h> #include<conio.h> int w[10],p[10],v[10],n,i,j,cap,x[10]={0}; int max (int, i, int j) { return ((i>j)? i: j); } int knap (int i, int j) { int value; if (v[i] [j] <0) { if (j<w[i]) value=kanp (i-1, j); else value = max (kanp (i-1, j), p[i] +kanp (i-1, j-w[i])); n[i] [j] =value; } return (v[i] [j]); } void main () { int profit, count =0; clrscr (); printf (\enter the number of elements \n); scanf (%d, &n); printf (\enter the profit and weights of the elements\n); for (i=1; i<=n; i++) {
Page | 11

printf (for item no %d\n, i); scanf (%d%d, &p*i+, &w*i+); } printf (\nenter the capacity \n); scanf (%d, &cap); for (i=0; i<=n; i++) for (j=0; j<cap; j++) if ((i==0) (j==0)) v[i] [j] =0; else v[i] [j] =-1; profit=knap (n, cap); i=n; j=cap; while (j1=0&&i1=0) { if (v[i] [j] 1=v [i-1] [j]) { x[i] =1; j=j-w[i]; i-; }

Page | 12

else i--; } printf (items included are \n); printf (si.no\tweight\tprofit\n); for (i=1; i<=n; i++) if(x[i]) printf (%d\t%d\t%d\n, ++count, w*i+, p[i]); printf (total profit = %d\n, profit); getch (); }

Page | 13

7. wap in c to implement travelling salesman problem.


/* about this algorithm: *have we use dynamic programming to find a solution to the * travelling salesperson problem. the problem consist of finding * the least cost cycle in a given set of modes*/ #include <stdio.h> #define max 100 # define infinity 999 int top_up (int c [] [max], int tour [], int start, int n); int main () { int n; /* number of cities*/ int i, j; /* loop counters */ int c [max] [max]; /* cost matrix. */ int cost; / * least cost. */ printf (this program demonstrates the tsp program.); printf (\nhow many cities to traverse?); scanf (%d, &n); printf (enter the cost matrix: (999: no connection)\n); for (i=0; i<n; i++) for (j=0; j<n; j++) scanf (%d, &c*i+ *j+); for (i=0; i<n; i++) tour [1] =i; cost=tsp_dp(c, tour, 0, n); printf (minimum cost: %d\n tour; cost); for (i=0; i<n; i++) printf (%d, tour*i+ +1); printf (1\n); } int tsp_dp (int c [] [max], int tour [], int start, int n) { int i, j, k; /* loop counter. */ int temp [max]; /* temporary during calculations.*/ int minimal tour [max); /* minimal tour array.*/ int mincost; /* minimal cost. */ int ccost; /* current cost. */
Page | 14

/* end of recursion condition. */ if (start == n-2) return c (tour [n-2]] [tour [n-1]] +c [tour [n-1]] [0]; /* compute the tour starting from the current city. */ mincost= infinity; for (i= start +1; i<n; i++) {for (j=0; j<n; i++) temp[j] =tour[j]; /* adjust positions. */ temp [start +1] = tour [i]; temp[i] = tour [start +1]; /* found a better cycle? (recurrence derivable)*/ if (c [tour [start]] [tour[i] + (ccost= tsp_dp (c, temp, start +1, n)) < mincost) { mincost = c [tour [start]] [tour[i]] +ccost; for (k=0; k<n; k++) mintour[k] = temp[k]; } } /* set the minimum tour array.*/ for (i=0; i<n; i++) tour [i] = mintour[i]; return mincost; }

Page | 15

8. wap in c to implement graph coloring.


# include<stdio.h> int g [50] [50].x [50]; // g: adjacency matrix, x: color void next_color (int k) { int i, j; x [k] =1 // coloring vertex color 1 for (i=0; i<k; i++) { // checking all k-1 vertices backtracking if (g[i] [k] 1=0&&x[k] ==x[i]) // if connected and has same color

x [k] = x [i] +1; // assign higher color than x [i] } } int main () { int n, e, i j, k, l; printf (enter no, of vertices :); scanf (%d, &n); // total vertices printf (enter no. of edges :); scanf (%d, &e); // total edges for (i=0; <n; i++) for (j=0; j<; j++) g[i] [j] =0; // assign 0 to all index of adjacency matrix printf (enter index where value is i-->\n); for (i=0; i<e; i++) { scanf (%d %d , &k, &l); g[k] [i] =1; g[i] [k] =1; }

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


Page | 16

next_color (i); // coloring each vertex printf (color of vertices-->\n); for (i=0 ;< n; i++) // displaying color of each vertex printf (vertex *%d\n, i+1, x*i+); return0; }

Page | 17

9. wap in c to implement prims algorithm.


#include<stdio.h> #include<conio.h> int n, cost [10] [10]; void prim () { int i, j, k, l, x, nr [10], temp, min_cost=0, tree [10] [3]; /* for first smallest edge*/ temp=cost [0] [0]; for (i=0; i<n; i++) { for (j=0; j<n; j++) { if (temp>cost [i] [j]) { temp= cost [i] [j]; k=i; i=j; } } /* now we have first smallest edge in graph */ tree [0] [0] =k; tree [0] [1] =i tree [0] [2] temp; min_cost= temp; /*now we have to find min dis of each vertex from either k or i by initializing nr [] array
Page | 18

*/ for (i=0; i<n; i++) { if (cost [i] [k] < cost [i] [i]) nr[i] =k; else nr[i] =i; } /* to indicate visited vertex initialize nr [] for them to 100 */ nr[k] =100; nr [i] =100; /* now find out remaining n-2 edges */ temp=99; for (i=1; i<n-1; i++) { if (nr[j] 1=100&& cost [j] [nr[j]] < temp) { temp=cost[j] [nr[j]]; x=j; } } /* now i have got next vertex */ tree [i] [0] =x; tree [i] [1] = nr [x]; tree[i] [2] =cost [x] [nr[x]]; min_cost=min_cost +cost[x] [nr[x]]; nr[x] =100; /*now find if x is nearest to any vertex than its previous near value*/
Page | 19

for (j=0; j<n; j++) { if (nr[j] 1=100 && cost [j] nr[j]]> cost [j] [x]) nr [j] =x; } temp=99; } /* now i have the answer, just going to print it */ printf (n the main spanning tree is :-); for (i=0; i<n-1; i++) { for (j=0; j<3; j++) printf (%d, tree *i+ *j+); printf (n); } printf (n min cost: - %d , mon_cost); } /////////////////////////////////////////////////////// { void main () { int i, j; clrscr (); printf (n enter the no. of vertices :-) scanf (%d, &n);

Page | 20

printf (enter the costs of edges in matrix form :-); for (i=0; i<n; i++) for (j=0; <n; j++) scanf (%d, /& cost *i+ *j+); printf (n the matrix is: - ); for (i=0; i<n; i++) { for (i=0; j<n; i++) printf (%dt, cost *i+ *j+); printf (n); } prim (); getch (); }

Page | 21

10. Wap in c to implement kruskals 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\n\t implementation of kruskals algorithm\n\n); printf (\nenter the no. of vertices\n); scanf (%d, &n); printf (\nenter the cost adjacency matrix \n); for (i=1; i<=n; j++) { scanf (%d, &cost *i+ *j+); if (cost [i] [j] ==0 cost[i] [] =999; } } printf (\n the edge of minimum cost spanning tree are \n\n); while (ne<n) { for (i=1, min=999: i<=n; i++) { for (j=1; j<=n; j++)
Page | 22

{ 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 (\n%d edge (%d, %d) =%d\n, ne++, a, b, min); mincost+= min; } cost[a] [b] =cost[b] [a] =999; } printf (\n\minimum cost = %d\n, mincost); getch (); } int find (int i) { while (parent[i]) i=parent[i]; return i; }

Page | 23

int uni (int, i, int, j) { if (i1=j) { parent [j] =i; return1; } return0; }

Page | 24

11. Wap in to implement dijakstra algorithm.


/* program to find shortested path using dijkstras algorithm this is a static implemention of program using a two demientional weight matrix , but this program can not support a searrio where number of nodes of a graph may change during execution */ #include<stdio.h> #include<conio.h> #define infinity 2000 #define maxnodes 4 #define member 1 #define nonmember 0

void shortpath (int weight [] (maxnodes}, int, int, int *, int precede []) ; int main (void) { int i, j, s, t; int weight [maxnodes] [maxnodes], precede [maxnodes], pd; printf (\nenter weight matrix ); for (i=0; < maxnodes; i++) for (j=0; j < maxnodes; j++) scanf (%d , & weight *i+ *j+); for (i0; i< maxnodes; i++) { printf (\n); printf (%d, weight *i+ *j+); } printf (\n enter the starting node and the ending node :); scanf(%d, %s, &t); shortpath (weight, s, t, & pd, precede);
Page | 25

printf (\n the shortest path from node %d to %d is; %d , s, t, pd); return (0); } void shortest path (int weight [] maxnodes], int s, int t, int * pd, int precede []) { int distance [maxnodes], perm [maxnodes]; int current, i, j, k, dc; int smalldist, newdist; /* initialization of perm and distance array */ for (i=0; i< maxnodes; i++) { perm[i] = nomember; distance[i] =infinity ;} } perm[s] =member; distance[s] =0; current=s; while (current 1=t) { smallest = i nfinity; dc= distance [current]; for (i= 0; i < maxnodes; i++) if (perm[i] ==nonmember) { newdist= dc + weight [current] [i]; if (newdist< distance[i]) { distance [1] =newdist; precede[i] =current;
Page | 26

} if (distance[i] < smalldist) { smalldist=distance[i]; k=i; } } */ end of for if */ current =k; perm [current] =member; } /* end while */ *pd = distance [t]; } /* end of shortpath function*/

Page | 27

12. wap in c to implement 15 puzzle problem.


#include<stdio.h> #include<stdlib.h> #include<time.h>

int table [4] [4] = {{1,2,3,4},{5,6,7,8,},{9,10,11,12,},{13,14,15,0}; int count=0; void displaytable (void); void movenumber (void); void moveright (void); void mobeleft (void); void moveup (void); void movedown (void); void ownsetup (void); void randomizer (void); int check for finish (void); main (void) { int rory; print (hello, and welcome to hunters 15 puzzle1\n); print (to play a randomly generated puzzle press r\n); printf (to set up your own puzzle press y\n); rory=getchar (); if (rory==r) randomizer (); if (rory==o) ownsetup ();

Page | 28

display table (); // function used to randomize the grid void randomize () { stand (time (null)); char unsigned x = rand () %4; if (x=0) { moveup (); } else if (x=1) { movedown(); } else if (x=2) { moveleft (); } else if (x=3); { moveright (); } } // function used to set up your own grid void ownsetup (); { printf (\n please enter the number for the top row from left to right\n); printf (followed by the number for the second row from left to right. etc. \n); printf (please do not forget to press enter in between each number. \n); scanf (%d, &table *0+ *0+);
Page | 29

scanf (%d, &table *0+ *1+); scanf (%d, &table *0+ *0+); scanf (%d, &table *0+ *1+); scanf (%d, &table *0+ *2+); scanf (%d, &table *0+ *3+); scanf (%d, &table *1+ *0+); scanf (%d, &table *1+ *1+); scanf (%d, &table [1] [2]); scanf (%d, &table *1+ *3+); scanf (%d, &table *2+ *0+); scanf (%d, &table *2+ *1+); scanf (%d, &table *2+ *2+); scanf (%d, &table *2+ *3+); scanf (%d, &table *3+ *0+); scanf (%d, &table *3+ *1+); scanf (%d, &table *3+ *2+); scanf (%d, &table *3+ *3+); } // function used to move the numbers in the grid void move number () { int key; printf (\n\enter a letter (i, j, k, or m) ;); key= getchar (); if (key==j) move left (); if (keyk) move right (); if (key==i) move up ();
Page | 30

if (key==m) movedown (); displaytable(); } // move the number to the right void moveright(); int temp, i, j; for (i=0; i<4; i++) { for (j=0; j<4; j++) { if (table[i] [j] ==0 &&j 1=0) { temp=table[i] [j-1]; table[i] [j] =temp; } } } }

// move the number to the left void moveleft () { int temp ,i, j; for (i=3 i>=0; i--) { for (j=3; j>=0; j--) { if (table {i] [j] ==0 &&j 1=3) { temp=table[i] [j+1]; table[i] [j+1] =0; table[i] [j] =temp; } } }
Page | 31

} // move the number up void moveup() int temp, i, j; for (i=3: i>=0; i--) { for (j=3; j>=0; j--) { if (table [i] [j] ==0 && i 1 = 3) { temp = table [i+1] [j]; table [i+1] [j] =0; table[i] [j] = temp; } } } } // move the number down void movedown () int temp, i, j; for (i=0; i<4; i++) { for (j=0; j<4; j++) { { {

if (table[i] [j] ==0&&i1=0) { temp=table[i-1] [j]; table[i] [j] =temp; } } } } // function used to display the table void display table {} { int i, j;
Page | 32

for (i=0; i<4; i++) { printf (\n\n); for (j=0; j<4; j++) { if (table [i] [j] ==0) printf ( ); else if (table [i] [j] <10) printf (%d, table *i+ *j+); else printf(%d, table*i+ *j+); } } printf(\n); } // function used to spot a winning board int checkfor finish (void) { int i, j, temp, result; result=0; temp=1; for (i=0; i<4; i++) { for (j=0; j<4; j++) { if (table[i] [j] ==temp) { temp+=1;
} } } if (temp==15) result=1; return result; }} Page | 33

Page | 34

You might also like