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)
| 685 | #--- GRAPH TRAVERSAL ------------------------------------------------------------------------------- |
| 686 | |
| 687 | def 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 | |
| 706 | dfs = depth_first_search; |
| 707 |
nothing calls this directly
no test coverage detected
searching dependent graphs…