opurag

OWT SEDOC AAD

Jun 11th, 2023 (edited)
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.57 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.  
  366. //Floyd
  367. #include<stdio.h>
  368. #include<time.h>
  369.  
  370.  
  371. void floyd(int a[10][10], int n)
  372. {
  373.     for(int k=0;k<n;k++)
  374.     {
  375.         for(int i=0;i<n;i++)
  376.         {
  377.             for(int j=0;j<n;j++)
  378.             {  
  379.                 if(a[i][j]>a[i][k]+a[k][j])
  380.                 {
  381.                     a[i][j]=a[i][k]+a[k][j];
  382.                 }
  383.             }
  384.         }
  385.     }
  386.     printf("All Pairs Shortest Path is :\n");
  387.     for(int i=0;i<n;i++)
  388.     {
  389.         for(int j=0;j<n;j++)
  390.         {
  391.             printf("%d ",a[i][j]);
  392.         }
  393.         printf("\n");
  394.     }
  395. }
  396.  
  397. int main()
  398. {
  399.     int cost[10][10],n;
  400.     printf("Enter the number of vertices\n");
  401.     scanf("%d",&n);
  402.     printf("Enter the cost adjacency matrix\n");
  403.     for(int i=0;i<n;i++)
  404.     {
  405.         for(int j=0;j<n;j++)
  406.         {
  407.             scanf("%d",&cost[i][j]);
  408.         }
  409.     }
  410.  
  411. clock_t start=clock();
  412. floyd(cost,n);
  413. clock_t end=clock();
  414. printf("Start time is %lf\n",(double)start);
  415. printf("End time is %lf\n",(double)end);
  416. printf("Total time is %lf\n",(double)(end-start));
  417. return 0;
  418. }
  419.  
  420.  
  421. //0/1 Knapsack
  422.     #include<stdio.h>
  423.     #include<time.h>
  424.     int max(int a, int b)
  425.     {
  426.         return (a > b)? a : b;
  427.     }
  428.    
  429.     int knapSack(int W, int wt[], int val[], int n)
  430.     {
  431.     int i, w;
  432.     int K[n+1][W+1];
  433.     for (i = 0; i <= n; i++)
  434.         {
  435.             for (w = 0; w <= W; w++)
  436.             {
  437.             if (i==0 || w==0)
  438.                 K[i][w] = 0;
  439.             else if (wt[i-1] <= w)
  440.                 K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
  441.             else
  442.                 K[i][w] = K[i-1][w];
  443.             }
  444.         }
  445.     return K[n][W];
  446.     }
  447.    
  448.    
  449.     int main()
  450.     {
  451.     int i, n, val[20], wt[20], W;
  452.     printf("Enter number of items:");
  453.     scanf("%d", &n);
  454.     printf("Enter value and weight of items:\n");
  455.     for(i = 0;i < n; ++i)
  456.         {
  457.             scanf("%d%d", &val[i], &wt[i]);
  458.         }
  459.     printf("Enter size of knapsack:");
  460.     scanf("%d", &W);
  461.     clock_t start=clock();
  462.     printf("The maximum profit is %d\n", knapSack(W, wt, val, n));
  463.     clock_t end=clock();
  464.     printf("Start time is %lf\n",(double)start);
  465.     printf("End time is %lf\n",(double)end);
  466.     printf("Total time is %lf\n",(double)(end-start));
  467.     return 0;
  468.     }
  469.  
  470.  
  471. //DIJIKSTRAS
  472. #include <stdio.h>
  473. #include<time.h>
  474. #define INFINITY 999
  475. #define MAX 10
  476.  
  477. void Dijkstra(int Graph[MAX][MAX], int n, int start);
  478.  
  479. void Dijkstra(int Graph[MAX][MAX], int n, int start) {
  480.  int cost[MAX][MAX], distance[MAX], pred[MAX];
  481.  int visited[MAX], count, mindistance, nextnode, i, j;
  482.  
  483.  // Creating cost matrix
  484.  for (i = 0; i < n; i++)
  485.     for (j = 0; j < n; j++)
  486.         if (Graph[i][j] == 0)
  487.             cost[i][j] = INFINITY;
  488.         else
  489.             cost[i][j] = Graph[i][j];
  490.  
  491.  for (i = 0; i < n; i++) {
  492.     distance[i] = cost[start][i];
  493.     pred[i] = start;
  494.     visited[i] = 0;
  495.  }
  496.  distance[start] = 0;
  497.  visited[start] = 1;
  498.  count = 1;
  499.  
  500.  while (count < n - 1) {
  501.  mindistance = INFINITY;
  502.     for (i = 0; i < n; i++)
  503.         if (distance[i] < mindistance && !visited[i]) {
  504.             mindistance = distance[i];
  505.         nextnode = i;
  506.     }
  507.  
  508.  visited[nextnode] = 1;
  509.  for (i = 0; i < n; i++)
  510.     if (!visited[i])
  511.         if (mindistance + cost[nextnode][i] < distance[i]) {
  512.             distance[i] = mindistance + cost[nextnode][i];
  513.             pred[i] = nextnode;
  514.         }
  515.  count++;
  516.  }
  517.  // Printing the distance
  518.  for (i = 0; i < n; i++)
  519.     if (i != start) {
  520.         printf("\nDistance from source to %d: %d", i, distance[i]);
  521.  }
  522. }
  523.  
  524. int main() {
  525.  int Graph[MAX][MAX], i, j, n, u;
  526.  printf("Enter number of nodes:\n");
  527.  scanf("%d",&n);
  528.  printf("Enter Matrix Elements( Cost Adjacency Matrix):");
  529.  for (i=0;i<n;i++)
  530.     {
  531.     for(j=0;j<n;j++)
  532.         {
  533.         scanf("%d",&Graph[i][j]);
  534.  //printf("%d",Graph[i][j]);
  535.         }
  536.     }
  537. printf("Select source vertex:\n");
  538.  scanf("%d",&u);
  539.  clock_t start=clock();
  540.  Dijkstra(Graph, n, u);
  541.  clock_t end=clock();
  542.  
  543.  printf("\n Start time is %lf\n",(double)start);
  544.  printf("End time is %lf\n",(double)end);
  545.  printf("Total time is %lf\n",(double)(end-start));
  546.  return 0;
  547. }
  548.  
  549.  
  550. //DIJIKSTRAS 2.0
  551. #include <limits.h>
  552. #include <stdbool.h>
  553. #include <stdio.h>
  554. #include <time.h>
  555.  
  556. #define MAX_V 10
  557.  
  558. int minDistance(int dist[], bool sptSet[], int V) {
  559.     int min = INT_MAX, min_index;
  560.     for (int v = 0; v < V; v++)
  561.         if (sptSet[v] == false && dist[v] <= min)
  562.             min = dist[v], min_index = v;
  563.     return min_index;
  564. }
  565.  
  566. void printSolution(int dist[], int V) {
  567.     printf("Vertex \t\t Distance from Source\n");
  568.     for (int i = 0; i < V; i++)
  569.         printf("%d \t\t\t\t %d\n", i, dist[i]);
  570. }
  571.  
  572. void dijkstra(int graph[MAX_V][MAX_V], int V, int src) {
  573.     int dist[MAX_V];
  574.     bool sptSet[MAX_V];
  575.     for (int i = 0; i < V; i++)
  576.         dist[i] = INT_MAX, sptSet[i] = false;
  577.     dist[src] = 0;
  578.     for (int count = 0; count < V - 1; count++) {
  579.         int u = minDistance(dist, sptSet, V);
  580.         sptSet[u] = true;
  581.         for (int v = 0; v < V; v++)
  582.             if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
  583.                 dist[v] = dist[u] + graph[u][v];
  584.     }
  585.  
  586.     printSolution(dist, V);
  587. }
  588.  
  589. int main() {
  590.     int V, p, q;
  591.     printf("Enter the number of vertices: ");
  592.     scanf("%d", &V);
  593.  
  594.     int graph[MAX_V][MAX_V];
  595.     printf("Enter the matrix elements:\n");
  596.     for (p = 0; p < V; p++) {
  597.         for (q = 0; q < V; q++) {
  598.             scanf("%d", &graph[p][q]);
  599.         }
  600.     }
  601.  
  602.     clock_t start = clock();
  603.     dijkstra(graph, V, 0);
  604.     clock_t end = clock();
  605.  
  606.     printf("Start time is %lf\n", (double)start / CLOCKS_PER_SEC);
  607.     printf("End time is %lf\n", (double)end / CLOCKS_PER_SEC);
  608.     printf("Total time is %lf\n", (double)(end - start) / CLOCKS_PER_SEC);
  609.  
  610.     return 0;
  611. }
  612.  
  613.  
  614. //TSP:
  615. #include <stdio.h>
  616. #include<time.h>
  617. int tsp_g[10][10];/* = {
  618.     {12, 30, 33, 10, 45},
  619.     {56, 22, 9, 15, 18},
  620.     {29, 13, 8, 5, 12},
  621.     {33, 28, 16, 10, 3},
  622.     {1, 4, 30, 24, 20}
  623. };*/
  624. int visited[10], n, cost = 0;
  625.  
  626.  
  627. void travellingsalesman(int c){
  628.     int k, adj_vertex = 999;
  629.     int min = 999;
  630.    
  631.  
  632.     visited[c] = 1;
  633.    
  634.  
  635.     printf("%d ", c + 1);
  636.    
  637.    
  638.     for(k = 0; k < n; k++) {
  639.         if((tsp_g[c][k] != 0) && (visited[k] == 0)) {
  640.             if(tsp_g[c][k] < min) {
  641.             min = tsp_g[c][k];
  642.             }
  643.             adj_vertex = k;
  644.         }
  645.     }
  646.     if(min != 999) {
  647.         cost = cost + min;
  648.     }
  649.     if(adj_vertex == 999) {
  650.         adj_vertex = 0;
  651.         printf("%d", adj_vertex + 1);
  652.         cost = cost + tsp_g[c][adj_vertex];
  653.         return;
  654.     }
  655.     travellingsalesman(adj_vertex);
  656. }
  657.  
  658.  
  659. int main(){
  660.     int p,q,m;
  661.     printf("Enter the size of adjacency matrix\n");
  662.     scanf("%d",&m);
  663.     printf("Enter the matrix elements\n");
  664.     for(p=0;p<m;p++)
  665.         for(q=0;q<m;q++)
  666.             {
  667.                 scanf("%d",&tsp_g[p][q]);
  668.             }  
  669.     int i, j;
  670.     n = 5;
  671.     for(i = 0; i < n; i++) {
  672.         visited[i] = 0;
  673.     }
  674.     printf("\n\nShortest Path:\t");
  675.     clock_t start=clock();
  676.     travellingsalesman (0);
  677.     clock_t end=clock();
  678.     printf("\n minimum Cost: \t");
  679.     printf("%d\n", cost);
  680.     printf("Start time is %lf\n",(double)start);
  681.     printf("End time is %lf\n",(double)end);
  682.     printf("Total time is %lf\n",(double)(end-start));
  683.     return 0;
  684.  
  685. }
  686.  
  687.  
  688. //TSP_2
  689. #include <stdio.h>
  690.  
  691. int tsp_g[10][10];
  692. int visited[10], n, cost = 0;
  693.  
  694. void travellingsalesman(int c) {
  695.   int k, adj_vertex = 999;
  696.   int min = 999;
  697.  
  698.   visited[c] = 1;
  699.   printf("%d ", c + 1);
  700.  
  701.   for (k = 0; k < n; k++) {
  702.     if ((tsp_g[c][k] != 0) && (visited[k] == 0)) {
  703.       if (tsp_g[c][k] < min) {
  704.         min = tsp_g[c][k];
  705.       }
  706.       adj_vertex = k;
  707.     }
  708.   }
  709.   if (min != 999) {
  710.     cost = cost + min;
  711.   }
  712.   if (adj_vertex == 999) {
  713.     adj_vertex = 0;
  714.     printf("%d", adj_vertex + 1);
  715.     cost = cost + tsp_g[c][adj_vertex];
  716.     return;
  717.   }
  718.   travellingsalesman(adj_vertex);
  719. }
  720.  
  721. int main() {
  722.   int i, j;
  723.   printf("Enter the number of vertices: \n");
  724.   scanf("%d", &n);
  725.   printf("Enter matrix elements:\n");
  726.   for (i = 0; i < n; i++) {
  727.     for (j = 0; j < n; j++) {
  728.       //printf("Enter the cost between vertex %d and vertex %d: ", i + 1, j + 1);
  729.       scanf("%d", &tsp_g[i][j]);
  730.     }
  731.   }
  732.   for (i = 0; i < n; i++) {
  733.     visited[i] = 0;
  734.   }
  735.   printf("\n\nShortest Path:\t");
  736.   travellingsalesman(0);
  737.   printf("\n\nminimum Cost: \t");
  738.   printf("%d\n", cost);
  739.   return 0;
  740. }
  741.  
Advertisement
Add Comment
Please, Sign In to add comment