-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph_undirected_bitset.h
78 lines (63 loc) · 1.85 KB
/
graph_undirected_bitset.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef GRAPH_UNDIRECTED_BITSET
#define GRAPH_UNDIRECTED_BITSET
#include "boost.h"
class graph_undirected_bitset {
public:
graph_undirected_bitset(unsigned numNodes) {
graph = vector<boost::dynamic_bitset<unsigned long> >(numNodes, boost::dynamic_bitset<unsigned long>(numNodes));
numTotalEdges = 0;
}
inline unsigned get_bits_per_block() {
return graph[0].bits_per_block;
}
inline unsigned get_num_blocks() {
return graph[0].num_blocks();
}
inline void addEdge(unsigned node1, unsigned node2) {
graph[node1][node2] = 1;
graph[node2][node1] = 1;
++numTotalEdges;
}
inline unsigned numEdges() {
return numTotalEdges;
}
const boost::dynamic_bitset<> & getInteractors(unsigned nodeID) {
return graph[nodeID];
}
inline bool isEdge(unsigned node1, unsigned node2) {
return graph[node1].test(node2);
}
static void getIntersection(boost::dynamic_bitset<unsigned long> & set1, boost::dynamic_bitset<unsigned long> & set2, boost::dynamic_bitset<unsigned long> & result) {
result = set1;
result &= set2;
}
inline unsigned numNodes() {
return graph.size();
}
void printAll(vector<string> & nodeIDsToNames) {
cout << "@ PRINTING GRAPH" << endl;
unsigned numEdgesFound = 0;
for (unsigned i = 0; i < graph.size(); ++i) {
unsigned long j = graph[i].find_first();
while (j < i) {
j = graph[i].find_next(j);
}
if (j != graph[i].npos) {
cout << "@ " << nodeIDsToNames[i] << "\t" << nodeIDsToNames[j];
while ((j = graph[i].find_next(j)) < graph[i].npos) {
cout << "," << nodeIDsToNames[j];
++numEdgesFound;
}
cout << endl;
}
if (numEdgesFound == numTotalEdges) {
break;
}
}
cout << "@ DONE PRINTING GRAPH" << endl;
}
private:
vector<boost::dynamic_bitset<> > graph;
unsigned numTotalEdges;
};
#endif // GRAPH_UNDIRECTED_BITSET