Module: MPathGraph::OddHole
- Defined in:
- lib/mpath_graph.rb
Class Method Summary collapse
- .equivalents(hole) ⇒ Object
-
.is_an_odd_hole?(path, edges, size = 5) ⇒ Boolean
Given a path in a graph G, check if it is an odd-hole with an specific size.
-
.order_uedge(v, u) ⇒ Object
Order entries of a tuple.
- .rw_search(edges, options = {}) ⇒ Object
- .rw_search_first(edges, options = {}) ⇒ Object
Class Method Details
.equivalents(hole) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/mpath_graph.rb', line 194 def self.equivalents(hole) perms = [hole.reverse] tmp = Array.new hole (hole.size-1).times do tmp << tmp.shift perms << Array.new(tmp) perms << Array.new(tmp.reverse) end perms end |
.is_an_odd_hole?(path, edges, size = 5) ⇒ Boolean
Given a path in a graph G, check if it is an odd-hole with an specific size.
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/mpath_graph.rb', line 312 def self.is_an_odd_hole?(path, edges, size = 5) raise ArgumentError, "Hole size must be at least 5" if size<5 raise ArgumentError, "Hole size must be odd" if size % 2 == 0 raise ArgumentError, "Path must have odd-hole size" if path.size != size cycle_edge = order_uedge(path[0],path[-1]) return false unless edges.include?(cycle_edge) (0..size-3).each do |i| # First vertex (2..size-2).each do |j| # Offset from first vertex next if i+j > size-1 uedge = order_uedge(path[i],path[i+j]) return false if edges.include?(uedge) end end true end |
.order_uedge(v, u) ⇒ Object
Order entries of a tuple.
332 333 334 |
# File 'lib/mpath_graph.rb', line 332 def self.order_uedge(v,u) v < u ? [v,u] : [u,v] end |
.rw_search(edges, options = {}) ⇒ Object
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 |
# File 'lib/mpath_graph.rb', line 207 def self.rw_search(edges, = {}) [:holes_limit] ||= 0 holes_found = [] [:worm_size] ||= 5 return nil if edges.size < [:worm_size] prng = Random.new rnd_idx = prng.rand(edges.size) [:initial_vertex] ||= edges[rnd_idx][rnd_idx%2] worm = [[:initial_vertex]] while worm.size < [:worm_size] return nil if worm.empty? neighbors = MPathGraph::find_neighbors(worm.last, edges) neighbors = neighbors - worm if neighbors.empty? worm.pop next else rnd_idx = prng.rand(neighbors.size) worm << neighbors[rnd_idx] neighbors = nil if worm.size == [:worm_size] if is_an_odd_hole?(worm, edges, [:worm_size]) # Check if current hole is also found repeated = false holes_found.each do |h| if worm == h repeated = true break end equivalents(h).each do |e| if worm == e repeated = true break end end break if repeated end # Add to found list if not found yet unless repeated holes_found << worm if [:holes_limit] > 0 && holes_found.size == [:holes_limit] return holes_found end end # Leave only first element in worm worm = [worm.last] next else worm.shift next end end end end end |
.rw_search_first(edges, options = {}) ⇒ Object
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 |
# File 'lib/mpath_graph.rb', line 271 def self.rw_search_first(edges, = {}) [:worm_size] ||= 5 return nil if edges.size < [:worm_size] prng = Random.new rnd_idx = prng.rand(edges.size) [:initial_vertex] ||= edges[rnd_idx][rnd_idx%2] worm = [[:initial_vertex]] while worm.size < [:worm_size] return nil if worm.empty? neighbors = MPathGraph::find_neighbors(worm.last, edges) neighbors = neighbors - worm if neighbors.empty? worm.pop next else rnd_idx = prng.rand(neighbors.size) worm << neighbors[rnd_idx] neighbors = nil if worm.size == [:worm_size] if is_an_odd_hole?(worm, edges, [:worm_size]) return worm else worm.shift next end end end end end |