1+ #include " community.h"
2+
3+ Community::Community (Graph*& main_graph, unsigned int depth_best) : main_graph(main_graph), depth_best(depth_best)
4+ {
5+ index_community = 0 ;
6+ }
7+
8+ Community::~Community ()
9+ {
10+ for (auto & e : map_similar_community)
11+ delete e.second ;
12+ }
13+
14+ void Community::detect (Graph*& graph_projection, vector<unsigned int >& list_ending_nodes)
15+ {
16+ vector<unsigned int > list_index_nodes;
17+ unordered_set<unsigned int > list_remaining_nodes;
18+ unsigned int index = 0 ;
19+
20+ while (index < graph_projection->size_list_nodes )
21+ {
22+ if (graph_projection->list_nodes [index]->nb_neighbors == 0 )
23+ list_ending_nodes.push_back (graph_projection->list_nodes [index]->main_index );
24+ else
25+ list_index_nodes.push_back (index);
26+
27+ index += graph_projection->list_nodes [index]->nb_neighbors + 1 ;
28+ }
29+
30+ if (list_index_nodes.size () > 0 )
31+ {
32+ cycles (graph_projection, list_index_nodes, list_remaining_nodes);
33+ aggregate (graph_projection, list_remaining_nodes, list_ending_nodes);
34+ }
35+
36+ save_communities (graph_projection);
37+ map_community.clear ();
38+ }
39+
40+ void Community::cycles (Graph*& graph_projection, vector<unsigned int >& list_index_nodes, unordered_set<unsigned int >& list_remaining_nodes)
41+ {
42+ vector<unsigned int > list_visited_nodes;
43+
44+ unsigned int current, current_tmp, most_similar;
45+ vector<unsigned int >::iterator it_current, it_list_visited, it_list_index;
46+ vector<unsigned int >* vec_depth;
47+ int h;
48+
49+ unordered_map<unsigned int , vector<unsigned int >*> map_cycle_nodes;
50+ for (auto & index : list_index_nodes)
51+ {
52+ map_cycle_nodes[index] = new vector<unsigned int >;
53+ vec_depth = map_cycle_nodes[index];
54+
55+ if (depth_best > graph_projection->list_nodes [index]->nb_neighbors )
56+ vec_depth->push_back (graph_projection->list_nodes [index]->nb_neighbors );
57+ else
58+ vec_depth->push_back (depth_best);
59+
60+ vec_depth->push_back (0 );
61+ }
62+
63+ srand (unsigned (time (NULL )));
64+
65+ int i = rand () % list_index_nodes.size ();
66+ bool new_node = true ;
67+
68+ do
69+ {
70+ if (new_node)
71+ {
72+ list_visited_nodes.clear ();
73+ i = rand () % list_index_nodes.size ();
74+ current = list_index_nodes[i];
75+ new_node = false ;
76+ }
77+
78+ list_visited_nodes.push_back (current);
79+
80+ most_similar = graph_projection->list_nodes [current+map_cycle_nodes[current]->at (1 )+1 ]->index ;
81+
82+ it_list_visited = find (list_visited_nodes.begin (), list_visited_nodes.end (), most_similar);
83+ it_list_index = find (list_index_nodes.begin (), list_index_nodes.end (), most_similar);
84+
85+ if (it_list_visited == list_visited_nodes.end () && it_list_index != list_index_nodes.end ())
86+ current = most_similar;
87+ else
88+ {
89+ if (it_list_visited != list_visited_nodes.end ())
90+ {
91+ while (it_list_visited != list_visited_nodes.end ())
92+ {
93+ map_community[*it_list_visited].push_back (index_community);
94+ ++it_list_visited;
95+ }
96+
97+ index_community++;
98+ }
99+
100+ for (auto & index : list_visited_nodes)
101+ {
102+ vec_depth = map_cycle_nodes[index];
103+ vec_depth->at (1 )++;
104+
105+ if (vec_depth->at (1 ) == vec_depth->at (0 ))
106+ {
107+ if (map_community.find (index) == map_community.end ())
108+ list_remaining_nodes.insert (index);
109+
110+ list_index_nodes.erase (remove (list_index_nodes.begin (), list_index_nodes.end (), index));
111+ }
112+ }
113+
114+ new_node = true ;
115+ }
116+
117+ } while (list_index_nodes.size () > 0 );
118+
119+ for (auto & index : list_index_nodes)
120+ delete map_cycle_nodes[index];
121+ }
122+
123+ void Community::aggregate (Graph*& graph_projection, unordered_set<unsigned int >& list_remaining_nodes, vector<unsigned int >& list_ending_nodes)
124+ {
125+ unordered_map<unsigned int , float >* map_similar_community_tmp;
126+ vector<pair<unsigned int , unsigned int >> map_community_tmp;
127+ Node* node;
128+ unsigned int index_neighbor, i;
129+ float max;
130+ int most_similar_com;
131+
132+ for (auto & index : list_remaining_nodes)
133+ {
134+ node = graph_projection->list_nodes [index];
135+
136+ if (map_similar_community.find (node->main_index ) == map_similar_community.end ())
137+ map_similar_community[node->main_index ] = new unordered_map<unsigned int , float >();
138+ map_similar_community_tmp = map_similar_community[node->main_index ];
139+
140+ for (i=0 ; i<node->nb_neighbors ; i++)
141+ {
142+ if (map_community.find (graph_projection->list_nodes [index+i+1 ]->index ) == map_community.end ())
143+ continue ;
144+
145+ for (auto & index_com : map_community[graph_projection->list_nodes [index+i+1 ]->index ])
146+ {
147+ if (map_similar_community_tmp->find (index_com) == map_similar_community_tmp->end ())
148+ map_similar_community_tmp->insert (make_pair (index_com, 0.0 ));
149+ map_similar_community_tmp->at (index_com) += node->neighbor_weights [i];
150+ }
151+ }
152+
153+ max = 0.0 ;
154+ most_similar_com = -1 ;
155+
156+ for (auto & e : *map_similar_community_tmp)
157+ {
158+ if (e.second > max)
159+ {
160+ max = e.second ;
161+ most_similar_com = e.first ;
162+ }
163+ }
164+
165+ if (most_similar_com == -1 )
166+ list_ending_nodes.push_back (graph_projection->list_nodes [index]->main_index );
167+ else
168+ map_community_tmp.push_back (make_pair (index, most_similar_com));
169+ }
170+
171+ for (auto & e : map_community_tmp)
172+ map_community[e.first ].push_back (e.second );
173+ }
174+
175+ void Community::save_communities (Graph*& graph_projection)
176+ {
177+ for (auto & e : map_community)
178+ {
179+ for (auto & index_com : e.second )
180+ community[index_com].push_back (main_graph->list_nodes [graph_projection->list_nodes [e.first ]->main_index ]);
181+ }
182+ }
0 commit comments