-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcsr.go
More file actions
297 lines (241 loc) · 7.55 KB
/
csr.go
File metadata and controls
297 lines (241 loc) · 7.55 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package container
import (
"github.com/specterops/dawgs/cardinality"
"github.com/specterops/dawgs/graph"
)
// csrDigraph implements a mutable directed graph using compressed sparse row storage.
type csrDigraph struct {
// Mapping between external IDs and dense CSR indices
idToDenseIdx map[uint64]uint64 // external ID → dense index
denseIdxToID []uint64 // dense index → external ID
// CSR storage for outgoing edges
outOffsets []uint64 // length = NumNodes()+1
outAdj []uint64 // concatenated outgoing neighbour IDs (external IDs)
// CSR storage for incoming edges
inOffsets []uint64
inAdj []uint64
}
// idx returns the dense CSR index for an external node ID. Returns (idx, true) if the node exists, otherwise (0, false).
func (s *csrDigraph) idx(node uint64) (uint64, bool) {
idx, exists := s.idToDenseIdx[node]
return idx, exists
}
// csrRange returns the slice of neighbours for a given vertex index and direction. The returned slice contains external IDs.
func (s *csrDigraph) csrRange(idx uint64, dir graph.Direction) []uint64 {
switch dir {
case graph.DirectionOutbound:
var (
start = s.outOffsets[idx]
end = s.outOffsets[idx+1]
)
return s.outAdj[start:end]
case graph.DirectionInbound:
var (
start = s.inOffsets[idx]
end = s.inOffsets[idx+1]
)
return s.inAdj[start:end]
default:
var (
outStart, outEnd = s.outOffsets[idx], s.outOffsets[idx+1]
inStart, inEnd = s.inOffsets[idx], s.inOffsets[idx+1]
)
merged := make([]uint64, 0, (outEnd-outStart)+(inEnd-inStart))
merged = append(merged, s.outAdj[outStart:outEnd]...)
merged = append(merged, s.inAdj[inStart:inEnd]...)
return merged
}
}
func (s *csrDigraph) NumNodes() uint64 {
return uint64(len(s.denseIdxToID))
}
func (s *csrDigraph) NumEdges() uint64 {
// each directed edge appears once in outAdj
return uint64(len(s.outAdj))
}
func (s *csrDigraph) ContainsNode(node uint64) bool {
for _, storedNode := range s.denseIdxToID {
if node == storedNode {
return true
}
}
return false
}
func (s *csrDigraph) EachNode(delegate func(node uint64) bool) {
for _, id := range s.denseIdxToID {
if !delegate(id) {
return
}
}
}
func (s *csrDigraph) AdjacentNodes(node uint64, direction graph.Direction) []uint64 {
if idx, ok := s.idx(node); ok {
return s.csrRange(idx, direction)
}
return nil
}
func (s *csrDigraph) Degrees(node uint64, direction graph.Direction) uint64 {
if idx, ok := s.idx(node); ok {
switch direction {
case graph.DirectionOutbound:
return s.outOffsets[idx+1] - s.outOffsets[idx]
case graph.DirectionInbound:
return s.inOffsets[idx+1] - s.inOffsets[idx]
case graph.DirectionBoth:
return (s.outOffsets[idx+1] - s.outOffsets[idx]) + (s.inOffsets[idx+1] - s.inOffsets[idx])
}
}
return 0
}
func (s *csrDigraph) EachAdjacentNode(node uint64, direction graph.Direction, delegate func(adjacent uint64) bool) {
if idx, exists := s.idx(node); exists {
switch direction {
case graph.DirectionOutbound:
for next, end := s.outOffsets[idx], s.outOffsets[idx+1]; next < end; next++ {
if !delegate(s.outAdj[next]) {
return
}
}
case graph.DirectionInbound:
for next, end := s.inOffsets[idx], s.inOffsets[idx+1]; next < end; next++ {
if !delegate(s.inAdj[next]) {
return
}
}
default:
for next, end := s.outOffsets[idx], s.outOffsets[idx+1]; next < end; next++ {
if !delegate(s.outAdj[next]) {
return
}
}
for next, end := s.inOffsets[idx], s.inOffsets[idx+1]; next < end; next++ {
if !delegate(s.inAdj[next]) {
return
}
}
}
}
}
func (s *csrDigraph) Normalize() ([]uint64, DirectedGraph) {
// Reverse map: denseIdx → original ID
reverse := make([]uint64, len(s.denseIdxToID))
copy(reverse, s.denseIdxToID)
// Build a new CSR graph where the external IDs are the dense indices
newGraph := &csrDigraph{
idToDenseIdx: make(map[uint64]uint64, len(s.denseIdxToID)),
denseIdxToID: make([]uint64, len(s.denseIdxToID)),
// Directly reuse the CSR structure (but translate neighbour IDs).
outOffsets: make([]uint64, len(s.outOffsets)),
inOffsets: make([]uint64, len(s.inOffsets)),
outAdj: make([]uint64, len(s.outAdj)),
inAdj: make([]uint64, len(s.inAdj)),
}
// Populate bitmap + identity maps for dense IDs.
for denseIdx := range s.denseIdxToID {
newGraph.idToDenseIdx[uint64(denseIdx)] = uint64(denseIdx)
newGraph.denseIdxToID[denseIdx] = uint64(denseIdx)
}
// Copy offsets (they are already correct because vertex ordering is identical).
copy(newGraph.outOffsets, s.outOffsets)
copy(newGraph.inOffsets, s.inOffsets)
// Translate neighbour IDs from original → dense.
for i, origNeighbour := range s.outAdj {
newGraph.outAdj[i] = s.idToDenseIdx[origNeighbour]
}
for i, origNeighbour := range s.inAdj {
newGraph.inAdj[i] = s.idToDenseIdx[origNeighbour]
}
return reverse, newGraph
}
type CSRDigraphBuilder struct {
idToDenseIdx map[uint64]uint64 // external ID → dense index
denseIdxToID []uint64 // dense index → external ID
outTmp AdjacencyMap
inTmp AdjacencyMap
}
func NewCSRDigraphBuilder() DigraphBuilder {
return &CSRDigraphBuilder{
idToDenseIdx: make(map[uint64]uint64),
outTmp: AdjacencyMap{},
inTmp: AdjacencyMap{},
}
}
// ensureNode registers a vertex if it does not already exist. Returns the dense CSR index for the given external ID.
func (s *CSRDigraphBuilder) ensureNode(id uint64) uint64 {
if idx, ok := s.idToDenseIdx[id]; ok {
return idx
}
idx := uint64(len(s.denseIdxToID))
s.idToDenseIdx[id] = idx
s.denseIdxToID = append(s.denseIdxToID, id)
// Allocate empty adjacency sets for the builder.
s.outTmp[idx] = cardinality.NewBitmap64()
s.inTmp[idx] = cardinality.NewBitmap64()
return idx
}
func (s *CSRDigraphBuilder) AddNode(node uint64) {
s.ensureNode(node)
}
func (s *CSRDigraphBuilder) AddEdge(start, end uint64) {
var (
startIdx = s.ensureNode(start)
endIdx = s.ensureNode(end)
)
// Outgoing edge
if existingBitmap, exists := s.outTmp[startIdx]; exists {
existingBitmap.Add(endIdx)
} else {
s.outTmp[startIdx] = cardinality.NewBitmap64With(endIdx)
}
// Incoming edge
if existingBitmap, exists := s.inTmp[endIdx]; exists {
existingBitmap.Add(startIdx)
} else {
s.inTmp[endIdx] = cardinality.NewBitmap64With(startIdx)
}
}
func (s *CSRDigraphBuilder) Build() DirectedGraph {
numNodes := uint64(len(s.denseIdxToID))
// Allocate offset slices (len = numNodes+1)
var (
outOffsets = make([]uint64, numNodes+1)
inOffsets = make([]uint64, numNodes+1)
)
// Compute prefix sums (total edge counts per vertex)
var outTotal, inTotal uint64
for nextNode := uint64(0); nextNode < numNodes; nextNode++ {
outTotal += s.outTmp[nextNode].Cardinality()
inTotal += s.inTmp[nextNode].Cardinality()
outOffsets[nextNode+1] = outTotal
inOffsets[nextNode+1] = inTotal
}
// Allocate and fill adjacency arrays
var (
outAdj = make([]uint64, outTotal)
inAdj = make([]uint64, inTotal)
)
for nextNode := uint64(0); nextNode < numNodes; nextNode++ {
var (
outDenseIdx = outOffsets[nextNode]
inDenseIdx = inOffsets[nextNode]
)
s.outTmp[nextNode].Each(func(adjacentDenseIndex uint64) bool {
outAdj[outDenseIdx] = s.denseIdxToID[adjacentDenseIndex]
outDenseIdx++
return true
})
s.inTmp[nextNode].Each(func(adjacentDenseIndex uint64) bool {
inAdj[inDenseIdx] = s.denseIdxToID[adjacentDenseIndex]
inDenseIdx++
return true
})
}
return &csrDigraph{
idToDenseIdx: s.idToDenseIdx,
denseIdxToID: s.denseIdxToID,
outOffsets: outOffsets,
outAdj: outAdj,
inOffsets: inOffsets,
inAdj: inAdj,
}
}