Permalink
Browse files

Allow edges to be created by passing in two vertex names. Part of #5

  • Loading branch information...
1 parent d2d2c12 commit 006df60b571a8ecaa9261df29732cfc413aa50a5 @D4L committed Jan 6, 2013
@@ -18,3 +18,4 @@ def name=(name)
end
require "abstract_graph/composition/edge/initialize"
+require "abstract_graph/composition/edge/is_coincident"
@@ -0,0 +1,15 @@
+# required in "abstract_graph/composition/edge"
+
+module AbstractGraph
+ module Composition
+ class Edge
+
+ # returns whether or not e covers the same two vertices
+ # p: Edge e is the comparing edge
+ def is_coincident? e
+ return ( ( e.vertices | @vertices ) - ( e.vertices & @vertices ) ).empty?
+ end
+
+ end
+ end
+end
@@ -9,6 +9,7 @@ class Graph
include Composition
attr_accessor :vertices
+ attr_accessor :edges
end
@@ -19,3 +20,4 @@ class Graph
require "abstract_graph/graph/has_vertex"
require "abstract_graph/graph/dup"
require "abstract_graph/graph/delete_vertex"
+require "abstract_graph/graph/add_edge"
@@ -0,0 +1,30 @@
+# required in "abstract_graph/graph"
+
+module AbstractGraph
+ class Graph
+
+ # add a vertex named s to the graph joining
+ # the vertices named v1 and v2
+ # p: String s represents name
+ # Vertex name v1 and v2 are the two vertices
+ # r: errors when v1, v2 doesn't exist, loop, or multiple edge
+ def add_edge( s, v1, v2 )
+ v1 = @vertices[v1] or return nil
+ v2 = @vertices[v2] or return nil
+
+ raise Exception if v1 == v2
+
+ # create the edge
+ edge = Edge.new s, v1, v2
+
+ # check if it's an multiple edge
+ @edges.each_value do |e|
+ raise Exception if e.is_coincident? edge
+ end
+
+ @edges.add edge
+ self
+ end
+
+ end
+end
@@ -7,6 +7,8 @@ class Graph
def initialize
@vertices = UniqueNameCollection.new
+ @edges = UniqueNameCollection.new
+ @vertices.link @edges
end
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+module AbstractGraph
+ module Composition
+ describe Edge do
+
+ before :all do
+ @v1 = Vertex.new "v1"
+ @v2 = Vertex.new "v2"
+ @v3 = Vertex.new "v3"
+ end
+
+ before :each do
+ @edge = Edge.new( "e1", @v1, @v2 )
+ end
+
+ describe "#is_coincident?(Edge)" do
+
+ it "returns true if it joins the same two vertices" do
+ edge2 = Edge.new( "e2", @v1, @v2 )
+ @edge.is_coincident?( edge2 ).should be_true
+ end
+
+ it "returns false if the two edges span >= 3 vertices" do
+ edge2 = Edge.new( "e2", @v1, @v3 )
+ @edge.is_coincident?( edge2 ).should be_false
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+module AbstractGraph
+ describe Graph do
+
+ before :each do
+ @vertex1 = "Vertex1"
+ @vertex2 = "Vertex2"
+ @vertex3 = "Vertex3"
+ @graph = Graph.new
+ @graph.add_vertex @vertex1
+ @graph.add_vertex @vertex2
+ @graph.add_vertex @vertex3
+ end
+
+ describe "#add_edge(String, String, String)" do
+
+ it "returns an object of class Graph" do
+ @graph.add_edge( "MyEdge", @vertex1, @vertex2 ).should be_an_instance_of(Graph)
+ end
+
+ it "throws an exception when adding edge of existing name" do
+ @graph.add_edge( "MyEdge", @vertex1, @vertex2 ).should be_an_instance_of(Graph)
+ expect { @graph.add_edge( "MyEdge", @vertex1, @vertex3 ) }.to raise_error
+ expect { @graph.add_edge( @vertex1, @vertex1, @vertex3) }.to raise_error
+ end
+
+ it "doesn't allow an edge to be a loop" do
+ expect { @graph.add_edge( "MyEdge", @vertex1, @vertex1 ) }.to raise_error
+ end
+
+ it "doesn't allow multiple edges" do
+ @graph.add_edge( "MyEdge", @vertex1, @vertex2 )
+ expect { @graph.add_edge( "MyEdge2", @vertex1, @vertex2 ) }.to raise_error
+ end
+
+ it "returns nil if any of the vertices don't exist" do
+ @graph.add_edge( "MyEdge", @vertex1, "randomString" ).should be_nil
+ @graph.add_edge( "MyEdge", "randomString", @vertex1 ).should be_nil
+ @graph.edges["MyEdge"].should be_nil
+ end
+
+ end
+
+ end
+end

0 comments on commit 006df60

Please sign in to comment.