-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathshortest_path_test.go
216 lines (207 loc) · 6.15 KB
/
shortest_path_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
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
package goraph
import (
"fmt"
"os"
"strings"
"testing"
)
func TestGraph_Dijkstra_03(t *testing.T) {
f, err := os.Open("testdata/graph.json")
if err != nil {
t.Error(err)
}
defer f.Close()
g, err := NewGraphFromJSON(f, "graph_03")
if err != nil {
t.Error(err)
}
path, distance, err := Dijkstra(g, StringID("S"), StringID("T"))
if err != nil {
t.Error(err)
}
ts := []string{}
for _, v := range path {
ts = append(ts, fmt.Sprintf("%s(%.2f)", v, distance[v]))
}
if strings.Join(ts, " → ") != "S(0.00) → B(14.00) → E(32.00) → F(38.00) → T(44.00)" {
t.Errorf("Expected the shortest path S(0.00) → B(14.00) → E(32.00) → F(38.00) → T(44.00) but %s", strings.Join(ts, " → "))
}
if distance[StringID("T")] != 44.0 {
t.Errorf("Expected 44.0 but %f", distance[StringID("T")])
}
fmt.Println("graph_03:", strings.Join(ts, " → "))
}
func TestGraph_Dijkstra_04(t *testing.T) {
f, err := os.Open("testdata/graph.json")
if err != nil {
t.Error(err)
}
defer f.Close()
g, err := NewGraphFromJSON(f, "graph_04")
if err != nil {
t.Error(err)
}
path, distance, err := Dijkstra(g, StringID("A"), StringID("E"))
if err != nil {
t.Error(err)
}
ts := []string{}
for _, v := range path {
ts = append(ts, fmt.Sprintf("%s(%.2f)", v, distance[v]))
}
if strings.Join(ts, " → ") != "A(0.00) → C(9.00) → F(11.00) → E(20.00)" {
t.Errorf("Expected the shortest path A(0.00) → C(9.00) → F(11.00) → E(20.00) but %s", strings.Join(ts, " → "))
}
if distance[StringID("E")] != 20.0 {
t.Errorf("Expected 20.0 but %f", distance[StringID("E")])
}
fmt.Println("graph_04:", strings.Join(ts, " → "))
}
func TestGraph_Dijkstra_09_0(t *testing.T) {
f, err := os.Open("testdata/graph.json")
if err != nil {
t.Error(err)
}
defer f.Close()
g, err := NewGraphFromJSON(f, "graph_09")
if err != nil {
t.Error(err)
}
path, distance, err := Dijkstra(g, StringID("A"), StringID("E"))
if err != nil {
t.Error(err)
}
ts := []string{}
for _, v := range path {
ts = append(ts, fmt.Sprintf("%s(%.2f)", v, distance[v]))
}
if strings.Join(ts, " → ") != "A(0.00) → C(9.00) → B(19.00) → D(34.00) → E(36.00)" {
t.Errorf("Expected the shortest path A(0.00) → C(9.00) → B(19.00) → D(34.00) → E(36.00) but %s", strings.Join(ts, " → "))
}
if distance[StringID("E")] != 36.0 {
t.Errorf("Expected 36.0 but %f", distance[StringID("E")])
}
fmt.Println("graph_09:", strings.Join(ts, " → "))
}
func TestGraph_Dijkstra_09_1(t *testing.T) {
f, err := os.Open("testdata/graph.json")
if err != nil {
t.Error(err)
}
defer f.Close()
g, err := NewGraphFromJSON(f, "graph_09")
if err != nil {
t.Error(err)
}
path, distance, err := Dijkstra(g, StringID("E"), StringID("A"))
if err != nil {
t.Error(err)
}
ts := []string{}
for _, v := range path {
ts = append(ts, fmt.Sprintf("%s(%.2f)", v, distance[v]))
}
if strings.Join(ts, " → ") != "E(0.00) → F(9.00) → C(11.00) → B(21.00) → A(22.00)" {
t.Errorf("Expected the shortest path E(0.00) → F(9.00) → C(11.00) → B(21.00) → A(22.00) but %s", strings.Join(ts, " → "))
}
if distance[StringID("A")] != 22.0 {
t.Errorf("Expected 22.0 but %f", distance[StringID("A")])
}
fmt.Println("graph_09:", strings.Join(ts, " → "))
}
func TestGraph_Dijkstra_10_0(t *testing.T) {
f, err := os.Open("testdata/graph.json")
if err != nil {
t.Error(err)
}
defer f.Close()
g, err := NewGraphFromJSON(f, "graph_10")
if err != nil {
t.Error(err)
}
path, distance, err := Dijkstra(g, StringID("S"), StringID("T"))
if err != nil {
t.Error(err)
}
ts := []string{}
for _, v := range path {
ts = append(ts, fmt.Sprintf("%s(%.2f)", v, distance[v]))
}
if strings.Join(ts, " → ") != "S(0.00) → A(11.00) → B(16.00) → D(46.00) → E(49.00) → T(68.00)" {
t.Errorf("Expected the shortest path S(0.00) → A(11.00) → B(16.00) → D(46.00) → E(49.00) → T(68.00) but %s", strings.Join(ts, " → "))
}
if distance[StringID("T")] != 68.0 {
t.Errorf("Expected 68.0 but %f", distance[StringID("T")])
}
fmt.Println("graph_10:", strings.Join(ts, " → "))
}
func TestGraph_Dijkstra_10_1(t *testing.T) {
f, err := os.Open("testdata/graph.json")
if err != nil {
t.Error(err)
}
defer f.Close()
g, err := NewGraphFromJSON(f, "graph_10")
if err != nil {
t.Error(err)
}
path, distance, err := Dijkstra(g, StringID("T"), StringID("S"))
if err != nil {
t.Error(err)
}
ts := []string{}
for _, v := range path {
ts = append(ts, fmt.Sprintf("%s(%.2f)", v, distance[v]))
}
if strings.Join(ts, " → ") != "T(0.00) → D(10.00) → E(13.00) → B(31.00) → S(48.00)" {
t.Errorf("Expected the shortest path T(0.00) → D(10.00) → E(13.00) → B(31.00) → S(48.00) but %s", strings.Join(ts, " → "))
}
if distance[StringID("S")] != 48.0 {
t.Errorf("Expected 48.0 but %f", distance[StringID("S")])
}
fmt.Println("graph_10:", strings.Join(ts, " → "))
}
func TestGraph_BellmanFord_11(t *testing.T) {
f, err := os.Open("testdata/graph.json")
if err != nil {
t.Error(err)
}
defer f.Close()
g, err := NewGraphFromJSON(f, "graph_11")
if err != nil {
t.Error(err)
}
path, distance, err := BellmanFord(g, StringID("S"), StringID("T"))
if err != nil {
t.Errorf("There should be no negative-weight cycle but found one with %v", err)
}
ts := []string{}
for _, v := range path {
ts = append(ts, fmt.Sprintf("%s(%.2f)", v, distance[v]))
}
if strings.Join(ts, " → ") != "S(0.00) → A(7.00) → C(4.00) → B(2.00) → T(-2.00)" {
t.Errorf("Expected the shortest path S(0.00) → A(7.00) → C(4.00) → B(2.00) → T(-2.00) but %s", strings.Join(ts, " → "))
}
if distance[StringID("T")] != -2.0 {
t.Errorf("Expected -2.0 but %f", distance[StringID("T")])
}
fmt.Println("graph_11:", strings.Join(ts, " → "))
}
func TestGraph_BellmanFord_12(t *testing.T) {
f, err := os.Open("testdata/graph.json")
if err != nil {
t.Error(err)
}
defer f.Close()
g, err := NewGraphFromJSON(f, "graph_12")
if err != nil {
t.Error(err)
}
path, distance, err := BellmanFord(g, StringID("S"), StringID("T"))
if err == nil {
t.Errorf("There should be negative-weight cycle but %v", err)
}
if path != nil || distance != nil {
t.Errorf("Expected nil, nil but %v, %v", path, distance)
}
}