# Graphs with decorated edges

**URL:** <https://www.ruby-forum.com/t/graphs-with-decorated-edges/87567>\
**Category:** Ruby\
**Created:** [March 4, 2007, 1:09am UTC](https://www.ruby-forum.com/t/graphs-with-decorated-edges/87567 "2007-03-04T01:09:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edgardo\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@Edgardo\_H](https://www.ruby-forum.com/u/Edgardo_H)\
**Post date:** [March 4, 2007, 1:09am UTC](https://www.ruby-forum.com/t/graphs-with-decorated-edges/87567/1 "2007-03-04T01:09:03Z")

</div>

Hello dear rubyists,

I’m working on an application that simulates several components  
interacting with each other. I would like to represent this situation  
using a graph, where each component is a vertex and the interaction of  
2 components is an edge between them.

I’ve taking using RGL [0] to handle graphs and it seems very nice.  
However, I don’t find a way to add a label or a list of labels to the  
edges.

Have I overlooked this functionality in RGL? Can you suggest an  
alternative lib to handle graphs?

Thanks and kind regards,  
Ed

[0] [http://rgl.rubyforge.org/rgl/index.html](http://rgl.rubyforge.org/rgl/index.html)

–  
Encontrá a “Tu psicópata favorito” [http://tuxmaniac.blogspot.com](http://tuxmaniac.blogspot.com)

The future is not what it used to be.  
– Paul Valéry

---

<div class="post-metadata">

**Author:** ![Edgardo\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@Edgardo\_H](https://www.ruby-forum.com/u/Edgardo_H)\
**Post date:** [March 4, 2007, 2:06am UTC](https://www.ruby-forum.com/t/graphs-with-decorated-edges/87567/2 "2007-03-04T02:06:09Z")

</div>

I’ve just recently worked on something similar (making a picture of  
the graph of components configured in an XML file). I ended up using  
GRATR, which is just a fork of RGL: [http://gratr.rubyforge.org/](http://gratr.rubyforge.org/) It  
also didn’t seem to handle the labels for edges, so I used the  
DOT::DOTDigraph class directly instead of the actual graph object.

I added the following directly to that class to get the output using  
DOT functionality:

class DOT::DOTDigraph  
def write\_to\_graphic\_file(fmt=‘png’, dotfile=‘graph’)  
src = dotfile + ‘.dot’  
dot = dotfile + ‘.’ + fmt

```
File.open(src, 'w') {|f| f << self.to_s << "\n"}

system( "dot -T#{fmt} #{src} -o #{dot}" )
dot

```

end  
end

Adding nodes looks like this:

@graph \<\< DOT::DOTNode.new(‘name’ =\> name, ‘label’ =\> label,  
‘fontsize’ =\> fontsize)

And adding edges looks like this:

@graph \<\< DOT::DOTDirectedArc.new(‘from’ =\> from, ‘to’ =\> to, ‘label’  
=\> label, ‘fontsize’ =\> fontsize)

There may be a better way to do it, but that’s what I came up with.

-Stephen

---

<div class="post-metadata">

**Author:** ![Edgardo\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@Edgardo\_H](https://www.ruby-forum.com/u/Edgardo_H)\
**Post date:** [December 21, 2010, 10:20pm UTC](https://www.ruby-forum.com/t/graphs-with-decorated-edges/87567/3 "2010-12-21T22:20:49Z")

</div>

Using GRATR, you can add labels to the edges of the graph by adding a  
third parameter when you do that add\_edge! which is a Hash of options  
for that edge. E.g. g.add\_edge!(node1, node2, :label =\> “My edge  
label”)

---

<div class="post-metadata">

**Author:** ![Edgardo\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@Edgardo\_H](https://www.ruby-forum.com/u/Edgardo_H)\
**Post date:** [March 4, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/graphs-with-decorated-edges/87567/4 "2007-03-04T16:36:10Z")

</div>

> I’ve taking using RGL [0] to handle graphs and it seems very nice.  
> However, I don’t find a way to add a label or a list of labels to the  
> edges.
> 
> Have I overlooked this functionality in RGL? Can you suggest an  
> alternative lib to handle graphs?  
> You can check the wrapper around boost::graph I wrote for one of my  
> projets.  
> Check the documentation of the BGL module at  
> [http://www.laas.fr/~sjoyeux/doc/api/roby/](http://www.laas.fr/~sjoyeux/doc/api/roby/)

The main features are:

- a vertex can be any Ruby object
- a vertex can be part of many graphs (see Vertex#each\_graph)
- edges can be decorated (‘info’ parameter to Graph#link and  
Vertex#[])
- availability of basic boost::graph algorithms: component, DFS, BFS.  
I  
usually add more when I need them …

You can also check Roby::RelationGraph and  
Roby::DirectedRelationSupport,  
which are specific extensions to the graph API I use for the Roby  
project.

If you find it useful, I can create a separate project for it.
