Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. template <typename _TVertexValue, typename _TEdgeWeight>
  2. void GraphGenerationAPI<_TVertexValue, _TEdgeWeight>::R_MAT(EdgesListGraph<_TVertexValue, _TEdgeWeight> &_graph,
  3.                                                             int _vertices_count, long long _edges_count,
  4.                                                             int _a_prob, int _b_prob, int _c_prob,
  5.                                                             int _d_prob, DirectionType _direction_type)
  6. {
  7.     int n = (int)log2(_vertices_count);
  8.     int vertices_count = _vertices_count;
  9.     long long edges_count = _edges_count;
  10.    
  11.     int step = 1;
  12.     if(_direction_type)
  13.     {
  14.         _graph.resize(vertices_count, edges_count);
  15.     }
  16.     else
  17.     {
  18.         step = 2;
  19.         _graph.resize(vertices_count, 2*edges_count);
  20.     }
  21.    
  22.     int *src_ids = _graph.get_src_ids();
  23.     int *dst_ids = _graph.get_dst_ids();
  24.     _TEdgeWeight *weights = _graph.get_weights();
  25.    
  26.     RandomGenerationAPI rng_api;
  27.     rng_api.generate_array_of_random_values<_TVertexValue>(_graph.get_vertex_values(), vertices_count, 1000);
  28.    
  29.     int threads_count = omp_get_max_threads();
  30.    
  31.     // generate and add edges to graph
  32.     unsigned int seed = 0;
  33.     #pragma omp parallel private(seed) num_threads(threads_count)
  34.     {
  35.         seed = int(time(NULL)) * omp_get_thread_num();
  36.        
  37.         #pragma omp for schedule(static)
  38.         for (long long cur_edge = 0; cur_edge < edges_count; cur_edge += step)
  39.         {
  40.             int x_middle = _vertices_count / 2, y_middle = _vertices_count / 2;
  41.             for (long long i = 1; i < n; i++)
  42.             {
  43.                 int a_beg = 0, a_end = _a_prob;
  44.                 int b_beg = _a_prob, b_end = b_beg + _b_prob;
  45.                 int c_beg = _a_prob + _b_prob, c_end = c_beg + _c_prob;
  46.                 int d_beg = _a_prob + _b_prob + _c_prob, d_end = d_beg + _d_prob;
  47.                
  48.                 int step = (int)pow(2, n - (i + 1));
  49.                
  50.                 int probability = rand_r(&seed) % 100;
  51.                 if (a_beg <= probability && probability < a_end)
  52.                 {
  53.                     x_middle -= step, y_middle -= step;
  54.                 }
  55.                 else if (b_beg <= probability && probability < b_end)
  56.                 {
  57.                     x_middle -= step, y_middle += step;
  58.                 }
  59.                 else if (c_beg <= probability && probability < c_end)
  60.                 {
  61.                     x_middle += step, y_middle -= step;
  62.                 }
  63.                 else if (d_beg <= probability && probability < d_end)
  64.                 {
  65.                     x_middle += step, y_middle += step;
  66.                 }
  67.             }
  68.             if (rand_r(&seed) % 2 == 0)
  69.                 x_middle--;
  70.             if (rand_r(&seed) % 2 == 0)
  71.                 y_middle--;
  72.            
  73.             int from = x_middle;
  74.             int to = y_middle;
  75.             _TEdgeWeight edge_weight = (rand_r(&seed) % 10) + static_cast <float> (rand_r(&seed)) / static_cast <float> (RAND_MAX);
  76.            
  77.             src_ids[cur_edge] = from;
  78.             dst_ids[cur_edge] = to;
  79.             weights[cur_edge] = edge_weight;
  80.            
  81.             if(!_direction_type)
  82.             {
  83.                 src_ids[cur_edge + 1] = to;
  84.                 dst_ids[cur_edge + 1] = from;
  85.                 weights[cur_edge + 1] = edge_weight;
  86.             }
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement