def find_children(graph: Matrix, starting_node: int, has_visited: List[int], remaining_targets: List[int], curr_matrix: Matrix) -> Iterator[Matrix]: # Get all children of root. children_of_root = graph.get_connected_lst(starting_node) for child in children_of_root: if child == starting_node: logging.debug("Skipping duplicate node: {0}".format(child)) continue if child in has_visited: logging.debug("Skipping visited child: {0}".format(child)) continue if child in remaining_targets: # We found one of our target nodes! logging.debug("Found a child node: {0}".format(child)) # TODO: Bug here is two found nodes have the same parent, (not in serial), this algorithm fails. # Remove child from target nodes, and recurse remaining_children = [x for x in remaining_targets if x != child] else: # We didn't hit a child, so no changes. remaining_children = remaining_targets # Add an edge between the current node and the child new_matrix = deepcopy(curr_matrix) new_matrix.add_edge(starting_node, child) if len(remaining_children) == 0: # We've found all children, we're done! yield new_matrix else: yield from find_children( graph=graph, starting_node=child, # Make sure we don't go in circles has_visited=has_visited + [child], remaining_targets=remaining_children, curr_matrix=new_matrix)
def test_connected(self): m = Matrix(size = 3) m.add_edge(0, 1) assert 1 in m.get_connected_lst(0)