Ok, first to get the terminology straight, 'node' eq 'vertex' and 'edge' eq 'link'.
In your code both nodes and links have an id. Let's look at your original code:
Yes, your algorithm is fine, it's just the implementation that has a little bug in it. Here's my (correct) code again, this time written explicitly:# this marks the current vertex $explored->{$node->{_id}}++; foreach my $link (@{$node->{_outlinks}}) { # $link isa Link, so $link->{_id} is the id of the link, # not of the vertex!! $do_search->($link->{_to}) unless ($explored->{$link->{_id}}); }
$explored->{$node->{_id}}++; foreach my $link (@{$node->{_outlinks}}) { my $new_node = $link->{_to}; $do_search->($new_node) unless ($explored->{$new_node->{_id}}); }
-- Hofmator
In reply to Re3: Depth First Search through Digraph Results in Memory Leak
by Hofmator
in thread Depth First Search through Digraph Results in Memory Leak
by djantzen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |