-
Notifications
You must be signed in to change notification settings - Fork 13
/
types.go
61 lines (52 loc) · 1.75 KB
/
types.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
/*
An implementation of the algorithm described in "A Dual Ascent-Based Branch-and-Bound
Framework for the Prize-Collecting Steiner Tree and Related Problems" by Leitner et al.
*/
package dapcstp
import (
"math"
"strconv"
)
// Value is the type for representing costs and prizes.
type Value float64
func (v Value) ToString() string {
return strconv.FormatFloat(float64(v), 'f', -1, 64)
}
// ValueMax is the maximal value for Value
const ValueMax = Value(math.MaxFloat64)
// Graph represents the problem statement.
type Graph struct {
// Edges
Arcs int // The number of edges in the graph
Cost []Value // The cost for including the given edge
Src []int // The source vertex id for the edge
Dst []int // The dst vertex id for the edge
// Vertices
Root int // The vertex (the id) of the root of the solution tree
Nodes int // The number of vertices
Prize []Value // The reward for including the given vertex in the tree.
Fixed []bool // True if the given vertex must be part of the solution
Terminal []bool // The Prize is positive or Fixed is true
Incoming [][]int // For a given vertex, the list of incoming edge ids
Outgoing [][]int // For a given vertex, the list of outgoing edge ids
}
func NewSolution(g *Graph) *Solution {
return &Solution{Graph: g, Profit: 0,
Arcs: make([]bool, g.Arcs), Nodes: make([]bool, g.Nodes)}
}
type Solution struct {
Arcs []bool
Nodes []bool
Graph *Graph
Profit Value
}
// BuildArc maintains the profit and the nodes when building an arc.
func (s *Solution) BuildArc(arcID int) {
if s.Arcs[arcID] {
panic("Already built")
}
dst := s.Graph.Dst[arcID]
s.Nodes[dst] = true // Assume src is already connected
s.Arcs[arcID] = true
s.Profit += s.Graph.Prize[dst] - s.Graph.Cost[arcID]
}