Class: GRATR::Cycle

Inherits:
Object
  • Object
show all
Defined in:
lib/gratr/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

Direct Known Subclasses

Complete

Instance Method Summary collapse

Constructor Details

#initialize(n) ⇒ Cycle

Returns a new instance of Cycle.



41
# File 'lib/gratr/common.rb', line 41

def initialize(n) @size = n;       end

Instance Method Details

#directed?Boolean

Returns:

  • (Boolean)


42
# File 'lib/gratr/common.rb', line 42

def directed?()     false;           end

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

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/gratr/common.rb', line 45

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

#edgesObject



49
# File 'lib/gratr/common.rb', line 49

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

#vertex?(v) ⇒ Boolean

Returns:

  • (Boolean)


44
# File 'lib/gratr/common.rb', line 44

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

#verticesObject



43
# File 'lib/gratr/common.rb', line 43

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