-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.go
88 lines (80 loc) · 2.24 KB
/
solver.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
// biGraph-Solver project main.go
package main
type SolutionData struct {
sourceData [][]int
solutions [][]int
temporarySolution []int
usedVertices []bool
resultCount int
maxSecondVertice int
secondVerticesData map[int]int
}
func countSecondVerticesRelations(solution *SolutionData) {
solution.secondVerticesData = make(map[int]int)
for _, v := range solution.sourceData {
for _, v2 := range v {
solution.secondVerticesData[v2]++
if v2 > solution.maxSecondVertice {
solution.maxSecondVertice = v2
}
}
}
}
func iterateOver(v int, solution *SolutionData) {
if v >= len(solution.sourceData) {
tCount := len(solution.temporarySolution)
if tCount >= solution.resultCount {
if tCount > solution.resultCount {
solution.solutions = nil
solution.resultCount = tCount
}
ts := make([]int, len(solution.temporarySolution))
copy(ts, solution.temporarySolution)
solution.solutions = append(solution.solutions, ts)
}
return
}
if len(solution.sourceData[v]) == 0 {
iterateOver(v+1, solution)
}
for _, vertice := range solution.sourceData[v] {
if solution.usedVertices[vertice] {
continue
}
solution.usedVertices[vertice] = true
tempLen := len(solution.temporarySolution)
solution.temporarySolution = append(solution.temporarySolution, v, vertice)
iterateOver(v+1, solution)
solution.temporarySolution = solution.temporarySolution[:tempLen]
solution.usedVertices[vertice] = false
}
iterateOver(v+1, solution)
}
func GetSolutions(data *[][]int) *[][]int {
solution := new(SolutionData)
solution.sourceData = *data
solution.solutions = nil
solution.temporarySolution = make([]int, 0, len(*data)*2)
countSecondVerticesRelations(solution)
solution.usedVertices = make([]bool, solution.maxSecondVertice+1)
iterateOver(0, solution)
return &solution.solutions
}
/*func main() {
var n, m, k int
//fmt.Print("Write n, m: ")
fmt.Scan(&n, &m)
var data = make([][]int, n)
for i := 0; i < n; i++ {
//fmt.Printf("Number of relations for vertice %v: ", i+1)
fmt.Scan(&k)
data[i] = make([]int, k)
//fmt.Printf("Write relations %v: ", i+1)
for j := 0; j < k; j++ {
fmt.Scan(&data[i][j])
}
}
//fmt.Println(data)
solutions := GetSolutions(&data)
fmt.Println(solutions)
}*/