示例#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
示例#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()
示例#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
示例#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
示例#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)
示例#6
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
示例#7
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))
示例#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))
示例#9
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)
示例#10
0
 def run(self, source=None):
     """Executable pseudocode."""
     if source is None:
         source = self.graph.iternodes().next()
     self.source = source
     algorithm = PrimMatrixMSTWithEdges(self.graph)  # O(V**2) time
     algorithm.run(self.source)
     self.mst = algorithm.to_tree()
     # Obchodzenie MST w kolejnosci preorder z uzyciem DFS.
     # Hamiltonian cycle as a list of nodes.
     order = list()
     algorithm = SimpleDFS(self.mst)  # O(V) time
     algorithm.run(self.source, pre_action=lambda node: order.append(node))
     order.append(self.source)  # close cycle, length V+1
     # Finding edges for the Hamiltonian cycle, O(V**2) time.
     for i in xrange(self.graph.v()):
         source = order[i]
         target = order[i + 1]
         for edge in self.graph.iteroutedges(source):
             if edge.target == target:
                 self.hamiltonian_cycle.add_edge(edge)
                 break
示例#11
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()
示例#12
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))
示例#13
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()