1 # A simple graph library
5 # An edge (or multiedge) in a graph. The edge can have arbitrary attributes,
6 # treated as method names.
8 # Each connection is handled by a Graph::Connection object, so that each end
9 # of the Edge can have it's own attributes.
10 class Edge
< OpenStruct
11 def initialize(values
= {})
17 # Connect this edge to a vertex
21 other
.edges
<< self unless other
.edges
.include? self
26 # Return the set of vertices this edge connects.
28 self.connections
.map
{|c
| c
.end}
31 # Return the connection object that joins this Edge to the specified Vertex
32 def connection_at(vertex
)
33 self.connections
.find
{|c
| c
.end.equal
? vertex
}
36 # Return the vertex at the other end of the one given.
37 # Self-loops should still return the vertex
39 if self.vertices
[0] == vertex
47 '<E: ' + self.type
.to_s
+ ' [' + self.vertices
.map
{|n
| n
.to_s
}.join(', ') + '] >'
54 dot
= self.connections
[0].end.object_id
.to_s
+ " -- " + self.connections
[1].end.object_id
.to_s
57 dot
<< opts
.keys
.map
{ |k
|
58 (k
.to_s
+ ' = "' + self.instance_eval(opts
[k
].to_s
).to_s
) + '"'
68 # A connection between an Edge and a Vertex.The connection can have arbitrary attributes,
69 # treated as method names.
70 class Connection
< OpenStruct
71 def initialize(values
= {})