Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re3: Depth First Search through Digraph Results in Memory Leak

by Hofmator (Curate)
on Jan 09, 2004 at 13:44 UTC ( #320109=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Depth First Search through Digraph Results in Memory Leak
in thread Depth First Search through Digraph Results in Memory Leak

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:

# 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}}); }
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:
$explored->{$node->{_id}}++; foreach my $link (@{$node->{_outlinks}}) { my $new_node = $link->{_to}; $do_search->($new_node) unless ($explored->{$new_node->{_id}}); }

-- Hofmator


Comment on Re3: Depth First Search through Digraph Results in Memory Leak
Select or Download Code
Re: Re3: Depth First Search through Digraph Results in Memory Leak
by djantzen (Priest) on Jan 09, 2004 at 14:00 UTC

    Whoops, my mistake. The example was simple enough not to affect the outcome but you're quite right. Fortunately my real code doesn't contain that error.


    "The dead do not recognize context" -- Kai, Lexx

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-10-22 06:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (114 votes), past polls