Advertisement
xopsuei

randomize_Bipartition

Dec 12th, 2012
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. /**
  2.  * Outputs the distribution of the nodes of a graph in two parts randomly so that each side
  3.  * has the same ammount of nodes
  4.  *
  5.  * @param int  size the size of the graph
  6.  * @param vi&  v    the new distribution of the graph
  7.  *
  8.  * @returns void
  9.  *
  10.  */
  11. void randomize_Bipartition(int size, vi& v){
  12.     int threshold = (size%2 == 0) ? size/2 : size/2 + 1;
  13.     int r_size, l_size;
  14.     r_size = l_size = 0;
  15.  
  16.     v = vi(size);
  17.  
  18.     srand (time(NULL));
  19.  
  20.     int i = 0;
  21.     while(r_size < threshold and l_size < threshold){
  22.         if(rand()%2 == 0){
  23.             v[i] = 0;
  24.             ++r_size;
  25.         }
  26.         else{
  27.             v[i] = 1;
  28.             ++l_size;
  29.         }
  30.         ++i;
  31.     }
  32.  
  33.     if(r_size == threshold){
  34.         while(r_size + l_size != size){
  35.             v[i] == 1;
  36.             ++i;
  37.             ++l_size;
  38.         }
  39.     }
  40.     else{
  41.        while(r_size + l_size != size){
  42.             v[i] == 0;
  43.             ++i;
  44.             ++r_size;
  45.         }
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement