-
Notifications
You must be signed in to change notification settings - Fork 5
/
emst.go
114 lines (91 loc) · 3.31 KB
/
emst.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
package mlpack
/*
#cgo CFLAGS: -I./capi -Wall
#cgo LDFLAGS: -L. -lmlpack_go_emst
#include <capi/emst.h>
#include <stdlib.h>
*/
import "C"
import "gonum.org/v1/gonum/mat"
type EmstOptionalParam struct {
LeafSize int
Naive bool
Verbose bool
}
func EmstOptions() *EmstOptionalParam {
return &EmstOptionalParam{
LeafSize: 1,
Naive: false,
Verbose: false,
}
}
/*
This program can compute the Euclidean minimum spanning tree of a set of input
points using the dual-tree Boruvka algorithm.
The set to calculate the minimum spanning tree of is specified with the
"Input" parameter, and the output may be saved with the "Output" output
parameter.
The "LeafSize" parameter controls the leaf size of the kd-tree that is used to
calculate the minimum spanning tree, and if the "Naive" option is given, then
brute-force search is used (this is typically much slower in low dimensions).
The leaf size does not affect the results, but it may have some effect on the
runtime of the algorithm.
For example, the minimum spanning tree of the input dataset data can be
calculated with a leaf size of 20 and stored as spanning_tree using the
following command:
// Initialize optional parameters for Emst().
param := mlpack.EmstOptions()
param.LeafSize = 20
spanning_tree := mlpack.Emst(data, param)
The output matrix is a three-dimensional matrix, where each row indicates an
edge. The first dimension corresponds to the lesser index of the edge; the
second dimension corresponds to the greater index of the edge; and the third
column corresponds to the distance between the two points.
Input parameters:
- input (mat.Dense): Input data matrix.
- LeafSize (int): Leaf size in the kd-tree. One-element leaves give
the empirically best performance, but at the cost of greater memory
requirements. Default value 1.
- Naive (bool): Compute the MST using O(n^2) naive algorithm.
- Verbose (bool): Display informational messages and the full list of
parameters and timers at the end of execution.
Output parameters:
- output (mat.Dense): Output data. Stored as an edge list.
*/
func Emst(input *mat.Dense, param *EmstOptionalParam) (*mat.Dense) {
params := getParams("emst")
timers := getTimers()
disableBacktrace()
disableVerbose()
// Detect if the parameter was passed; set if so.
gonumToArmaMat(params, "input", input, false)
setPassed(params, "input")
// Detect if the parameter was passed; set if so.
if param.LeafSize != 1 {
setParamInt(params, "leaf_size", param.LeafSize)
setPassed(params, "leaf_size")
}
// Detect if the parameter was passed; set if so.
if param.Naive != false {
setParamBool(params, "naive", param.Naive)
setPassed(params, "naive")
}
// Detect if the parameter was passed; set if so.
if param.Verbose != false {
setParamBool(params, "verbose", param.Verbose)
setPassed(params, "verbose")
enableVerbose()
}
// Mark all output options as passed.
setPassed(params, "output")
// Call the mlpack program.
C.mlpackEmst(params.mem, timers.mem)
// Initialize result variable and get output.
var outputPtr mlpackArma
output := outputPtr.armaToGonumMat(params, "output")
// Clean memory.
cleanParams(params)
cleanTimers(timers)
// Return output(s).
return output
}