Professional Documents
Culture Documents
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
Page | 4
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
printf (sorted list in ascending order: \n); for(c=0; c<n; c++) printf (%d\n, array*c+); return (0);}
Page | 7
} 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++]; } } }
#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
/* 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
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; }
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
*/ 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
{ 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
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
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