1 # A simple graph library
4 # A vertex in a graph. The edge can have arbitrary attributes,treated as
6 class Vertex
< OpenStruct
7 def initialize(values
= {})
13 # Connect this vertex to another, creating an Edge to do so, and returning
15 def connect(other
, edge_attributes
= {})
16 e
= Edge
.new edge_attributes
19 # other.edges << e unless self === other
23 # Connect this vertex to another, creating an Edge to do so, and returning
30 # Return the set of neighbouring vertices
32 #vertices = self.edges.map {|e| e.vertices}.flatten
33 #vertices_to_me = vertices.select {|v| v == self}
34 #other_vertices = vertices.select {|v| v != self}
35 #(vertices_to_me[1..-1] || []) + other_vertices#
36 self.edges
.map
{|e
| e
.vertices
.take_while
{|v
| v
!= self} +
37 e
.vertices
.drop_while
{|v
| v
!= self}[1..-1]}.flatten
41 '<V: ' + self.name
.to_s
+ '>'
48 dot
= self.object_id
.to_s
51 dot
<< opts
.keys
.map
{ |k
|
52 (k
.to_s
+ ' = "' + self.instance_eval(opts
[k
].to_s
).to_s
) + '"'