Module: Plexus::CycleBuilder

Included in:
CompleteBuilder
Defined in:
lib/plexus/common.rb

Overview

This class defines a cycle graph of size n. This is easily done by using the base Graph class and implemeting the minimum methods needed to make it work. This is a good example to look at for making one’s own graph classes.

Instance Method Summary collapse

Instance Method Details

#directed?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/plexus/common.rb', line 13

def directed?
  false
end

#edge?(u, v = nil) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/plexus/common.rb', line 25

def edge?(u,v = nil)
  u, v = [u.source, v.target] if u.is_a? Plexus::Arc
  vertex?(u) && vertex?(v) && ((v-u == 1) or (u == @size && v = 1))
end

#edgesObject



30
31
32
# File 'lib/plexus/common.rb', line 30

def edges
  Array.new(@size) { |i| Plexus::Edge[i+1, (i+1) == @size ? 1 : i+2]}
end

#initialize(n) ⇒ Object



9
10
11
# File 'lib/plexus/common.rb', line 9

def initialize(n)
  @size = n;
end

#vertex?(v) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/plexus/common.rb', line 21

def vertex?(v)
  v > 0 and v <= @size
end

#verticesObject



17
18
19
# File 'lib/plexus/common.rb', line 17

def vertices
  (1..@size).to_a
end