Skip to content
This repository
Browse code

Added adjacency check between two vertices. Part of #9

  • Loading branch information...
commit c7c7738edb71032b9c84ddff8c3a82d10d4986c6 1 parent dd58b25
Austin Lee authored January 23, 2013
1  lib/abstract_graph/graph.rb
@@ -25,3 +25,4 @@ class Graph
25 25
 require "abstract_graph/graph/delete_edge"
26 26
 require "abstract_graph/graph/change_vertex_name"
27 27
 require "abstract_graph/graph/change_edge_name"
  28
+require "abstract_graph/graph/is_adjacent"
20  lib/abstract_graph/graph/is_adjacent.rb
... ...
@@ -0,0 +1,20 @@
  1
+# required in "abstract_graph/graph"
  2
+
  3
+module AbstractGraph
  4
+  class Graph
  5
+
  6
+    # returns whether or not v1 and v2 are joined by an edge
  7
+    # p: String v1, v2 represents the names of the vertices
  8
+    def is_adjacent?( v1, v2 )
  9
+      vertices = [v1, v2].sort!
  10
+
  11
+      ( @edges.each_value.find do |e|
  12
+        vertices == e.vertices.map do |v|
  13
+          v.name
  14
+        end.sort
  15
+      end && true ) || false
  16
+
  17
+    end
  18
+
  19
+  end
  20
+end
38  spec/abstract_graph/graph/is_adjacent_spec.rb
... ...
@@ -0,0 +1,38 @@
  1
+require 'spec_helper'
  2
+
  3
+module AbstractGraph
  4
+  module Composition
  5
+    describe Graph do
  6
+
  7
+      before :all do
  8
+        @vertex1 = "Vertex1"
  9
+        @vertex2 = "Vertex2"
  10
+        @vertex3 = "Vertex3"
  11
+        @edge1 = "Edge1"
  12
+        @edge2 = "Edge2"
  13
+      end
  14
+
  15
+      before :each do
  16
+        @graph = Graph.new
  17
+        @graph.add_vertex @vertex1
  18
+        @graph.add_vertex @vertex2
  19
+        @graph.add_vertex @vertex3
  20
+        @graph.add_edge @edge1, @vertex1, @vertex2
  21
+        @graph.add_edge @edge2, @vertex2, @vertex3
  22
+      end
  23
+
  24
+      describe "#is_adjacent?(String,String)" do
  25
+
  26
+        it "returns true if two vertices are joined with an edge" do
  27
+          @graph.is_adjacent?( @vertex1, @vertex2 ).should be_true
  28
+        end
  29
+
  30
+        it "returns false if two vertices are not joined with an edge" do
  31
+          @graph.is_adjacent?( @vertex1, @vertex3 ).should be_false
  32
+        end
  33
+
  34
+      end
  35
+
  36
+    end
  37
+  end
  38
+end

0 notes on commit c7c7738

Please sign in to comment.
Something went wrong with that request. Please try again.