# Flexible graph library - sans hash

**URL:** <https://www.ruby-forum.com/t/flexible-graph-library-sans-hash/96440>\
**Category:** Ruby\
**Created:** [June 28, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/flexible-graph-library-sans-hash/96440 "2006-06-28T20:01:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 28, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/flexible-graph-library-sans-hash/96440/1 "2006-06-28T20:01:14Z")

</div>

anyone out there know of a graph library that doesn’t use a hash in the  
impl,  
thereby restricting only one edge from u --\>\> v? i’ve used rgl  
extensively in  
the past but it uses a hash under the hood and has this restriction.

i’m coding a FSM class so i need to be able to have multiple labeled  
edges  
from u --\>\> v.

regards.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 29, 2006, 5:04am UTC](https://www.ruby-forum.com/t/flexible-graph-library-sans-hash/96440/2 "2006-06-29T05:04:19Z")

</div>

On 6/28/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i’m coding a FSM class so i need to be able to have multiple labeled edges  
> from u --\>\> v.

Try Graphviz - Graph Visualization Software ([http://www.graphviz.org/](http://www.graphviz.org/))  
it is driven by a simple text language, which is easy to generate and  
drive from Ruby.

pth

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 29, 2006, 2:40pm UTC](https://www.ruby-forum.com/t/flexible-graph-library-sans-hash/96440/3 "2006-06-29T14:40:35Z")

</div>

On 6/28/06, Phil T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why can’t you use a hash of arrays?
> 
> graph = { ‘a’=\>[‘b’,‘c’],  
> ‘b’=\>[‘a’],  
> ‘c’=\>[‘b’, ‘c’] }
> 
> I’ve used that sort of structure for FSMs in the past.
> 
> Phil

Wow I feel stupid, not enough sleep and the word graph, I thought you  
were trying to visualize a FSM. Implementing a graph – I have used a  
hash of arrays and other times I have used a list of lists (when my  
states had a numeric or at least sequential index).

Good thing I am off on vacation, good luck  
pth

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 29, 2006, 5:07am UTC](https://www.ruby-forum.com/t/flexible-graph-library-sans-hash/96440/4 "2006-06-29T05:07:43Z")

</div>

On 6/28/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> anyone out there know of a graph library that doesn’t use a hash in the impl,  
> thereby restricting only one edge from u --\>\> v? i’ve used rgl extensively in  
> the past but it uses a hash under the hood and has this restriction.
> 
> i’m coding a FSM class so i need to be able to have multiple labeled edges  
> from u --\>\> v.

Why can’t you use a hash of arrays?

graph = { ‘a’=\>[‘b’,‘c’],  
‘b’=\>[‘a’],  
‘c’=\>[‘b’, ‘c’] }

I’ve used that sort of structure for FSMs in the past.

Phil

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 29, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/flexible-graph-library-sans-hash/96440/5 "2006-06-29T16:36:49Z")

</div>

On Thu, 29 Jun 2006, Patrick H. wrote:

> Wow I feel stupid, not enough sleep and the word graph, I thought you  
> were trying to visualize a FSM. Implementing a graph – I have used a  
> hash of arrays and other times I have used a list of lists (when my  
> states had a numeric or at least sequential index).
> 
> Good thing I am off on vacation, good luck  
> pth

no problem - i was actually trying to both. i’ve built something on top  
of  
robert felt’s graphing toolkit which includes simple visulization, dsl  
for  
building state machines, and a blocking thread-safe state change  
notification.

roberts toolkit had support for multiple edges with metadata between  
states.

i’ll try to put out a release soon.

regards.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 30, 2006, 2:53am UTC](https://www.ruby-forum.com/t/flexible-graph-library-sans-hash/96440/6 "2006-06-30T02:53:35Z")

</div>

On 6/28/06, Phil T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why can’t you use a hash of arrays?
> 
> graph = { ‘a’=\>[‘b’,‘c’],  
> ‘b’=\>[‘a’],  
> ‘c’=\>[‘b’, ‘c’] }
> 
> I’ve used that sort of structure for FSMs in the past.

Oh, now that I reread the request, it seems you need to do something  
like:

graph = { ‘a’ =\> [‘b’, ‘b’, ‘c’] …}

correct? But then maybe this hash of arrays isn’t all that useful, or  
perhaps you need one more level, like:

graph = { ‘a’ =\> [ { ‘b’ =\> ‘label1’}, {‘b’=\>‘label2’},  
{‘c’=\>‘labelA2C’}] … }

I can’t remember how many times I’ve implemented FSM classes (I’ve  
lost count), but the big question always seems to be do I need an Edge  
class of some sort or are the edges implicit in the graph data  
structure (it can go either way depending on what you need to do with  
the graph).  
It’s kind of like looking at one of those optical illusions where at  
one moment the ink defines what you perceive and at another moment  
it’s the whitespace… or something like that. But in this case where  
you can have multiple edges going from u–\>v maybe it would be  
probably be helpful to have an edge class.

If you had Nodes and Edges, like so:

class Node  
def initialize(name, &action)  
@name = name  
@action = action  
end  
def do\_action  
@action.call  
end  
attr\_accessor :name  
end

class Edge  
#you could probably also put conditions  
#here as in the condition that will  
#activate this transition from the ‘from’  
#state to the ‘to’ state  
def initialize(from, to, label)  
@from, @to = from,to  
end  
attr\_accessor :from, :to, :label  
end

#and an FSM class to keep track of it all:  
class FSM  
attr\_accessor :nodes, :graph  
def initialize( \*nodes) #note: starting node should be first  
@start = start\_node  
@nodes = nodes  
@graph = Hash.new {|hash,key| hash[key] = [] }  
end  
def add\_edge edge  
@graph[edge.from] \<\< edge  
end

end

… or something along those lines. Then you have a hash keyed by the  
nodes where each node points to a list of edges which start from that  
node (and you can always get the destination node from the edge).

Phil
