-
Notifications
You must be signed in to change notification settings - Fork 33
/
generate.go
71 lines (67 loc) · 1.79 KB
/
generate.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
package dijkstra
import (
"math"
"math/rand"
)
// Generate generates a graph with a moderate amount of connections with semi
// random weights
func Generate(verticies int) Graph {
//reproducable random
seeded := rand.New(rand.NewSource(int64(verticies)))
graph := Graph{}
var from int
for from = 0; from < verticies; from++ {
graph.AddEmptyVertex(from)
}
for from = 0; from < verticies; from++ {
// v := map[int]int64{}
min := from - verticies/4
if min < 2 {
min = 2
}
max := from + verticies/4
if max > verticies {
max = verticies
}
for to := min; to < max; to++ {
if to == from {
continue
}
graph.AddArc(from, to, uint64(2*verticies-to)+uint64(seeded.Int63n(int64(verticies)*int64(verticies-to+1))))
}
}
return graph
}
func generateWorstCaseShortest(verticies int) (Graph, BestPath[int]) {
graph := Graph{}
var from int
for from = 0; from < verticies; from++ {
graph.AddEmptyVertex(from)
}
for from = 0; from < verticies; from++ {
for to := from + 1; to < verticies-1; to++ {
distance := uint64(to - from)
graph.AddArc(from, to, uint64((verticies-from))*distance*distance)
}
}
graph.AddArc(0, verticies-1, math.MaxUint64/2)
graph.AddArc(verticies-1, 0, 0)
shortestResult := BestPath[int]{math.MaxUint64 / 2, []int{0, verticies - 1}}
return graph, shortestResult
}
func generateWorstCaseLongest(verticies int) (Graph, BestPath[int]) {
graph := Graph{}
var from int
for from = 0; from < verticies; from++ {
graph.AddEmptyVertex(from)
}
for from = 0; from < verticies; from++ {
for to := from + 1; to < verticies-1; to++ {
distance := uint64(verticies - (to - from))
graph.AddArc(from, to, distance*distance)
}
}
graph.AddArc(0, verticies-1, 0)
longestResult := BestPath[int]{0, []int{0, verticies - 1}}
return graph, longestResult
}