Beispiel #1
0
 def test_exceptions(self):
     self.G.add_edge(Edge(0, 4))
     algorithm = BipartiteGraphBFS(self.G)
     self.assertRaises(ValueError, algorithm.run)
     algorithm = BipartiteGraphDFS(self.G)
     self.assertRaises(ValueError, algorithm.run)
     self.assertRaises(ValueError, BipartiteGraphBFS, Graph(2,
                                                            directed=True))
     self.assertRaises(ValueError, BipartiteGraphDFS, Graph(2,
                                                            directed=True))
Beispiel #2
0
 def run(self):
     """Executable pseudocode."""
     try:
         algorithm = BipartiteGraphBFS(self.graph)
         algorithm.run()
         self.color = algorithm.color
     except ValueError:
         order = find_peo_spgraph(self.graph)
         for source in reversed(order):
             self._greedy_color(source)
Beispiel #3
0
 def __init__(self, graph):
     """The algorithm initialization.
     
     Parameters
     ----------
     graph : undirected graph
     """
     if graph.is_directed():
         raise ValueError("the graph is directed")
     self.graph = graph
     self.mate = dict((node, None) for node in self.graph.iternodes())
     self.cardinality = 0
     algorithm = BipartiteGraphBFS(self.graph)
     algorithm.run()
     self.color = algorithm.color
Beispiel #4
0
 def __init__(self, graph):
     """The algorithm initialization.
     
     Parameters
     ----------
     graph : undirected graph
     """
     if graph.is_directed():
         raise ValueError("the graph is directed")
     self.graph = graph
     self.mate = dict((node, None) for node in self.graph.iternodes())
     self.cardinality = 0
     algorithm = BipartiteGraphBFS(self.graph)
     algorithm.run()
     self.v1 = list()
     self.v2 = list()
     for node in self.graph.iternodes():  # O(V) time
         if algorithm.color[node] == 1:
             self.v1.append(node)
         else:
             self.v2.append(node)
Beispiel #5
0
 def test_bipartite_bfs(self):
     self.assertEqual(self.G.v(), self.N)
     algorithm = BipartiteGraphBFS(self.G)
     algorithm.run(0)
     color_expected = {0: 0, 2: 0, 4: 0, 1: 1, 3: 1, 5: 1}
     self.assertEqual(algorithm.color, color_expected)