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
223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/mpath_graph.rb', line 223 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.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/mpath_graph.rb', line 350 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.
370 371 372 |
# File 'lib/mpath_graph.rb', line 370 def self.order_uedge(v,u) v < u ? [v,u] : [u,v] end |
.rw_search(edges, options = {}) ⇒ Object
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 |
# File 'lib/mpath_graph.rb', line 236 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]] = ProgressBar.create( title: "Steps walked", starting_at: 0, output: STDERR, format: "%t |%B| [%c]", total: nil) 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] .increment 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
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/mpath_graph.rb', line 309 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 |