Permalink
Browse files

Added adjacency check between two vertices. Part of #9

  • Loading branch information...
D4L committed Jan 23, 2013
1 parent dd58b25 commit c7c7738edb71032b9c84ddff8c3a82d10d4986c6
@@ -25,3 +25,4 @@ class Graph
require "abstract_graph/graph/delete_edge"
require "abstract_graph/graph/change_vertex_name"
require "abstract_graph/graph/change_edge_name"
+require "abstract_graph/graph/is_adjacent"
@@ -0,0 +1,20 @@
+# required in "abstract_graph/graph"
+
+module AbstractGraph
+ class Graph
+
+ # returns whether or not v1 and v2 are joined by an edge
+ # p: String v1, v2 represents the names of the vertices
+ def is_adjacent?( v1, v2 )
+ vertices = [v1, v2].sort!
+
+ ( @edges.each_value.find do |e|
+ vertices == e.vertices.map do |v|
+ v.name
+ end.sort
+ end && true ) || false
+
+ end
+
+ end
+end
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+module AbstractGraph
+ module Composition
+ describe Graph do
+
+ before :all do
+ @vertex1 = "Vertex1"
+ @vertex2 = "Vertex2"
+ @vertex3 = "Vertex3"
+ @edge1 = "Edge1"
+ @edge2 = "Edge2"
+ end
+
+ before :each do
+ @graph = Graph.new
+ @graph.add_vertex @vertex1
+ @graph.add_vertex @vertex2
+ @graph.add_vertex @vertex3
+ @graph.add_edge @edge1, @vertex1, @vertex2
+ @graph.add_edge @edge2, @vertex2, @vertex3
+ end
+
+ describe "#is_adjacent?(String,String)" do
+
+ it "returns true if two vertices are joined with an edge" do
+ @graph.is_adjacent?( @vertex1, @vertex2 ).should be_true
+ end
+
+ it "returns false if two vertices are not joined with an edge" do
+ @graph.is_adjacent?( @vertex1, @vertex3 ).should be_false
+ end
+
+ end
+
+ end
+ end
+end

0 comments on commit c7c7738

Please sign in to comment.