Anatoly Yakovenko | 16 May 07:34
Picon

fgl Data.Graph examples

Can someone post some simple examples of using the Data.Graph library?
So I can define a simple graph

let g = buildG (1,2) [(1,2), (2,1)]

but how do i label my edges and nodes?  i want to be able to name my
nodes, and add weights to the edges and be able to update those
weights as well.

Thanks,
Anatoly
Gökhan San | 16 May 14:13
Favicon

Re: fgl Data.Graph examples

Hi,

I'm new to this, but I'll give it a shot.

> import Data.Graph.Inductive

There are several ways to build a graph. Let's say, node labels are 'String's 
and edge labels are 'Double's. Edge labels would be the weights.

> grs :: [Gr String Double]

The below four lines generate the same graph. From labeled nodes and edges:

> grs = [mkGraph [(2, "start"), (1, "end")] [(2, 1, 1.0)], 

>        insEdge (2, 1, 1.0) $ insNodes [(2, "start"), (1, "end")] empty, 

From contexts:

>        ([], 2, "start", [(1.0, 1)]) & (([], 1, "end", []) & empty),

>        buildGr [([], 2, "start", [(1.0, 1)]), ([], 1, "end", [])]]

Test graph equality:

> gr = head grs
> same = all (equal gr) (tail grs)

You can populate the graph using (&) and ins... functions while 
using 'newNodes' to get a bunch of unused node indexes ('Node's) to avoid 
(Continue reading)

Anatoly Yakovenko | 16 May 20:29
Picon

Re: fgl Data.Graph examples

Thank you, that's exactly what i was looking for.

> I don't think there is a straightforward way to tweak individual nodes and
> edges other than using graph construction tools. Maybe someone could shed a
> light on this.

I think i can define updateEdge in terms of insEdge and delEdge

Gmane