Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
My knowledge about Graphs is quite limited, so I hope this is an easy question for some of you: I need a class that only accepts DAGs (Directed Acyclic Graphs). Based on Graph, the easiest solution is:
package DAG; use strict; use warnings; use Graph::Directed; use Carp; use base 'Graph::Directed'; sub new { my ( $self, %args ) = @_; $args{multiedged} = 1; my $obj = $self->SUPER::new(%args); return $obj; } sub set_parents { my ( $self, $p1, $p2, $c, $checks ) = @_; $checks ||=1; if ( ( $self->in_degree($c) || 0) > 0) { croak "already has parents"; } $self->add_edge_by_id($p1, $c, 1); $self->add_edge_by_id($p2, $c, 2); if ($checks && !$self->is_dag()) { $self->delete_edge_by_id($p1, $c, 1); $self->delete_edge_by_id($p2, $c, 2); croak 'Not a DAG anymore'; } return $self; }
I add always parents-child triples. So add_parents() ist just a method that adds two edges. This code is really slow. My current attempt is a simple breadth first search:
... sub is_ancestor { my ( $self, $v1, $v2 ) = @_; for my $child ( $self->get_successors($v1) ) { return 1 if $child eq $v2; } for my $child ( $self->get_successors($v1) ) { my $found = $self->is_ancestor( $child, $v2 ); return 1 if $found; } return 0; } sub set_parents { my ( $self, $p1, $p2, $c, $checks ) = @_; $checks ||=1; if ( ( $self->in_degree($c) || 0) > 0) { croak "already has parents"; } $self->add_edge_by_id($p1, $c, 1); if ($checks && $self->is_ancestor($c, $p1)) { $self->delete_edge_by_id($p1, $c, 1); croak 'Not a DAG anymore'; } $self->add_edge_by_id($p2, $c, 2); if ($checks && $p1 ne $p2 && $self->is_ancestor($c, $p2)) { $self->delete_edge_by_id($p2, $c, 2); croak 'Not a DAG anymore'; } return $self; }
Which is faster, but still too slow. Do you see how I could improve the performance? My goal is a simple random walk optimization, so I always remove the two incoming edges of a node and then add new ones. The number of nodes is between 100 and 700.

Am I doing something wrong here? Should I use another Graph module as base?

Update: Sorry for writing so unclear :( . What I now have is a big set of parents-child triples:

P1 --+ +--> Child P2 --+
I start with greedy adding triples to the graph until all nodes are added. Then I start a simple MCMC optimization in the way that I select randomly a node, remove the incoming edges if they exist and then add two new valid incoming edges (another triple where the selected node is a child). So I have a DAG, remove zero or two edges, add two new ones (both directing in the same node) and want that the class very quickly checks whether the two new edges have introduced a directed circle.

With hundred nodes I get only about 50 iterations a second (without any scoring function). I am quite sure that there is a lot to improve.

Update 2: Graph::edges is quite expensive. After removing this in my MCMC iterations,I get about 200 iterations a second, which is at least something I can live with.


In reply to A fast DAG class needed by lima1

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 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?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 21:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found