MCPcopy Index your code
hub / github.com/clips/pattern / depth_first_search

Function depth_first_search

pattern/graph/__init__.py:687–704  ·  view source on GitHub ↗

Visits all the nodes connected to the given root node, depth-first. The visit function is called on each node. Recursion will stop if it returns True, and subsequently dfs() will return True. The traversable function takes the current node and edge, and returns True

(node, visit=lambda node: False, traversable=lambda node, edge: True, _visited=None)

Source from the content-addressed store, hash-verified

685#--- GRAPH TRAVERSAL -------------------------------------------------------------------------------
686
687def depth_first_search(node, visit=lambda node: False, traversable=lambda node, edge: True, _visited=None):
688 """ Visits all the nodes connected to the given root node, depth-first.
689 The visit function is called on each node.
690 Recursion will stop if it returns True, and subsequently dfs() will return True.
691 The traversable function takes the current node and edge,
692 and returns True if we are allowed to follow this connection to the next node.
693 For example, the traversable for directed edges is follows:
694 lambda node, edge: node == edge.node1
695 """
696 stop = visit(node)
697 _visited = _visited or {}
698 _visited[node.id] = True
699 for n in node.links:
700 if stop: return True
701 if traversable(node, node.links.edge(n)) is False: continue
702 if not n.id in _visited:
703 stop = depth_first_search(n, visit, traversable, _visited)
704 return stop
705
706dfs = depth_first_search;
707

Callers

nothing calls this directly

Calls 1

edgeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…