Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Community Voices

Dr. Dobb's Bloggers

Google Treasure Hunt 3: a Depth-First Search Solution

July 26, 2008

This is another Google Treasure Hunt I solved. I know that with this one, you really doesn’t need to use any kind of programming skills. But I guess that there would be no fun if you just use only your mind to solve it! Try to teach your computer to make it for you… The problem I got was the following:

“Below is a diagram of a computer network. The nodes are hosts on the network, and the lines between them are links. A packet is sent out from host P with a destination of Which nodes does the packet pass through on its way to the destination? (include start and final node in your answer)”

The diagram for the given computer network can be designated by the routing table below:

A                  => =>  =>            

B              => => =>           

C                 => => =>           

D              => => =>           

E               => =>                          =>    

F                   => => =>           

G              =>    => =>           

H                => => =>           

I                  => => =>          

J             =>  =>  =>           

K               => => =>           

L           => => =>           

M           => => =>           

N              =>  => =>           

O              => => =>           

P             => => =>           

As you may suppose, the first symbol is the character identifying the network node, followed by its IP number. Next, you have a sequence of 3 routing rules, where the related node can see if one of these rules can fit with the incoming packet. The last IP number gives us the “default route”, just in the case the network node couldn’t find any matching rule. In this case, the packet will be forwarded through the outgoing network card to the given host in the last column.

I proposed a solution with C++, which uses depth-first search as the method to look for a valid path going from a given point to another in the problem network model. My main idea is to put my depth-first search function walking node by node (labeled as A, B, C, etc.), iterating over each one of the routing table rules, and searching for one of these rules that can route my packet through the network. The function below is self-explainable:

bool RoutingTable::routeTo( string node, string* resp )


       bool do_it_again = true;


       RoutingEntry rentry = findRouteEntry( node );


       if ( rentry.doneThis() )

               return false;


       resp->append( rentry.getId() );


       if ( rentry.getAddress().compare(getFinalDestination()) == 0 ) {

               return false;



       vector<RoutingRule> routing_rules = rentry.getRules();

       vector<RoutingRule>::iterator i;



       // go through each of the routing rules

       for ( i = routing_rules.begin(); ( i != routing_rules.end() ) &&

                      do_it_again; i++ )


               if ( (*i).canRoute(getFinalDestination()) ) {

                      do_it_again = routeTo( (*i).getRedirection(), resp );

               } // if              

       } // for


       // no rules; asks the default gateway…

       if ( do_it_again )


               do_it_again = routeTo( rentry.getDefaultRoute().getRedirection(),


Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.