-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs_stacked.go
More file actions
325 lines (257 loc) · 6.62 KB
/
dfs_stacked.go
File metadata and controls
325 lines (257 loc) · 6.62 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package kspa
import (
"container/list"
"fmt"
"math"
)
type DfsStacked struct {
deepLimit int
cycles []map[uint64]MEdgeSeq
}
func (st *DfsStacked) SetDeepLimit(v int) {
st.deepLimit = v
}
func (st *DfsStacked) TopK(g *MultiGraph, srcId int, targetId int, topK int) (res PriorityQueue) {
var pq PriorityQueue
src := g.VertexIndex[srcId]
predecessors := removeNotConnectedParts(g, src)
pq, st.cycles = dfs(g, predecessors, src, src, st.deepLimit, topK)
res = ProcessOutsideEdges(pq, st.deepLimit, topK, true, false)
return
}
func (st *DfsStacked) TopKOneToOne(g *MultiGraph, srcIds []int, targetIds []int, topK int) (res []PriorityQueue) {
panic(fmt.Errorf("DfsStacked.TopKOneToOne is not implemented"))
}
func (st *DfsStacked) TopKOneToMany(g *MultiGraph, srcIds []int, targetIds []int, topK int) (res []PriorityQueue) {
panic(fmt.Errorf("DfsStacked.TopKOneToMany is not implemented"))
}
func (st *DfsStacked) TopKCycles(topK int) (res []PriorityQueue) {
return getTopkCyclesByVertex(st.cycles, st.deepLimit, topK)
}
func (st *DfsStacked) BestCycles() (res []EdgeSeq) {
return getBestCycleByVertex(st.cycles, st.deepLimit)
}
func removeNotConnectedParts(g *MultiGraph, src int) (pred []MEdgeSeq) {
vertexCount := len(g.VertexIndex)
visited := make([]bool, vertexCount)
adj := make([][]int, vertexCount)
pred = make([]MEdgeSeq, vertexCount)
q := list.New()
q.PushBack(src)
visited[src] = true
for q.Len() != 0 {
e := q.Front()
u, _ := e.Value.(int)
q.Remove(e)
for _, edge := range g.Succ(u) {
v := edge.Data.Id2i
if adj[v] == nil {
adj[v] = make([]int, vertexCount)
}
if pred[v] == nil {
pred[v] = make(MEdgeSeq, 0)
}
if adj[v][u] == 0 {
adj[v][u]++
pred[v] = append(pred[v], edge)
}
if visited[v] {
continue
}
visited[v] = true
q.PushBack(v)
}
}
return
}
func dfs(g *MultiGraph, pred []MEdgeSeq, src int, target int, deepLimit int, topK int) (pq PriorityQueue, cycles []map[uint64]MEdgeSeq) {
type vm struct {
Vert int
Deep int
Parent int
Index int
}
level_m1 := 0
level := 0
var node vm
vertexCount := len(g.VertexIndex)
visited := make([]bool, vertexCount)
s := make([]vm, 0)
path := make([]int, deepLimit+1)
psa := make([]float64, deepLimit+2)
edges := make(MEdgeSeq, deepLimit+1)
cycles = make([]map[uint64]MEdgeSeq, vertexCount)
pq = NewPriorityQueue(0, topK)
maxWeight := math.Inf(-1)
pathCount := 0
counter := 0
s = append(s, vm{src, 1, -1, 0})
counter++
for counter > 0 {
counter--
node = s[counter]
level = node.Deep
level_m1 = level - 1
s = s[:counter]
for i := level_m1; i < deepLimit+1; i++ {
visited[path[i]] = false
path[i] = 0
edges[i] = nil
}
if visited[node.Vert] {
indx := firstIndexOf(node.Vert, path[:level-1])
edge := pred[node.Parent][node.Index]
weight := psa[level-1] + edge.Weight - psa[indx+1]
bits := math.Float64bits(weight) >> 2
if weight >= 0.0 {
continue
}
if cycles[path[indx]] == nil {
cycles[path[indx]] = make(map[uint64]MEdgeSeq)
}
if _, ok := cycles[path[indx]][bits]; !ok {
cedges := make(MEdgeSeq, level-1)
edges[level_m1] = edge
copy(cedges, edges[1:level])
cycles[path[indx]][bits] = cedges
}
continue
}
path[level_m1] = node.Vert
psa[level] = psa[level_m1]
if node.Parent != -1 {
edges[level_m1] = pred[node.Parent][node.Index]
psa[level] += edges[level_m1].Weight
}
visited[node.Vert] = true
if level > deepLimit {
continue
}
for i, edge := range pred[node.Vert] {
u := edge.Data.Id1i
if target == u {
pathCount++
edges[level] = edge
weight := psa[level] + edge.Weight
if weight >= 0 {
continue
}
if pq.Len() < topK {
cedges := make(MEdgeSeq, deepLimit)
copy(cedges, edges[1:])
pq.Append(cedges, weight)
if pq.Len() == topK {
pq.Init()
maxWeight = pq[0].Priority
}
continue
}
if weight < maxWeight {
ms, _ := pq[0].Value.(MEdgeSeq)
copy(ms, edges[1:])
pq.Update(pq[0], pq[0].Value, weight)
maxWeight = pq[0].Priority
}
continue
}
s = append(s, vm{u, level + 1, node.Vert, i})
counter++
}
}
if pq.Len() < topK {
pq.Init()
}
return
}
func getBestCycleByVertex(cycles []map[uint64]MEdgeSeq, deepLimit int) (res []EdgeSeq) {
res = make([]EdgeSeq, len(cycles))
for id, cyclePool := range cycles {
var minWeight float64 = 0.0
var minKey uint64 = 0
for key, cycle := range cyclePool {
weight := 0.0
for i := 0; i < len(cycle); i++ {
weight += cycle[i].Weight
}
if minWeight > weight {
minWeight = weight
minKey = key
}
}
if minKey != 0 {
cycle := cyclePool[minKey]
path := make(EdgeSeq, deepLimit)
for i := 0; i < len(cycle); i++ {
path[i] = &SingleEdge{Data: cycle[i].Data, Weight: cycle[i].Weight}
}
path[:len(cycle)].ReverseEdgeSeq()
res[id] = path
}
}
return
}
func getTopkCyclesByVertex(cycles []map[uint64]MEdgeSeq, deepLimit int, topK int) (res []PriorityQueue) {
res = make([]PriorityQueue, len(cycles))
mask := make([]int, deepLimit)
limits := make([]int, deepLimit)
path := make(EdgeSeq, deepLimit)
for id, cyclePool := range cycles {
maxWeight := 0.0
topCycles := NewPriorityQueue(0, topK)
for _, cycle := range cyclePool {
for i := 0; i < deepLimit; i++ {
path[i] = nil
}
weight := 0.0
seqSize := len(cycle)
for i := 0; i < seqSize; i++ {
limits[i] = len(cycle[i].edges)
path[i] = cycle[i].edges[0]
weight += path[i].Weight
}
rem := 0
for {
for i := 1; i < seqSize && rem > 0; i++ {
curEdges := cycle[i].edges
weight -= curEdges[mask[i]].Weight
mask[i] += rem
mask[i], rem = mask[i]%limits[i], mask[i]/limits[i]
path[i] = curEdges[mask[i]]
weight += path[i].Weight
}
if rem > 0 {
break
}
if weight <= maxWeight {
if topCycles.Len() < topK {
cpath := make(EdgeSeq, deepLimit)
copy(cpath, path)
cpath[:seqSize].ReverseEdgeSeq()
topCycles.Append(cpath, weight)
if topCycles.Len() == topK {
topCycles.Init()
maxWeight = topCycles[0].Priority
}
} else {
ms, _ := topCycles[0].Value.(EdgeSeq)
copy(ms, path)
ms[:seqSize].ReverseEdgeSeq()
topCycles.Update(topCycles[0], topCycles[0].Value, weight)
maxWeight = topCycles[0].Priority
}
}
curEdges := cycle[0].edges
weight -= curEdges[mask[0]].Weight
mask[0] += 1
mask[0], rem = mask[0]%limits[0], mask[0]/limits[0]
path[0] = curEdges[mask[0]]
weight += path[0].Weight
}
}
if topCycles.Len() < topK {
topCycles.Init()
}
res[id] = topCycles
}
return
}