/
graph.go
183 lines (164 loc) · 4.23 KB
/
graph.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
package randomizer
import (
"fmt"
)
// map of names to set of nodes in a directed graph
type graph map[string]*node
// returns a new, initialized graph.
func newGraph() graph {
return graph(make(map[string]*node))
}
// adds relationships in bulk between existing nodes in the graph, by name.
// attempting to link a name not in the graph results in a panic.
func (g graph) addParents(links map[string][]string) {
for childName, parentNames := range links {
if child, ok := g[childName]; ok {
for _, parentName := range parentNames {
if parent, ok := g[parentName]; ok {
child.addParent(parent)
} else {
panic("no node named " + parentName)
}
}
} else {
panic("no child named " + childName)
}
}
}
// resets all the nodes in a graph to an unreached state (unless it's true
// without any reached parents). this is required any time relationships in the
// graph change.
func (g graph) reset() {
for _, n := range g {
n.indegree = 0
n.reached = n.indegree >= n.mindegree()
}
}
// determines the number of parents required for a node to be considered
// reached; see node.bounds().
type nodeType uint8
const (
andNode nodeType = iota
orNode
countNode
rupeesNode
)
// a single vertex in the graph.
type node struct {
name string
ntype nodeType
reached bool
indegree int // number of reached parents
minCount int // for countNodes, minimum parents to reach
parents []*node
children []*node
player int
}
// returns a new unconnected graph node, not yet part of any graph.
func newNode(name string, nt nodeType) *node {
// create node
n := &node{
name: name,
ntype: nt,
parents: make([]*node, 0),
children: make([]*node, 0),
}
n.reached = n.indegree >= n.mindegree()
return n
}
// returns the minimum and reached parents at which a node is considered true.
func (n *node) mindegree() int {
switch n.ntype {
case andNode:
return len(n.parents)
case orNode, rupeesNode:
return 1
case countNode:
return n.minCount
default:
panic("unknown type for node: " + n.name)
}
}
// makes a node a parent of another node. a node can be a parent of another
// node multiple times; i.e. it can appear twice or more in the child's list of
// parents.
func (n *node) addParent(parent *node) {
n.parents = append(n.parents, parent)
parent.children = append(parent.children, n)
}
// removes the given node from this node's parents, once. it panics if the
// given node isn't actually a parent of this node.
func (n *node) removeParent(parent *node) {
for i, p := range n.parents {
if p == parent {
for i, c := range p.children {
if c == n {
p.children = append(p.children[:i], p.children[i+1:]...)
break
}
}
n.parents = append(n.parents[:i], n.parents[i+1:]...)
return
}
}
panic(fmt.Sprintf("removeParent: %v is not a parent of %v", parent, n))
}
// removes all parent connections from the node.
func (n *node) clearParents() {
for _, p := range n.parents {
for i, c := range p.children {
if c == n {
p.children = append(p.children[:i], p.children[i+1:]...)
break
}
}
}
n.parents = n.parents[:0]
}
// satisfies the fmt.Stringer interface.
func (n *node) String() string { return n.name }
// explores the graph starting from the given node, assuming the given node is
// reachable, marking successors as appropriate.
func (n *node) explore() {
n.reached = true
// rupees node sets indegree of children to total # of rupees reached
if n.ntype == rupeesNode {
for _, c := range n.children {
c.indegree = n.indegree
if !c.reached {
c.exploreIfReachable()
}
}
return
}
for _, c := range n.children {
// add rupee value of parent to rupees node
if c.ntype == rupeesNode {
c.indegree += rupeeValues[n.name]
c.explore()
return
} else {
c.indegree++
}
if !c.reached {
c.exploreIfReachable()
} else {
// handle count nodes
for _, cc := range c.children {
if !cc.reached && cc.ntype == countNode {
cc.indegree++
cc.exploreIfReachable()
} else if cc.ntype == rupeesNode {
cc.indegree += rupeeValues[c.name]
cc.explore()
}
}
}
}
}
// like explore, but only works if the node's indegree is in bounds.
func (n *node) exploreIfReachable() {
if n.indegree >= n.mindegree() {
n.explore()
}
}