Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: How to print/draw a network graph by blokhead
in thread How to print/draw a network graph by GrandFather

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2022-05-23 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?