Merge branch 'master' into graph-product
authorNeil Smith <neil.github@njae.me.uk>
Mon, 5 Mar 2012 16:56:49 +0000 (16:56 +0000)
committerNeil Smith <neil.github@njae.me.uk>
Mon, 5 Mar 2012 16:56:49 +0000 (16:56 +0000)
Gemfile
Gemfile.lock
lib/graph.njae/edge.rb
lib/graph.njae/graph.rb
lib/graph.njae/vertex.rb
spec/graph/edge_spec.rb
spec/graph/graph_spec.rb
spec/graph/vertex_spec.rb

diff --git a/Gemfile b/Gemfile
index d6b498fb33e8fc5f7a6d1556d256581d62551779..f677f3d2db9e7086230d0faa88ab187cbba78e09 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -6,9 +6,8 @@ source "http://rubygems.org"
 # Add dependencies to develop your gem here.
 # Include everything needed to run rake, tests, features, etc.
 group :development do
-  gem "rspec", "~> 2.6.0"
-  gem "bundler", "~> 1.0.0"
-  gem "jeweler", "~> 1.6.2"
-  gem "rcov", ">= 0"
+  gem "rspec"
+  gem "bundler"
+  gem "jeweler"
   gem "rdoc"
 end
index 024a43aff37e114ec4542cae7494b01eb7830a87..9b1b95bce3c4f321cef4e1b43dc973e6891a0f5c 100644 (file)
@@ -1,30 +1,31 @@
 GEM
   remote: http://rubygems.org/
   specs:
-    diff-lcs (1.1.2)
+    diff-lcs (1.1.3)
     git (1.2.5)
-    jeweler (1.6.2)
+    jeweler (1.8.3)
       bundler (~> 1.0)
       git (>= 1.2.5)
       rake
-    rake (0.9.2)
-    rcov (0.9.9)
-    rdoc (3.7)
-    rspec (2.6.0)
-      rspec-core (~> 2.6.0)
-      rspec-expectations (~> 2.6.0)
-      rspec-mocks (~> 2.6.0)
-    rspec-core (2.6.4)
-    rspec-expectations (2.6.0)
+      rdoc
+    json (1.6.5)
+    rake (0.9.2.2)
+    rdoc (3.12)
+      json (~> 1.4)
+    rspec (2.8.0)
+      rspec-core (~> 2.8.0)
+      rspec-expectations (~> 2.8.0)
+      rspec-mocks (~> 2.8.0)
+    rspec-core (2.8.0)
+    rspec-expectations (2.8.0)
       diff-lcs (~> 1.1.2)
-    rspec-mocks (2.6.0)
+    rspec-mocks (2.8.0)
 
 PLATFORMS
   ruby
 
 DEPENDENCIES
-  bundler (~> 1.0.0)
-  jeweler (~> 1.6.2)
-  rcov
+  bundler
+  jeweler
   rdoc
-  rspec (~> 2.6.0)
+  rspec
index f38a32b21e693bbf645c25bdcca159579f97dceb..4a1ba790d4f33570f53525cbd3f3c23660135240 100644 (file)
@@ -10,8 +10,8 @@ module GraphNjae
   # Each connection is handled by a Graph::Connection object, so that each end
   # of the Edge can have it's own attributes.
   class Edge < OpenStruct
-    def initialize
-      super
+    def initialize(values = {})
+      super(values)
       self.connections = []
       self
     end
@@ -20,6 +20,7 @@ module GraphNjae
     def <<(other)
       c = Connection.new
       c.end = other
+      other.edges << self unless other.edges.include? self
       self.connections << c
       self
     end
@@ -38,8 +39,8 @@ module GraphNjae
   # A connection between an Edge and a Vertex.The connection can have arbitrary attributes,
   # treated as method names.
   class Connection < OpenStruct
-    def initialize
-      super
+    def initialize(values = {})
+      super(values)
       self
     end
   end
index e08a1ad8544d12aec4cacf3a095ca5e1e5df6aea..faaaf46bca692f1f4367a5aec36b8db4fc290aa2 100644 (file)
@@ -7,8 +7,8 @@ module GraphNjae
   # A container for all the parts of a graph.  The graph can have arbitrary attributes,
   # treated as method names.
   class Graph < OpenStruct
-    def initialize
-      super
+    def initialize(values = {})
+      super(values)
       self.edges = Array.new
       self.vertices = Array.new
       self
@@ -23,5 +23,40 @@ module GraphNjae
       end
       self
     end
-  end
+    
+    # Connects two vertices, creating and storing a new edge
+    # Also adds the vertices, unless they're already in the graph
+    def connect(vertex1, vertex2)
+      self.vertices << vertex1 unless self.vertices.include? vertex1
+      self.vertices << vertex2 unless self.vertices.include? vertex2
+      edge = Edge.new
+      self.edges << edge
+      edge << vertex1 << vertex2
+    end
+    
+    # Form a product graph of this graph and the other.
+    # Return the product graph.
+    def product(other)
+      product_graph = Graph.new
+      self.vertices.each do |v1|
+        other.vertices.each do |v2|
+          product_graph << Vertex.new({:g1_vertex => v1, :g2_vertex => v2})
+        end
+      end
+      self.edges.each do |e1|
+        e1_vertices = e1.vertices
+        other.edges.each do |e2|
+          e2_vertices = e2.vertices
+          source = product_graph.vertices.find {|v| v.g1_vertex == e1_vertices[0] and v.g2_vertex == e2_vertices[0]}
+          destination = product_graph.vertices.find {|v| v.g1_vertex == e1_vertices[1] and v.g2_vertex == e2_vertices[1]}
+          product_graph.connect source, destination
+          source = product_graph.vertices.find {|v| v.g1_vertex == e1_vertices[0] and v.g2_vertex == e2_vertices[1]}
+          destination = product_graph.vertices.find {|v| v.g1_vertex == e1_vertices[1] and v.g2_vertex == e2_vertices[0]}
+          product_graph.connect source, destination
+        end
+      end
+      product_graph
+    end
+    
+  end # class
 end
index 4a75b6df2c628c0cba8a25fead7aff670a1c1563..aeabe30559be5f1f0a4cf1acf19a7607ff6a1249 100644 (file)
@@ -6,8 +6,8 @@ module GraphNjae
   # A vertex in a graph. The edge can have arbitrary attributes,treated as 
   # method names.
   class Vertex < OpenStruct
-    def initialize
-      super
+    def initialize(values = {})
+      super(values)
       self.edges = []
       self
     end
@@ -17,8 +17,8 @@ module GraphNjae
     def connect(other)
       e = Edge.new
       e << self << other
-      self.edges << e
-      other.edges << e unless self === other
+      self.edges << e
+      other.edges << e unless self === other
       e
     end
     
index 2e7dbfcc9a64863648ed928699155ea173c004b0..837386421ef66ae70e726a8404b9e9a9f6669121 100644 (file)
@@ -8,6 +8,15 @@ module GraphNjae
         e = Edge.new
         e.connections.should be_empty
       end
+      
+      it "creates an edge with some parameters" do
+        e = Edge.new :value1 => 1, :value2 => "value2", :value3 => :v3
+        e.value1.should == 1
+        e.value2.should == "value2"
+        e.value3.should == :v3
+        e.value4.should be_nil
+      end
+
     end # #initialize
     
     describe "adds attribues" do
@@ -26,11 +35,13 @@ module GraphNjae
         e.should have(1).connections
         e.should have(1).vertices
         e.vertices.should include(v1)
+        v1.edges.should include(e)
         e << v2
         e.should have(2).connections
         e.should have(2).vertices
         e.vertices.should include(v1)
         e.vertices.should include(v2)
+        v2.edges.should include(e)
       end
       
       it "adds several vertices to an edge" do
index 09cc6a433cddd36d28d78fa4c09fc9f2542450da..670d252892cbf0a3d1f6bbc09f0a04915ed61d63 100644 (file)
@@ -16,6 +16,15 @@ module GraphNjae
         g.edges.should be_empty
         g.vertices.should be_empty
       end
+      
+      it "creates a graph with some parameters" do
+        g = Graph.new :value1 => 1, :value2 => "value2", :value3 => :v3
+        g.value1.should == 1
+        g.value2.should == "value2"
+        g.value3.should == :v3
+        g.value4.should be_nil
+      end
+
     end # #initialize
       
     describe "adds attribues" do
@@ -75,8 +84,51 @@ module GraphNjae
 
     describe "connect" do
       it "adds and records an edge between vertices" do
+        g.vertices.should be_empty
+        g.edges.should be_empty
+        v1 = Vertex.new(:name => :v1)
+        v2 = Vertex.new(:name => :v2)
+        g.connect(v1, v2)
+        
+        g.should have(2).vertices
+        g.vertices.should include(v1)
+        g.vertices.should include(v2)
+        g.should have(1).edges
       end
     end # #connect
     
+    describe "product" do
+      it "finds a product graph of a pair of one-vertex graphs" do
+        g1 = Graph.new
+        g2 = Graph.new
+        g1v1 = Vertex.new
+        g1 << g1v1
+        g2v1 = Vertex.new
+        g2 << g2v1
+        product = g1.product g2
+        
+        product.should have(1).vertices
+        product.vertices.first.g1_vertex.should == g1v1
+        product.vertices.first.g2_vertex.should == g2v1
+        product.edges.should be_empty
+      end
+
+      it "finds a product graph of a pair of simple graphs" do
+        g1 = Graph.new
+        g2 = Graph.new
+        g1v1 = Vertex.new(:name => :g1v1)
+        g1v2 = Vertex.new(:name => :g1v2)
+        g1.connect(g1v1, g1v2)
+        g2v1 = Vertex.new(:name => :g2v1)
+        g2v2 = Vertex.new(:name => :g2v2)
+        g2.connect(g2v1, g2v2)
+        pg = g1.product g2
+        
+        pg.should have(4).vertices
+        pg.should have(2).edges
+      end
+
+    end
+    
   end
 end
index 49ce58ccf92b6c234b9ce2a4264b4e6f4ece74c0..b193bc81057558d24f9cf10ca7674937cb5d570d 100644 (file)
@@ -9,6 +9,14 @@ module GraphNjae
         v = Vertex.new
         v.edges.should be_empty
       end
+      
+      it "creates a vertex with some parameters" do
+        v = Vertex.new :value1 => 1, :value2 => "value2", :value3 => :v3
+        v.value1.should == 1
+        v.value2.should == "value2"
+        v.value3.should == :v3
+        v.value4.should be_nil
+      end
     end # #initialize
     
     describe "adds attribues" do