Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 23rd, 2012  |  syntax: None  |  size: 14.30 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. /******* SELECTION SORT *******/
  3.  
  4. #include <stdio.h>
  5. void selection_sort(int a[], int size);
  6. int main(void)
  7.  {
  8.      int arr[10] = {10, 2, 4, 1, 6, 5, 8, 7, 3, 9};
  9.      int i = 0;
  10.      clrscr();
  11.      printf("before:\n");
  12.      for(i = 0; i < 10; i++) printf("%d ", arr[i]);
  13.      printf("\n");
  14.      selection_sort(arr, 10);
  15.       printf("after:\n");
  16.       for(i = 0; i < 10; i++) printf("%d ", arr[i]);
  17.       printf("\n");
  18.       getch();
  19.       return 0;
  20. }
  21. void selection_sort(int a[], int size)
  22.  {
  23.    int i = 0;
  24.    int j = 0;
  25.    int large = 0;
  26.    int index = 0;
  27.   for(i = size-1; i > 0; i--)
  28.  {
  29.   large = a[0];
  30.   index = 0;
  31.   for(j = 1; j <= i; j++)
  32.   if(a[j] > large)
  33.  { large = a[j];
  34.     index = j; }
  35.      a[index] = a[i];
  36.      a[i] = large;
  37.      }
  38. }
  39.  
  40.  
  41.  
  42.  
  43. /******* HEAP SORT *******/
  44.  
  45.  
  46.  
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #define MAXARRAY 5
  50. void heapsort(int ar[], int len);
  51. void heapbubble(int pos, int ar[], int len);
  52. int main(void)
  53. {
  54.    int array[MAXARRAY];
  55.    int i = 0;
  56.    clrscr();
  57.    for(i = 0; i < MAXARRAY; i++)
  58.    array[i] = rand() % 100;
  59.    printf("Before heapsort: ");
  60.    for(i = 0; i < MAXARRAY; i++)
  61.  {
  62.    printf(" %d ", array[i]);
  63.  }
  64.    printf("\n");
  65.    heapsort(array, MAXARRAY);
  66.    printf("After  heapsort: ");
  67.    for(i = 0; i < MAXARRAY; i++)
  68. {
  69.    printf(" %d ", array[i]);
  70.  }
  71.    printf("\n");
  72.    getch();
  73.    return 0;
  74. }
  75.  
  76.  void heapbubble(int pos, int array[], int len)
  77.  {
  78.    int z = 0;
  79.    int max = 0;
  80.    int tmp = 0;
  81.    int left = 0;
  82.    int right = 0;
  83.    z = pos;
  84.    for(;;)
  85. {
  86.    left = 2 * z + 1;  
  87.    right = left + 1;
  88.    if(left >= len)
  89.    return;
  90.    else if(right >= len)
  91.    max = left;
  92.    else if(array[left] > array[right])
  93.    max = left;
  94.    else
  95.    max = right;
  96.    if(array[z] > array[max])
  97.    return;
  98.    tmp = array[z];
  99.    array[z] = array[max];  
  100.    array[max] = tmp;
  101.    z = max;
  102.  }
  103.  
  104. }
  105.   void heapsort(int array[], int len)
  106.  {
  107.    int i = 0;
  108.    int tmp = 0;
  109.    for(i = len / 2; i >= 0; --i)
  110.    heapbubble(i, array, len);
  111.    for(i = len - 1; i > 0; i--)
  112.  {
  113.    tmp = array[0];  
  114.    array[0] = array[i];
  115.    array[i] = tmp;
  116.    heapbubble(0, array, i);
  117.   }
  118. }
  119.  
  120.  
  121.  
  122. /******* MERGE SORT *******/
  123.  
  124.  
  125.  
  126.  
  127. #include<stdio.h>
  128. #include<conio.h>
  129. #define MAXARY 100
  130. void mergesort(int x[],int end,int start);
  131. int main(void)
  132. {
  133.         int ary[MAXARY];
  134.         int j=0,n;
  135.         printf("Enter the number of elements in array:");
  136.         scanf("%d",&n);
  137.         printf("\n\nEnter the elements to be sorted: \n");
  138.         for(j=0;j<n;j++)
  139.         scanf("%d",&ary[j]);
  140.         printf("Before :");
  141.         for(j=0;j<n;j++)
  142.         printf(" %d",ary[j]);
  143.         printf("\n");
  144.         mergesort(ary,0,n-1);
  145.         printf("After Merge Sort :");
  146.         for(j=0;j<n;j++)
  147.         printf("%d",ary[j]);
  148.         printf("\n");
  149.         getch();
  150.         return 0;
  151. }
  152. void mergesort(int x[],int end,int start)
  153. {
  154.         int j=0;
  155.         const int size=start-end+1;
  156.         int mid=0;
  157.         int mrg1=0;
  158.         int mrg2=0;
  159.         int executing[MAXARY];
  160.         if(end==start)
  161.         return;
  162.         mid=(end+start)/2;
  163.         mergesort(x,end,mid);
  164.         mergesort(x,mid+1,start);
  165.         for(j=0;j<size;j++)
  166.         executing[j]=x[end+j];
  167.         mrg1=0;
  168.         mrg2=mid-end+1;
  169.         for(j=0;j<size;j++)
  170.         {
  171.                 if(mrg2<=start-end)
  172.                 if(mrg1<=mid-end)
  173.                 if(executing[mrg1]>executing[mrg2])
  174.                 x[j+end]=executing[mrg2++];
  175.                 else
  176.                 x[j+end]=executing[mrg1++];
  177.                 else
  178.                 x[j+end]=executing[mrg2++];
  179.                 else
  180.                 x[j+end]=executing[mrg1++];
  181.         }
  182. }
  183.  
  184.  
  185.  
  186. /******* QUICK SORT *******/
  187.  
  188.  
  189.  
  190.  
  191. #include<stdio.h>
  192. #include<conio.h>
  193. #define MAXARRAY 10
  194. void quicksort(int arr[],int low,int high);
  195. int main(void)
  196. {
  197.          int array[MAXARRAY]={0};
  198.          int i=0;
  199.          clrscr();
  200.          for(i=0;i<MAXARRAY;i++)
  201.          array[i]=rand () %100;
  202.          printf("Before quicksort:");
  203.          for(i=0;i<MAXARRAY;i++)
  204.          {
  205.                   printf("%d  ",array[i]);
  206.          }
  207.          printf("\n");
  208.          quicksort(array,0,(MAXARRAY-1));
  209.          printf("After  quicksort: ");
  210.          for(i=0;i<MAXARRAY;i++)
  211.          {
  212.                  printf("%d  ",array[i]);
  213.          }
  214.          printf("\n");
  215.          getch();
  216.          return 0;
  217. }
  218. void quicksort(int arr[],int low,int high)
  219. {
  220.          int i=low;
  221.          int j=high;
  222.          int y=0;
  223.          int z=arr[(low+high)/2];
  224.          do
  225.          {
  226.                    while(arr[i]<z) i++;
  227.                    while(arr[j]>z) j--;
  228.                    if(i<=j)
  229.                    {
  230.                            y=arr[i];
  231.                            arr[i]=arr[j];
  232.                            arr[j]=y;
  233.                            i++;
  234.                            j--;
  235.                    }
  236.          }while(i<=j);
  237.          if(low<j)
  238.          quicksort(arr,low,j);
  239.          if(i<high)
  240.          quicksort(arr,i,high);
  241. }
  242.  
  243.  
  244.  
  245.  
  246. /******* BST *******/
  247.  
  248.  
  249.  
  250. #include<stdio.h>
  251. #include<conio.h>
  252. void main()
  253. {
  254.         int mid,num,lower=0,upper=8,flag=1,n,i,arr[10];
  255.         clrscr();
  256.         printf("\nEnter the elements:");
  257.         scanf("%d",&n);
  258.         printf("\nEnter the elements in sorted order");
  259.         for(i=0;i<n;i++)
  260.         scanf("%d",&arr[i]);
  261.         printf("\nEnter the elements to be searched:");
  262.         scanf("%d",&num);
  263.         for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
  264.         {
  265.                 if(arr[mid]==num)
  266.                 {
  267.                         printf("\nThe number is at location %d",mid);
  268.                         flag=0;
  269.                         break;
  270.                 }
  271.                 if(arr[mid]<num)
  272.                   lower=mid+1;
  273.                 else
  274.                   upper=mid-1;
  275.         }
  276.         if(flag==1)
  277.         printf("\nElement not in the list");
  278.         getch();
  279. }
  280.  
  281.  
  282.  
  283.  
  284. /******* KRUSKALS *******/
  285.  
  286.  
  287.  
  288. #include<stdio.h>
  289. #include<conio.h>
  290. int parent[10];
  291. int a,b,u,v,i,j,n,noofedges=1,min,mincost=0,cost[10][10];
  292. void kruskals(int cost[][10],int n);
  293. void main()
  294. {
  295.    clrscr();
  296.    printf("\nEnter the no. of vertices");
  297.    scanf("%d",&n);
  298.    printf("\nEnter the Adjacency Matrix\n");
  299.    for(i=1;i<=n;i++)
  300.       for(j=1;j<=n;j++)
  301.       {
  302.          scanf("%d",&cost[i][j]);
  303.          if(cost[i][j]==0)
  304.             cost[i][j]=999;
  305.       }
  306.    kruskals(cost,n);
  307.    getch();
  308. }
  309. void kruskals(int cost[][10],int n)
  310. {
  311.    printf("\nThe Minimum Cost Edges are:\n");
  312.    while(noofedges<n)
  313.    {
  314.       for(i=1,min=999;i<=n;i++)
  315.          for(j=1;j<=n;j++)
  316.             if(cost[i][j]<min)
  317.             {
  318.                min=cost[i][j];
  319.                a=u=i;
  320.                b=v=j;
  321.             }
  322.       while(parent[u])
  323.          u=parent[u];
  324.       while(parent[v])
  325.          v=parent[v];
  326.       if(u!=v)
  327.       {
  328.          noofedges++;
  329.          printf("\nEdge (%d->%d)=%d",a,b,min);
  330.          mincost+=min;
  331.          parent[v]=u;
  332.       }
  333.       cost[a][b]=cost[b][a]=999;
  334.    }
  335.    printf("\nMinimum Cost=%d",mincost);
  336. }
  337.  
  338.  
  339.  
  340.  
  341. /******* PRIMS *******/
  342.  
  343.  
  344.  
  345. #include<stdio.h>
  346. #include<conio.h>
  347. int a,b,u,v,i,j,n,noofedge=1;
  348. int visited[10],min,minicost=0,cost[10][10];
  349. void prims(int cost[][10],int n)
  350. {
  351. for(i=0;i<=n;i++)
  352. visited[i]=0;
  353. printf("\n edges in the spanning tree are\n");
  354. visited[1]=1;
  355. while(noofedge<n)
  356. {
  357. for(i=1,min=999;i<=n;i++)
  358. for(j=1;j<=n;j++)
  359. if(cost[i][j]<min)
  360. if(visited[i]==0)
  361. continue;
  362. else
  363. {
  364. min=cost[i][j];
  365. a=u=i;
  366. b=v=j;
  367. }
  368. if(visited[u]==0||visited[v]==0)
  369. {
  370. noofedge++;
  371. printf("\n edge(%d->%d)=%d",a,b,min);
  372. minicost+=min;
  373. visited[b]=1;
  374. }
  375. cost[a][b]=cost[b][a]=999;
  376. }
  377. printf("\nMinimumcost Spanning Tree %d",minicost);
  378. }
  379. void main()
  380. {
  381. clrscr();
  382. printf("\n prims algorithm\n");
  383. printf("\nenter the no of vertices");
  384. scanf("%d",&n);
  385. printf("\nenter the adjacency matrix");
  386. for(i=1;i<=n;i++)
  387. {
  388. for(j=1;j<=n;j++)
  389. {scanf("%d",&cost[i][j]);
  390. if(cost[i][j]==0)
  391. cost[i][j]=999;
  392. }
  393. }
  394. prims(cost,n);
  395. getch();
  396. }
  397.  
  398.  
  399.  
  400. /******* KNAPSACK *******/
  401.  
  402.  
  403.  
  404.  
  405. #include<stdio.h>
  406. #include<conio.h>
  407. int i,j,n,temp=0,index[20];
  408. float p[20],w[20],x[20],max,capacity;
  409. void read();
  410. void knapsack(float[],float[],float[],float,int);
  411. void display();
  412. void main()
  413. {
  414.         clrscr();
  415.         read();
  416.         knapsack(p,w,x,max,n);
  417.         display();
  418.         getch();
  419. }
  420. void read()
  421. {
  422.         printf("\nEnter maximum weight:\t");
  423.         scanf("%f",&max);
  424.         printf("\nEnter no of objects:\t");
  425.         scanf("%d",&n);
  426.         for(i=0;i<n;i++)
  427.         {
  428.                 printf("\nEnter weight of %d item w[%d]:\t",i+1,i+1);
  429.                 scanf("%f",&w[i]);
  430.                 printf("\nEnter profit of %d item p[%d]:\t",i+1,i+1);
  431.                 scanf("%f",&p[i]);
  432.         }
  433. }
  434. void knapsack(float p[],float w[],float x[],float max,int n)
  435. {
  436.         for(i=0,index[temp]=i;i<n;i++)
  437.         for(temp=0,j=0;j<n;j++)
  438.                         if((i!=j)&&(p[i]/w[i])<(p[j]/w[j]))
  439.                                 temp++;
  440.         capacity=max;
  441.         for(i=0;i<n;i++)
  442.         {
  443.                 if(w[index[i]]>capacity)
  444.                         break;
  445.                 x[index[i]]=1.0;
  446.                 capacity=capacity-w[index[i]];
  447.         }
  448.         if(i<n)
  449.                 x[index[i]]=capacity/w[index[i]];
  450. }
  451. void display()
  452. {
  453.         float profit=0.0,max_cap=0.0;
  454.         for(i=0;i<n;i++)
  455.                 profit=profit+(x[i]*p[i]);
  456.         for(i=0;i<n;i++)
  457.                 max_cap=max_cap+(w[i]*x[i]);
  458.         printf("\nThe optimal solution is\n");
  459.         printf("\n%9s %9s %9s %7s","    " ," Weight","Profit","X");
  460.         printf("\n\n%9s %9s %9s %9s","   i","w[i]","p[i] ","x[i]");
  461.         printf("\n------------------------------------------------\n");
  462.         for(i=0;i<n;i++)
  463.                 printf("\n%9d%9.2f  %9.2f %9.2f",i+1,w[i],p[i],x[i]);
  464.         printf("\n\nThe sum of p[i]*x[i]=%0.2f",profit);
  465.         printf("\n\nThe sum of w[i]*x[i]=%0.2f",max_cap);
  466. }
  467.  
  468.  
  469.  
  470. /******* MULTISTAGE GRAPH *******/
  471.  
  472.  
  473.  
  474.  
  475. #include<stdio.h>
  476. #include<conio.h>
  477. int G[50][50],n,i,j,h,k;
  478. void FGraph();
  479. int findR();
  480. void main ()
  481. {
  482.  clrscr();
  483.  printf("\t\t\t\tMultistage Graph");
  484.  printf("\nEnter the number of the vertices: ");
  485.  scanf("%d",&n);
  486.  printf("\nEnter the total number of stages in your graph: ");
  487.  scanf("%d",&k);
  488.  printf("\nIf there is a edge between the following vertices enter its weight else 0:\n");
  489.  for(i=1;i<=n;i++)
  490.   for(j=1;j<=n;j++)
  491.   {
  492.         G[i][j] = 0;
  493.         if((i!=j)&&(i<j))
  494.         {
  495.          printf("%d and %d :",i,j);
  496.          scanf("%d",&G[i][j]);
  497.         }
  498.   }
  499.  FGraph();
  500.  getch();
  501. }
  502. void FGraph()
  503. {
  504.  int cost[50],d[50],p[50],r;
  505.  for (i = 1; i<=n; i++)
  506.   cost[i] = 0;
  507.  for(j=n-1; j>=1; j--)
  508.  {
  509.   r = findR(j+1);
  510.   cost[j] = G[j][r]+cost[r];
  511.   d[j] = r;
  512.  }
  513.  p[1] = 1;
  514.  p[k] = n;
  515.  for(j = 2; j<k; j++)
  516.   p[j] = d[p[j-1]];
  517.  printf("%d-",d[1]);
  518.  for(j=2; j<=n; j++)
  519.  {
  520.   if((d[j] == d[j-1]) || (d[j] == 0))
  521.    continue;
  522.   if(d[j]<=n)
  523.   printf("%d-",d[j]);
  524.  }
  525.  printf("%d",n);
  526. }
  527. int findR(int cu)
  528. {
  529.  int r1 = n+1;
  530.  for(h =1; h<=n; h++)
  531.  {
  532.   if( (G[h][cu] != 0) && (  r1 == n+1 ) )
  533.   {
  534.    r1 = h;
  535.    continue;
  536.   }
  537.   if (G[h][cu] != 0)
  538.   {
  539.    if(G[h][cu] < G[r1][cu] )
  540.     r1 = h;
  541.   }
  542.  }
  543.  return r1;
  544. }
  545.  
  546.  
  547.  
  548. /******* 8 QUEENS *******/
  549.  
  550.  
  551.  
  552.  
  553. #include<stdio.h>
  554. #include<conio.h>
  555. int n,count,a[18][18];
  556. void refresh(int k)
  557. {
  558.    int i;
  559.    for(i=0;i<=n;i++)
  560.       a[i][k]=0;
  561. }
  562.  
  563. void display()
  564. {
  565.    int i,j;
  566.    count++;
  567.    for(i=0;i<n;i++)
  568.    {
  569.       for(j=0;j<n;j++)
  570.          if(a[i][j])
  571.             printf("Q");
  572.          else
  573.             printf("#");
  574.       printf("\n\n");
  575.    }
  576.    printf("\n\n\n\n");
  577. }
  578.  
  579. void eq(int k)
  580. {
  581.    int i,j,r,count;
  582.    if(k==n)
  583.    {
  584.       display();
  585.       return;
  586.    }
  587.    r=0;
  588.    while(r<n)
  589.    {
  590.       count=1;
  591.       for(j=0;j<=n;j++)
  592.          if(a[r][j]==1)
  593.             count=0;
  594.       i=r;
  595.       j=k;
  596.       while(count&&(i<n)&&(j<n))
  597.       {
  598.          if(a[i][j])
  599.             count=0;
  600.          i++;
  601.          j++;
  602.       }
  603.       i=r;
  604.       j=k;
  605.       while(count&&(i>-1)&&(j>-1))
  606.       {
  607.          if(a[i][j])
  608.             count=0;
  609.          i--;
  610.          j--;
  611.       }
  612.       i=r;
  613.       j=k;
  614.       while(count&&(i>-1)&&(j<n))
  615.       {
  616.          if(a[i][j])
  617.             count=0;
  618.          i--;
  619.          j++;
  620.       }
  621.       i=r;
  622.       j=k;
  623.       while(count&&(i<n)&&(j>-1))
  624.       {
  625.          if(a[i][j])
  626.             count=0;
  627.          i++;
  628.          j--;
  629.       }
  630.       if(count)
  631.       {
  632.          a[r][k]=1;
  633.          eq(k+1);
  634.          refresh(k);
  635.       }
  636.       r++;
  637.    }
  638. }
  639.  
  640. void main()
  641. {
  642.    clrscr();
  643.    printf("\nEnter the no. of queens:");
  644.    scanf("%d",&n);
  645.    printf("\n");
  646.    eq(0);
  647.    printf("\nThe total no. of combinations is %d",count);
  648.    getch();
  649. }
  650.  
  651.  
  652.  
  653. /******* GRAPH COLOURING *******/
  654.  
  655.  
  656.  
  657.  
  658. #include<stdio.h>
  659. #include<conio.h>
  660. #define MAX 10
  661. int g[MAX][MAX],m,edges,color_tab[MAX];
  662. void main()
  663. {
  664.  int i,j,n;
  665.  void gen_col_value(int,int);
  666.  void gr_coloring(int,int);
  667.  int get_data();
  668.  void display(int);
  669.  clrscr();
  670.  printf("\n\n\t\tGRAPH COLORING...\n\n");
  671.  for(i=0;i<MAX;i++)
  672.  {
  673.   for(j=0;j<MAX;j++)
  674.   {
  675.    g[i][j]=0;
  676.    color_tab[i]=0;
  677.   }
  678.  }
  679.  n=get_data();
  680.  gr_coloring(1,n);
  681.  display(n);
  682.  getch();
  683. }
  684. void gen_col_value(int k,int n)
  685. {
  686.  int j,a,b;
  687.  while(1)
  688.  {
  689.   a=color_tab[k]+1;
  690.   b=m+1;
  691.   color_tab[k]=a%b;
  692.   if(color_tab[k]==0)
  693.   return;
  694.   for(j=1;j<=n;j++)
  695.   {
  696.    if(g[k][j]&&color_tab[k]==color_tab[j])
  697.    break;
  698.   }
  699.   if(j==n+1)
  700.   return;
  701.  }
  702. }
  703. void gr_coloring(int k,int n)
  704. {
  705.  gen_col_value(k,n);
  706.  if(color_tab[k]==0)
  707.  return;
  708.  if(k==n)
  709.  return;
  710.  else
  711.  gr_coloring(k+1,n);
  712. }
  713. int get_data()
  714. {
  715.  int v1,v2,i,n;
  716.  printf("Enter the no.of nodes:");
  717.  scanf("%d",&n);
  718.  printf("\nEnter the no.of edges:");
  719.  scanf("%d",&edges);
  720.  m=n-1;
  721.  printf("\n\nEnter the edges of the graph:");
  722.  printf("By given values of v1 & v2 \n\n");
  723.  for(i=1;i<=edges;i++)
  724.  {
  725.   scanf("%d %d",&v1,&v2);
  726.   g[v1][v2]=g[v2][v1]=1;
  727.  }
  728.  return n;
  729. }
  730. void display(int n)
  731. {
  732.  int i;
  733.  printf("\nThe vertices to be colored as.....\n\n");
  734.  for(i=1;i<=n;i++)
  735.  {
  736.   printf("\n\n");
  737.  printf("\n\tvertices %d=",i);
  738.  if(color_tab[i]==1)
  739.  {
  740.   printf("RED");
  741.  }
  742.  if(color_tab[i]==2)
  743.  {
  744.   printf("GREEN");
  745.  }
  746.  if(color_tab[i]==3)
  747.  {
  748.   printf("BLUE");
  749.  }
  750.  if(color_tab[i]==4)
  751.  {
  752.   printf("YELLOW");
  753.   }
  754.  }
  755. }
  756.  
  757.  
  758.  
  759. /******* TSP *******/
  760.  
  761.  
  762.  
  763.  
  764. #include"stdio.h"
  765. int x[15],used[15];
  766. int adj[15][15]={0};
  767. int path[15][15],wght[15];
  768. int c,min;
  769. int path_ok(int k,int n)
  770. {
  771.    if(used[x[k]])
  772.       return 0;
  773.    if(k<n-1)
  774.       return(adj[x[k-1]][x[k]]);
  775.    else
  776.       return(adj[x[k-1]][x[k]] && adj[x[k]][x[0]]);
  777. }
  778. void TSP(int k,int n)
  779. {
  780.    int i,sum;
  781.    for(x[k]=1;x[k]<n;x[k]++)
  782.    {
  783.       if(path_ok(k,n))
  784.       {
  785.          used[x[k]]=1;
  786.          if(k==n-1)
  787.          {
  788.             sum=0;
  789.             printf("\n\n\tPOSSIBLE PATH %d : ",c+1);
  790.             for(i=0;i<n;i++)
  791.             {
  792.                printf("%d\t",x[i]);
  793.                path[c][i]=x[i];
  794.                sum+=adj[x[i]][x[i+1]];
  795.             }
  796.             printf(" : %d",sum);
  797.             wght[c]=sum;
  798.             if(c==0 || sum<min)
  799.                min=sum;
  800.             c++;
  801.             used[x[k]]=0;
  802.             getch();
  803.          }
  804.          else
  805.             TSP(k+1,n);
  806.          used[x[k]]=0;
  807.       }
  808.    }
  809. }
  810. void findmin(int n)
  811. {
  812.    int i,j;
  813.    for(i=0;i<c;i++)
  814.       if(wght[i]==min)
  815.       {
  816.          printf("\n\n\tMINIMUM PATH : ");
  817.          for(j=0;j<n;j++)
  818.             printf("%d\t",path[i][j]);
  819.       }
  820. }
  821. void main()
  822. {
  823.    int i,n,j;
  824.    int edg;
  825.    clrscr();
  826.    printf("\n\n\t\tTRAVELLING SALESMAN PROBLEM\n\n");
  827.    printf("\n\tEnter the no. of Cities : ");
  828.    scanf("%d",&n);
  829.    printf("\n\n Enter the Cost if path Exist Between cities.:{c1,c2}.Else Enter 0\n\n");
  830.    printf("\n\tCITIES\t\tCOST\n\n");
  831.    for(i=0;i<n;i++)
  832.       for(j=i+1;j<n;j++)
  833.       {
  834.          printf("\n\t %d------ %d \t:",i,j);
  835.          scanf("%d",&edg);
  836.         if(edg)
  837.            adj[i][j]=adj[j][i]=edg;
  838.       }
  839.    used[0]=1;
  840.    TSP(1,n);
  841.    if(!c)
  842.       printf("\n\n\t\tNO PATH FOUND TO COVER ALL THE CITIES\n\n");
  843.    else
  844.    {
  845.       printf("\n\n\t\tMINIMUM COST IS %d AND THE PATHS ARE \n\n",min);
  846.       findmin(n);
  847.    }
  848.    getch();
  849. }