<?xml version="1.0" encoding="windows-1252"?>
<node id="320109" title="Re3: Depth First Search through Digraph Results in Memory Leak" created="2004-01-09 08:44:09" updated="2004-08-22 16:45:38">
<type id="11">
note</type>
<author id="85506">
Hofmator</author>
<data>
<field name="doctext">
&lt;p&gt;Ok, first to get the terminology straight, 'node' eq 'vertex' and 'edge' eq 'link'.
&lt;p&gt;In your code both nodes and links have an id. Let's look at your original code:
&lt;code&gt;# this marks the current vertex
$explored-&gt;{$node-&gt;{_id}}++;

foreach my $link (@{$node-&gt;{_outlinks}}) {
   # $link isa Link, so $link-&gt;{_id} is the id of the link,
   # not of the vertex!!
   $do_search-&gt;($link-&gt;{_to}) unless ($explored-&gt;{$link-&gt;{_id}});
}
&lt;/code&gt;
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:
&lt;code&gt;$explored-&gt;{$node-&gt;{_id}}++;
foreach my $link (@{$node-&gt;{_outlinks}}) {
   my $new_node = $link-&gt;{_to};
   $do_search-&gt;($new_node) unless ($explored-&gt;{$new_node-&gt;{_id}});
}
&lt;/code&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-85506"&gt;
&lt;P&gt;-- Hofmator&lt;/P&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
319780</field>
<field name="parent_node">
320105</field>
</data>
</node>
