Advertisement
d1i2p3a4k5

aoa

Apr 29th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 20.28 KB | None | 0 0
  1. expt1
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<time.h>
  5. #define len 100000
  6.     double selectionsort(int a[],long int n)
  7.     {
  8.     long int i,j,pos;
  9.     int min;
  10.     clock_t t1,t2;
  11.     double time_elapsed;
  12.     t1=clock();
  13.     for(i=0;i<n;i++)
  14.     {
  15.         min=a[i];
  16.         pos=i;
  17.         for(j=i+1;j<n;j++)
  18.         {
  19.         if(min>a[j])
  20.         {
  21.             min=a[j];
  22.             pos=j;
  23.         }
  24.         }
  25.         a[pos]=a[i];
  26.         a[i]=min;
  27.     }
  28.     t2=clock();
  29.     time_elapsed=(double)(t2-t1)/CLOCKS_PER_SEC;  //time elapsed for selection sort
  30.     return time_elapsed;
  31.      }
  32.  
  33.     double insertionsort(int a[],long int n)
  34.     {
  35.     long int i,j;int temp;
  36.     clock_t t1,t2;
  37.     double time_elapsed;
  38.     t1=clock();
  39.     for(j=1;j<n;j++)
  40.     {
  41.         i=j-1;
  42.         temp=a[j];
  43.         while(i>=0 && a[i]>temp)
  44.         {
  45.         a[i+1]=a[i];
  46.         i--;
  47.         }
  48.         a[i+1]=temp;
  49.     }
  50.     t2=clock();
  51.     time_elapsed=(double)(t2-t1)/CLOCKS_PER_SEC;  //time elapsed for insertion sort
  52.     return time_elapsed;
  53.      }
  54.  void dataread(int a[])
  55.      {
  56.      long int i;
  57.      FILE *fp;
  58.      fp=fopen("data.txt","r");
  59.      for(i=0;i<len;i++)
  60.      {
  61.          fscanf(fp,"%d",&a[i]);
  62.      }
  63.      fclose(fp);
  64.      }
  65.  
  66.      int main(void)
  67.      {
  68.      int a[len],nextrand;
  69.      long int i;
  70.      FILE *fp;
  71.      fp=fopen("data.txt","w");
  72.      for(i=0;i<len;i++)
  73.      {
  74.          nextrand=rand()%100;
  75.          fprintf(fp,"%d\n",nextrand);
  76.      }
  77.      fclose(fp);
  78.      printf("\t\t  Insertion\t        Selection\n");
  79.      for(i=10;i<=len;i=i*10)  //multiples of 10 number of integers starting from 10
  80.      {
  81.        printf("%ld integers",i);
  82.        dataread(a);
  83.        printf("\t%.9lf\t",insertionsort(a,i));
  84.        printf("\t%.9lf",insertionsort(a,i));
  85.        printf("\n");
  86.     }
  87.     return 0;
  88.      }
  89.  
  90.  
  91. expt2
  92.  
  93. #include <stdio.h>
  94. #include <stdlib.h>
  95. #include<time.h>
  96. #define len 250000
  97.  
  98.     void merge(int a[],long int lb, long int m, long int ub)
  99.     {
  100.     int temp[len];
  101.     long int i,j,k;
  102.     i=lb;
  103.     j = m+1;
  104.     k = 0;
  105.     while(i<=m && j<=ub)
  106.      if (a[i]<a[j])
  107.           temp[k++] = a[i++];
  108.      else temp[k++] = a[j++];
  109.     while (i<=m)
  110.         temp[k++] = a[i++];
  111.     while (j<=ub)
  112.         temp[k++] = a[j++];
  113.     k=0;
  114.     for (i=lb; i<=ub; i++)
  115.          a[i] = temp[k++];
  116.     return;
  117.     }
  118.  
  119.     void mergesort(int a[],long int lb,long int ub)
  120.     {
  121.     long int m;
  122.     if (lb < ub)
  123.     {
  124.      m = (lb+ub)/2;
  125.      mergesort(a, lb, m);
  126.      mergesort(a, m+1, ub);
  127.      merge(a, lb, m, ub);
  128.      }
  129.      return;
  130.     }
  131.  
  132.      long int partition(int a[], long int lb, long int ub)
  133.      {
  134.      long int down, up;
  135.      int val, temp;
  136.      val = a[lb];
  137.      down = lb+1;
  138.      up = ub;
  139.      while(down<=up)
  140.      {
  141.       while (down<=up && a[down]<=val)
  142.         down = down + 1;
  143.       while (a[up]>val)
  144.         up = up - 1;
  145.       if (down < up)
  146.          {
  147.           temp = a[down];
  148.           a[down] = a[up];
  149.           a[up] = temp;
  150.          }
  151.      }
  152.      a[lb] = a[up];
  153.      a[up] = val;
  154.      return up;
  155.      }
  156.     void quicksort(int a[],long int lb, long int ub)
  157.     {
  158.     long int p;
  159.     if (lb<ub)
  160.     {
  161.      p = partition(a, lb, ub);
  162.      quicksort(a, lb, p-1);
  163.      quicksort(a, p+1, ub);
  164.     }
  165.     return;
  166.      }
  167.  
  168.      void dataread(int a[])
  169.      {
  170.      FILE *fp;
  171.      long int i;
  172.      fp=fopen("data.txt","r");
  173.      for(i=0;i<len;i++)
  174.      {
  175.          fscanf(fp,"%d",&a[i]);
  176.      }
  177.      fclose(fp);
  178.      }
  179.  
  180.      int main(void)
  181.      {
  182.      int a[len],nextrand;
  183.      long int i;
  184.      double time_elapsed,time;
  185.      clock_t t1,t2,t3,t4;
  186.      FILE *fp;
  187.      fp=fopen("data.txt","w");
  188.      for(i=0;i<len;i++)
  189.      {
  190.          nextrand = rand()%100;
  191.          fprintf(fp,"%d\n",nextrand);
  192.      }
  193.      fclose(fp);
  194.      printf("\t\t  Merge\t\t        Quick\n");
  195.      for(i=10;i<=len;i=i*10)  //multiples of 10 number of integers starting from 10
  196.      {
  197.         printf("%ld integers",i);
  198.         dataread(a);
  199.         t1 = clock();
  200.         mergesort(a,0,i);
  201.         t2 = clock();
  202.         time_elapsed = (double)(t2-t1)/CLOCKS_PER_SEC;
  203.         printf("\t%lf\t",time_elapsed);
  204.         quicksort(a,0,i);
  205.         quicksort(a,0,i);
  206.         t3 = clock();
  207.         mergesort(a,0,i);
  208.         t4 = clock();
  209.         time = (double)(t4-t3)/CLOCKS_PER_SEC;
  210.         printf("\t%lf",time);
  211.         printf("\n");
  212.      }
  213.      return 0;
  214.      }
  215.  
  216.  
  217. expt 3
  218.  
  219. #include<stdio.h>
  220. #include<stdlib.h>
  221. #include<conio.h>
  222.  
  223. void getminmax(int a[],int lb,int ub,int*fmin,int*fmax)
  224. {
  225.  int m,hmin,hmax,gmin,gmax;
  226.  if(ub==lb)
  227.  {
  228.   *fmin = *fmax = a[ub];
  229.   return;
  230.  }
  231.  if(ub==lb+1)
  232.  {
  233.   if(a[ub]<=a[lb])
  234.   {
  235.    *fmin=a[ub];
  236.    *fmax=a[lb];
  237.    return;
  238.   }
  239.   else
  240.   {
  241.    *fmin=a[lb];
  242.    *fmax=a[ub];
  243.    return;
  244.   }
  245.  }
  246.  m=(lb+ub)/2;
  247.  getminmax(a,lb,m,&hmin,&hmax);
  248.  getminmax(a,m+1,ub,&gmin,&gmax);
  249.  if(hmin <= gmin)
  250.       *fmin = hmin;
  251.  else *fmin = gmin;
  252.  if(hmax > gmax)
  253.       *fmax = hmax;
  254.  else *fmax = gmax;
  255.  return;
  256. }
  257.  
  258.  
  259. void main(void)
  260. {
  261.  int n,i,a[30000],nextrand,min,max;
  262.  FILE *fp;
  263.  min=0;
  264.  max=0;
  265.  printf("Enter number of integers: ");
  266.  scanf("%d",&n);
  267.  fp=fopen("data.txt","w");
  268.  for(i=0;i<n;i++)
  269.  {
  270.   nextrand=rand()%100;
  271.   fprintf(fp,"%d ",nextrand);
  272.  }
  273.  fclose(fp);
  274.  fp=fopen("data.txt","r");
  275.  for(i=0;i<n;i++)
  276.  {
  277.   fscanf(fp,"%d ",&a[i]);
  278.   printf("%d ",a[i]);
  279.  }
  280.  fclose(fp);
  281.  getminmax(a,0,n-1,&min,&max);
  282.  printf("\n\nMaximum = %d",max);
  283.  printf("\nMinimum = %d\n",min);
  284.  getch();
  285. }
  286.  
  287.  
  288. expt 4
  289.  
  290. #include<stdio.h>
  291.  
  292. void shiftup(int a[][512],int n,int lb,int ub)
  293. {
  294.     int i,j;  //function to circular shift
  295.     j=a[lb][n];
  296.     for(i=lb;i<ub;i++)
  297.         a[i][n]=a[i+1][n];
  298.     a[ub][n]=j;
  299. }
  300. void shiftdown(int a[][512],int n,int lb,int ub)
  301. {
  302.     int i,j; //function for reverse circular shift
  303.     j=a[ub][n];
  304.     for(i=ub;i>lb;i--)
  305.         a[i][n]=a[i-1][n];
  306.     a[lb][n]=j;
  307. }
  308. void compute(int a[][512],int lb,int ub)
  309. {
  310.     int m,i,j,k=(ub-lb)/2,d;
  311.     if(ub-lb==1)
  312.     {
  313.         a[ub][0]=lb+1;  //only two teams
  314.         a[lb][0]=ub+1;
  315.         return;
  316.     }
  317.     m=(lb+ub)/2;     // divide stage
  318.     compute(a,lb,m);
  319.     compute(a,m+1,ub);
  320.  
  321.     j=m+2;
  322.     for(i=lb;i<=m;i++)    //creating a new upper column after all dealing with all the sub parts.
  323.     {
  324.         a[i][k]=j;
  325.         j++;
  326.     }
  327.     for(i=k+1;i<ub;i++)  // circular shifting the columns
  328.     {
  329.         for(j=lb;j<=m;j++)
  330.             a[j][i]=a[j][i-1];
  331.         shiftup(a,i,lb,m);
  332.     }
  333.  
  334.     j=lb+1;
  335.     for(i=m+1;i<=ub;i++)   //creating a new lower column after all dealing with all the sub parts.
  336.     {
  337.         a[i][k]=j;
  338.         j++;
  339.     }
  340.     for(i=k+1;i<ub;i++) //reverse circular shifting the columns
  341.     {
  342.         for(j=m+1;j<=ub;j++)
  343.             a[j][i]=a[j][i-1];
  344.         shiftdown(a,i,m+1,ub);
  345.     }
  346. }
  347.  
  348. void main()
  349. {
  350.     int i,j,n,a[512][512];
  351.     printf("Enter the number of players: ");
  352.     scanf("%d",&n);
  353.     compute(a,0,n-1);
  354.     printf("The tournament schedule is:\n\nDays:\t");
  355.         for(i=1;i<n;i++)
  356.             printf("D%.2d ",i);   //displaying the days row
  357.         printf("\n");
  358.         printf("Players:\n");
  359.         for(i=0;i<n;i++)
  360.         {
  361.             printf("%2d\t",i+1);   //displaying team number
  362.             for(j=0;j<n-1;j++)
  363.                 printf("%3d ",a[i][j]);    //displaying the required schedule
  364.             printf("\n");
  365.         }
  366. }
  367.  
  368.  
  369. expt 5
  370.  
  371. #include<stdio.h>
  372. #include<conio.h>
  373.  
  374. typedef struct
  375. {
  376.  float p[20],w[20];
  377. }knapsack;
  378.  
  379. void bubblesort(knapsack *k, int n, float a[])
  380. {
  381.  int i, j;
  382.  float temp;
  383.  for (i=0; i<n; i++)
  384.     {
  385.      a[i] = k->p[i]/k->w[i];
  386.     }
  387.  for (i=0; i<n-1; i++)
  388.     for (j=0; j<n-i-1; j++)
  389.         if (a[j] < a[j+1])
  390.         {
  391.          temp = a[j];
  392.          a[j] = a[j+1];
  393.          a[j+1] = temp;
  394.          temp = k->p[j];
  395.          k->p[j] = k->p[j+1];
  396.          k->p[j+1] = temp;
  397.          temp = k->w[j];
  398.          k->w[j] = k->w[j+1];
  399.          k->w[j+1] = temp;
  400.         }
  401.  return;
  402. }
  403.  
  404. void fknap(knapsack k, int n, int M)
  405. {
  406.  int i;
  407.  float cp=0,cw=0,a[20];
  408.  bubblesort(&k,n,a);
  409.  printf("The sorted list is:\n");
  410.  for (i=0; i<n; i++)
  411.     {
  412.      printf("\n%.0f | ",k.p[i]);
  413.      printf("%.0f | ",k.w[i]);
  414.      printf("%f ",a[i]);
  415.     }
  416.  printf("\n");
  417.  for (i=0; i<n; i++)
  418.     if ((cw+k.w[i]) < M)
  419.         {
  420.          cp = cp+k.p[i];
  421.          cw = cw+k.w[i];
  422.         }
  423.     else
  424.         {
  425.          cp = cp+((k.p[i]/k.w[i])*(M-cw));
  426.          cw = M;
  427.          break;
  428.         }
  429.  printf("\nProfit = %f\n",cp);
  430.  printf("Weight = %f\n",cw);
  431.  return;
  432. }
  433.  
  434. void knap0_1(knapsack k, int n, int M)
  435. {
  436.  int i,cp=0,cw=0;
  437.  float a[20];
  438.  bubblesort(&k,n,a);
  439.  printf("The sorted list is:\n");
  440.  for (i=0; i<n; i++)
  441.     {
  442.      printf("\n%.0f | ",k.p[i]);
  443.      printf("%.0f | ",k.w[i]);
  444.      printf("%f ",a[i]);
  445.     }
  446.  printf("\n");
  447.  for (i=0; i<n; i++)
  448.     {
  449.      if ((cw+k.w[i]) <= M)
  450.         {
  451.          cp = cp+k.p[i];
  452.          cw = cw+k.w[i];
  453.         }
  454.      if (cw == M)
  455.          break;
  456.     }
  457.  printf("\nProfit = %d\n",cp);
  458.  printf("Weight = %d\n",cw);
  459.  return;
  460. }
  461.  
  462. void main(void)
  463. {
  464.  int i,n,M,ch;
  465.  knapsack k;
  466.  printf("Enter no. of items: ");
  467.  scanf("%d",&n);
  468.  printf("Enter the profits of %d items: ",n);
  469.  for (i=0; i<n; i++)
  470.     scanf("%f", &k.p[i]);
  471.  printf("Enter the weights of %d items: ",n);
  472.  for (i=0; i<n; i++)
  473.     scanf("%f", &k.w[i]);
  474.  printf("Enter maximum capacity of the sack: ");
  475.  scanf("%d",&M);
  476.  do
  477.  {
  478.   printf("\nEnter choice from the following\n1. Fractional Knapsack\n2. 0/1 Knapsack\n3. Exit\n\n");
  479.   scanf("%d",&ch);
  480.   switch(ch)
  481.   {
  482.    case 1   : printf("Fractional Knapsack's result:\n");
  483.               fknap(k,n,M);
  484.               break;
  485.    case 2   : printf("0/1 Knapsack's result:\n");
  486.               knap0_1(k,n,M);
  487.               break;
  488.    case 3   : printf("THANK YOU\n");
  489.               break;
  490.    default  : printf("INVALID CHOICE\n");
  491.               break;
  492.   }
  493.  }while(ch>=1&&ch<=2);
  494.  getch();
  495. }
  496.  
  497. expt 6
  498. #include <stdio.h>
  499. #include <stdlib.h>
  500. #define INFY 32000
  501.  
  502. void input_weight_matrix(int v[][50],int n,int e)
  503. {
  504.     int x,y,cost;
  505.     for(x=1;x<=n;x++)
  506.         for(y=1;y<=n;y++)
  507.             v[x][y]=INFY;
  508.     while(n>0)
  509.         v[n][n--]=0;
  510.     while(e>0)
  511.     {
  512.         printf("Enter Edge as\"u_v_cost\":");
  513.         scanf("%d %d %d",&x,&y,&cost);
  514.         v[x][y]=cost;
  515.         v[y][x]=cost;
  516.         e--;
  517.     }
  518. }
  519.  
  520. void add_edge(int mcst[][50],int x,int y,int w)
  521. {
  522.     mcst[x][y]=w;
  523.     mcst[y][x]=w;
  524. }
  525.  
  526. void add(int a[],int *n,int x)
  527. {
  528.     a[(*n)]=x;
  529.     (*n)++;
  530. }
  531.  
  532. int extract(int a[],int *n,int b)
  533. {
  534.     int r,i;
  535.     for(i=0;a[i]!=b;i++);
  536.     r=a[i];
  537.     (*n)--;
  538.     for(i;i<(*n);i++)
  539.         a[i]=a[i+1];
  540.     return r;
  541. }
  542.  
  543. void primms_mcst(int w[][50],int mcst[][50],int n,int s)
  544. {
  545.     int v[50],uv[50],vn=0,uvn=0,i=1,j=1,x,y,mw=INFY;
  546.     add(v,&vn,s);
  547.     while(i<=n)
  548.     {
  549.         if(i!=s)
  550.         add(uv,&uvn,i);
  551.         i++;
  552.     }
  553.     while(uvn>0)
  554.     {
  555.         mw=INFY;
  556.         for(i=0;i<vn;i++)
  557.             for(j=0;j<uvn;j++)
  558.                 {
  559.                 if((w[(v[i])][(uv[j])])<mw)
  560.                 {
  561.                     x=v[i];
  562.                     y=uv[j];
  563.                     mw=w[v[i]][uv[j]];
  564.                 }
  565.                 }
  566.         add_edge(mcst,x,y,mw);
  567.         v[vn++]=extract(uv,&uvn,y);
  568.     }
  569. }
  570.  
  571. void display_mat(int a[][50],int n)
  572. {
  573.     int i,j;
  574.     for(i=1;i<=n;i++)
  575.     {
  576.         printf("\n");
  577.         for(j=1;j<=n;j++)
  578.             if(a[i][j]==INFY)
  579.             printf(" Inf");
  580.             else printf("%4d",a[i][j]);
  581.     }
  582. }
  583.  
  584. void print_mcst(int mcst[][50],int n)
  585. {
  586.     int cost=0,i,j;
  587.     for(i=1;i<=n;i++)
  588.         for(j=i;j<=n;j++)
  589.             {
  590.             if(mcst[i][j]==0)
  591.                 continue;
  592.             printf("(%d,%d),",i,j);
  593.             cost+=mcst[i][j];
  594.             }
  595.     printf("\nMinimum Cost = %d",cost);
  596. }
  597.  
  598. int main()
  599. {
  600.     int w[50][50],n,mcst[50][50],e,s;
  601.     printf("Enter the No.of vertices and edges: ");
  602.     scanf("%d %d",&n,&e);
  603.     input_weight_matrix(w,n,e);
  604.     display_mat(w,n);
  605.     printf("\nEnter the starting node: ");
  606.     scanf("%d",&s);
  607.     primms_mcst(w,mcst,n,s);
  608.     printf("\nPrims MCST_MAT:\n");
  609.     display_mat(mcst,n);
  610.     printf("\nThat is RESULT: ");
  611.     print_mcst(mcst,n);
  612.     return 0;
  613. }
  614.  
  615.  
  616. expt 7
  617.  
  618. #include<stdio.h>
  619.  
  620. void init(int COST[][50],int D[][50],int n)
  621. {
  622.  int i,j,c;
  623.  for (i=0; i<n; i++)
  624.      for (j=0; j<n; j++)
  625.      {
  626.          COST[i][j] = 999;
  627.          D[i][j] = -1;
  628.      }
  629.  c=n-1;
  630.  while(c>=0)
  631.  {
  632.   COST[c][c--]=0;
  633.  }
  634. }
  635.  
  636. void readEdges(int COST[][50],int D[][50],int e)
  637. {
  638.  int u,v,c;
  639.  while (e!=0)
  640.  {
  641.   printf("Enter edge as \"u_v_cost\": ");
  642.   scanf("%d %d %d",&u,&v,&c);
  643.   COST[u-1][v-1]=c;
  644.   D[u-1][v-1]=u;
  645.   e--;
  646.  }
  647. }
  648.  
  649. void displayMat(int Mat[][50], int n)
  650. {
  651.  int i,j;
  652.  for (i=0;i<n;i++)
  653.  {
  654.      for (j=0;j<n;j++)
  655.         printf("%3d ", Mat[i][j]);
  656.      printf("\n");
  657.  }
  658. }
  659.  
  660. void apsp(int COST[][50],int A[][50],int D[][50],int n)
  661. {
  662.  int i,j,k,t;
  663.  for (i=0; i<n; i++)
  664.      for (j=0;j<n;j++)
  665.          A[i][j]=COST[i][j];
  666.  for (k=0;k<n;k++)
  667.      for (i=0;i<n;i++)
  668.         for(j=0;j<n;j++)
  669.         {
  670.            t = A[i][k]+A[k][j];
  671.            if (A[i][j]>t)
  672.            {
  673.                A[i][j] = t;
  674.                D[i][j] = k+1;
  675.            }
  676.         }
  677. }
  678.  
  679. void pathfinding(int COST[][50],int D[][50],int s, int d)
  680. {
  681.  int i=1,j,FINAL[50];
  682.  j=d;
  683.  FINAL[0]=d;
  684.  while(D[s-1][d-1]!= -1)
  685.  {
  686.   FINAL[i++]=D[s-1][d-1];
  687.   if (COST[d-1][j-1]==999)
  688.       {
  689.           s = D[s-1][d-1];
  690.           continue;
  691.       }
  692.   d = D[s-1][d-1];
  693.  }
  694.  printf("\nPath: ");
  695.  for(j=i-1;j>0;j--)
  696.     printf("%d-",FINAL[j]);
  697.  printf("%d",FINAL[0]);
  698. }
  699.  
  700. int main(void)
  701. {
  702.  int A[50][50],COST[50][50],D[50][50],n,e,s,q;
  703.  printf("Enter no. of vertices: ");
  704.  scanf("%d",&n);
  705.  printf("Enter no. of edges: ");
  706.  scanf("%d",&e);
  707.  init(COST,D,n);
  708.  readEdges(COST,D,e);
  709.  printf("\nWeight Mat:\n");
  710.  displayMat(COST,n);
  711.  printf("Decision Mat initially:\n");
  712.  displayMat(D,n);
  713.  apsp(COST,A,D,n);
  714.  printf("\nFinal Length Mat:\n");
  715.  displayMat(A,n);
  716.  printf("Final Decision Mat:\n");
  717.  displayMat(D,n);
  718.  printf("\nEnter starting and ending nodes: ");
  719.  scanf("%d%d",&s,&q);
  720.  printf("Minimum Length between %d and %d = %d",s,q,A[s-1][q-1]);
  721.  pathfinding(COST,D,s,q);
  722.  return 0;
  723. }
  724.  
  725.  
  726. expt 8
  727.  
  728. #include<stdio.h>
  729.  
  730. typedef struct
  731. {
  732.     int n[50];
  733.     int v;
  734.     int min;
  735. }PS;
  736.  
  737. PS tsp(int s,PS list,int D[][50],int m)
  738. {
  739.    int i,j;
  740.    PS nlist,npath,nmin;
  741.    if(list.v==0)
  742.    {
  743.        nmin.min=D[s][1];
  744.        nmin.n[m-1]=s;
  745.        nmin.v=m;
  746.        return nmin;
  747.    }
  748.    for(i=0;i<list.v;i++)
  749.    {
  750.       nlist.v=0;
  751.       for(j=0;j<list.v;j++)
  752.          if(i!=j)
  753.            nlist.n[nlist.v++]=list.n[j];
  754.       npath=tsp(list.n[i],nlist,D,m);
  755.       npath.min=D[s][list.n[i]]+npath.min;
  756.       npath.n[m-list.v-1]=s;
  757.       if(i==0)
  758.           nmin=npath;
  759.       else if(npath.min<nmin.min)
  760.                nmin=npath;
  761.    }
  762.    return nmin;
  763. }
  764.  
  765. void display(PS path)
  766. {
  767.   int i;
  768.   printf("\n\nThe minimum cost is: %d\n",path.min);
  769.   printf("\n\nThe path is...\n");
  770.   for(i=0;i<path.v;i++)
  771.       printf("%d--",path.n[i]);
  772.   printf("%d",path.n[0]);
  773. }
  774.  
  775. int main()
  776. {
  777.    int i,j,D[50][50],m;
  778.    PS graph,path;
  779.    printf("Enter the number of nodes: ");
  780.    scanf("%d",&m);
  781.    for(i=1;i<=m;i++)
  782.    {
  783.        for(j=1;j<=m;j++)
  784.           if(i==j)
  785.              D[j][j]=0;
  786.        else
  787.        {
  788.          printf("Enter distance from node %d to %d: ",i,j);
  789.          scanf("%d",&D[i][j]);
  790.        }
  791.        if(i>1)
  792.           graph.n[i-2]=i;
  793.    }
  794.    graph.v=m-1;
  795.    path=tsp(1,graph,D,m);
  796.    display(path);
  797.    return 0;
  798. }
  799.  
  800.  
  801. expt 9
  802.  
  803.  
  804. #include<stdio.h>
  805. #include<conio.h>
  806. #include<math.h>
  807.  
  808. void display(int x[],int n)
  809. {
  810.  int i,j;
  811.  for (i=0; i<n; i++)
  812.  {
  813.     for (j=0; j<n; j++)
  814.        if(x[i]==j)
  815.         printf("Q ");
  816.        else printf("# ");
  817.    printf("\n");
  818.  }
  819.  printf("\n\n");
  820. }
  821.  
  822. void nqueens(int x[],int r,int n)
  823. {
  824.  int c;
  825.  for(c=0;c<n; c++)
  826.  {
  827.    if(place(x,r,c)==1)
  828.    {
  829.      x[r]=c;
  830.      if(r==n-1)
  831.      display(x,n);
  832.      else nqueens(x,r+1,n);
  833.    }
  834.  }
  835. }
  836.  
  837. int place(int x[],int r,int c)
  838. {
  839.   int j;
  840.   for(j=0; j<r; j++)
  841.      if(x[j]==c || abs(x[j]-c)==abs(j-r))
  842.     return 0;
  843.   return 1;
  844. }
  845.  
  846. int main()
  847. {
  848.  int x[20],n;
  849.  printf("Enter no of queens: ");
  850.  scanf("%d",&n);
  851.  nqueens(x,0,n);
  852.  getch();
  853.  return 0;
  854. }
  855.  
  856.  
  857. expt 10
  858.  
  859. #include<stdio.h>
  860. #include<conio.h>
  861.  
  862. void display(int x[], int n)
  863. {
  864.  int i;
  865.  for (i=0; i<n; i++)
  866.      printf("%d ",x[i]);
  867.  printf("\n");
  868. }
  869.  
  870. void sumofsub(int w[],int x[],int M,int n,int s,int k,int r)
  871. {
  872.  int i;
  873.  for (i=k; i<n; i++)
  874.     x[i]=0;
  875.  x[k]=1;
  876.  if(s+w[k]==M)
  877.      display(x,n);
  878.  else if(s+w[k]+w[k+1]<=M)
  879.         sumofsub(w,x,M,n,s+w[k],k+1,r-w[k]);
  880.  if(s+r-w[k]>=M && s+w[k+1]<=M)
  881.  {
  882.    x[k]=0;
  883.    sumofsub(w,x,M,n,s,k+1,r-w[k]);
  884.  }
  885. }
  886.  
  887. int main()
  888. {
  889.  int i,r=0;
  890.  int w[20],n,M,x[20];
  891.  printf("Enter no of weights: ");
  892.  scanf("%d",&n);
  893.  printf ("Enter the weights: ");
  894.  for(i=0; i<n; i++)
  895.  {
  896.    scanf("%d",&w[i]);
  897.    r=r+w[i];
  898.  }
  899.  printf("Enter required sum: ");
  900.  scanf("%d",&M);
  901.  printf("Selected List is:\n");
  902.  sumofsub(w,x,M,n,0,0,r);
  903.  return 0;
  904. }
  905.  
  906. graph color
  907.  
  908. #include<conio.h>
  909. #include<stdio.h>
  910. #include<math.h>
  911. int n,m,x[20]={0},ct=0,adj[10][10];
  912. void nextval(int k)
  913. {
  914.  int j=1;
  915.  x[k]=(x[k]+1)%(m+1);
  916.  if(x[k]==0)
  917.   return;
  918.  while(j<k)
  919.  {
  920.   if(adj[j][k]==1 && x[j]==x[k])
  921.    {
  922.     x[k]=(x[k]+1)%(m+1);
  923.     if(x[k]==0)
  924.      return;
  925.      j=1;
  926.  
  927.    }
  928.   else j++;
  929.   }
  930.    return;
  931. }
  932. void dis()
  933. {
  934.  int i;
  935.  ct=ct+1;
  936.  printf("soluition=%d\n",ct);
  937.  for(i=1;i<=n;i++)
  938.   printf("%d ",x[i]);
  939.  printf("\n");
  940.  }
  941.  
  942. void colorg(int k)
  943. {
  944.  int i;
  945.  for(i=1;i<=n;i++)
  946.  {
  947.  nextval(k);
  948.  if(x[k]==0)
  949.   return;
  950.   if(k==n)
  951.   {
  952.    dis();
  953.    }
  954.   else colorg(k+1);
  955.  }
  956. }
  957.  
  958.  
  959. void main()
  960. {
  961.  int i,j;
  962.  clrscr();
  963.  printf("Enter no of nodes\n");
  964.  scanf("%d",&n);
  965.  printf("Enter no of colors\n");
  966.  scanf("%d",&m);
  967.  for(i=1;i<=n;i++)
  968.   for(j=1;j<=n;j++)
  969.    scanf("%d",&adj[i][j]);
  970.  
  971.  colorg(1);
  972.  getch();
  973. }
  974.  
  975.  
  976. lcs and clcs
  977.  
  978. #include<stdio.h>
  979. #include<string.h>
  980.  
  981. int i,j,m,n,a,L[20][20];
  982. char x[15],y[15],D[20][20];
  983.  
  984. void print_lcs(int i,int j)
  985. {
  986.     if(i==0 || j==0)
  987.         return;
  988.     if(D[i][j]=='d')
  989.     {
  990.         print_lcs(i-1,j-1);
  991.         printf(" %c",x[i-1]);
  992.     }
  993.     else if(D[i][j]=='u')
  994.         print_lcs(i-1,j);
  995.     else
  996.         print_lcs(i,j-1);
  997. }
  998.  
  999. void lcs(void)
  1000. {
  1001.     m=strlen(x);
  1002.     n=strlen(y);
  1003.     for(i=0; i<=m; i++)
  1004.         L[i][0]=0;
  1005.     printf("\n\n");
  1006.     for(i=0; i<=n; i++)
  1007.     {
  1008.         printf("0 \t");
  1009.         L[0][i]=0;
  1010.     }
  1011.     printf("\n");
  1012.     for(i=1; i<=m ;i++)
  1013.     {
  1014.         printf("0 \t");
  1015.         for(j=1; j<=n; j++)
  1016.         {
  1017.             if(x[i-1]==y[j-1])
  1018.             {
  1019.                 L[i][j]=L[i-1][j-1]+1;
  1020.                 D[i][j]='d';
  1021.                 printf("%d D\t",L[i][j]);
  1022.             }
  1023.             else if(L[i-1][j] > L[i][j-1])
  1024.             {
  1025.                 L[i][j] = L[i-1][j];
  1026.                 D[i][j] = 'u';
  1027.                 printf("%d U\t",L[i][j]);
  1028.             }
  1029.             else
  1030.             {
  1031.                 L[i][j]=L[i][j-1];
  1032.                 D[i][j]='l';
  1033.                 printf("%d L\t",L[i][j]);
  1034.             }
  1035.         }
  1036.         printf("\n");
  1037.     }
  1038.     printf("\nLongest common subsequence is :");
  1039.     print_lcs(m,n);
  1040.     printf("\n");
  1041.     printf("The length of Subsequence is: %d",L[m][n]);
  1042. }
  1043.  
  1044. void main()
  1045. {
  1046.     printf("Enter 1st sequence : ");
  1047.     scanf("%s",x);
  1048.     printf("Enter 2nd sequence : ");
  1049.     scanf("%s",y);
  1050.     lcs();
  1051.     printf("\n");
  1052.     getch();
  1053. }
  1054.  
  1055. naive pattern
  1056.  
  1057. #include<stdio.h>
  1058. #include<stdlib.h>
  1059. #include<string.h>
  1060.  
  1061. int brute_force(char x[],char y[])
  1062. {
  1063.  int i,j,n,m;
  1064.  n = strlen(x);
  1065.  m = strlen(y);
  1066.  for (i=0; i<n-m; i++)
  1067.     {
  1068.      j = 0;
  1069.      while(j<m && x[i+j]==y[j])
  1070.         j = j+1;
  1071.      if (j==m)
  1072.         return i;    //starting index of the text part where pattern is matched
  1073.     }
  1074.  return -1;          //pattern not matched in the entire text
  1075. }
  1076.  
  1077. int main()
  1078. {
  1079.  char t[20],p[10];
  1080.  int result;
  1081.  printf("Enter the text: ");
  1082.  scanf("%s",t);
  1083.  printf("Enter the pattern needed to be found: ");
  1084.  scanf("%s",p);
  1085.  result = brute_force(t,p);
  1086.  if(result != -1)
  1087.       printf("\nThe pattern is matched in the text from: %d index of the text.",result);
  1088.  else printf("\nThe pattern didn't matched!!!");
  1089.  printf("\n");
  1090.  getch();
  1091. }
  1092.  
  1093. kmp and cmkp
  1094.  
  1095. #include<conio.h>
  1096. #include<stdio.h>
  1097. #include<string.h>
  1098. int f[30];
  1099. void kmpfail(char p[],int m)
  1100. {
  1101.  int i=1,j=0;
  1102.  f[0]=0;
  1103.  while(i<m)
  1104.  {
  1105.   if(p[j]==p[i])
  1106.    {
  1107.     f[i]=j+1;
  1108.     i=i+1,j=j+1;
  1109.    }
  1110.   else if(j>0)
  1111.      j=f[j-1];
  1112.        else
  1113.     {
  1114.      f[i]=0;
  1115.      i=i+1;
  1116.     }
  1117.   }
  1118.  return;
  1119. }
  1120. int kmp(char t[],char p[], int n,int m)
  1121. {
  1122.  int i=0,j=0;
  1123.  while(i<n)
  1124.  {
  1125.   if(p[j]==t[i])
  1126.     if(j==m-1)
  1127.      return i-m+1;
  1128.     else i=i+1,j=j+1;
  1129.   else if(j>0)
  1130.     j=f[j-1];
  1131.        else i=i+1;
  1132.   }
  1133.  return -1;
  1134. }
  1135. void main()
  1136. {
  1137.  int n,m;
  1138.  char t[30],p[10];
  1139.  clrscr();
  1140.  printf("Enter text\n");
  1141.  gets(t);
  1142.   n=strlen(t);
  1143.  printf("Enter pattern\n");
  1144.  gets(p);
  1145.  m=strlen(p);
  1146.  kmpfail(p,m);
  1147.  if(kmp(t,p,n,m)!=-1)
  1148.   printf("pattern is present at %d",kmp(t,p,n,m));
  1149.  else printf("pattern not found");
  1150.  getch();
  1151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement