mpath_graph 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YTdmNTQ4NDUyNzgzNGE4YWE1NWZmN2E2ZDhkZDFmNTljODNhZTVlNQ==
5
- data.tar.gz: !binary |-
6
- YzU1MjkzOWRhZWYyNDkyMmI0MjA0MDMzNzMxZjVlOTRkNDMzZmQ0MQ==
2
+ SHA1:
3
+ metadata.gz: d7b6e50d3b2aba78f3916fe417dbb9652e6c028d
4
+ data.tar.gz: 4e65ecd1d69aa8b692183050da44819dc63a7e56
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZDc4MTA4YzYzMDgxNzY5MzVlYzAwYzhkNmY2ZmZmY2FjMjY2MDkxOTIwOTAy
10
- YWNiZjM2YmJjYTNkZjIzMTc3YzA2ZDNhNGEyZjk0YTdkOGU5Y2M0OTY4MzUw
11
- YjlkNjhjMDM3OGE3ZGI3YjNhMDk2Y2ZiMzc0YmI4YzJlODJkZDk=
12
- data.tar.gz: !binary |-
13
- ZTU5ODFkMzI5NzQ4MDYzNjhhZGQwNTYzYjYzZmRkODYyZjc3ZTQxMzQ5Njdk
14
- ZjMxMDE1MGI1NDQ5ODgwZjE0N2Y5NzdhODVjNzMwNjFhYWQ2MDVlMzI5OGRk
15
- MDNhYmZlNThkMzY0ZjNiNjgzN2E5ZDVjMzQzMzgwYzA0ZTc3YmE=
6
+ metadata.gz: da13ccac14270b6e38af0cc5130dee05e97cf7a627b4db0a7eddd03b643b83331ede683346e1a8cb9ffaa32af039e12d23682760c5b1d9e5a74c7aabd81acb50
7
+ data.tar.gz: 2664b8ee4407f970234e452f968cc041fbe21be8cc02e763fdf67d62b5fd88a26c8b83125850bf1bdb19c5a75ea1f8eadcdcf42dc291cf25b8c8a14d3d83f101
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
- # MpathGraph
1
+ # MPathGraph
2
+ [![Gem Version](https://badge.fury.io/rb/mpath_graph.svg)](http://badge.fury.io/rb/mpath_graph)
3
+ [![Gittip](http://img.shields.io/badge/tips-poor-yellow.svg)](https://www.gittip.com/neoriddle/)
2
4
 
3
- TODO: Write a gem description
5
+ **MPathGraph** is a library to work with _m_-path graphs generated from a hamiltonian cycle in a hypercube graph.
4
6
 
5
7
  ## Installation
6
8
 
@@ -18,7 +20,8 @@ Or install it yourself as:
18
20
 
19
21
  ## Usage
20
22
 
21
- TODO: Write usage instructions here
23
+ - TODO: Write usage instructions here.
24
+ - TODO: Include some chunks of code as examples.
22
25
 
23
26
  ## Contributing
24
27
 
@@ -1,3 +1,3 @@
1
1
  module MPathGraph
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/mpath_graph.rb CHANGED
@@ -60,14 +60,15 @@ module MPathGraph
60
60
  worm = []
61
61
  neighbors = nil
62
62
  steps_walked = 0
63
- initial = options[:initial_vertex] || 0
64
- worm_size = options[:worm_size] || 5
65
- walk_limit = options[:walk_limit] || -1
66
- pause = options[:pause] || false
67
- induced_cycles_limit = options[:induced_cycles_limit] || -1
63
+ options[:initial_vertex] ||= 0
64
+ options[:worm_size] ||= 5
65
+ options[:walk_limit] ||= -1
66
+ options[:pause] ||= false
67
+ options[:induced_cycles_limit] ||= -1
68
+ options[:print_non_cycles] ||= false
68
69
 
69
70
  # Initialize worm with initial vertex
70
- worm << initial
71
+ worm << options[:initial_vertex]
71
72
 
72
73
  # Initialize pseudo-random number generator
73
74
  prng = Random.new
@@ -76,7 +77,7 @@ module MPathGraph
76
77
  puts ["k-path","Induced cycle","Steps walked"].to_csv
77
78
 
78
79
  # Fill worm
79
- while worm.size < worm_size
80
+ while worm.size < options[:worm_size]
80
81
  # Find neighbors of last vertex
81
82
  neighbors = find_neighbors(worm.last, edges) unless neighbors
82
83
 
@@ -96,7 +97,7 @@ module MPathGraph
96
97
  worm << neighbors[rnd_idx] # Append vertex
97
98
  neighbors = nil # Clean neighbors
98
99
 
99
- if worm.size == worm_size # When worm is full
100
+ if worm.size == options[:worm_size] # When worm is full
100
101
  steps_walked += 1 # Increment worm steps walked
101
102
 
102
103
  # Output well-formed worm
@@ -105,8 +106,8 @@ module MPathGraph
105
106
 
106
107
  # if has_cycles?(worm, edges, verbose: options[:verbose])
107
108
  if (cycle = find_cycles(worm, edges))
108
- csv_row << cycle
109
- csv_row << steps_walked
109
+ # Flush csv row data to STDIN
110
+ puts [worm, cycle, steps_walked].to_csv
110
111
 
111
112
  # if options[:verbose] # Verbose worm with cycle
112
113
  # puts "Worm\t#{worm.inspect}"
@@ -114,24 +115,21 @@ module MPathGraph
114
115
  # end
115
116
 
116
117
  # Pause when verbose
117
- # if pause
118
+ # if options[:pause]
118
119
  # STDERR.puts "Press a key to continue..."
119
120
  # STDIN.getc
120
121
  # end
121
122
 
122
123
  # Check induced cycles counter limit
123
- return if induced_cycles_limit==0
124
- induced_cycles_limit-=1
124
+ return if options[:induced_cycles_limit]==0
125
+ options[:induced_cycles_limit]-=1
125
126
  else
126
- csv_row << nil
127
- csv_row << steps_walked
127
+ # Flush csv row data to STDIN
128
+ puts [worm, nil, steps_walked].to_csv if options[:print_non_cycles]
128
129
  end
129
130
 
130
- # Flush csv row data to STDIN
131
- puts csv_row.to_csv
132
-
133
131
  worm.shift # Drop first vertex in worm (as queue)
134
- break if steps_walked > walk_limit && walk_limit > 0 # Stop walking
132
+ break if steps_walked > options[:walk_limit] && options[:walk_limit] > 0 # Stop walking
135
133
  end
136
134
  end
137
135
  end
@@ -191,4 +189,72 @@ module MPathGraph
191
189
 
192
190
  paths
193
191
  end
192
+
193
+ module OddHole
194
+ def self.rw_search_first(edges, options = {})
195
+ options[:worm_size] ||= 5
196
+ return nil if edges.size < options[:worm_size]
197
+
198
+ prng = Random.new
199
+ rnd_idx = prng.rand(edges.size)
200
+ options[:initial_vertex] ||= edges[rnd_idx][rnd_idx%2]
201
+ worm = [options[:initial_vertex]]
202
+
203
+ while worm.size < options[:worm_size]
204
+ return nil if worm.empty?
205
+
206
+ neighbors = MPathGraph::find_neighbors(worm.last, edges)
207
+ neighbors = neighbors - worm
208
+
209
+ if neighbors.empty?
210
+ worm.pop
211
+ next
212
+ else
213
+ rnd_idx = prng.rand(neighbors.size)
214
+ worm << neighbors[rnd_idx]
215
+ neighbors = nil
216
+
217
+ if worm.size == options[:worm_size]
218
+ if is_an_odd_hole?(worm, edges, options[:worm_size])
219
+ return worm
220
+ else
221
+ worm.shift
222
+ next
223
+ end
224
+ end
225
+ end
226
+ end
227
+ end
228
+
229
+ # Given a path in a graph G, check if it is an odd-hole with an
230
+ # specific size.
231
+ #
232
+ # @param path [Array<Integer>] Sequence of vertices to check.
233
+ # @param edges [Array<Array<Integer>>] Set of edges of graph G.
234
+ # @param size [Array<Array<Integer>>] Odd-hole size to search.
235
+ def self.is_an_odd_hole?(path, edges, size = 5)
236
+ raise ArgumentError, "Hole size must be at least 5" if size<5
237
+ raise ArgumentError, "Hole size must be odd" if size % 2 == 0
238
+ raise ArgumentError, "Path must have odd-hole size" if path.size != size
239
+
240
+ cycle_edge = order_uedge(path[0],path[-1])
241
+ return false unless edges.include?(cycle_edge)
242
+
243
+ (0..size-3).each do |i| # First vertex
244
+ (2..size-2).each do |j| # Offset from first vertex
245
+ next if i+j > size-1
246
+ uedge = order_uedge(path[i],path[i+j])
247
+ return false if edges.include?(uedge)
248
+ end
249
+ end
250
+
251
+ true
252
+ end
253
+
254
+ # Order entries of a tuple.
255
+ def self.order_uedge(v,u)
256
+ v < u ? [v,u] : [u,v]
257
+ end
258
+
259
+ end
194
260
  end
data/mpath_graph.gemspec CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_runtime_dependency 'ruby-progressbar'
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.3"
22
24
  spec.add_development_dependency "rake"
23
25
  spec.add_development_dependency "rspec"
@@ -0,0 +1,36 @@
1
+ require 'mpath_graph'
2
+ require 'minitest/autorun'
3
+ require 'minitest/unit'
4
+
5
+ # Author:: Israel Buitron (mailto:ibuitron@computacion.cs.cinvestav.mx)
6
+ class TestMPathGraphOddHole < MiniTest::Unit::TestCase
7
+
8
+ def setup
9
+ @oh5_e = [[1,2],[1,5],[2,3],[3,4],[4,5]]
10
+ @oh7_e = [[1,2],[1,7],[2,3],[3,4],[4,5],[5,6],[6,7]]
11
+ @g = [[1,2],[1,5],[2,3],[3,4],[4,5]]
12
+ end
13
+
14
+ def test_is_an_odd_hole?
15
+ # Test with 5-odd-hole
16
+ assert_raises(ArgumentError) { MPathGraph::OddHole::is_an_odd_hole?(nil, @oh5_e, 2) }
17
+ assert_raises(ArgumentError) { MPathGraph::OddHole::is_an_odd_hole?(nil, @oh5_e, 3) }
18
+ assert_raises(ArgumentError) { MPathGraph::OddHole::is_an_odd_hole?((1..6).to_a, @oh5_e, 5) }
19
+ refute MPathGraph::OddHole::is_an_odd_hole?([1,2,3,4,10], @oh5_e, 5)
20
+ assert MPathGraph::OddHole::is_an_odd_hole?([1,2,3,4,5], @oh5_e, 5)
21
+
22
+ # Test with 7-odd-hole
23
+ assert MPathGraph::OddHole::is_an_odd_hole?((1..7).to_a, @oh7_e, 7)
24
+ end
25
+
26
+ def test_order_uedge
27
+ assert [1,2], MPathGraph::OddHole::order_uedge(1,2)
28
+ assert [1,2], MPathGraph::OddHole::order_uedge(2,1)
29
+ assert [1,1], MPathGraph::OddHole::order_uedge(1,1)
30
+ end
31
+
32
+ def test_rw_search_first
33
+ computed = MPathGraph::OddHole::rw_search_first(@g)
34
+ assert_equal [1,2,3,4,5], computed.sort
35
+ end
36
+ end
metadata CHANGED
@@ -1,55 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mpath_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Israel Buitron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-03 00:00:00.000000000 Z
11
+ date: 2014-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-progressbar
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - ~>
31
+ - - "~>"
18
32
  - !ruby/object:Gem::Version
19
33
  version: '1.3'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
- - - ~>
38
+ - - "~>"
25
39
  - !ruby/object:Gem::Version
26
40
  version: '1.3'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - ! '>='
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
47
  version: '0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - ! '>='
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - ! '>='
59
+ - - ">="
46
60
  - !ruby/object:Gem::Version
47
61
  version: '0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - ! '>='
66
+ - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  description: m-Path graphs library
@@ -59,7 +73,7 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
62
- - .gitignore
76
+ - ".gitignore"
63
77
  - Gemfile
64
78
  - LICENSE.txt
65
79
  - README.md
@@ -67,6 +81,7 @@ files:
67
81
  - lib/mpath_graph.rb
68
82
  - lib/mpath_graph/version.rb
69
83
  - mpath_graph.gemspec
84
+ - test/test_mpath_graph.rb
70
85
  homepage: ''
71
86
  licenses:
72
87
  - MIT
@@ -77,18 +92,19 @@ require_paths:
77
92
  - lib
78
93
  required_ruby_version: !ruby/object:Gem::Requirement
79
94
  requirements:
80
- - - ! '>='
95
+ - - ">="
81
96
  - !ruby/object:Gem::Version
82
97
  version: '0'
83
98
  required_rubygems_version: !ruby/object:Gem::Requirement
84
99
  requirements:
85
- - - ! '>='
100
+ - - ">="
86
101
  - !ruby/object:Gem::Version
87
102
  version: '0'
88
103
  requirements: []
89
104
  rubyforge_project:
90
- rubygems_version: 2.3.0
105
+ rubygems_version: 2.2.2
91
106
  signing_key:
92
107
  specification_version: 4
93
108
  summary: m-Path graphs library
94
- test_files: []
109
+ test_files:
110
+ - test/test_mpath_graph.rb