Python Forum
Requesting help with my implementation of depth-first search
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Requesting help with my implementation of depth-first search
#6
Here's the more efficient version I was thinking of:

def DFS(graph, root):
    visited = set([root])
    current = root
    possibles = (node for node in graph[current] if node not in visited)
    search_stack = []
    edges = []
    while len(visited) < len(graph):
        try:
            possible = next(possibles)
            edges.append((current, possible))
            visited.add(possible)
            search_stack.append((current, possibles))
            current = possible
            possibles = (node for node in graph[current] if node not in visited)
        except StopIteration:
            current, possibles = search_stack.pop()
    return edges
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: Requesting help with my implementation of depth-first search - by ichabod801 - Sep-25-2019, 01:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Max recursion depth.... Error MeloB 2 1,965 Feb-16-2022, 05:21 PM
Last Post: MeloB
  requesting help for a faster alternative of pd.groupby zollen 1 1,720 Jul-07-2021, 06:33 PM
Last Post: ndc85430
Bug maximum recursion depth exceeded while calling a Python object error in python3 Prezess 4 3,819 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  Requesting help with 3D plotting with quivers Akan2019 1 1,649 May-15-2019, 07:46 AM
Last Post: Akan2019
  RecursionError: maximum recursion depth exceeded in comparison ? leoahum 11 13,217 Mar-18-2019, 01:53 PM
Last Post: leoahum
  variable loop depth Skaperen 5 4,430 Jul-18-2018, 02:48 AM
Last Post: Skaperen
  maximum recursion depth exceeded saba_keon 3 7,508 Apr-08-2018, 07:30 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020