-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuden.rb
308 lines (247 loc) · 5.33 KB
/
tuden.rb
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
class Tuden
INFINITY = Float::INFINITY
def initialize
@node_map = {}
end
class Node
attr_reader :id
attr_reader :edges
attr_reader :dist
attr_accessor :done
attr_accessor :from
def initialize(id)
@id = id
@edges = []
end
def reset
@dist = INFINITY
@done = false
@from = nil
end
def update_dist(dist, nid)
if dist < @dist
@dist = dist
@from = nid
end
end
def inspect
inner = [
"N",
id,
"dist=#{@dist}",
"from=#{@from}",
"edges=#{@edges.inspect}"
].join(" ")
"(" + inner + ")"
end
end
class Edge
attr_reader :id
attr_reader :nid1
attr_reader :nid2
attr_writer :state
def initialize(id, nid1, nid2, all_switches_on)
@id = id
@nid1 = nid1
@nid2 = nid2
@state = nil
@all_switches_on = all_switches_on
end
def on?
@state
end
def all_switches_on?
@all_switches_on
end
def opposite_nid(nid)
if @nid1 == nid
@nid2
elsif @nid2 == nid
@nid1
else
raise "must not happen"
end
end
def connected_to?(nid)
nid == @nid1 || nid == @nid2
end
def inspect
inner = [
"E",
id,
"nid=#{@nid1},#{@nid2}"
].join(" ")
"(" + inner + ")"
end
end
def self.tuden?(switches)
switches.all? { |switch| switch.on? }
end
def find_node(nid)
@node_map[nid]
end
# TODO ボトルネックになるようであれば優先度付きキューを検討
def find_min_node(nodes)
dist_min = nil
min_node = nil
nodes.each { |n|
if ( dist_min == nil ||
n.dist < dist_min
)
dist_min = n.dist
min_node = n
end
}
min_node
end
def make_dist_map(
all_edges, all_nodes, nid_goal,
nids_ignore
)
target_edges = all_edges.select { |edge|
nids_ignore.all? {|nid_ignore|
! edge.connected_to?(nid_ignore)
}
}
target_nodes = all_nodes.select { |node|
! nids_ignore.include?(node.id)
}
target_nodes.each { |node|
node.reset()
}
rest_nodes = target_nodes.dup
find_node(nid_goal).update_dist(0, nil)
from_nid = nid_goal
loop do
rest_nodes = rest_nodes.select { |node| ! node.done }
break if rest_nodes.empty?
min_node = find_min_node(rest_nodes)
min_node.done = true
# TODO 改善候補
# グラフ全体の隣接リストを作る必要はなく、
# min_node=対象のエッジ端点ならここで中断してよい。
from_nid = min_node.id
to_nodes = min_node.edges
.map { |edge|
nid = edge.opposite_nid(min_node.id)
find_node(nid)
}
.select { |node|
! node.done &&
! nids_ignore.include?(node.id)
}
from_node = find_node(from_nid)
to_dist = from_node.dist + 1
to_nodes.each { |node|
node.update_dist(to_dist, from_nid)
}
end
dist_map = {}
target_nodes.each { |node|
dist_map[node.id] = [node.dist, node.from]
}
dist_map
end
def walk(dist_map, start_nid, goal_nid)
path = [start_nid]
return path if start_nid == goal_nid
current_nid = start_nid
loop do
if dist_map.key?(current_nid)
dist, next_nid = dist_map[current_nid]
else
return nil
end
if dist == 0
return path
end
current_nid = next_nid
path << current_nid
end
end
def walk_saki_ato(
edges,
nodes,
nid_saki, goal_nid_saki,
nid_ato, goal_nid_ato
)
dist_map_saki = make_dist_map(
edges, nodes, goal_nid_saki,
[nid_ato]
)
path_saki = walk(dist_map_saki, nid_saki, goal_nid_saki)
# TODO 改善候補
# return false if path_saki.nil?
dist_map_ato = make_dist_map(
edges, nodes, goal_nid_ato,
path_saki.nil? ? [] : path_saki
)
path_ato = walk(dist_map_ato, nid_ato, goal_nid_ato)
! path_saki.nil? && ! path_ato.nil?
end
def edge_tuden?(
nid_plus,
nid_minus,
edges,
nodes,
edge
)
return false unless edge.all_switches_on?
# nid1 => plus
# nid2 => mminus
result1 =
walk_saki_ato(
edges,
nodes,
edge.nid1, nid_plus,
edge.nid2, nid_minus
)
# nid1 => minus
# nid2 => plus
result2 =
walk_saki_ato(
edges,
nodes,
edge.nid1, nid_minus,
edge.nid2, nid_plus
)
# TODO 改善候補
result1 || result2 || false
end
def update(
edges,
nodes,
nid_plus,
nid_minus
)
nodes.each { |node| @node_map[node.id] = node }
edges.each { |edge|
node1 = find_node(edge.nid1)
node1.edges << edge if edge.all_switches_on?
node2 = find_node(edge.nid2)
node2.edges << edge if edge.all_switches_on?
}
edges.each { |edge|
edge.state = edge_tuden?(
nid_plus,
nid_minus,
edges,
nodes,
edge
)
}
end
def self.update(
edges,
nodes,
nid_plus,
nid_minus
)
Tuden.new.update(
edges,
nodes,
nid_plus,
nid_minus
)
end
end