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.



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

def initialize(n) @size = n;       end

Instance Method Details

#directed?Boolean

Returns:

  • (Boolean)


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

def directed?()     false;           end

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

Returns:

  • (Boolean)


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

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



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

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

#vertex?(v) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#verticesObject



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

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