class Graph
  attr_accessor :graph

  def initialize
    @graph = Hash.new { |hash, key| hash[key] = [] }
  end

  def addEdge(a, b)
    @graph[a] << b
    @graph[b] << a
  end


  def searchPath(a, b, result,&bl)
    result = result+[a]
  bl.call(result) if a == b
    @graph[a].each do |v|
         searchPath(v, b, result,&bl) if ! result.include?(v)
    end
  end

end

g2 = Graph.new
g2.addEdge('a','e')
g2.addEdge('e','b')
g2.addEdge('e','f')
g2.addEdge('f','b')
g2.addEdge('a','c')
g2.addEdge('c','b')
p g2.graph
g2.searchPath('a','b', []) { |path|
  puts "path : #{path.join(", ")} / len=#{path.size}"
}

-- 
Posted via http://www.ruby-forum.com/.