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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:42:22+00:00 2026-05-16T23:42:22+00:00

I am searching for a non-recursive depth first search algorithm on graphs in Pascal

  • 0

I am searching for a non-recursive depth first search algorithm on graphs
in Pascal (Delphi).

I need DFS for computing strongly or bi-connected components of large graphs.
Currently I am using a recursive variant of the algorithm: http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm

The problem is that for such algorithm I must define a large amount of memory
to be used for a stack and that makes later problems in Windows 7,
where Open and Save Dialogs do not work because of several threads generated….

So again: I do not see how to rewrite the Tarjan DFS algorithm
to work without recursion. Do you have any suggestion –
or point to a non recursice algorithm for depth first search on graphs?

Thanks.

  • 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-16T23:42:22+00:00Added an answer on May 16, 2026 at 11:42 pm

    The algorithm as described on Wikipedia looks to reasonably easily be made non-recursive with an explicit stack. Starting out with that (included here for reference, in case Wikipedia changes):

    Input: Graph G = (V, E)
    
    index = 0                                         // DFS node number counter 
    S = empty                                         // An empty stack of node
    for all v in V do
      if (v.index is undefined)                       // Start a DFS at each node
        tarjan(v)                                     // we haven't visited yet
    
    procedure tarjan(v)
      v.index = index                                 // Set the depth index for v
      v.lowlink = index                               // SHOULD BE v.lowlink = MAX_INT?
      index = index + 1
      S.push(v)                                       // Push v on the stack
      for all (v, v') in E do                         // Consider successors of v
        if (v'.index is undefined)                    // Was successor v' visited?
            tarjan(v')                                // Recurse
            v.lowlink = min(v.lowlink, v'.lowlink)
        else if (v' is in S)                          // Was successor v' in stack S? 
            v.lowlink = min(v.lowlink, v'.index )     // v' is in stack but it isn't in the dfs tree
      if (v.lowlink == v.index)                       // Is v the root of an SCC?
        print "SCC:"
        repeat
          v' = S.pop                                  
          print v'
        until (v' == v)
    

    Step 1: Remove loops containing recursion, adding labels and gotos. This is necessary to make loop variables explicit, savable and restorable (needed during recursion-simulation with stacks). A label needs to be added after tarjan()‘s return, as we’ll jump to it in a moment.

    procedure tarjan(v)
      v.index = index                                 // Set the depth index for v
      v.lowlink = index                               // SHOULD BE v.lowlink = MAX_INT?
      index = index + 1
      S.push(v)                                       // Push v on the stack
      succ = all (v, v') in E      // Consider successors of v
      succIndex = 0                // presume succ is 0-based
    loop_top:
      if succIndex >= Length(succ) goto skip_loop
      v' = succ[succIndex]
      if (v'.index is undefined)                    // Was successor v' visited?
          tarjan(v')                                // Recurse
    recursion_returned:
          v.lowlink = min(v.lowlink, v'.lowlink)
      else if (v' is in S)                          // Was successor v' in stack S? 
          v.lowlink = min(v.lowlink, v'.index )     // v' is in stack but it isn't in the dfs tree
      succIndex = succIndex + 1
      goto loop_top
    skip_loop:
      if (v.lowlink == v.index)                       // Is v the root of an SCC?
        print "SCC:"
        repeat
          v' = S.pop                                  
          print v'
        until (v' == v)
    

    Step 2: Introduce a stack which contains all the relevant state for storing our position and computation in the loop at any point where we may be returning from recursion, or starting out at the top of the loop.

    The stack:

    T = empty // T will be our stack, storing (v, v', succ, succIndex, state)
    

    state is an enumeration (TopState, ReturnedState) encoding the location in the procedure. Here’s the procedure rewritten to use this stack and state rather than recursion:

    procedure tarjan(v)
      while (T is not empty) do
        (v, v', succ, succIndex, state) = T.pop
        case state of
          TopState: goto top
          ReturnedState: goto recursion_returned
        end case
    top:
        v.index = index                                 // Set the depth index for v
        v.lowlink = index                               // SHOULD BE v.lowlink = MAX_INT?
        index = index + 1
        S.push(v)                                       // Push v on the stack
        succ = all (v, v') in E      // Consider successors of v
        succIndex = 0                // presume succ is 0-based
    loop_top:
        if succIndex >= Length(succ) goto skip_loop
        v' = succ[succIndex]
        if (v'.index is undefined)                    // Was successor v' visited?
          // instead of recursing, set up state for return and top and iterate
          T.push(v, v', succ, succIndex, ReturnedState) // this is where we return to
          T.push(v', empty, empty, empty, TopState) // but this is where we go first
          continue // continue the while loop at top
    recursion_returned:
          v.lowlink = min(v.lowlink, v'.lowlink)
        else if (v' is in S)                          // Was successor v' in stack S? 
          v.lowlink = min(v.lowlink, v'.index )     // v' is in stack but it isn't in the dfs tree
        succIndex = succIndex + 1
        goto loop_top
    skip_loop:
        if (v.lowlink == v.index)                       // Is v the root of an SCC?
        print "SCC:"
        repeat
          v' = S.pop                                  
          print v'
        until (v' == v)
    

    Step 3: Finally, we need to make sure the entry conditions are correct, for the top-level code which calls tarjan. That can easily be done by an initial push:

    procedure tarjan(v)
      T.push(v, empty, empty, empty, TopState)
      while (T is not empty) do
        (v, v', succ, succIndex, state) = T.pop
        case state of
          TopState: goto top
          ReturnedState: goto recursion_returned
        end case
    top:
        v.index = index                                 // Set the depth index for v
        v.lowlink = index                               // SHOULD BE v.lowlink = MAX_INT?
        index = index + 1
        S.push(v)                                       // Push v on the stack
        succ = all (v, v') in E      // Consider successors of v
        succIndex = 0                // presume succ is 0-based
    loop_top:
        if succIndex >= Length(succ) goto skip_loop
        v' = succ[succIndex]
        if (v'.index is undefined)                    // Was successor v' visited?
          // instead of recursing, set up state for return and top and iterate
          T.push(v, v', succ, succIndex, ReturnedState) // this is where we return to
          T.push(v', empty, empty, empty, TopState) // but this is where we go first
          continue // continue the while loop at top
    recursion_returned:
          v.lowlink = min(v.lowlink, v'.lowlink)
        else if (v' is in S)                          // Was successor v' in stack S? 
          v.lowlink = min(v.lowlink, v'.index )     // v' is in stack but it isn't in the dfs tree
        succIndex = succIndex + 1
        goto loop_top
    skip_loop:
        if (v.lowlink == v.index)                       // Is v the root of an SCC?
        print "SCC:"
        repeat
          v' = S.pop                                  
          print v'
        until (v' == v)
    

    It could also be done by a jump, jumping immediately to top. The code can be further cleaned up, perhaps converted to use a while or repeat loop to eliminate some of the gotos, etc., but the above should be at least functionally equivalent, eliminating the explicit recursion.

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

Sidebar

Related Questions

While searching for a proper way to trim non-breaking space from parsed HTML, I've
Searching with '[Delphi] source control' didn't return much, so here goes: For those of
Searching from google.com, like www.abc.com Search Result Rank the pages like Title..... Description... www.abc.com
Searching a Python dictionary based on the value first, to get a key output
Partials in XML builder are proving to be non-trivial. After some initial Google searching,
While searching the web for concurrency in jvm I found questions about searching Non-blocking
I've tested a weird query that involves me searching for non-existent records. In a
When first searching the web for a simple CakePHP way to convert small news
I need to mark some elements as non required in the XSD schema. How
Disclaimer: I did try searching first, and even found this comment , but it

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.