class Object

Constants

RGL_VERSION

base.rb

Module RGL defines the namespace for all modules and classes of the graph library. The main module is RGL::Graph which defines the abstract behavior of all graphs in the library.

Public Instance Methods

bfs_example(g = cycle(5), start = g.detect { |x| true }) click to toggle source
# File examples/examples.rb, line 73
def bfs_example(g = cycle(5), start = g.detect { |x| true })
  require 'rgl/traversal'

  g.bfs_search_tree_from(start)
end
complete(n) click to toggle source

Complete Graph with n vertices

# File examples/examples.rb, line 37
def complete (n)
  set = n.integer? ? (1..n) : n
  RGL::ImplicitGraph.new { |g|
    g.vertex_iterator { |b| set.each(&b) }
    g.adjacent_iterator { |x, b|
      set.each { |y| b.call(y) unless x == y }
    }
  }
end
cycle(n) click to toggle source

Cyclic graph with n vertices

# File examples/examples.rb, line 28
def cycle (n)
  RGL::ImplicitGraph.new { |g|
    g.vertex_iterator { |b| 0.upto(n - 1, &b) }
    g.adjacent_iterator { |x, b| b.call((x + 1) % n) }
    g.directed = true
  }
end
divisors(n) click to toggle source

Shows graph of divisors of all integers from 2 to n.

# File examples/examples.rb, line 63
def divisors(n)
  RGL::ImplicitGraph.new { |g|
    g.vertex_iterator { |b| 2.upto(n, &b) }
    g.adjacent_iterator { |x, b|
      n.downto(x + 1) { |y| b.call(y) if y % x == 0 }
    }
    g.directed = true
  }
end
graph_from_dotfile(file) click to toggle source

Would like to have GraphXML here

# File examples/examples.rb, line 80
def graph_from_dotfile (file)
  g = RGL::AdjacencyGraph.new
  pattern = /\s*([^\"]+)[\"\s]*--[\"\s]*([^\"\[\;]+)/ # ugly but works
  IO.foreach(file) { |line|
    case line
      when /^digraph/
        g = RGL::DirectedAdjacencyGraph.new
        pattern = /\s*([^\"]+)[\"\s]*->[\"\s]*([^\"\[\;]+)/
      when pattern
        g.add_edge $1, $2
      else
        nil
    end
  }
  g
end
graph_from_string(s) click to toggle source
# File test/components_test.rb, line 8
def graph_from_string(s)
  g = DirectedAdjacencyGraph.new(Array)
  s.split(/\n/).collect { |x| x.split(/->/) }.each do |a|
    from = a[0].strip
    a[1].split.each do |to|
      g.add_edge from, to
    end
  end
  g
end
module_graph() click to toggle source

Directed graph of ruby modules. Edges are defined by the method ancestors

# File examples/examples.rb, line 48
def module_graph
  RGL::ImplicitGraph.new { |g|
    g.vertex_iterator { |b|
      ObjectSpace.each_object(Module, &b)
    }
    g.adjacent_iterator { |x, b|
      x.ancestors.each { |y|
        b.call(y) unless x == y || y == Kernel || y == Object
      }
    }
    g.directed = true
  }
end
modulo(n, m) click to toggle source

modulo(30, 5).dotty

# File examples/examples.rb, line 18
def modulo (n, m)
  result = RGL::AdjacencyGraph.new
  1.upto(n) { |x|
    1.upto(n) { |y|
      result.add_edge x, y if x != y && x % m == y % m }
  }
  result
end
partite(n, m) click to toggle source

partite 8, 5

# File examples/examples.rb, line 7
def partite(n, m)
  result = RGL::DirectedAdjacencyGraph.new
  1.upto(n) { |i|
    1.upto(m) { |j|
      result.add_edge('a' + i.to_s, 'b' + j.to_s)
    }
  }
  result
end