Skip to content

Commit

Permalink
Added adjacency check between two vertices. Part of #9
Browse files Browse the repository at this point in the history
  • Loading branch information
D4L committed Jan 23, 2013
1 parent dd58b25 commit c7c7738
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/abstract_graph/graph.rb
Expand Up @@ -25,3 +25,4 @@ class Graph
require "abstract_graph/graph/delete_edge" require "abstract_graph/graph/delete_edge"
require "abstract_graph/graph/change_vertex_name" require "abstract_graph/graph/change_vertex_name"
require "abstract_graph/graph/change_edge_name" require "abstract_graph/graph/change_edge_name"
require "abstract_graph/graph/is_adjacent"
20 changes: 20 additions & 0 deletions lib/abstract_graph/graph/is_adjacent.rb
@@ -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
38 changes: 38 additions & 0 deletions spec/abstract_graph/graph/is_adjacent_spec.rb
@@ -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.