Advertisement
Guest User

khotkin

a guest
Jan 24th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include "defs.h"
  2. #include <stack>
  3. #include <queue>
  4. #include <list>
  5. #include <cstdio>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. typedef stack<vertex_id_t> b_stack;
  11. typedef queue<vertex_id_t> b_queue;
  12. typedef list<vertex_id_t> b_list;
  13.  
  14. /* algorithm */
  15. void run(graph_t *G, double *result)
  16. {
  17.   //FIXME: Your solution
  18.    
  19.     /* result array */
  20.     vertex_id_t num = G->n;
  21.     for (vertex_id_t v = 0; v < num; ++v) {
  22.         result[v] = 0;
  23.     }
  24.    
  25.     /* participant vertices and graph indeces */
  26.     vertex_id_t V_vert;
  27.     vertex_id_t W_vert;
  28.     edge_id_t Vert_deg;
  29.     edge_id_t Vert_cur;
  30.    
  31.     /* count, dist, aux arrays */
  32.     int* count = new int[num];
  33.     int* dist = new int[num];
  34.     double* aux = new double[num];
  35.    
  36.     /* queue, stack and list */
  37.     b_stack b_S;
  38.     b_queue b_Q;
  39.     b_list* b__P = new b_list[num];
  40.    
  41.     /* main cycle */
  42.     for (vertex_id_t s = 0; s < num; ++s) {
  43.         /* set starting values */
  44.         for (vertex_id_t v = 0; v < num; ++v) {
  45.             count[v] = 0;
  46.             dist[v] = -1;
  47.             aux[v] = 0.0;
  48.         }
  49.         count[s] = 1;
  50.         dist[s] = 0;
  51.         b_Q.push(s);
  52.        
  53.         /* count shortest paths */
  54.         while (!b_Q.empty()) {
  55.             V_vert = b_Q.front();
  56.             b_Q.pop();
  57.             b_S.push(V_vert);
  58.            
  59.             Vert_cur = G->rowsIndices[V_vert];
  60.             Vert_deg = G->rowsIndices[V_vert + 1] - Vert_cur;
  61.             for(edge_id_t E = 0; E < Vert_deg; ++E) {
  62.                 /* next neighbour */
  63.                 W_vert = G->endV[Vert_cur + E];
  64.                
  65.                 /* vertex encountered for the 1st time */
  66.                 if (dist[W_vert] < 0) {
  67.                     b_Q.push(W_vert);
  68.                     dist[W_vert] = dist[V_vert] + 1;
  69.                 }
  70.                
  71.                 /* vertex provides the shortest path */
  72.                 if (dist[W_vert] == dist[V_vert] + 1) {
  73.                     count[W_vert] += count[V_vert];
  74.                     b__P[W_vert].push_back(V_vert);
  75.                 }
  76.             }
  77.         }
  78.        
  79.         /* accumulate */
  80.         while (!b_S.empty()) {
  81.             W_vert = b_S.top();
  82.             b_S.pop();
  83.            
  84.             /* change aux according to list */
  85.             while(!b__P[W_vert].empty()) {
  86.                 V_vert = b__P[W_vert].front();
  87.                 aux[V_vert] += (1.0 + aux[W_vert]) * (double(count[V_vert]) / double(count[W_vert]));
  88.                 b__P[W_vert].pop_front();
  89.             }
  90.            
  91.             /* change betweeness centrality */
  92.             if (W_vert != s) {
  93.                 result[W_vert] += aux[W_vert];
  94.             }
  95.         }
  96.  
  97.         if (!b_Q.empty()) std::cout << "QUEUE!\n";
  98.         if (!b_S.empty()) std::cout << "STACK!\n";
  99.         for (vertex_id_t v = 0; v < num; ++v) {
  100.             if(!b__P[v].empty()) std::cout << "LIST " << v << " !\n";
  101.         }
  102.     }
  103.    
  104.     /* free memory */
  105.     delete aux;
  106.     delete dist;
  107.     delete count;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement