Exemple #1
0
 def run(self):
     """Executable pseudocode."""
     order = list()
     algorithm = SimpleBFS(self.graph)
     algorithm.run(pre_action=lambda node: order.append(node))
     for source in order:
         self._greedy_color(source)
Exemple #2
0
 def run(self):
     """Executable pseudocode."""
     n = self.graph.v()
     e = self.graph.e()  # this may be O(E)
     if n < 4:
         raise ValueError("the number of nodes is less then 4")
     elif n == 4 and e == 6:  # complete graph K4
         self.hub = next(self.graph.iternodes())
         return
     if e != 2 * n - 2:
         raise ValueError("bad number of edges")
     dset = set(self.graph.degree(node) for node in self.graph.iternodes())
     if dset != set([3, n - 1]):
         raise ValueError("bad set of node degrees")
     self.hub = max(self.graph.iternodes(), key=self.graph.degree)
     # Remove edges from the hub.
     removed = list(self.graph.iteroutedges(self.hub))
     for edge in removed:
         self.graph.del_edge(edge)
     # Now a cycle should be present.
     # Choose a node different from the hub.
     for node in self.graph.iternodes():
         if node != self.hub:
             source = node
             break
     order = []
     algorithm = SimpleBFS(self.graph)
     algorithm.run(source, pre_action=lambda node: order.append(node))
     # Restore edges.
     for edge in removed:
         self.graph.add_edge(edge)
     if len(order) != n - 1:
         raise ValueError("not a wheel graph")
Exemple #3
0
 def run(self):
     """Executable pseudocode."""
     n = self.graph.v()
     e = self.graph.e()   # this may be O(E)
     if n < 4:
         raise ValueError("the number of nodes is less then 4")
     elif n == 4 and e == 6:   # complete graph K4
         self.hub = next(self.graph.iternodes())
         return
     if e != 2 * n - 2:
         raise ValueError("bad number of edges")
     dset = set(self.graph.degree(node) for node in self.graph.iternodes())
     if dset != set([3, n-1]):
         raise ValueError("bad set of node degrees")
     self.hub = max(self.graph.iternodes(), key=self.graph.degree)
     # Remove edges from the hub.
     removed = list(self.graph.iteroutedges(self.hub))
     for edge in removed:
         self.graph.del_edge(edge)
     # Now a cycle should be present.
     # Choose a node different from the hub.
     for node in self.graph.iternodes():
         if node != self.hub:
             source = node
             break
     order = []
     algorithm = SimpleBFS(self.graph)
     algorithm.run(source, pre_action=lambda node: order.append(node))
     # Restore edges.
     for edge in removed:
         self.graph.add_edge(edge)
     if len(order) != n-1:
         raise ValueError("not a wheel graph")
Exemple #4
0
 def run(self):
     """Executable pseudocode."""
     order = list()
     algorithm = SimpleBFS(self.graph)
     algorithm.run(pre_action=lambda node: order.append(node))
     for source in order:
         self._greedy_color(source)
Exemple #5
0
 def run(self):
     """Executable pseudocode."""
     algorithm = SimpleBFS(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
Exemple #6
0
 def _is_bridge(self, edge):
     """Bridge test."""
     list1 = list()
     list2 = list()
     algorithm = SimpleBFS(self._graph_copy)
     algorithm.run(edge.source, pre_action=lambda node: list1.append(node))
     self._graph_copy.del_edge(edge)
     algorithm = SimpleBFS(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)
Exemple #7
0
 def _is_bridge(self, edge):
     """Bridge test."""
     list1 = list()
     list2 = list()
     algorithm = SimpleBFS(self._graph_copy)
     algorithm.run(edge.source, pre_action=lambda node: list1.append(node))
     self._graph_copy.del_edge(edge)
     algorithm = SimpleBFS(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)
Exemple #8
0
 def test_simple_bfs(self):
     self.assertEqual(self.G.v(), self.N)
     pre_order = []
     post_order = []
     algorithm = SimpleBFS(self.G)
     algorithm.run(1, pre_action=lambda node: pre_order.append(node),
                     post_action=lambda node: post_order.append(node))
     order_expected = [1, 0, 5, 4, 2, 6, 3, 7]
     self.assertEqual(pre_order, order_expected)
     self.assertEqual(post_order, order_expected)
     parent_expected = {0: 1, 1: None, 2: 5, 3: 2, 4: 0, 5: 1, 6: 5, 7: 6}
     self.assertEqual(algorithm.parent, parent_expected)
     self.assertEqual(algorithm.path(1, 7), [1, 5, 6, 7])
     self.assertEqual(algorithm.path(1, 4), [1, 0, 4])
     self.assertRaises(ValueError, algorithm.path, 4, 7)
     #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))
Exemple #9
0
 def test_simple_bfs(self):
     self.assertEqual(self.G.v(), self.N)
     pre_order = []
     post_order = []
     algorithm = SimpleBFS(self.G)
     algorithm.run(1,
                   pre_action=lambda node: pre_order.append(node),
                   post_action=lambda node: post_order.append(node))
     order_expected = [1, 0, 5, 4, 2, 6, 3, 7]
     self.assertEqual(pre_order, order_expected)
     self.assertEqual(post_order, order_expected)
     parent_expected = {0: 1, 1: None, 2: 5, 3: 2, 4: 0, 5: 1, 6: 5, 7: 6}
     self.assertEqual(algorithm.parent, parent_expected)
     self.assertEqual(algorithm.path(1, 7), [1, 5, 6, 7])
     self.assertEqual(algorithm.path(1, 4), [1, 0, 4])
     self.assertRaises(ValueError, algorithm.path, 4, 7)
     #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))
Exemple #10
0
 def run(self):
     """Executable pseudocode."""
     for source in self.graph.iternodes():
         algorithm = SimpleBFS(self.graph)
         algorithm.run(source, pre_action=lambda node:
             self.T[source].__setitem__(node, True))