Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: How to print/draw a network graph

by blokhead (Monsignor)
on Jun 29, 2006 at 05:59 UTC ( #558224=note: print w/replies, xml ) Need Help??


in reply to How to print/draw a network graph

I don't know whether you need an ASCII-art representation or an image, but for this job, GraphViz springs instantly to mind for me. Its raison d'être is to layout graphs, and there's a lot of research that's gone into it. It does its job very well.

If you'd rather do something by hand, then as a first step here is how you "rank" the vertices to get a nice attractive flow graph for a DAG:

  • The vertices that have no incoming edges (the "sources"), get rank = 0.
  • All other vertices get rank 1+max{rank(u)}, where the max is over all predecessors u.
You can compute the ranks by doing a kind of breadth-first search from the source vertices (it's easy to check whether a vertex is a source). Set them as rank=0, then repeatedly set the outgoing neighbors of all the rank-i vertices as the rank-(i+1) vertices. Note that you may end up setting a vertex's rank several times. And if there's a cycle, you never halt.

Now if you lay out the vertices in columns so that the i-th column contains the rank-i vertices, then when you draw in the edges, they will all go from left to right (it's easy to see this from the definition of rank). It should look very pretty.

This is essentially what GraphViz (dot engine) does anyway, at least as a first step according to its description on the main GraphViz page. It also has smart ways to arrange the vertices within each column, to minimize the number of edge crossings and other Ugly Things. And it will do something sensible when there are cycles. And it allows you to override and tweak everything of course.

blokhead

Replies are listed 'Best First'.
Re^2: How to print/draw a network graph
by GrandFather (Saint) on Jun 30, 2006 at 02:21 UTC

    Following a little egging on and a simple example from blokhead (my thanks for that!) the following code generates the graph I was after. Note that this code uses dot from GraphViz.

    use warnings; use strict; my @lines = <DATA>; my @splitLines = map {[/'(....)'..([-\d]*)\) -> '?(....)'?..([-\d]*)/] +} @lines; my $graphStr = <<GRAPH; digraph G { graph [rankdir=LR]; node [shape=rect]; GRAPH $graphStr .= " $_->[0]$_->[1] -> $_->[2]$_->[3]\n" for @splitLines; $graphStr =~ s/-(?!>)/_/g; $graphStr .= "}\n"; open outFile, '>', 'graph.dot'; print outFile $graphStr; close outFile; `dot -Tpng -ograph.png graph.dot`; __DATA__ 'TAvi' (-37) -> 'TDMk' (-32) 'TAvi' (-38) -> 'TMrk' (-34) 'TDMk' (-32) -> xfer (-12) 'TDif' (-7) -> xfer (-11) 'TDif' (-8) -> 'TMCF' (-35) 'TDig' (-27) -> 'TSpN' (-33) 'TLCI' (-36) -> 'TAvi' (-37) 'TMCF' (-35) -> 'TLCI' (-36) 'TMrk' (-34) -> xfer (-28) 'TSpN' (-33) -> 'TAvi' (-38) 'TSpN' (-33) -> 'TDMk' (-32) 'TSpN' (-33) -> 'TMCF' (-35)

    DWIM is Perl's answer to Gödel
Re^2: How to print/draw a network graph
by GrandFather (Saint) on Jun 29, 2006 at 09:31 UTC

    Thanks for that. GraphViz is probably over kill, at least in the sense of how much work may be required getting up to speed with it, for this task.

    However your analysis of an algorythm is along the lines that I'd been thrashing around and gives me enough confidence to have another go. I was reluctant to do all the backtracking and fixups that seemed to be required and spent a lot of time trying to find a more direct approach. I'll post the code when I get it sorted out.


    DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://558224]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2022-05-24 04:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?