Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How do I use Graph::Traversal?

by runrig (Abbot)
on Jul 06, 2005 at 23:34 UTC ( [id://472973]=note: print w/replies, xml ) Need Help??


in reply to How do I use Graph::Traversal?

You are doing a Depth First Search, so 'E' is the first result. Perhaps what you want is a Breadth First Search?

Update: that still wouldn't give you what you expect though...I think BFS would return A, B, b, C, c, D, d, E, e).

Last Update?: What you want is DFS, but use $t->preorder instead of $t->dfs. This returns (A, b, c, d, e, B, C, D, E) which maybe acceptably close to what you are expecting.

Aha, and use the next_alphabetic option:

my $trav = Graph::Traversal::DFS->new($graph, next_alphabetic=>1); my $v; print "$v\n" while $v = $trav->preorder; __END__ A B C D E b c d e

Replies are listed 'Best First'.
Re^2: How do I use Graph::Traversal?
by thor (Priest) on Jul 07, 2005 at 02:54 UTC
    I wish I could use the next_alphabetic. However, my example script is a paper tiger; it doesn't reflect the nature of my data (which is not ordered alphabetically). Looking at my actual data and the Graph module, topological_sort might get the job done.

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

      A topo sort is not strict enough for what you want (it just guarantees parents are returned before children), and in this example returns (A, B, C, D, b, c, d, e, E). Is there any way to force an alphabetical order on your vertices? I'd try adding a fixed length prefix to every vertex as you're adding edges, if at all possible. E.g., start with $p = "0000" (or however many places necessary), and just do $p++ before adding vertex "${p}_$v". And then strip the prefix as each vertex is returned before you need to use it.
        As it turns out, a topological sort is good enough for my purposes. The actual scenario is thus: I have a series of batch jobs that are related by dependencies. I noticed that some jobs submitted before the jobs on which they depend submit. So, for each job, I want to find the earliest possible submittal time such that at submittal time, the given job has a chance to run. As you said, a topological sort returns parents before children, this'll suffice for the given task. Thanks for your help!

        Incidentally, I was originally going to go with a breadth-first search. I ran in to the same sort of problem that I did with depth-first: the nodes weren't returning in the order that I expected. It was in the trial and error that I tried to do anything with depth-first at all.

        thor

        Feel the white light, the light within
        Be your own disciple, fan the sparks of will
        For all of us waiting, your kingdom will come

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 20:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found