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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:34:38+00:00 2026-06-12T14:34:38+00:00

I have been given a DFS algorithm that returns the shortest path to a

  • 0

I have been given a DFS algorithm that returns the shortest path to a goal node. It takes as arguments a graph (with all of the nodes and their paths), a starting node, a goal node, and a list of nodes that have been visited (initialized as empty). Here is the code:

def shortestPath(graph, start, end, visited = []):
    path = str(start)
    if start == end:
        return path
    shortest = None
    for node in graph.childrenOf(start):
        if str(node) not in visited:
            visited = visited + [str(node)]
            newPath = shortestPath(graph, start, end, visited)
            if newPath = None:
                continue
            if shortest == None or len(newPath) < shortest:
                shortest = newPath
    if shortest != None:
         path = path + shortest
    else:
         path = None
    return path

I understand the concept of Depth First Search, but this recursion is boggling my mind. Let me know if my train of thought is at all on track and where I’m getting derailed.

So basically, the recursion occurs when newPath is assigned a value, which of course is a call to shortestPath. At that point, the recursion goes all the way down the graph until it either reaches a node with no children or the goal node. If it reaches a node with no children that isn’t the goal, the base case is ignored, and the entire for loop is ignored, returning a value of none. That None then is simply passed up the chain of recursion. If it reaches the goal node, then the bottom layer of the recursion actually returns a value (path), which bubbles up to the top of the recursion.

At that point I get confused, because of what is going on with “shortest.” So every time an actual value is returned for newPath, shortest is assigned that value, but then it is added to path. But let’s say I have multiple paths to the goal node. I successfully find the first path, and path is equal to the sum of all of the newPaths/shortests. But then for the next recursion that successfully reaches the goal, doesn’t path just keep adding on more newPaths? So won’t the final result be a long list of every path I COULD visit to reach the goal node instead of the shortest path?

  • 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-12T14:34:39+00:00Added an answer on June 12, 2026 at 2:34 pm

    The path variable is local to the function. Every recursion call has its own stack frame – it is independent from the other calls). That means when the next call starts, then everything is brand new.

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

Sidebar

Related Questions

I have been given a static library to work with that accepts arguments as
I have been given a DLL (InfoLookup.dll) that internally allocates structures and returns pointers
I have been given a WSDL with all the method requests and responses, and
I have been given a task to create a Python file that will take
I have been given a file that I would like to extract the useful
I have been given the following code that should perform an ssl handshake and
I have been given a theme from a designer, and that theme has a
I have been given some 'reports' from another piece of software that contains data
I have been given a code that also involves an asynchronous socketing which I
I have been given the task of calling a web service which returns an

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.