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

  • SEARCH
  • Home
  • 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 7521601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:16:30+00:00 2026-05-30T02:16:30+00:00

I have a graph which I traverse using a typical visitor pattern. I’ve run

  • 0

I have a graph which I traverse using a typical visitor pattern. I’ve run into an issue where I need to know if the node being visited has already been visited during the current traversal.

I’ve developed a solution that I think would work, but it would require creating and destroying node “flags” during/after the graph traversal.

That is, as each node is visited, a flag object pointer member in the node would be checked. If it’s NULL, the visitor would create a flag object and assign it to the node’s flag object pointer. Then, the visitor would push a reference to the flag pointer member into it’s own internal list ( of pointers to flag object pointers, of course ). Otherwise, if the node’s flag object pointer isn’t NULL, the visitor stops traversal on that node.

Cleanup would then be a matter of popping / deleting the flag objects from the visitor’s list after the traversal is complete, and reassigning each node flag pointer in the list to NULL.

It’s a bit involved and strikes me as being potentially leak-prone, but I’ve no better ideas…

Thoughts?

As an addendum, the purpose is to list out in a text console the structure of a tree. If I have several nodes, however, which are parents of a common subgraph, I want to list that subgraph only once and then refer to it using some nomenclature like “[ Subnode1…]” everywhere else.

I mean this for two purposes –

  1. I don’t want to constantly dump the same data to the screen several times
  2. I want a way to visually indicate where a node is simply referencing another part of the existing graph.

As such, setting / clearing a bool as each node is traversed defeats the purpose. I don’t want to clear any bools until the root node traversal is complete (i.e., the very last step of the traversal). And, of course, by that point, the question becomes, how do I get all of those flags to reset themselves without re-visiting the entire graph?

Anyway, I’d rather not traverse the graph twice (once to do the work and again to clear flags) or constantly iterate a list each time I visit a node to determine if I’ve visited it before. The graph isn’t large, but it’s part of a render subsystem and the traversal occurs between frames, so I want it to make sure it runs quickly…

  • 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-05-30T02:16:32+00:00Added an answer on May 30, 2026 at 2:16 am

    Typical Visitor pattern for a single Node class:

    class Node;
    class NodeVisitorInterface
    {
        public:
            virtual ~NodeVisitor() {}
            virtual bool visitNode(Node& node) = 0;
    };
    
    // Note: I have made the accept() method virtual
    //       But if you do derive from node you should add each derived type to 
    //       the `NodeVisitorInterface` with its own specific version of visitNode.
    //       
    //       What makes graphs easy with the visitor pattern is that there is usually only
    //       one type of node. Thus the visitor interface is trivially easy. 
    class Node
    {
        public:
           virtual void accept(NodeVisitorInterface& visitor)
           {
               // For the generic this node you call the visitor
               if (visitor.visitNode(*this))
               {
    
                   // For all linked nodes you get them to accept the visitor
                   // So they can call visitNode() for themselves.
                   //
                   foreach(LinkedNode as node)            // Note pseudo code as I don't 
                   {                                      // know how you specify links
                        node.accept(visitor);
                   }
               }
           }
     };
    

    The above defined the generic implementation of a visitor for a graph.
    The thing about graphs is that they usually only have one node type so that makes the visitor interface very easy. Now a simple implementation of the visitor interface that makes sure we don’t processes nodes more than once.

     class VisitNodesOnce: public NodeVisitorInterface
     {
        public:
            virtual bool visitNode(Node& node)
            {
                if (visitedNodes.find(&node) != visitedNodes.end())
                {
                     // Node already handled just return.
                     return false;
                }
                // The address of a node should be a unique identifier of the node
                // Thus by keeping the address of all the visited nodes we can track
                // them and not repeat any work on these nodes.
                visitedNodes.insert(&node);
    
                this->doWorkOnUniqueNode(node);
                return true;
            }
            virtual void doWorkOnUniqueNode(Node& node) = 0;
    
        private:
            set<Node*>   visitedNodes;
     };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a recursive DFS algorithm to traverse a graph: void Graph<E, N>::DFS(Node
I have a graph which I would like to represent using an image on
Say I have a set (or graph) which is partitioned into groups. I am
I have a graph which contains an unknown number of disconnected subgraphs. What's a
I have made a sample application which constructs a filter graph to capture audio
Today I have such graph. I run it on windows (source: narod.ru ) I
I have created a graph component in AS3 which extends UIComponent. I created an
I have a custom made class called Graph in which I use adjacency lists.
Let's say I have a graph with heavy nodes, that is each node is
I have a graph-theoretic (which is also related to combinatorics) problem that is illustrated

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.