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 9009087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:06:10+00:00 2026-06-16T02:06:10+00:00

I’m trying to traverse a graph with DFS. But when I tried to pass

  • 0

I’m trying to traverse a graph with DFS.

But when I tried to pass visited node list as a function’s parameter, I found there is a problem.

When I reached at the node which has no connected node except its previous node, the recursive call ends and the information about visited nodes disappears so fall into infinite loop…

Is there an any way to keep an information about visited nodes except using imperative way?

  • 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-16T02:06:11+00:00Added an answer on June 16, 2026 at 2:06 am

    Elaborating on Jeffrey’s answer, you have several different styles available. I give here only snippets that I haven’t tested, so there may be small or large mistakes.

    1. You can use side-effects everywhere:

      module NodeSet = Set.Make(...)
      
      let traverse action graph_root =
        let visited = ref NodeSet.empty in
        let rec loop node =
          action node;
          visited := NodeSet.add node !visited;
          let handle child =
            if not (NodeSet.mem child !visited)
            then loop acc child in
          List.iter handle (children node)
        in loop graph_root
      

      The “visit” applies the imperative function action to all nodes in
      the graph.

    2. You can store the visited node in a mutable reference, but thread
      the state of the traversal as an accumulator acc instead of
      sequencing side-effects directly. This would correspond to a use of
      the State monad.

      let traverse action init_state graph_root =
        let visited = ref NodeSet.empty in
        let rec loop acc node =
          let acc = action acc node in
          visited := NodeSet.add node !visited;
          let handle acc child =
            if NodeSet.mem child !visited
            then acc
            else loop acc child in
          List.fold_left handle acc (children node)
        in loop init_state graph_root
      
    3. You can reuse this state-passing logic to also pass the visited
      node information.

      let traverse action init_state graph_root =
        let rec loop acc visited node =
          let acc = action acc node in
          let visited = NodeSet.add node visited in
          let handle (acc, visited) child =
            if NodeSet.mem child !visited
            then (acc, visited)
            else loop acc visited child in
          List.fold_left handle (acc, visited) (children node)
        in loop init_state NodeSet.empty graph_root
      
    4. Finally, you can move to a tail-recursive traversal by passing
      information about which nodes should be computed next in the first
      recursive call. This corresponds to a general transformation into
      Continuation Passing Style, but with a domain-specific representation
      of continuations (simply nodes to visit).

      let traverse action init_state graph_root =
        let rec loop acc visited = function
          | [] -> acc
          | node::to_visit ->
             if NodeSet.mem node visited then loop acc visited to_visit
             else begin
               let acc = action acc node in
               let visited = NodeSet.add node visited in
               let to_visit = children node @ to_visit in
               loop acc visited to_visit
             end
        in loop NodeSet.empty init_state [graph_root]
      

      Jeffrey remarks that with this presentation, you can change the
      traversal order from DFS to BFS by simply changing the way to_visit
      is updated, adding children nodes to the end of the sequence rather
      than at the beginning (which requires a queue structure to be
      algorithmically efficient).

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

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.