Product graphs working initially
[graph.njae.git] / lib / graph.njae / vertex.rb
1 require 'ostruct'
2
3 # A simple graph library
4
5 module GraphNjae
6 # A vertex in a graph. The edge can have arbitrary attributes,treated as
7 # method names.
8 class Vertex < OpenStruct
9 def initialize(values = {})
10 super(values)
11 self.edges = []
12 self
13 end
14
15 # Connect this vertex to another, creating an Edge to do so, and returning
16 # the Edge
17 def connect(other)
18 e = Edge.new
19 e << self << other
20 # self.edges << e
21 # other.edges << e unless self === other
22 e
23 end
24
25 # Connect this vertex to another, creating an Edge to do so, and returning
26 # this Vertex
27 def <<(other)
28 connect(other)
29 self
30 end
31
32 # Return the set of neighbouring vertices
33 def neighbours
34 #vertices = self.edges.map {|e| e.vertices}.flatten
35 #vertices_to_me = vertices.select {|v| v == self}
36 #other_vertices = vertices.select {|v| v != self}
37 #(vertices_to_me[1..-1] || []) + other_vertices#
38 self.edges.map {|e| e.vertices.take_while {|v| v != self} +
39 e.vertices.drop_while {|v| v != self}[1..-1]}.flatten
40 end
41
42 end
43 end