Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8958753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:17:14+00:00 2026-06-15T15:17:14+00:00

i have implemented a simple DFS (non recursive) that ‘tests’ if a Path between

  • 0

i have implemented a simple DFS (non recursive) that ‘tests’ if a Path between StartNode and EndNode exists. It works as expected ( handling bidirectional/directional graphs ) – but i just can’t figure out how to store the Path for later usage.

Currently i am debug-printing Visited nodes, but it’s not what is should be storing.

Can somebody please help me out / shed a bit of light on – what exactly should i store and at what point to return a list of nodes from NodeStart to NodeEnd ?

Here is the example Graph:

Graph

Here is the DFS traversing function:

bool DFS(CNavigationGraph *pNavGraph, CNavigationGraphNode* pFromNode, CNavigationGraphNode* pToNode)
{
    assert(pNavGraph);
    assert(pFromNode);
    assert(pToNode);

    std::vector<CNavigationGraphNode*> vpVisitedNodes;
    std::vector<CNavigationGraphNode*> stack;

    stack.push_back(pFromNode);

    while(!stack.empty())
    {
        CNavigationGraphNode     *pTop = stack.back();
        stack.pop_back();

        // Ok We've reached pToNode - means, path pFromNode to pToNode available
        if(pTop == pToNode)
        {
            for(int a = 0; a < vpVisitedNodes.size(); a++)
            {
                CLogger::Instance()->Write(XLOGEVENT_LOCATION, "{VISITED} %s",vpVisitedNodes[a]->GetNodeName().data());
            }
            return true;
        }

        // Add to visited list
        vpVisitedNodes.push_back(pTop);

        // Look for adjacent Nodes for pTop
        std::vector<CNavigationGraphNode*> vpAdjacentNodes;
        pNavGraph->GetAdjacentNodes(pTop->GetNodeName(), vpAdjacentNodes);
        for(int x = 0; x < vpAdjacentNodes.size(); x++)
        {
            // Add to stack if not visited
            if(IsVisited(vpVisitedNodes, vpAdjacentNodes[x]) == false)
                stack.push_back(vpAdjacentNodes[x]);
        }
    }

    // Path not found
    return false;
}

Here is the Debug Output:

Find Path Between Node1 and Node3

<main> [] DFS TRAVERSE TEST (DIRECTIONAL)
<DFS> [] {VISITED} Node1
<DFS> [] {VISITED} Node4
<DFS> [] {VISITED} Node5
<main> [] Path from Node1 to Node3 - YES

Find Path Between Node3 and Node1

<main> [] DFS TRAVERSE TEST (DIRECTIONAL)
<main> [] Path from Node3 to Node1 - NO
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:17:15+00:00Added an answer on June 15, 2026 at 3:17 pm

    If I understand your algorithm correctly (and it is a DFS),
    from the starting point you are taking a step to the direction of the first non-visited node. If there is no route to your target from that node, you step back and try to go to the next non-visited node, in the mean time carefully administrating which nodes were visited.

    All you need to add is a stack to which you always push the node you are taking a step to,
    and pop it from the stack if you had to step back. This stack will store your route from the start_node to the target. It also helps you to determine where to step back.

    Here is your code, finally it grew a bit lengthier than I’ve thought but here it is:

    // I call fromNode: start_node, toNode: target_node.
    
    std::stack<CNavigationGraphNode*> DFS(CNavigationGraph *pNavGraph, CNavigationGraphNode* start_node, CNavigationGraphNode* target_node)
    {
        using namespace std;
    
        stack<CNavigationGraphNode*> route; // route to target
        unordered_set<CNavigationGraphNode*> visited_nodes;  // hash set on who is visited
        vector<CNavigationGraphNode*> adjacent_nodes;
    
        CNavigationGraphNode* current_node = start_node;
        while(current_node!=target_node)
        {
          pNavGraph->GetAdjacentNodes(current_node->GetNodeName(), adjacent_nodes); 
    
          // "for each"; you can use any form of looping through the neighbors of current_node.
          bool found_non_visited = false;
          for(auto node : adjacent_nodes) 
          {
            if(visited_nodes.find(node) == visited_nodes.end())
            {
              route.push(current_node);
              visited_nodes.insert(node);
              current_node = node;
              found_non_visited = true;
              break;
            }
          }
          if(found_non_visited) 
            continue;
    
          if(route.empty())
            return route; // empty route means no route found
          current_node = route.pop();
        }
        route.push(target);
        return route;
    }
    

    And now you can pop_back your way from start to target or pop your way from target to start. If the route is empty, that corresponds to returning false from your original function.

    Reference on unordered_set and stack, both in STL. It is implemented with a bucket has so it is much faster than set or map, which are usually implemented with red-black trees.

    Remark: std::unordered_set is a C++11 extension, feel free to replace it with the slower std::set if you use C++03.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a simple entity ejb with a @version annotation. I expect that
I have implemented a simple appmod that handle WebSockets and echo back the messages.
I have implemented a very simple method: private String getProfileName(String path) { String testPath
I have implemented a simple D3.js line chart that can be zoomed and panned.
I have implemented a simple GPS program that fetches the co-ordinates and displays them
I have implemented a simple little DSL that generates classes from input files, by
I have implemented a simple Android application that I now would like to test
We have implemented a drag and drop feature that shows a nice simple representation
I have implemented a simple search function that highlights a row in a DataGrid
I have implemented a simple linux shell in c. Now, I am adding some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.