-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbellman_ford.go
More file actions
74 lines (58 loc) · 1.65 KB
/
bellman_ford.go
File metadata and controls
74 lines (58 loc) · 1.65 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
package kspa
import (
"fmt"
"math"
)
type BellmanFord struct {
deepLimit int
uniquePaths bool
}
func (st *BellmanFord) TopK(g *MultiGraph, srcId int, targetId int, topK int) (res PriorityQueue) {
if topK != 1 {
panic(fmt.Errorf("BellmanFord.TopK doesn't support several paths searching"))
}
vertexCount := len(g.VertexIndex)
edges := g.Edges
src := g.VertexIndex[srcId]
d := make([]float64, vertexCount)
p := make([]int, vertexCount)
for i := 0; i < vertexCount; i++ {
p[i] = -1
d[i] = math.Inf(0)
}
d[src] = 0
if st.deepLimit <= 0 || st.deepLimit > vertexCount-1 {
st.deepLimit = vertexCount - 1
}
for i := 1; i < st.deepLimit+1; i++ {
for _, edge := range edges {
if d[edge.Data.Id1i]+edge.Weight < d[edge.Data.Id2i] {
d[edge.Data.Id2i] = d[edge.Data.Id1i] + edge.Weight
p[edge.Data.Id2i] = edge.Data.Id1i
}
}
}
res = NewPriorityQueue(0, topK)
visited := make([]bool, vertexCount)
for _, edge := range edges {
if visited[edge.Data.Id2i] {
continue
}
if d[edge.Data.Id1i]+edge.Weight < d[edge.Data.Id2i] {
cycle := traceNegativeCycle(edge.Data.Id2i, p, st.deepLimit, st.uniquePaths, visited)
if cycle == nil {
continue
}
//TODO
//replace path by MultiEdge sequence and process weight
res.Append(cycle, 0.0)
}
}
return
}
func (st *BellmanFord) TopKOneToOne(g *MultiGraph, srcIds []int, targetIds []int, topK int) (res []PriorityQueue) {
panic(fmt.Errorf("BellmanFord.TopKOneToOne is not provided"))
}
func (st *BellmanFord) TopKOneToMany(g *MultiGraph, srcIds []int, targetIds []int, topK int) (res []PriorityQueue) {
panic(fmt.Errorf("BellmanFord.TopKOneToMany is not provided"))
}