-
Notifications
You must be signed in to change notification settings - Fork 2
/
pseudoNA.go
218 lines (197 loc) · 5.37 KB
/
pseudoNA.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
217
218
// pseudoNA.go - package extension for passing preprocessed Dimacs file.
package pseudo
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
"strings"
)
// N is the dimacs 'n' entry
type N struct {
Val uint
Node string
}
// A is the dimacs 'a' entry
type A struct {
From uint
To uint
Capacity int
}
// RunNAWriter solves optimal flow given slices of 'n' and 'a' dimacs entries.
func (s *Session) RunNAWriter(numNodes, numArcs uint, nodes []N, arcs []A, w io.Writer, header ...string) error {
if err := s.loadNA(numNodes, numArcs, nodes, arcs); err != nil {
return err
}
return s.process(w, header...)
}
func (s *Session) loadNA(nn, na uint, n []N, a []A) error {
s.numNodes, s.numArcs = nn, na
// allocate & initialize storage
s.adjacencyList = make([]*node, s.numNodes)
s.strongRoots = make([]*root, s.numNodes)
s.labelCount = make([]uint, s.numNodes)
s.arcList = make([]*arc, s.numArcs)
var i uint
for i = 0; i < s.numNodes; i++ {
s.strongRoots[i] = &root{} // newRoot()
s.adjacencyList[i] = s.newNode(uint(i + 1))
}
for i = 0; i < s.numArcs; i++ {
s.arcList[i] = &arc{direction: 1} // newArc(1)
}
// process N values
if len(n) != 2 {
return fmt.Errorf("want 2 N vals, have %d", len(n))
}
var haveSrc, haveSink bool
for _, v := range n {
if v.Node == "s" {
s.source = v.Val
haveSrc = true
} else if v.Node == "t" {
s.sink = v.Val
haveSink = true
} else {
return fmt.Errorf("unrecognized character %s in N.Node value", v.Node)
}
}
// check if there are 2 source or sink values
if haveSrc && !haveSink {
return fmt.Errorf("N slice does not include a sink - N.Node == t - value")
}
if !haveSrc && haveSink {
return fmt.Errorf("N slice does not include a source - N.Node == s - value")
}
// process A values
first := uint(0)
last := s.numArcs - 1
for _, v := range a {
if (v.From+v.To)%2 != 0 {
s.arcList[first].from = s.adjacencyList[v.From-1]
s.arcList[first].to = s.adjacencyList[v.To-1]
s.arcList[first].capacity = v.Capacity
first++
} else {
s.arcList[last].from = s.adjacencyList[v.From-1]
s.arcList[last].to = s.adjacencyList[v.To-1]
s.arcList[last].capacity = v.Capacity
last--
}
s.adjacencyList[v.From-1].numAdjacent++
s.adjacencyList[v.To-1].numAdjacent++
}
// finish initialization
for i = 0; i < s.numNodes; i++ {
s.adjacencyList[i].createOutOfTree()
}
var from, to uint
var capacity int
for i = 0; i < s.numArcs; i++ {
to = s.arcList[i].to.number
from = s.arcList[i].from.number
capacity = s.arcList[i].capacity
if !(s.source == to || s.sink == from || from == to) {
if s.source == from && to == s.sink {
s.arcList[i].flow = capacity
} else if from == s.source || to != s.sink {
s.adjacencyList[from-1].addOutOfTreeNode(s.arcList[i])
} else if to == s.sink {
s.adjacencyList[to-1].addOutOfTreeNode(s.arcList[i])
} else {
s.adjacencyList[from-1].addOutOfTreeNode(s.arcList[i])
}
}
}
return nil
}
// ParseDimacsReader generates input data for s.RunNAWriter. It is generally for tests.
func ParseDimacsReader(r io.Reader) (uint, uint, []N, []A, error) {
var numNodes, numArcs uint
n := []N{}
a := []A{}
var i, from, to uint
var capacity int
var ch1 string
buf := bufio.NewReader(r)
var atEOF bool
var num uint64
for {
if atEOF {
break
}
line, err := buf.ReadBytes('\n')
if err != nil && err != io.EOF {
return numNodes, numArcs, n, a, err
} else if err == io.EOF {
if len(bytes.TrimSpace(line)) == 0 {
break // nothing more to process
}
// ... at EOF with data but no '\n' line termination.
// While not necessary for os.Stdin; it can happen in a file.
atEOF = true
} else {
// Strip off EOL and white space
line = bytes.TrimSpace(line[:len(line)-1])
if len(line) == 0 {
continue // skip empty lines
}
}
switch line[0] {
case 'p':
vals := strings.Fields(string(line))
if len(vals) != 4 {
return numNodes, numArcs, n, a, fmt.Errorf("p entry doesn't have 3 values, has: %d", len(vals))
}
num, err = strconv.ParseUint(vals[2], 10, 64)
if err != nil {
return numNodes, numArcs, n, a, err
}
numNodes = uint(num)
num, err = strconv.ParseUint(vals[3], 10, 64)
if err != nil {
return numNodes, numArcs, n, a, err
}
numArcs = uint(num)
case 'a':
vals := strings.Fields(string(line))
if len(vals) != 4 {
return numNodes, numArcs, n, a, fmt.Errorf("a entry doesn't have 3 values, has: %d", len(vals))
}
num, err = strconv.ParseUint(vals[1], 10, 64)
if err != nil {
return numNodes, numArcs, n, a, err
}
from = uint(num)
num, err = strconv.ParseUint(vals[2], 10, 64)
if err != nil {
return numNodes, numArcs, n, a, err
}
to = uint(num)
num, err = strconv.ParseUint(vals[3], 10, 64)
if err != nil {
return numNodes, numArcs, n, a, err
}
capacity = int(num)
a = append(a, A{from, to, capacity})
case 'n':
vals := strings.Fields(string(line))
if len(vals) != 3 {
return numNodes, numArcs, n, a, fmt.Errorf("n entry doesn't have 2 values, has: %d", len(vals))
}
num, err = strconv.ParseUint(vals[1], 10, 64)
if err != nil {
return numNodes, numArcs, n, a, err
}
i = uint(num)
ch1 = vals[2]
n = append(n, N{i, ch1})
case 'c':
continue // catches "comment" lines
default:
return numNodes, numArcs, n, a, fmt.Errorf("unknown data: %s", string(line))
}
}
return numNodes, numArcs, n, a, nil
}