forked from realstolz/polymer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.h
164 lines (157 loc) · 6.08 KB
/
graph.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "parallel.h"
using namespace std;
// **************************************************************
// ADJACENCY ARRAY REPRESENTATION
// **************************************************************
struct symmetricVertex {
intE* neighbors;
intT degree;
intT fakeDegree;
void del() {free(neighbors); }
symmetricVertex(intE* n, intT d) : neighbors(n), degree(d) {}
uintE getInNeighbor(intT j) { return neighbors[j]; }
uintE getOutNeighbor(intT j) { return neighbors[j]; }
intE* getInNeighborPtr() { return neighbors;}
intE* getOutNeighborPtr() { return neighbors;}
intT getInDegree() { return degree; }
intT getOutDegree() { return degree; }
intT getFakeInDegree() { return fakeDegree;}
intT getFakeDegree() { return fakeDegree; }
void setInNeighbors(intE* _i) { neighbors = _i; }
void setOutNeighbors(intE* _i) { neighbors = _i; }
void setInDegree(intT _d) { degree = _d; }
void setOutDegree(intT _d) { degree = _d; }
void setFakeDegree(intT _d) { fakeDegree = _d; }
void setFakeInDegree(intT _d) { fakeDegree = _d; }
void flipEdges() {}
};
struct asymmetricVertex {
intE* inNeighbors;
intE* outNeighbors;
intT outDegree;
intT fakeInDegree;
intT fakeOutDegree;
intT inDegree;
void del() {free(inNeighbors); free(outNeighbors);}
asymmetricVertex(intE* iN, intE* oN, intT id, intT od) : inNeighbors(iN), outNeighbors(oN), inDegree(id), outDegree(od) {}
uintE getInNeighbor(intT j) { return inNeighbors[j]; }
uintE getOutNeighbor(intT j) { return outNeighbors[j]; }
intE* getInNeighborPtr() { return inNeighbors;}
intE* getOutNeighborPtr() { return outNeighbors;}
intT getInDegree() { return inDegree; }
intT getOutDegree() { return outDegree; }
intT getFakeInDegree() { return fakeInDegree;}
intT getFakeDegree() { return fakeOutDegree; }
void setInNeighbors(intE* _i) { inNeighbors = _i; }
void setOutNeighbors(intE* _i) { outNeighbors = _i; }
void setInDegree(intT _d) { inDegree = _d; }
void setOutDegree(intT _d) { outDegree = _d; }
void setFakeDegree(intT _d) { fakeOutDegree = _d; }
void setFakeInDegree(intT _d) { fakeInDegree = _d; }
void flipEdges() { swap(inNeighbors,outNeighbors); swap(inDegree,outDegree); }
};
template <class vertex>
struct graph {
vertex *V;
intT n;
uintT m;
intE* allocatedInplace;
intE* inEdges;
intT* flags;
graph(vertex* VV, intT nn, uintT mm)
: V(VV), n(nn), m(mm), allocatedInplace(NULL), flags(NULL) {}
graph(vertex* VV, intT nn, uintT mm, intE* ai, intE* _inEdges = NULL)
: V(VV), n(nn), m(mm), allocatedInplace(ai), inEdges(_inEdges), flags(NULL) {}
void del() {
if (flags != NULL) free(flags);
if (allocatedInplace == NULL)
for (intT i=0; i < n; i++) V[i].del();
else free(allocatedInplace);
free(V);
if(inEdges != NULL) free(inEdges);
}
void transpose() {
if(sizeof(vertex) == sizeof(asymmetricVertex)) {
parallel_for(intT i=0;i<n;i++) {
V[i].flipEdges();
}
}
}
};
struct symmetricWghVertex {
//weights are stored in the entry after the neighbor ID
//so size of neighbor list is twice the degree
intE* neighbors;
intT degree;
intT fakeDegree;
void del() {free(neighbors);}
symmetricWghVertex(intE* n, intT d) : neighbors(n), degree(d) {}
intE getInNeighbor(intT j) { return neighbors[2*j]; }
intE getOutNeighbor(intT j) { return neighbors[2*j]; }
intE* getInNeighborPtr() { return neighbors;}
intE* getOutNeighborPtr() { return neighbors;}
intE getInWeight(intT j) { return neighbors[2*j+1]; }
intE getOutWeight(intT j) { return neighbors[2*j+1]; }
intT getInDegree() { return degree; }
intT getOutDegree() { return degree; }
intT getFakeDegree() { return fakeDegree; }
intT getFakeInDegree() { return fakeDegree; }
void setInNeighbors(intE* _i) { neighbors = _i; }
void setOutNeighbors(intE* _i) { neighbors = _i; }
void setInDegree(intT _d) { degree = _d; }
void setOutDegree(intT _d) { degree = _d; }
void setFakeDegree(intT _d) { fakeDegree = _d; }
void setFakeInDegree(intT _d) { fakeDegree = _d; }
};
struct asymmetricWghVertex {
//weights are stored in the entry after the neighbor ID
//so size of neighbor list is twice the degree
intE* inNeighbors;
intE* outNeighbors;
intT inDegree;
intT outDegree;
intT fakeOutDegree;
intT fakeInDegree;
void del() {free(inNeighbors); free(outNeighbors);}
asymmetricWghVertex(intE* iN, intE* oN, intT id, intT od) : inNeighbors(iN), outNeighbors(oN), inDegree(id), outDegree(od) {}
intE getInNeighbor(intT j) { return inNeighbors[2*j]; }
intE getOutNeighbor(intT j) { return outNeighbors[2*j]; }
intE* getInNeighborPtr() { return inNeighbors;}
intE* getOutNeighborPtr() { return outNeighbors;}
intE getInWeight(intT j) { return inNeighbors[2*j+1]; }
intE getOutWeight(intT j) { return outNeighbors[2*j+1]; }
intT getInDegree() { return inDegree; }
intT getOutDegree() { return outDegree; }
intT getFakeDegree() { return fakeOutDegree; }
intT getFakeInDegree() { return fakeInDegree; }
void setInNeighbors(intE* _i) { inNeighbors = _i; }
void setOutNeighbors(intE* _i) { outNeighbors = _i; }
void setInDegree(intT _d) { inDegree = _d; }
void setOutDegree(intT _d) { outDegree = _d; }
void setFakeDegree(intT _d) { fakeOutDegree = _d; }
void setFakeInDegree(intT _d) { fakeInDegree = _d; }
};
template <class vertex>
struct wghGraph {
vertex *V;
intT n;
uintT m;
intE* allocatedInplace;
intE* inEdges;
intT* flags;
wghGraph(vertex* VV, intT nn, uintT mm)
: V(VV), n(nn), m(mm), allocatedInplace(NULL), flags(NULL) {}
wghGraph(vertex* VV, intT nn, uintT mm, intE* ai, intE* _inEdges=NULL)
: V(VV), n(nn), m(mm), allocatedInplace(ai), inEdges(_inEdges), flags(NULL) {}
void del() {
if(flags != NULL) free(flags);
if (allocatedInplace == NULL)
for (intT i=0; i < n; i++) V[i].del();
else { free(allocatedInplace); }
free(V);
if(inEdges != NULL) free(inEdges);
}
};