3 # A simple graph library
7 # A container for all the parts of a graph. The graph can have arbitrary attributes,
8 # treated as method names.
9 class Graph
< OpenStruct
10 def initialize(values
= {})
12 self.edges
= Array
.new
13 self.vertices
= Array
.new
17 # Add a Vertex or Edge to the graph.
19 if other
.class.ancestors
.include? Vertex
20 self.vertices
<< other
21 elsif other
.class.ancestors
.include? Edge
27 # Form a product graph of this graph and the other.
28 # Return the new graph.
30 product_graph
= Graph
.new
31 self.vertices
.each
do |v1
|
32 other
.vertices
.each
do |v2
|
33 product_vertex
= Vertex
.new
34 product_vertex
.left_node
= v1
35 product_vertex
.right_node
= v2
36 product_graph
<< product_vertex