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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.