def base(graph): ''' Finds the base nodes of (un)directed graph. ''' base_nodes = {node for node in graph} for node in graph: if node in base_nodes: for u, v in dfs(graph, node): base_nodes.discard(v) return base_nodes
def test_dfs_with_digraph_missing_node(self): with self.assertRaises(NodeNotFound): list(dfs(self.digraph, 'NotANode'))
def test_dfs_with_digraph(self): self.assertEqual(list(dfs(self.digraph, 1)), [(1, 2), (2, 3), (2, 4)])
def test_dfs_with_graph_missing_node(self): with self.assertRaises(NodeNotFound): list(dfs(self.graph, 999))
def test_dfs_with_graph(self): self.assertEqual(list(dfs(self.graph, 1)), [(1, 2), (2, 3), (2, 7), (7, 'Sofia'), (3, 5)])