Advertisement
coffeebeforecode

vertex_cover_bruteforce.c

Dec 8th, 2021
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include <stdbool.h>
  5. #include <math.h>
  6.  
  7. bool checkVertexCover(int ** G, int *S, int n){
  8.     int i, j;
  9.     int **Gdash = (int**)malloc(sizeof(int*)*n);
  10.     for(i = 0; i < n; i++){
  11.         Gdash[i] = (int*)malloc(sizeof(int)*n);
  12.  
  13.         for(j = 0; j < n; j++){
  14.             Gdash[i][j] = G[i][j];
  15.         }
  16.     }
  17.  
  18.     for(i = 0; i < n; i++){
  19.         if (S[i] == 1){
  20.             for (j = 0; j < n; j++){
  21.                 Gdash[i][j] = 0;
  22.                 Gdash[j][i] = 0;
  23.             }
  24.         }
  25.     }
  26.  
  27.     bool cover = true;
  28.  
  29.     for(i = 0; i < n; i++){
  30.         for(j = 0; j < n; j++){
  31.             if (Gdash[i][j] == 1){
  32.                 cover = false;
  33.                 break;
  34.             }
  35.         }
  36.         if (cover == false){
  37.             break;
  38.         }
  39.     }
  40.  
  41.     return cover;
  42. }
  43.  
  44.  
  45. void vertexCoverBruteforce(int **G, int set_size)
  46. {
  47.     unsigned int pow_set_size = pow(2, set_size);
  48.     int counter, j;
  49.     /*
  50.     int* set = (int*)malloc(sizeof(int)*set_size);
  51.  
  52.     for(j = 0; j < set_size; j++){
  53.         set[j] = j;
  54.     }
  55.     */
  56.     int curr_min = INT_MAX;
  57.     //int* curr_best = (int*)malloc(sizeof(int)*set_size);
  58.     int curr_best;
  59.     int* check = (int*)malloc(sizeof(int)*set_size);
  60.     int temp;
  61.  
  62.     bool isVertex;
  63.  
  64.     /*Run from counter 000..0 to 111..1*/
  65.     for(counter = 0; counter < pow_set_size; counter++)
  66.     {
  67.         temp = 0;
  68.         for(j = 0; j < set_size; j++){
  69.             /* Check if jth bit in the counter is set
  70.             If set then print jth element from set */
  71.            
  72.             if(counter & (1<<j)){
  73.                 check[j] = 1;
  74.                 temp += 1;
  75.             }
  76.             else{
  77.                 check[j] = 0;
  78.             }      
  79.         }
  80.  
  81.         if (temp < curr_min){
  82.             isVertex = checkVertexCover(G, check, set_size);
  83.             if (isVertex){
  84.                 curr_best = counter;
  85.                 curr_min = temp;
  86.             }
  87.         }
  88.     }
  89.  
  90.     for(j = 0; j < set_size; j++){
  91.         /* Check if jth bit in the counter is set
  92.         If set then print jth element from set */
  93.        
  94.         if(curr_best & (1<<j)){
  95.             printf("1 ");
  96.         }
  97.         else{
  98.             printf("0 ");
  99.         }
  100.     }
  101.  
  102. }
  103.  
  104. void solve(){
  105.     int n; // number of vertex
  106.     scanf("%d", &n);
  107.  
  108.     int **G = (int**)malloc(sizeof(int*)*n);
  109.     int i, j;
  110.     for(i = 0; i < n; i++){
  111.         G[i] = (int*)malloc(sizeof(int)*n);
  112.     }
  113.  
  114.     for(i = 0; i < n; i++){
  115.         for(j = 0; j < n; j++){
  116.             scanf("%d", &G[i][j]);
  117.         }
  118.     }
  119.  
  120.     vertexCoverBruteforce(G, n);
  121.  
  122. }
  123. int main(){
  124.     solve();
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement