Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added adjacency check between two vertices. Part of #9
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |