Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Can query adjacent vertices given a vertex. closes #9

  • Loading branch information...
commit dc52970eb6bb451b2f1b644f8e4ea4ae3cd47939 1 parent c7c7738
@D4L authored
View
1  lib/abstract_graph/graph.rb
@@ -26,3 +26,4 @@ class Graph
require "abstract_graph/graph/change_vertex_name"
require "abstract_graph/graph/change_edge_name"
require "abstract_graph/graph/is_adjacent"
+require "abstract_graph/graph/adjacency_list"
View
26 lib/abstract_graph/graph/adjacency_list.rb
@@ -0,0 +1,26 @@
+# required in "abstract_graph/graph"
+
+module AbstractGraph
+ class Graph
+
+ # return the adjacent vertices in of a vertex
+ # p: String s represents the name of the query vertex
+ def adjacency_list( s )
+ result = @edges.collect do |id,e|
+ e.vertices.collect do |v|
+ v.name
+ end - [s]
+ end.delete_if do |adj|
+ adj.size == 2
+ end.flatten
+
+ # return nil if result is empty
+ if result.size == 0
+ return nil
+ else
+ return result
+ end
+ end
+
+ end
+end
View
46 spec/abstract_graph/graph/adjacency_list_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+module AbstractGraph
+ module Composition
+ describe Graph do
+
+ before :all do
+ @vertex1 = "Vertex1"
+ @vertex2 = "Vertex2"
+ @vertex3 = "Vertex3"
+ @vertex4 = "Vertex4"
+ @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_vertex @vertex4
+ @graph.add_edge @edge1, @vertex1, @vertex2
+ @graph.add_edge @edge2, @vertex2, @vertex3
+ end
+
+ describe "#adjacency_list(String)" do
+
+ it "returns an array if there are adjacent vertices" do
+ @graph.adjacency_list( @vertex1 ).should be_an_instance_of(Array)
+ end
+
+ it "returns nil if there are no adjacent vertices" do
+ @graph.adjacency_list( @vertex4).should be_nil
+ end
+
+ it "returns the vertices that are connected with edges" do
+ @graph.adjacency_list( @vertex2 ).should include(@vertex1)
+ @graph.adjacency_list( @vertex2 ).should include(@vertex3)
+ @graph.adjacency_list( @vertex1 ).should include(@vertex2)
+ end
+
+ end
+
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.