opurag

SEDOC AAD

Jun 11th, 2023
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.13 KB | None | 0 0
  1. //toh
  2. #include<stdio.h>
  3. #include<time.h>
  4.  
  5. void toh(int n, char from_d, char to_d, char aux)
  6. {
  7.     if(n==1)
  8.     {
  9.         printf("Move disk 1 from %c to %c \n",from_d,to_d);
  10.         return ;
  11.     }
  12.  
  13.     toh(n-1,from_d,aux,to_d);
  14.     printf("Move disk %d from %c to %c\n",n,from_d,to_d);
  15.     toh(n-1,aux,to_d,from_d);
  16. }
  17.  
  18. void main()
  19. {
  20.     int n;
  21.     printf("Enter the number of disks\n");
  22.     scanf("%d",&n);
  23.     clock_t start=clock();
  24.     toh(n,'A','C','B');
  25.     clock_t end=clock();
  26.  
  27.     printf("Start time is %f",(double)start);
  28.     printf("End time: %f",(double)end);
  29.     printf("Time taken: %f", (double)(end-start));
  30.  
  31.  
  32. }
  33.  
  34. //binarySearch
  35. #include <stdio.h>
  36. #include <time.h>
  37.  
  38. int binarySearch(int arr[], int l, int r, int x)
  39. {
  40.     if (r >= l)
  41.     {
  42.         int mid = l + (r - l) / 2;
  43.         if (arr[mid] == x)
  44.             return mid;
  45.         if (arr[mid] > x)
  46.             return binarySearch(arr, l, mid - 1, x);
  47.         return binarySearch(arr, mid + 1, r, x);
  48.     }
  49.     return -1;
  50. }
  51.  
  52. int main()
  53. {
  54.     int n,x;
  55.     printf("Enter size\n");
  56.     scanf("%d",&n);
  57.     int arr[n];
  58.     printf("Enter array elements\n");
  59.     for(int i=0;i<n;i++)
  60.     {
  61.         scanf("%d",&arr[i]);
  62.     }
  63.     printf("Enter key\n");
  64.     scanf("%d",&x);
  65.     clock_t start=clock();
  66.     int result = binarySearch(arr, 0, n - 1, x);
  67.     clock_t end=clock();
  68.     if(result == -1)
  69.         printf("Element is not present in array\n");
  70.     else
  71.         printf("Element is present at index %d\n", result);
  72.     printf("\nStart time is %lf\n",(double)start);
  73.     printf("\nEnd time is %lf\n",(double)end);
  74.     printf("\nTotal time is %lf\n",(double)(end-start));
  75.     return 0;
  76. }
  77.  
  78. //MergeSort
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81.  
  82.  
  83. void merge(int arr[], int l, int m, int r)
  84. {
  85.     int i, j, k;
  86.     int n1 = m - l + 1;
  87.     int n2 = r - m;
  88.  
  89.    
  90.     int L[n1], R[n2];
  91.  
  92.  
  93.     for (i = 0; i < n1; i++)
  94.         L[i] = arr[l + i];
  95.     for (j = 0; j < n2; j++)
  96.         R[j] = arr[m + 1 + j];
  97.  
  98.  
  99.     i = 0;
  100.     j = 0;
  101.     k = l;
  102.     while (i < n1 && j < n2) {
  103.         if (L[i] <= R[j]) {
  104.             arr[k] = L[i];
  105.             i++;
  106.         }
  107.         else {
  108.             arr[k] = R[j];
  109.             j++;
  110.         }
  111.         k++;
  112.     }
  113.  
  114.  
  115.     while (i < n1) {
  116.         arr[k] = L[i];
  117.         i++;
  118.         k++;
  119.     }
  120.  
  121.  
  122.     while (j < n2) {
  123.         arr[k] = R[j];
  124.         j++;
  125.         k++;
  126.     }
  127. }
  128.  
  129.  
  130. void mergeSort(int arr[], int l, int r)
  131. {
  132.     if (l < r) {
  133.        
  134.        
  135.         int m = l + (r - l) / 2;
  136.  
  137.        
  138.         mergeSort(arr, l, m);
  139.         mergeSort(arr, m + 1, r);
  140.  
  141.         merge(arr, l, m, r);
  142.     }
  143. }
  144.  
  145.  
  146.  
  147. void printArray(int A[], int size)
  148. {
  149.     int i;
  150.     for (i = 0; i < size; i++)
  151.         printf("%d ", A[i]);
  152.     printf("\n");
  153. }
  154.  
  155.  
  156. int main()
  157. {
  158.     int arr[50],arr_size,i;
  159.     printf("Enter the number of elements in the array\n");
  160.     scanf("%d",&arr_size);
  161.  
  162.     printf("Enter the elements\n");
  163.     for(i=0;i<arr_size;i++)
  164.     {
  165.         scanf("%d",&arr[i]);
  166.     }
  167.  
  168.     printf("Given array is \n");
  169.     printArray(arr, arr_size);
  170.  
  171.     mergeSort(arr, 0, arr_size - 1);
  172.  
  173.     printf("\nSorted array is \n");
  174.     printArray(arr, arr_size);
  175.     return 0;
  176. }
  177.  
  178.  
  179. //QuickSort
  180. #include <stdio.h>
  181. #include<time.h>
  182.  
  183.  
  184. void swap(int* a, int* b) {
  185.     int t = *a;
  186.     *a = *b;
  187.     *b = t;
  188. }
  189.  
  190.  
  191. int partition(int arr[], int low, int high) {
  192.     int pivot = arr[high];
  193.     int i = (low - 1);
  194.  
  195.     for (int j = low; j <= high - 1; j++) {
  196.         if (arr[j] < pivot) {
  197.             i++;
  198.             swap(&arr[i], &arr[j]);
  199.         }
  200.     }
  201.     swap(&arr[i + 1], &arr[high]);
  202.     return (i + 1);
  203. }
  204.  
  205.  
  206. void quickSort(int arr[], int low, int high) {
  207.     if (low < high) {
  208.         int pi = partition(arr, low, high);
  209.         quickSort(arr, low, pi - 1);
  210.         quickSort(arr, pi + 1, high);
  211.     }
  212. }
  213.  
  214.  
  215. void printArray(int arr[], int size) {
  216.     int i;
  217.     for (i = 0; i < size; i++)
  218.         printf("%d ", arr[i]);
  219.     printf("\n");
  220. }
  221.  
  222.  
  223. int main() {
  224.     int arr[50],arr_size,i;
  225.     printf("Enter the number of elements in the array\n");
  226.     scanf("%d",&arr_size);
  227.  
  228.     printf("Enter the elements\n");
  229.     for(i=0;i<arr_size;i++)
  230.     {
  231.         scanf("%d",&arr[i]);
  232.     }
  233.     clock_t start=clock();
  234.     quickSort(arr, 0, arr_size - 1);
  235.     clock_t end=clock();
  236.  
  237.     printf("Sorted array: \n");
  238.     printArray(arr, arr_size);
  239.  
  240.     printf("Start time is %f",(double)start);
  241.     printf("End time: %f",(double)end);
  242.     printf("Time taken: %f", (double)(end-start));
  243.     return 0;
  244. }
  245.  
  246.  
  247. //Prims
  248. #include<stdio.h>
  249. #include<time.h>
  250.  
  251. int visited[10]={0}, cost[10][10], min, mincost=0;
  252. int i,j,ne=1, a, b, u, v;
  253. int main()
  254. {
  255.     int num;
  256.     printf("\n\t\t\tPrim's Algorithm");
  257.     printf("\n\nEnter the number of nodes= ");
  258.     scanf("%d", &num);
  259.     printf("\nEnter the adjacency matrix\n\n");
  260.     for(i=1; i<=num; i++)
  261.     {
  262.         for(j=1; j<=num; j++)
  263.         {
  264.             scanf("%d", &cost[i][j]);
  265.             if(cost[i][j]==0)
  266.                 cost[i][j]=999;
  267.         }
  268.     }
  269.     clock_t start=clock();
  270.     visited[1]=1;
  271.     while(ne < num)
  272.     {
  273.         for(i=1,min=999;i<=num;i++)
  274.         for(j=1;j<=num;j++)
  275.         if(cost[i][j]< min)
  276.         if(visited[i]!=0)
  277.         {
  278.             min=cost[i][j];
  279.             a=u=i;
  280.             b=v=j;
  281.         }
  282.        
  283.         printf("\n Edge %d:(%d - %d) cost:%d",ne++,a,b,min);
  284.         mincost=mincost+min;
  285.         visited[b]=1;
  286.         cost[a][b]=cost[b][a]=999;
  287.     }
  288.     printf("\n\n\n Minimun cost=%d",mincost);
  289.     clock_t end=clock();
  290.     printf("\nStart time is %lf\n",(double)start);
  291.     printf("End time is %lf\n",(double)end);
  292.     printf("Total time is %lf\n",(double)(end-start));
  293.     return 0;
  294. }
  295.  
  296. //Kruskals
  297. #include <stdio.h>
  298. #include <time.h>
  299. int i, j, k, a, b, u, v, n, ne = 1;
  300. int min, mincost = 0, cost[9][9], parent[9];
  301. int find(int);
  302. int uni(int, int);
  303. void main()
  304. {
  305.     printf("\n\tImplementation of Kruskal's algorithm\n");
  306.     printf("\nEnter the no. of vertices:");
  307.     scanf("%d", &n);
  308.     printf("\nEnter the cost adjacency matrix:\n");
  309.     for (i = 1; i <= n; i++)
  310.     {
  311.         for (j = 1; j <= n; j++)
  312.         {
  313.             scanf("%d", &cost[i][j]);
  314.             if (cost[i][j] == 0)
  315.                 cost[i][j] = 999;
  316.         }
  317.     }
  318.     printf("The edges of Minimum Cost Spanning Tree are\n");
  319.     clock_t start = clock();
  320.     while (ne < n)
  321.     {
  322.         for (i = 1, min = 999; i <= n; i++)
  323.         {
  324.             for (j = 1; j <= n; j++)
  325.             {
  326.                 if (cost[i][j] < min)
  327.                 {
  328.                     min = cost[i][j];
  329.                     a = u = i;
  330.                     b = v = j;
  331.                 }
  332.             }
  333.         }
  334.         u = find(u);
  335.         v = find(v);
  336.         if (uni(u, v))
  337.         {
  338.             printf("%d edge (%d,%d) =%d\n", ne++, a, b, min);
  339.             mincost += min;
  340.         }
  341.         cost[a][b] = cost[b][a] = 999;
  342.     }
  343.     printf("\n\tMinimum cost = %d\n", mincost);
  344.     clock_t end = clock();
  345.     printf("Start time is %lf\n", (double)start);
  346.     printf("End time is %lf\n", (double)end);
  347.     printf("Total time is %lf\n", (double)(end - start));
  348. }
  349. int find(int i)
  350. {
  351.     while (parent[i])
  352.         i = parent[i];
  353.     return i;
  354. }
  355. int uni(int i, int j)
  356. {
  357.     if (i != j)
  358.     {
  359.         parent[j] = i;
  360.         return 1;
  361.     }
  362.     return 0;
  363. }
  364.  
  365. //0/1 Knapsack
  366.     #include<stdio.h>
  367.     #include<time.h>
  368.     int max(int a, int b)
  369.     {
  370.         return (a > b)? a : b;
  371.     }
  372.    
  373.     int knapSack(int W, int wt[], int val[], int n)
  374.     {
  375.     int i, w;
  376.     int K[n+1][W+1];
  377.     for (i = 0; i <= n; i++)
  378.         {
  379.             for (w = 0; w <= W; w++)
  380.             {
  381.             if (i==0 || w==0)
  382.                 K[i][w] = 0;
  383.             else if (wt[i-1] <= w)
  384.                 K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
  385.             else
  386.                 K[i][w] = K[i-1][w];
  387.             }
  388.         }
  389.     return K[n][W];
  390.     }
  391.    
  392.    
  393.     int main()
  394.     {
  395.     int i, n, val[20], wt[20], W;
  396.     printf("Enter number of items:");
  397.     scanf("%d", &n);
  398.     printf("Enter value and weight of items:\n");
  399.     for(i = 0;i < n; ++i)
  400.         {
  401.             scanf("%d%d", &val[i], &wt[i]);
  402.         }
  403.     printf("Enter size of knapsack:");
  404.     scanf("%d", &W);
  405.     clock_t start=clock();
  406.     printf("The maximum profit is %d\n", knapSack(W, wt, val, n));
  407.     clock_t end=clock();
  408.     printf("Start time is %lf\n",(double)start);
  409.     printf("End time is %lf\n",(double)end);
  410.     printf("Total time is %lf\n",(double)(end-start));
  411.     return 0;
  412.     }
  413.  
  414.  
  415. //DIJIKSTRAS
  416. #include <stdio.h>
  417. #include<time.h>
  418. #define INFINITY 999
  419. #define MAX 10
  420.  
  421. void Dijkstra(int Graph[MAX][MAX], int n, int start);
  422.  
  423. void Dijkstra(int Graph[MAX][MAX], int n, int start) {
  424.  int cost[MAX][MAX], distance[MAX], pred[MAX];
  425.  int visited[MAX], count, mindistance, nextnode, i, j;
  426.  
  427.  // Creating cost matrix
  428.  for (i = 0; i < n; i++)
  429.     for (j = 0; j < n; j++)
  430.         if (Graph[i][j] == 0)
  431.             cost[i][j] = INFINITY;
  432.         else
  433.             cost[i][j] = Graph[i][j];
  434.  
  435.  for (i = 0; i < n; i++) {
  436.     distance[i] = cost[start][i];
  437.     pred[i] = start;
  438.     visited[i] = 0;
  439.  }
  440.  distance[start] = 0;
  441.  visited[start] = 1;
  442.  count = 1;
  443.  
  444.  while (count < n - 1) {
  445.  mindistance = INFINITY;
  446.     for (i = 0; i < n; i++)
  447.         if (distance[i] < mindistance && !visited[i]) {
  448.             mindistance = distance[i];
  449.         nextnode = i;
  450.     }
  451.  
  452.  visited[nextnode] = 1;
  453.  for (i = 0; i < n; i++)
  454.     if (!visited[i])
  455.         if (mindistance + cost[nextnode][i] < distance[i]) {
  456.             distance[i] = mindistance + cost[nextnode][i];
  457.             pred[i] = nextnode;
  458.         }
  459.  count++;
  460.  }
  461.  // Printing the distance
  462.  for (i = 0; i < n; i++)
  463.     if (i != start) {
  464.         printf("\nDistance from source to %d: %d", i, distance[i]);
  465.  }
  466. }
  467.  
  468. int main() {
  469.  int Graph[MAX][MAX], i, j, n, u;
  470.  printf("Enter number of nodes:\n");
  471.  scanf("%d",&n);
  472.  printf("Enter Matrix Elements( Cost Adjacency Matrix):");
  473.  for (i=0;i<n;i++)
  474.     {
  475.     for(j=0;j<n;j++)
  476.         {
  477.         scanf("%d",&Graph[i][j]);
  478.  //printf("%d",Graph[i][j]);
  479.         }
  480.     }
  481. printf("Select source vertex:\n");
  482.  scanf("%d",&u);
  483.  clock_t start=clock();
  484.  Dijkstra(Graph, n, u);
  485.  clock_t end=clock();
  486.  
  487.  printf("\n Start time is %lf\n",(double)start);
  488.  printf("End time is %lf\n",(double)end);
  489.  printf("Total time is %lf\n",(double)(end-start));
  490.  return 0;
  491. }
Advertisement
Add Comment
Please, Sign In to add comment