-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathyen_test.go
152 lines (138 loc) · 5.75 KB
/
yen_test.go
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
// Copyright(c) 2016 Ethan Zhuang <[email protected]>.
package goraph
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"math"
)
var _ = Describe("Tests of Yen", func() {
var (
graph *Graph
)
Context("algorithem test", func() {
BeforeEach(func() {
graph = NewGraph()
graph.AddVertexWithEdges(&myVertex{"C", map[ID]float64{"D": 3, "E": 2}, map[ID]float64{}})
graph.AddVertexWithEdges(&myVertex{"D", map[ID]float64{"F": 4}, map[ID]float64{"C": 3, "E": 1}})
graph.AddVertexWithEdges(&myVertex{"E", map[ID]float64{"D": 1, "F": 2, "G": 3}, map[ID]float64{"C": 2}})
graph.AddVertexWithEdges(&myVertex{"F", map[ID]float64{"G": 2, "H": 1}, map[ID]float64{"D": 4, "E": 2}})
graph.AddVertexWithEdges(&myVertex{"G", map[ID]float64{"H": 2}, map[ID]float64{"E": 3, "F": 2}})
graph.AddVertexWithEdges(&myVertex{"H", map[ID]float64{}, map[ID]float64{"F": 1, "G": 2}})
Expect(graph.CheckIntegrity()).ShouldNot(HaveOccurred())
})
AfterEach(func() {
graph = nil
})
It("Given a non-negative edge graph, when call yen api with non-existed verten, then get error.", func() {
dist, path, err := graph.Yen("S", "T", 3)
Expect(err).Should(HaveOccurred())
Expect(dist).Should(BeNil())
Expect(path).Should(BeNil())
})
It("Given a graph with negative edge, when call yen api, then get error.", func() {
graph.AddEdge("F", "E", -1, nil)
Expect(graph.CheckIntegrity()).ShouldNot(HaveOccurred())
dist, path, err := graph.Yen("C", "H", 3)
Expect(err).Should(HaveOccurred())
Expect(dist).Should(BeNil())
Expect(path).Should(BeNil())
})
It("Given a graph without negative edge, when call yen api, then get the top k shortest paths from the source vertex to the destination vertex in the graph.", func() {
dist, path, err := graph.Yen("C", "H", 9)
Expect(err).ShouldNot(HaveOccurred())
Expect(dist[0]).Should(BeEquivalentTo(5))
Expect(path[0]).Should(BeEquivalentTo([]ID{"C", "E", "F", "H"}))
Expect(dist[1]).Should(BeEquivalentTo(7))
Expect(path[1]).Should(BeEquivalentTo([]ID{"C", "E", "G", "H"}))
Expect(dist[2]).Should(BeEquivalentTo(8))
Expect(path[2]).Should(BeEquivalentTo([]ID{"C", "D", "F", "H"}))
Expect(dist[3]).Should(BeEquivalentTo(8))
Expect(path[3]).Should(BeEquivalentTo([]ID{"C", "E", "F", "G", "H"}))
Expect(dist[4]).Should(BeEquivalentTo(8))
Expect(path[4]).Should(BeEquivalentTo([]ID{"C", "E", "D", "F", "H"}))
Expect(dist[5]).Should(BeEquivalentTo(11))
Expect(path[5]).Should(BeEquivalentTo([]ID{"C", "D", "F", "G", "H"}))
Expect(dist[6]).Should(BeEquivalentTo(11))
Expect(path[6]).Should(BeEquivalentTo([]ID{"C", "E", "D", "F", "G", "H"}))
Expect(dist[7]).Should(BeEquivalentTo(math.Inf(1)))
Expect(path[7]).Should(BeNil())
Expect(dist[8]).Should(BeEquivalentTo(math.Inf(1)))
Expect(path[8]).Should(BeNil())
})
It("Given another graph without negative edge, when call yen api, then get the top k shortest paths from the source vertex to the destination vertex in the graph.", func() {
mygraph := NewGraph()
mygraph.AddVertex("A", nil)
mygraph.AddVertex("B", nil)
mygraph.AddVertex("C", nil)
mygraph.AddVertex("D", nil)
mygraph.AddVertex("E", nil)
mygraph.AddEdge("A", "B", 1, nil)
mygraph.AddEdge("B", "C", 1, nil)
mygraph.AddEdge("C", "D", 1, nil)
mygraph.AddEdge("A", "D", 2, nil)
mygraph.AddEdge("B", "E", 2, nil)
mygraph.AddEdge("E", "D", 1, nil)
dist, path, err := mygraph.Yen("A", "D", 3)
Expect(err).ShouldNot(HaveOccurred())
Expect(dist[0]).Should(BeEquivalentTo(2))
Expect(path[0]).Should(BeEquivalentTo([]ID{"A", "D"}))
Expect(dist[1]).Should(BeEquivalentTo(3))
Expect(path[1]).Should(BeEquivalentTo([]ID{"A", "B", "C", "D"}))
Expect(dist[2]).Should(BeEquivalentTo(4))
Expect(path[2]).Should(BeEquivalentTo([]ID{"A", "B", "E", "D"}))
})
It("Support early stop when there are enough potential paths in each iteration", func() {
mygraph := NewGraph()
mygraph.AddVertex("0", nil)
mygraph.AddVertex("1", nil)
mygraph.AddVertex("2", nil)
mygraph.AddVertex("3", nil)
mygraph.AddVertex("4", nil)
mygraph.AddEdge("0", "1", 1, nil)
mygraph.AddEdge("0", "2", 1, nil)
mygraph.AddEdge("1", "2", 1, nil)
mygraph.AddEdge("2", "3", 1, nil)
mygraph.AddEdge("2", "4", 1, nil)
mygraph.AddEdge("3", "4", 1, nil)
dist, path, err := mygraph.Yen("0", "4", 3)
Expect(err).ShouldNot(HaveOccurred())
Expect(dist[0]).Should(BeEquivalentTo(2))
Expect(path[0]).Should(BeEquivalentTo([]ID{"0", "2", "4"}))
Expect(dist[1]).Should(BeEquivalentTo(3))
Expect(path[1]).Should(BeEquivalentTo([]ID{"0", "1", "2", "4"}))
Expect(dist[2]).Should(BeEquivalentTo(3))
Expect(path[2]).Should(BeEquivalentTo([]ID{"0", "2", "3", "4"}))
})
})
Context("bugfix test", func() {
BeforeEach(func() {
graph = NewGraph()
})
AfterEach(func() {
graph = nil
})
It("Issue reported by gsidebottom for missing path.", func() {
graph.AddVertex("0", nil)
graph.AddVertex("1", nil)
graph.AddVertex("2", nil)
graph.AddVertex("3", nil)
graph.AddVertex("4", nil)
graph.AddEdge("0", "1", 1, nil)
graph.AddEdge("0", "2", 1, nil)
graph.AddEdge("1", "2", 1, nil)
graph.AddEdge("2", "3", 1, nil)
graph.AddEdge("2", "4", 1, nil)
graph.AddEdge("3", "4", 1, nil)
dist, path, err := graph.Yen("0", "4", 5)
Expect(err).ShouldNot(HaveOccurred())
Expect(dist[0]).Should(BeEquivalentTo(2))
Expect(path[0]).Should(BeEquivalentTo([]ID{"0", "2", "4"}))
Expect(dist[1]).Should(BeEquivalentTo(3))
Expect(path[1]).Should(BeEquivalentTo([]ID{"0", "1", "2", "4"}))
Expect(dist[2]).Should(BeEquivalentTo(3))
Expect(path[2]).Should(BeEquivalentTo([]ID{"0", "2", "3", "4"}))
Expect(dist[3]).Should(BeEquivalentTo(4))
Expect(path[3]).Should(BeEquivalentTo([]ID{"0", "1", "2", "3", "4"}))
})
})
})