Beispiel #1
0
 def run(self):
     """Executable pseudocode."""
     algorithm = SimpleDFS(self.graph)
     for source in self.graph.iternodes():
         if self.cc[source] is None:
             algorithm.run(source, pre_action=lambda node:
                 self.cc.__setitem__(node, self.n_cc))
             self.n_cc += 1
Beispiel #2
0
def is_connected(graph):
    """Test if the undirected graph is connected."""
    if graph.is_directed():
        raise ValueError("the graph is directed")
    order = []
    source = next(graph.iternodes())
    algorithm = SimpleDFS(graph)
    algorithm.run(source, lambda node: order.append(node))
    return len(order) == graph.v()
Beispiel #3
0
 def _find_ncc(self):
     """Return the number of connected components."""
     visited = dict((node, False) for node in self.graph.iternodes())
     ncc = 0
     algorithm = SimpleDFS(self.graph)
     for source in self.graph.iternodes():
         if not visited[source]:
             algorithm.run(source, pre_action=lambda node:
                 visited.__setitem__(node, True))
             ncc += 1
     return ncc
Beispiel #4
0
 def _find_ncc(self):
     """Return the number of connected components."""
     visited = dict((node, False) for node in self.graph.iternodes())
     ncc = 0
     algorithm = SimpleDFS(self.graph)
     for source in self.graph.iternodes():
         if not visited[source]:
             algorithm.run(
                 source,
                 pre_action=lambda node: visited.__setitem__(node, True))
             ncc = ncc + 1
     return ncc
Beispiel #5
0
 def _is_bridge(self, edge):
     """Bridge test."""
     list1 = list()
     list2 = list()
     algorithm = SimpleDFS(self._graph_copy)
     algorithm.run(edge.source, pre_action=lambda node: list1.append(node))
     self._graph_copy.del_edge(edge)
     algorithm = SimpleDFS(self._graph_copy)
     algorithm.run(edge.source, pre_action=lambda node: list2.append(node))
     # Restore the edge.
     self._graph_copy.add_edge(edge)
     return len(list1) != len(list2)
Beispiel #6
0
 def _is_bridge(self, edge):
     """Bridge test."""
     list1 = list()
     list2 = list()
     algorithm = SimpleDFS(self._graph_copy)
     algorithm.run(edge.source, pre_action=lambda node: list1.append(node))
     self._graph_copy.del_edge(edge)
     algorithm = SimpleDFS(self._graph_copy)
     algorithm.run(edge.source, pre_action=lambda node: list2.append(node))
     # Restore the edge.
     self._graph_copy.add_edge(edge)
     return len(list1) != len(list2)
Beispiel #7
0
 def run(self):
     """Executable pseudocode."""
     algorithm = SimpleDFS(self.graph)
     order = []
     # use post_action!
     algorithm.run(post_action=lambda node: order.append(node))
     order.reverse()
     algorithm = SimpleDFS(self.graph.transpose())
     for source in order:
         if self.scc[source] is None:
             algorithm.run(source, pre_action=lambda
                 node: self.scc.__setitem__(node, self.n_scc))
             self.n_scc += 1
Beispiel #8
0
 def test_simple_dfs_with_recursion(self):
     self.assertEqual(self.G.v(), self.N)
     pre_order = []
     post_order = []
     algorithm = SimpleDFS(self.G)
     algorithm.run(1, pre_action=lambda node: pre_order.append(node),
                     post_action=lambda node: post_order.append(node))
     pre_order_expected = [1, 0, 4, 5, 2, 3, 6, 7]
     post_order_expected = [4, 0, 7, 6, 3, 2, 5, 1]
     self.assertEqual(pre_order, pre_order_expected)
     self.assertEqual(post_order, post_order_expected)
     parent_expected = {0: 1, 1: None, 2: 5, 3: 2, 4: 0, 5: 1, 6: 3, 7: 6}
     self.assertEqual(algorithm.parent, parent_expected)
     self.assertEqual(algorithm.path(1, 7), [1, 5, 2, 3, 6, 7])
     self.assertEqual(algorithm.path(1, 4), [1, 0, 4])
     #algorithm.dag.show()
     self.assertEqual(algorithm.dag.v(), self.N)
     self.assertEqual(algorithm.dag.e(), self.N-1)
     self.assertTrue(algorithm.dag.is_directed())
     for edge in algorithm.dag.iteredges():
         self.assertTrue(self.G.has_edge(edge))
         self.assertEqual(edge.weight, self.G.weight(edge))
Beispiel #9
0
 def test_simple_dfs_with_recursion(self):
     self.assertEqual(self.G.v(), self.N)
     pre_order = []
     post_order = []
     algorithm = SimpleDFS(self.G)
     algorithm.run(1,
                   pre_action=lambda node: pre_order.append(node),
                   post_action=lambda node: post_order.append(node))
     pre_order_expected = [1, 0, 4, 5, 2, 3, 6, 7]
     post_order_expected = [4, 0, 7, 6, 3, 2, 5, 1]
     self.assertEqual(pre_order, pre_order_expected)
     self.assertEqual(post_order, post_order_expected)
     parent_expected = {0: 1, 1: None, 2: 5, 3: 2, 4: 0, 5: 1, 6: 3, 7: 6}
     self.assertEqual(algorithm.parent, parent_expected)
     self.assertEqual(algorithm.path(1, 7), [1, 5, 2, 3, 6, 7])
     self.assertEqual(algorithm.path(1, 4), [1, 0, 4])
     #algorithm.dag.show()
     self.assertEqual(algorithm.dag.v(), self.N)
     self.assertEqual(algorithm.dag.e(), self.N - 1)
     self.assertTrue(algorithm.dag.is_directed())
     for edge in algorithm.dag.iteredges():
         self.assertTrue(self.G.has_edge(edge))
         self.assertEqual(edge.weight, self.G.weight(edge))
Beispiel #10
0
 def run(self):
     """Executable pseudocode."""
     algorithm = SimpleDFS(self.graph)
     algorithm.run(post_action=lambda node: self.sorted_nodes.append(node))
     self.sorted_nodes.reverse()
Beispiel #11
0
 def run(self):
     """Executable pseudocode."""
     for source in self.graph.iternodes():
         algorithm = SimpleDFS(self.graph)
         algorithm.run(source, pre_action=lambda node:
             self.T[source].__setitem__(node, True))
Beispiel #12
0
 def run(self):
     """Executable pseudocode."""
     algorithm = SimpleDFS(self.graph)
     algorithm.run(post_action=lambda node: self.sorted_nodes.append(node))
     self.sorted_nodes.reverse()