mpath_graph 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/mpath_graph.rb +77 -0
- data/lib/mpath_graph/version.rb +1 -1
- data/test/test_mpath_graph.rb +14 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d8a5503ae715087d19084692a16bbec0e69e2253
|
|
4
|
+
data.tar.gz: 541cfae204b640553c6237e23938eecb7b3a29c9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 44fdf9724d60e0467483dd98026b2b6416e208378b721eed0bb65aee077916f7f6b5ae6bd4e6d0e44766edd401c441d4154b89e6ffd2c88fc855eac0331d9cde
|
|
7
|
+
data.tar.gz: f5377c43f9aedb4e1ef4720cf07a01251eac237f9b52dd4a004ee1cf502d31e0e9fef605f5623417ab06dac28d4bd753ad7f13d9f9c4b325e4b100c6a211a8dc
|
data/lib/mpath_graph.rb
CHANGED
|
@@ -191,6 +191,83 @@ module MPathGraph
|
|
|
191
191
|
end
|
|
192
192
|
|
|
193
193
|
module OddHole
|
|
194
|
+
def self.equivalents(hole)
|
|
195
|
+
perms = [hole.reverse]
|
|
196
|
+
tmp = Array.new hole
|
|
197
|
+
|
|
198
|
+
(hole.size-1).times do
|
|
199
|
+
tmp << tmp.shift
|
|
200
|
+
perms << Array.new(tmp)
|
|
201
|
+
perms << Array.new(tmp.reverse)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
perms
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def self.rw_search(edges, options = {})
|
|
208
|
+
options[:holes_limit] ||= 0
|
|
209
|
+
holes_found = []
|
|
210
|
+
options[:worm_size] ||= 5
|
|
211
|
+
return nil if edges.size < options[:worm_size]
|
|
212
|
+
|
|
213
|
+
prng = Random.new
|
|
214
|
+
rnd_idx = prng.rand(edges.size)
|
|
215
|
+
options[:initial_vertex] ||= edges[rnd_idx][rnd_idx%2]
|
|
216
|
+
worm = [options[:initial_vertex]]
|
|
217
|
+
|
|
218
|
+
while worm.size < options[:worm_size]
|
|
219
|
+
return nil if worm.empty?
|
|
220
|
+
|
|
221
|
+
neighbors = MPathGraph::find_neighbors(worm.last, edges)
|
|
222
|
+
neighbors = neighbors - worm
|
|
223
|
+
|
|
224
|
+
if neighbors.empty?
|
|
225
|
+
worm.pop
|
|
226
|
+
next
|
|
227
|
+
else
|
|
228
|
+
rnd_idx = prng.rand(neighbors.size)
|
|
229
|
+
worm << neighbors[rnd_idx]
|
|
230
|
+
neighbors = nil
|
|
231
|
+
|
|
232
|
+
if worm.size == options[:worm_size]
|
|
233
|
+
if is_an_odd_hole?(worm, edges, options[:worm_size])
|
|
234
|
+
# Check if current hole is also found
|
|
235
|
+
repeated = false
|
|
236
|
+
holes_found.each do |h|
|
|
237
|
+
if worm == h
|
|
238
|
+
repeated = true
|
|
239
|
+
break
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
equivalents(h).each do |e|
|
|
243
|
+
if worm == e
|
|
244
|
+
repeated = true
|
|
245
|
+
break
|
|
246
|
+
end
|
|
247
|
+
end
|
|
248
|
+
break if repeated
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
# Add to found list if not found yet
|
|
252
|
+
unless repeated
|
|
253
|
+
holes_found << worm
|
|
254
|
+
if options[:holes_limit] > 0 && holes_found.size == options[:holes_limit]
|
|
255
|
+
return holes_found
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# Leave only first element in worm
|
|
260
|
+
worm = [worm.last]
|
|
261
|
+
next
|
|
262
|
+
else
|
|
263
|
+
worm.shift
|
|
264
|
+
next
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
|
|
194
271
|
def self.rw_search_first(edges, options = {})
|
|
195
272
|
options[:worm_size] ||= 5
|
|
196
273
|
return nil if edges.size < options[:worm_size]
|
data/lib/mpath_graph/version.rb
CHANGED
data/test/test_mpath_graph.rb
CHANGED
|
@@ -9,6 +9,7 @@ class TestMPathGraphOddHole < MiniTest::Unit::TestCase
|
|
|
9
9
|
@oh5_e = [[1,2],[1,5],[2,3],[3,4],[4,5]]
|
|
10
10
|
@oh7_e = [[1,2],[1,7],[2,3],[3,4],[4,5],[5,6],[6,7]]
|
|
11
11
|
@g = [[1,2],[1,5],[2,3],[3,4],[4,5]]
|
|
12
|
+
@doble_c5 = [[0,6],[0,9],[1,2],[1,5],[1,6],[2,3],[3,4],[4,5],[6,7],[7,8],[8,9]]
|
|
12
13
|
end
|
|
13
14
|
|
|
14
15
|
def test_is_an_odd_hole?
|
|
@@ -32,5 +33,18 @@ class TestMPathGraphOddHole < MiniTest::Unit::TestCase
|
|
|
32
33
|
def test_rw_search_first
|
|
33
34
|
computed = MPathGraph::OddHole::rw_search_first(@g)
|
|
34
35
|
assert_equal [1,2,3,4,5], computed.sort
|
|
36
|
+
|
|
37
|
+
def test_rw_search
|
|
38
|
+
found = MPathGraph::OddHole::rw_search(@doble_c5, holes_limit: 2)
|
|
39
|
+
assert_equal 2, found.size
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def test_equivalents
|
|
43
|
+
hole = [1,2,3,4,5]
|
|
44
|
+
exp = [[5,4,3,2,1],[2,3,4,5,1],[1,5,4,3,2],[3,4,5,1,2],[2,1,5,4,3],
|
|
45
|
+
[4,5,1,2,3],[3,2,1,5,4],[5,1,2,3,4],[4,3,2,1,5]]
|
|
46
|
+
result = MPathGraph::OddHole::equivalents(hole)
|
|
47
|
+
assert_same exp.size, result.size
|
|
48
|
+
assert [], exp-result
|
|
35
49
|
end
|
|
36
50
|
end
|