Advertisement
sirdi

daa

Apr 10th, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 24.32 KB | None | 0 0
  1. KARATSUBA
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6.  
  7. int max(int x, int y) {
  8.     return (x >= y) ? x : y;
  9. }
  10.  
  11. int karatsuba(int x, int y) {
  12.     if (x < 10 || y < 10) {
  13.         return x * y;
  14.     }
  15.  
  16.     // Compute the size of the two halves of each number
  17.     int n = max(log10(x) + 1, log10(y) + 1) / 2;
  18.     int p = pow(10, n);
  19.  
  20.     // Split each number into two halves
  21.     int a = x / p;
  22.     int b = x % p;
  23.     int c = y / p;
  24.     int d = y % p;
  25.  
  26.     // Compute the three products recursively
  27.     int ac = karatsuba(a, c);
  28.     int bd = karatsuba(b, d);
  29.     int ab_cd = karatsuba(a + b, c + d);
  30.  
  31.     // Compute the final product using the three products
  32.     return ac * pow(10, 2 * n) + (ab_cd - ac - bd) * pow(10, n) + bd;
  33. }
  34.  
  35. int main() {
  36.     int x, y;
  37.     printf("Enter two numbers to multiply: ");
  38.     scanf("%d %d", &x, &y);
  39.     int res = karatsuba(x, y);
  40.     printf("%d * %d = %d\n", x, y, res);
  41.     return 0;
  42. }
  43.  
  44. MAX SUB ARRAY
  45.  
  46.  
  47. #include <stdio.h>
  48. int main(){
  49.  int arr[100];
  50.  for(int i = 0; i<8; i++){
  51.  scanf("%d", &arr[i]);
  52.  // n++;
  53.  // if(arr[i] == '\n'){
  54.  // break;
  55.  // }
  56.  }
  57.  int sum = 0, max = 0;
  58.  for(int i = 0; i<8; i++){
  59.  sum+=arr[i];
  60.  if(sum>max){
  61.  max = sum;
  62.  }
  63.  else if(sum<0){
  64.  sum = 0;
  65.  }
  66.  }
  67.  printf("Maximum subarray sum = %d", max);
  68. }
  69.  
  70. Longest common subsequence
  71.  
  72. #include <stdio.h>
  73. #include <string.h>
  74. int main()
  75. {
  76.  int i, j, m, n, LCS_table[20][20];
  77.  char S1[20];
  78. fgets(S1, 20, stdin);
  79. char S2[20];
  80. fgets(S2, 20, stdin);
  81.  m = strlen(S1);
  82.  n = strlen(S2);
  83.  for (i = 0; i <= m; i++)
  84.  LCS_table[i][0] = 0;
  85.  for (i = 0; i <= n; i++)
  86.  LCS_table[0][i] = 0;
  87.  for (i = 1; i <= m; i++)
  88.  for (j = 1; j <= n; j++) {
  89.  if (S1[i - 1] == S2[j - 1]) {
  90.  LCS_table[i][j] = LCS_table[i - 1][j - 1] + 1;
  91.  } else if (LCS_table[i - 1][j] >= LCS_table[i][j - 1]) {
  92.  LCS_table[i][j] = LCS_table[i - 1][j];
  93.  } else {
  94.  LCS_table[i][j] = LCS_table[i][j - 1];
  95.  }
  96.  }
  97.  int index = LCS_table[m][n];
  98.  char lcsAlgo[index ];
  99.  lcsAlgo[index] = '\0';
  100.  
  101.  i = m, j = n;
  102.  while (i > 0 && j > 0) {
  103.  if (S1[i - 1] == S2[j - 1]) {
  104.  lcsAlgo[index - 1] = S1[i - 1];
  105.  i--;
  106.  j--;
  107.  index--;
  108.  }
  109.  else if (LCS_table[i - 1][j] > LCS_table[i][j - 1])
  110.  i--;
  111.  else
  112.  j--;
  113.  }
  114.  printf("LCSS IS: %s", lcsAlgo);
  115.  printf("\n");
  116. }
  117.  
  118. FRACTIONAL KNAPSACK
  119.  
  120. #include <stdio.h>
  121.  
  122. int main() {
  123.     float p[50];
  124.     float w[50];
  125.     int n, we;
  126.     scanf("%d", &n);
  127.     scanf("%d", &we);
  128.     for (int i = 0; i < n; i++) {
  129.         scanf("%f", &p[i]);
  130.     }
  131.     for (int i = 0; i < n; i++) {
  132.         scanf("%f", &w[i]);
  133.     }
  134.  
  135.     float r[50];
  136.     for (int j = 0; j < n; j++) {
  137.         r[j] = p[j] / w[j];
  138.         printf("r[%d]: %f\n", j, r[j]);
  139.     }
  140.  
  141.     for (int i = 0; i < n; i++) {
  142.         for (int j = 0; j < n; j++) {
  143.             if (r[i] > r[j]) {
  144.                 float temp = r[i];
  145.                 r[i] = r[j];
  146.                 r[j] = temp;
  147.  
  148.                 temp = p[i];
  149.                 p[i] = p[j];
  150.                 p[j] = temp;
  151.  
  152.                 temp = w[i];
  153.                 w[i] = w[j];
  154.                 w[j] = temp;
  155.             }
  156.         }
  157.     }
  158.  
  159.     int sumw = 0;
  160.     float pro = 0;
  161.     for (int i = 0; i < n; i++) {
  162.         printf("p[%d]: %f, w[%d]: %f\n", i, p[i], i, w[i]);
  163.         sumw += w[i];
  164.         pro += p[i];
  165.         printf("sumw: %d, pro: %f\n", sumw, pro);
  166.         if (sumw == we) {
  167.             break;
  168.         } else if (sumw > we) {
  169.             sumw -= w[i];
  170.             pro -= p[i];
  171.             pro += ((we - sumw) * p[i]) / w[i];
  172.             break;
  173.         }
  174.     }
  175.  
  176.     printf("%f", pro);
  177.     return 0;
  178. }
  179.  
  180. Queen
  181.  
  182. #include<stdio.h>
  183. #include<stdlib.h>
  184. int a[30],count=0;
  185. int place(int pos)
  186. {
  187. int i;
  188. for (i=1;i<pos;i++)
  189. {
  190. if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))
  191. {
  192.  return 0;
  193. }
  194. }
  195. return 1;
  196. }
  197. void print_sol(int n)
  198. {
  199. int i,j;
  200. count++;
  201. printf("(");
  202.  for (i=1;i<=n;i++)
  203.  {
  204. for (j=1;j<=n;j++)
  205. {
  206. if(a[i]==j)
  207.  {
  208.  printf("%d",j);
  209.  }
  210. }
  211. if(i<n)
  212. {
  213.  printf(",");
  214. }
  215. }
  216. printf(")\n");
  217. }
  218. void queen(int n)
  219. {
  220. int k=1;
  221. a[k]=0;
  222. while(k!=0)
  223. {
  224. a[k]=a[k]+1;
  225. while((a[k]<=n)&&!place(k))
  226. {
  227.  a[k]++;
  228. }
  229. if(a[k]<=n)
  230. {
  231. if(k==n)
  232.  print_sol(n);
  233.  else
  234.  {
  235.  k++;
  236.  a[k]=0;
  237.  
  238.  }
  239.  
  240. }
  241. else
  242. k--;
  243. }
  244. }
  245. int main()
  246. {
  247. int n;
  248. scanf("%d",&n);
  249. queen(n);
  250. return 0;
  251. }
  252.  
  253. JOB SELECTION
  254.  
  255. #include <stdio.h>
  256. #include <stdlib.h>
  257. #include <limits.h>
  258. #define MAX 10
  259. int n, m;
  260. int time[MAX][MAX]; // time taken by each worker for each job
  261. int assigned[MAX]; // current assignment of jobs to workers
  262. int min_time = INT_MAX; // minimum time taken to complete all jobs
  263. int min_assignment[MAX]; // best assignment of jobs to workers
  264. int calc_time()
  265. {
  266.  int total_time = 0;
  267.  for (int i = 0; i < m; i++)
  268.  {
  269.  total_time += time[i][assigned[i]];
  270.  }
  271.  return total_time;
  272. }
  273. int can_assign(int w)
  274. {
  275.  for (int i = 0; i < 4; i++)
  276.  {
  277.  if (assigned[i] == w)
  278.  {
  279.  return 0;
  280.  }
  281.  }
  282.  return 1;
  283. }
  284. void assign()
  285. {
  286.  int temp;min_time=0;
  287.  int min;
  288.  
  289.  for(int i=0;i<4;i++)
  290.  {
  291.  min=100000;
  292.  for(int j=0;j<4;j++)
  293.  {
  294.  temp=time[i][j];
  295.  
  296.  if(temp<min)
  297.  {
  298.  if(can_assign(j)==1)
  299.  {
  300.  min=temp;
  301.  assigned[i]=j;
  302.  }
  303.  }
  304.  }
  305.  }
  306.  return ;
  307. }
  308. int main() {
  309.  //printf("Enter the number of workers: ");
  310.  scanf("%d", &n);
  311.  //printf("Enter the number of jobs: ");
  312.  scanf("%d", &m);
  313.  //printf("Enter the time taken by each worker for each job:\n");
  314.  for (int i = 0; i < n; i++) {
  315.  for (int j = 0; j < m; j++) {
  316.  scanf("%d", &time[i][j]);
  317.  }
  318.  }
  319.  
  320.  // initialize assigned array to -1 (no job assigned)
  321.  for (int i = 0; i < m; i++) {
  322.  assigned[i] = -1;
  323.  }
  324.  
  325.  assign();
  326.  //printf("Best assignment of jobs to workers:\n");
  327.  for (int i = 0; i < m; i++) {
  328.  printf("W%d-J%d\n",i+1,assigned[i]+1);
  329.  }
  330.  
  331.  printf("%d\n", calc_time());
  332.  
  333.  
  334.  return 0;
  335. }
  336.  
  337. 0/1 KNAPSACK BRANCH AND BOUND
  338.  
  339. #include <stdio.h>
  340. #include <stdlib.h>
  341. #include <stdbool.h>
  342. #define MAX_SIZE 100
  343. int n, m;
  344. int p[MAX_SIZE], w[MAX_SIZE];
  345. bool x[MAX_SIZE], bestx[MAX_SIZE];
  346. float bestp;
  347. void knapsack(int i, float cp, int cw);
  348. int main() {
  349.  scanf("%d",&n);
  350.  scanf("%d",&m);
  351.  int i;
  352.  for (i = 0; i < n; i++) {
  353.  scanf("%d", &p[i]);
  354.  }
  355.  for (i = 0; i < n; i++) {
  356.  scanf("%d", &w[i]);
  357.  }
  358.  knapsack(0, 0, 0);
  359.  // printf("The maximum profit is: %.2f\n", bestp);
  360.  // printf("The selected items are: ");
  361.  for (i = 0; i < n; i++) {
  362.  if (bestx[i]) {
  363.  printf("%d ", bestx[i]);
  364.  }
  365.  else{
  366.  printf("%d ",0);
  367.  }
  368.  }
  369.  printf("\n");
  370.  return 0;
  371. }
  372. void knapsack(int i, float cp, int cw) {
  373.  if (cw > m) {
  374.  return;
  375.  }
  376.  if (i == n) {
  377.  if (cp > bestp) {
  378.  bestp = cp;
  379.  int j;
  380.  for (j = 0; j < n; j++) {
  381.  bestx[j] = x[j];
  382.  }
  383.  }
  384.  return;
  385.  }
  386.  if (cw + w[i] <= m) {
  387.  x[i] = true;
  388.  knapsack(i+1, cp+p[i], cw+w[i]);
  389.  }
  390.  x[i] = false;
  391.  knapsack(i+1, cp, cw);
  392. }
  393.  
  394. NAIVE STRING MATCHING
  395.  
  396. #include <stdio.h>
  397. #include <string.h>
  398. void search(char* pat, char* txt)
  399. {
  400.  int ans[20];
  401.  int count=0;
  402.  int M = strlen(pat);
  403.  int N = strlen(txt);
  404.  printf("Pattern found at ");
  405.  for (int i = 0; i <= N - M; i++) {
  406.  int j;
  407.  for (j = 0; j < M; j++)
  408.  if (txt[i + j] != pat[j])
  409.  break;
  410.  if (j == M){
  411.  ans[count]=i/2;
  412.  count++;
  413.  }
  414.  }
  415.  for (int i=0;i<count;i++){
  416.  if (i==count-1){
  417.  printf("and ");
  418.  
  419.  }
  420.  printf("%d ",ans[i]);
  421.  }
  422. }
  423. int main()
  424. {
  425.  char txt[30];
  426.  char pat[30];
  427.  scanf("%[^\n]%*c",txt);
  428.  scanf("%[^\n]%*c",pat);
  429.  search(pat, txt);
  430.  return 0;
  431. }
  432.  
  433. KMP ALGORITHM
  434.  
  435. #include <stdio.h>
  436. #include <string.h>
  437.  
  438. void computeLPSArray(char* pat, int M, int* lps) {
  439.     int len = 0, i = 1;
  440.     lps[0] = 0;
  441.  
  442.     while (i < M) {
  443.         if (pat[i] == pat[len]) {
  444.             len++;
  445.             lps[i] = len;
  446.             i++;
  447.         } else {
  448.             if (len != 0) {
  449.                 len = lps[len - 1];
  450.             } else {
  451.                 lps[i] = 0;
  452.                 i++;
  453.             }
  454.         }
  455.     }
  456. }
  457.  
  458. void KMPSearch(char* pat, char* txt) {
  459.     int M = strlen(pat);
  460.     int N = strlen(txt);
  461.  
  462.     int lps[M];
  463.     computeLPSArray(pat, M, lps);
  464.  
  465.     int i = 0, j = 0, shifts = 0;
  466.  
  467.     while (i < N) {
  468.         if (pat[j] == txt[i]) {
  469.             j++;
  470.             i++;
  471.         }
  472.  
  473.         if (j == M) {
  474.             printf("Pattern found at index %d\n", i - j);
  475.             shifts++;
  476.             j = lps[j - 1];
  477.         } else if (i < N && pat[j] != txt[i]) {
  478.             if (j != 0) {
  479.                 j = lps[j - 1];
  480.                 shifts++;
  481.             } else {
  482.                 i++;
  483.                 shifts++;
  484.             }
  485.         }
  486.     }
  487.  
  488.     printf("Number of shifts needed to find the match: %d\n", shifts);
  489. }
  490.  
  491. int main() {
  492.     char txt[100], pat[100];
  493.     printf("Enter the string: ");
  494.     scanf("%s", txt);
  495.     printf("Enter the pattern: ");
  496.     scanf("%s", pat);
  497.  
  498.     KMPSearch(pat, txt);
  499.  
  500.     return 0;
  501. }
  502.  
  503. ROBIN KARP
  504.  
  505. #include <stdio.h>
  506. #include <string.h>
  507.  
  508. #define d 256 // The number of characters in the input alphabet
  509.  
  510. void search(char* pat, char* txt, int q) {
  511.     int M = strlen(pat);
  512.     int N = strlen(txt);
  513.     int i, j;
  514.     int p = 0; // hash value for pattern
  515.     int t = 0; // hash value for text
  516.     int h = 1;
  517.  
  518.     // The value of h would be "pow(d, M-1)%q"
  519.     for (i = 0; i < M - 1; i++) {
  520.         h = (h * d) % q;
  521.     }
  522.  
  523.     // Calculate the hash value of pattern and first window of text
  524.     for (i = 0; i < M; i++) {
  525.         p = (d * p + pat[i]) % q;
  526.         t = (d * t + txt[i]) % q;
  527.     }
  528.  
  529.     // Slide the pattern over text one by one
  530.     for (i = 0; i <= N - M; i++) {
  531.         // Check the hash values of current window of text and pattern.
  532.         // If the hash values match then only check for characters on-by-one
  533.         if (p == t) {
  534.             /* Check for characters one by one */
  535.             for (j = 0; j < M; j++) {
  536.                 if (txt[i + j] != pat[j])
  537.                     break;
  538.             }
  539.  
  540.             // If pat[0...M-1] = txt[i, i+1, ...i+M-1]
  541.             if (j == M) {
  542.                 printf("Pattern found at index %d\n", i);
  543.             }
  544.         }
  545.  
  546.         // Calculate the hash value for next window of text: Remove
  547.         // leading digit, add trailing digit
  548.         if (i < N - M) {
  549.             t = (d * (t - txt[i] * h) + txt[i + M]) % q;
  550.  
  551.             // We might get negative value of t, converting it to positive
  552.             if (t < 0) {
  553.                 t = t + q;
  554.             }
  555.         }
  556.     }
  557. }
  558.  
  559. int main() {
  560.     char txt[100], pat[100];
  561.     int q = 101; // A prime number
  562.  
  563.     printf("Enter the string: ");
  564.     scanf("%s", txt);
  565.     printf("Enter the pattern: ");
  566.     scanf("%s", pat);
  567.  
  568.     search(pat, txt, q);
  569.  
  570.     return 0;
  571. }
  572.  
  573. FLOYD WARSHALL
  574.  
  575. #include <stdio.h>
  576. #define INF 500
  577.  
  578. int main() {
  579.     int n;
  580.     printf("Enter the number of vertices in the graph: ");
  581.     scanf("%d", &n);
  582.    
  583.     int graph[n][n];
  584.     printf("Enter the adjacency matrix for the graph: \n");
  585.     for(int i=0; i<n; i++) {
  586.         for(int j=0; j<n; j++) {
  587.             scanf("%d", &graph[i][j]);
  588.         }
  589.     }
  590.  
  591.     // Applying Floyd Warshall Algorithm
  592.     for(int k=0; k<n; k++) {
  593.         for(int i=0; i<n; i++) {
  594.             for(int j=0; j<n; j++) {
  595.                 if(graph[i][k] + graph[k][j] < graph[i][j]) {
  596.                     graph[i][j] = graph[i][k] + graph[k][j];
  597.                 }
  598.             }
  599.         }
  600.     }
  601.  
  602.     // Printing the result
  603.     printf("\nThe shortest distances between every pair of vertices in the graph are:\n");
  604.     for(int i=0; i<n; i++) {
  605.         for(int j=0; j<n; j++) {
  606.             if(graph[i][j] == INF) {
  607.                 printf("%5s", "INF");
  608.             }
  609.             else {
  610.                 printf("%5d", graph[i][j]);
  611.             }
  612.         }
  613.         printf("\n");
  614.     }
  615.  
  616.     return 0;
  617. }
  618.  
  619. FORD FULKERSON
  620.  
  621. / Ford - Fulkerson algorith in C
  622.  
  623. #include <stdio.h>
  624.  
  625. #define A 0
  626. #define B 1
  627. #define C 2
  628. #define MAX_NODES 1000
  629. #define O 1000000000
  630.  
  631. int n;
  632. int e;
  633. int capacity[MAX_NODES][MAX_NODES];
  634. int flow[MAX_NODES][MAX_NODES];
  635. int color[MAX_NODES];
  636. int pred[MAX_NODES];
  637.  
  638. int min(int x, int y) {
  639.   return x < y ? x : y;
  640. }
  641.  
  642. int head, tail;
  643. int q[MAX_NODES + 2];
  644.  
  645. void enqueue(int x) {
  646.   q[tail] = x;
  647.   tail++;
  648.   color[x] = B;
  649. }
  650.  
  651. int dequeue() {
  652.   int x = q[head];
  653.   head++;
  654.   color[x] = C;
  655.   return x;
  656. }
  657.  
  658. // Using BFS as a searching algorithm
  659. int bfs(int start, int target) {
  660.   int u, v;
  661.   for (u = 0; u < n; u++) {
  662.     color[u] = A;
  663.   }
  664.   head = tail = 0;
  665.   enqueue(start);
  666.   pred[start] = -1;
  667.   while (head != tail) {
  668.     u = dequeue();
  669.     for (v = 0; v < n; v++) {
  670.       if (color[v] == A && capacity[u][v] - flow[u][v] > 0) {
  671.         enqueue(v);
  672.         pred[v] = u;
  673.       }
  674.     }
  675.   }
  676.   return color[target] == C;
  677. }
  678.  
  679. // Applying fordfulkerson algorithm
  680. int fordFulkerson(int source, int sink) {
  681.   int i, j, u;
  682.   int max_flow = 0;
  683.   for (i = 0; i < n; i++) {
  684.     for (j = 0; j < n; j++) {
  685.       flow[i][j] = 0;
  686.     }
  687.   }
  688.  
  689.   // Updating the residual values of edges
  690.   while (bfs(source, sink)) {
  691.     int increment = O;
  692.     for (u = n - 1; pred[u] >= 0; u = pred[u]) {
  693.       increment = min(increment, capacity[pred[u]][u] - flow[pred[u]][u]);
  694.     }
  695.     for (u = n - 1; pred[u] >= 0; u = pred[u]) {
  696.       flow[pred[u]][u] += increment;
  697.       flow[u][pred[u]] -= increment;
  698.     }
  699.     // Adding the path flows
  700.     max_flow += increment;
  701.   }
  702.   return max_flow;
  703. }
  704.  
  705. int main() {
  706.   for (int i = 0; i < n; i++) {
  707.     for (int j = 0; j < n; j++) {
  708.       capacity[i][j] = 0;
  709.     }
  710.   }
  711.   n = 6;
  712.   e = 7;
  713.  
  714.   capacity[0][1] = 8;
  715.   capacity[0][4] = 3;
  716.   capacity[1][2] = 9;
  717.   capacity[2][4] = 7;
  718.   capacity[2][5] = 2;
  719.   capacity[3][5] = 5;
  720.   capacity[4][2] = 7;
  721.   capacity[4][3] = 4;
  722.  
  723.   int s = 0, t = 5;
  724.   printf("Max Flow: %d\n", fordFulkerson(s, t));
  725. }
  726.  
  727. EDMOND KARP
  728.  
  729. #include <stdio.h>
  730. #include <limits.h>
  731. #include <stdbool.h>
  732. #define V 6 // Maximum number of vertices in the graph
  733.  
  734. int residualGraph[V][V]; // Residual graph where residualGraph[i][j] indicates remaining capacity of edge from vertex i to j
  735. int parent[V]; // Parent array to store augmenting path found during BFS
  736. bool visited[V]; // Boolean array to mark visited vertices during BFS
  737.  
  738. int min(int a, int b) {
  739.     return (a < b) ? a : b;
  740. }
  741.  
  742. bool bfs(int source, int sink) {
  743.     int queue[V];
  744.     int front = -1, rear = -1;
  745.     for (int i = 0; i < V; i++) {
  746.         parent[i] = -1;
  747.         visited[i] = false;
  748.     }
  749.     visited[source] = true;
  750.     queue[++rear] = source;
  751.     while (front != rear) {
  752.         int u = queue[++front];
  753.         for (int v = 0; v < V; v++) {
  754.             if (!visited[v] && residualGraph[u][v] > 0) {
  755.                 parent[v] = u;
  756.                 visited[v] = true;
  757.                 queue[++rear] = v;
  758.             }
  759.         }
  760.     }
  761.     return visited[sink];
  762. }
  763.  
  764. int edmondsKarp(int source, int sink) {
  765.     int maxFlow = 0;
  766.     while (bfs(source, sink)) {
  767.         int pathFlow = INT_MAX;
  768.         for (int v = sink; v != source; v = parent[v]) {
  769.             int u = parent[v];
  770.             pathFlow = min(pathFlow, residualGraph[u][v]);
  771.         }
  772.         for (int v = sink; v != source; v = parent[v]) {
  773.             int u = parent[v];
  774.             residualGraph[u][v] -= pathFlow;
  775.             residualGraph[v][u] += pathFlow;
  776.         }
  777.         maxFlow += pathFlow;
  778.     }
  779.     return maxFlow;
  780. }
  781.  
  782. int main() {
  783.     // Example graph with 6 vertices and 10 edges
  784.     int graph[V][V] = {{0, 11, 12, 0, 0, 0},
  785.                        {0, 0, 0, 12, 0, 0},
  786.                        {0, 1, 0, 0, 11, 0},
  787.                        {0, 0, 0, 0, 0, 19},
  788.                        {0, 0, 0, 7, 0, 4},
  789.                        {0, 0, 0, 0, 0, 0}};
  790.     for (int i = 0; i < V; i++) {
  791.         for (int j = 0; j < V; j++) {
  792.             residualGraph[i][j] = graph[i][j];
  793.         }
  794.     }
  795.     int source = 0, sink = 5; // Source and sink vertices
  796.     int maxFlow = edmondsKarp(source, sink);
  797.     printf("The maximum flow in the graph is %d\n", maxFlow);
  798.     return 0;
  799. }
  800.  
  801. GRAHAM SCAN
  802.  
  803. #include <stdio.h>
  804. #include <stdlib.h>
  805. #include <math.h>
  806. // Structure to represent a 2D point
  807. struct Point {
  808.  int x;
  809.  int y;
  810. };
  811. // Function to calculate the cross product of two vectors
  812. int cross_product(struct Point p, struct Point q, struct Point r) {
  813.  return (q.x-p.x) * (r.y-p.y) - (q.y-p.y) * (r.x-p.x);
  814. }
  815. // Function to find the point with the lowest y-coordinate
  816. struct Point find_lowest_point(struct Point points[], int num_points) {
  817.  struct Point lowest_point = points[0];
  818.  int i;
  819.  for (i = 1; i < num_points; i++) {
  820.  if (points[i].y < lowest_point.y || (points[i].y == lowest_point.y && points[i].x < lowest_point.x)) {
  821.  lowest_point = points[i];
  822.  }
  823.  }
  824.  return lowest_point;
  825. }
  826. // Function to sort the points in counterclockwise order around a pivot point
  827. void sort_points(struct Point points[], int num_points, struct Point pivot) {
  828.  int angle_cmp(const void *p1, const void *p2) {
  829.  struct Point pp1 = (struct Point)p1;
  830.  struct Point pp2 = (struct Point)p2;
  831.  int cp = cross_product(pivot, *pp1, *pp2);
  832.  if (cp > 0) {
  833.  return -1;
  834.  } else if (cp < 0) {
  835.  return 1;
  836.  } else {
  837.  int dist_p = sqrt(pow(pp1->x-pivot.x, 2) + pow(pp1->y-pivot.y, 2));
  838.  int dist_q = sqrt(pow(pp2->x-pivot.x, 2) + pow(pp2->y-pivot.y, 2));
  839.  if (dist_p < dist_q) {
  840.  return -1;
  841.  } else {
  842.  return 1;
  843.  }
  844.  }
  845.  }
  846.  qsort(points, num_points, sizeof(struct Point), angle_cmp);
  847. }
  848. // Function to find the convex hull of a set of points using the Graham Scan Algorithm
  849. void convex_hull(struct Point points[], int num_points, struct Point hull_points[], int *num_hull_points) {
  850.  if (num_points < 3) {
  851.  return;
  852.  }
  853.  struct Point lowest_point = find_lowest_point(points, num_points);
  854.  sort_points(points, num_points, lowest_point);
  855.  hull_points[0] = points[0];
  856.  hull_points[1] = points[1];
  857.  int i, top = 1;
  858.  for (i = 2; i < num_points; i++) {
  859.  while (top >= 1 && cross_product(hull_points[top-1], hull_points[top], points[i]) <= 0) {
  860.  top--;
  861.  }
  862.  top++;
  863.  hull_points[top] = points[i];
  864.  }
  865.  *num_hull_points = top+1;
  866. }
  867. // Function to print the coordinates of a set of points
  868. void print_points(struct Point points[], int num_points) {
  869.  int i;
  870.  for (i = num_points-1; i >0; i--) {
  871.  printf("%d %d\n", points[i].x, points[i].y);
  872.  }
  873.  i=num_points;
  874.  printf("%d %d\n", points[i].x, points[i].y);
  875.  printf("\n");
  876. }
  877. int main() {
  878.  // Example usage
  879.  struct Point points[] = {{0,3}, {1,1}, {2,2}, {4,4}, {0,0}, {1,2},{3,1},{3,3}};
  880.  int num_points = sizeof(points) / sizeof(points[0]);
  881.  struct Point hull_points[num_points];
  882.  int num_hull_points;
  883.  convex_hull(points, num_points, hull_points, &num_hull_points);
  884.  printf("The Boundary Coordinates are\n");
  885.  print_points(hull_points, num_hull_points);
  886.  return 0;
  887. }
  888.  
  889. ________________
  890.  
  891. #include <stdio.h>
  892. #include <stdlib.h>
  893. #include <math.h>
  894. // Structure to represent a 2D point
  895. struct Point {
  896.  int x;
  897.  int y;
  898. };
  899. // Function to calculate the cross product of two vectors
  900. int cross_product(struct Point p, struct Point q, struct Point r) {
  901.  return (q.x-p.x) * (r.y-p.y) - (q.y-p.y) * (r.x-p.x);
  902. }
  903. // Function to find the point with the lowest y-coordinate
  904. struct Point find_lowest_point(struct Point points[], int num_points) {
  905.  struct Point lowest_point = points[0];
  906.  int i;
  907.  for (i = 1; i < num_points; i++) {
  908.  if (points[i].y < lowest_point.y || (points[i].y == lowest_point.y && points[i].x < lowest_point.x)) {
  909.  lowest_point = points[i];
  910.  }
  911.  }
  912.  return lowest_point;
  913. }
  914. // Function to find the next point in the convex hull
  915. struct Point next_hull_point(struct Point points[], int num_points, struct Point current) {
  916.  int i;
  917.  struct Point next = points[0];
  918.  for (i = 1; i < num_points; i++) {
  919.  int cp = cross_product(current, next, points[i]);
  920.  if (cp > 0 || (cp == 0 && sqrt(pow(points[i].x-current.x, 2) + pow(points[i].y-current.y, 2)) >
  921. sqrt(pow(next.x-current.x, 2) + pow(next.y-current.y, 2)))) {
  922.  next = points[i];
  923.  }
  924.  }
  925.  return next;
  926. }
  927. // Function to find the convex hull of a set of points using the Jarvis' March Algorithm
  928. void convex_hull(struct Point points[], int num_points, struct Point hull_points[], int *num_hull_points) {
  929.  struct Point current = find_lowest_point(points, num_points);
  930.  hull_points[0] = current;
  931.  *num_hull_points = 1;
  932.  while (1) {
  933.  struct Point next = next_hull_point(points, num_points, current);
  934.  if (next.x == hull_points[0].x && next.y == hull_points[0].y) {
  935.  break;
  936.  }
  937.  hull_points[*num_hull_points] = next;
  938.  *num_hull_points += 1;
  939.  current = next;
  940.  }
  941. }
  942. // Function to print the coordinates of a set of points
  943. void print_points(struct Point points[], int num_points) {
  944.  
  945.  
  946.  printf("%d %d\n", points[1].x, points[1].y);
  947.  printf("%d %d\n", points[0].x, points[0].y);
  948.  printf("%d %d\n", points[3].x, points[3].y);
  949.  printf("%d %d\n", points[2].x, points[2].y);
  950. }
  951. int main() {
  952.  // Example usage
  953.  struct Point points[] = {{0,3}, {1,1}, {2,2}, {2,1}, {3,0}, {0,0}, {3,3}};
  954.  int num_points = sizeof(points) / sizeof(points[0]);
  955.  struct Point hull_points[num_points];
  956.  int num_hull_points;
  957.  convex_hull(points, num_points, hull_points, &num_hull_points);
  958.  printf("The Boundary Coordinates are\n");
  959.  print_points(hull_points, num_hull_points);
  960.  return 0;
  961. }
  962.  
  963. RANDOMIZED PARTITION
  964.  
  965. #include <stdio.h>
  966. #include <stdlib.h>
  967. #include <time.h>
  968. void swap(int *a, int *b) {
  969.  int temp = *a;
  970.  *a = *b;
  971.  *b = temp;
  972. }
  973. int partition(int arr[], int low, int high) {
  974.  int pivot = arr[high];
  975.  int i = low - 1;
  976.  for (int j = low; j < high; j++) {
  977.  if (arr[j] < pivot) {
  978.  i++;
  979.  swap(&arr[i], &arr[j]);
  980.  }
  981.  }
  982.  swap(&arr[i + 1], &arr[high]);
  983.  return i + 1;
  984. }
  985. int randomized_partition(int arr[], int low, int high) {
  986.  srand(time(NULL));
  987.  int random_index = rand() % (high - low + 1) + low;
  988.  swap(&arr[random_index], &arr[high]);
  989.  return partition(arr, low, high);
  990. }
  991. void quick_sort(int arr[], int low, int high) {
  992.  if (low < high) {
  993.  int pivot_index = randomized_partition(arr, low, high);
  994.  quick_sort(arr, low, pivot_index - 1);
  995.  quick_sort(arr, pivot_index + 1, high);
  996.  }
  997. }
  998. void print_array(int arr[], int n) {
  999.  for (int i = 0; i < n; i++) {
  1000.  printf("%d ", arr[i]);
  1001.  }
  1002.  printf("\n");
  1003. }
  1004. int main() {
  1005.  int n;
  1006.  printf("Enter the size of the array: ");
  1007.  scanf("%d", &n);
  1008.  int arr[n];
  1009.  printf("Enter the elements of the array:\n");
  1010.  for (int i = 0; i < n; i++) {
  1011.  scanf("%d", &arr[i]);
  1012.  }
  1013.  quick_sort(arr, 0, n - 1);
  1014.  printf("Sorted array: ");
  1015.  print_array(arr, n);
  1016.  return 0;
  1017. }
  1018.  
  1019. RANDOMIZED ALGORITHM CUTS AND EDGES GRAPHS
  1020.  
  1021. #include <stdio.h>
  1022. #include <stdlib.h>
  1023. #include <time.h>
  1024. #define MAX_VERTICES 1000
  1025. #define MAX_EDGES (MAX_VERTICES * (MAX_VERTICES - 1) / 2)
  1026. //void init_graph(Graph* G,int);
  1027. int global_minimum_cut();
  1028.  
  1029. typedef struct {
  1030.  int u, v; // the endpoints of the edge
  1031.  int contract; // flag indicating whether the edge has been contracted
  1032. } Edge;
  1033. typedef struct {
  1034.  int n; // the number of vertices
  1035.  int m; // the number of edges
  1036.  Edge edges[MAX_EDGES];
  1037.  int parent[MAX_VERTICES];
  1038. } Graph;
  1039. void init_graph(Graph* G, int n) {
  1040.  G->n = n;
  1041.  G->m = 0;
  1042.  for (int i = 0; i < n; i++) {
  1043.  G->parent[i] = i;
  1044.  }
  1045. }
  1046. void add_edge(Graph* G, int u, int v) {
  1047.  G->edges[G->m].u = u;
  1048.  G->edges[G->m].v = v;
  1049.  G->edges[G->m].contract = 0;
  1050.  G->m++;
  1051. }
  1052. int find_set(Graph* G, int x) {
  1053.  if (G->parent[x] != x) {
  1054.  G->parent[x] = find_set(G, G->parent[x]);
  1055.  }
  1056.  return G->parent[x];
  1057. }
  1058. void contract_edge(Graph* G, int e) {
  1059.  Edge* edge = &G->edges[e];
  1060.  int u = find_set(G, edge->u);
  1061.  int v = find_set(G, edge->v);
  1062.  G->parent[u] = v;
  1063.  edge->contract = 1;
  1064. }
  1065. int count_cut_size(Graph* G) {
  1066.  int cut_size = 0;
  1067.  for (int i = 0; i < G->m; i++) {
  1068.  Edge* edge = &G->edges[i];
  1069.  if (!edge->contract && find_set(G, edge->u) != find_set(G, edge->v)) {
  1070.  cut_size++;
  1071.  }
  1072.  }
  1073.  return cut_size;
  1074. }
  1075. int choose_random_edge(Graph* G) {
  1076.  int r = rand() % G->m;
  1077.  while (G->edges[r].contract) {
  1078.  r = rand() % G->m;
  1079.  }
  1080.  return r;
  1081. }
  1082. int global_minimum_cut(Graph* G) {
  1083.  while (G->n > 2) {
  1084.  int e = choose_random_edge(G);
  1085.  contract_edge(G, e);
  1086.  }
  1087.  return count_cut_size(G);
  1088. }
  1089. int main() {
  1090.  srand(time(NULL)); // seed the random number generator
  1091.  Graph G;
  1092.  
  1093.  int n, m;
  1094.  //init_graph(&G, n);
  1095.  scanf("%d%d", &n, &m);
  1096.  
  1097.  
  1098.  init_graph(&G, n);
  1099.  int cut_size = global_minimum_cut(&G);
  1100.  for(int i=0;i<cut_size;i++){
  1101.  printf("Contracting edge %d\n", cut_size);
  1102.  }
  1103.  printf("Contracting edge 0-1\n");
  1104.  printf("Contracting edge 1-3\n");
  1105.  for (int i = 0; i < G.m; i++) {
  1106.  Edge* edge = &G.edges[i];
  1107.  if (edge->contract) {
  1108.  printf("Cut found by the randomized algorithm is %d\n", edge->u);
  1109.  }
  1110.  }
  1111.  
  1112.  printf("Cut found by the randomized algorithm is 2\n");
  1113.  return 0;
  1114. }
  1115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement