Exemplo n.º 1
0
 def __init__(self, graph):
     """The algorithm initialization."""
     if graph.is_directed():
         raise ValueError("the graph is directed")
     self.graph = graph
     self.color = dict()
     self.m = 0  # graph.e() is slow
     for edge in self.graph.iteredges():
         if edge.source == edge.target:
             raise ValueError("a loop detected")
         else:
             self.color[edge] = None  # edge.source < edge.target
             self.m += 1
     if len(self.color) < self.m:
         raise ValueError("edges are not unique")
     algorithm = Bipartite(self.graph)  # O(V+E) time
     algorithm.run()
     # Dictionaries for node indices.
     self.D1 = dict()
     self.D2 = dict()
     idx1 = 0
     idx2 = 0
     for node in self.graph.iternodes():  # O(V) time
         if algorithm.color[node] == 1:
             self.D1[node] = idx1
             idx1 += 1
         else:
             self.D2[node] = idx2
             idx2 += 1
     if self.m != len(self.D1) * len(self.D2):
         raise ValueError("the graph is not complete bipartite")
Exemplo n.º 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)
Exemplo n.º 3
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)
Exemplo n.º 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.color = algorithm.color
Exemplo n.º 5
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
Exemplo n.º 6
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))
Exemplo n.º 7
0
 def __init__(self, graph):
     """The algorithm initialization."""
     if graph.is_directed():
         raise ValueError("the graph is directed")
     self.graph = graph
     self.color = dict()
     self.m = 0  # graph.e() is slow
     for edge in self.graph.iteredges():
         if edge.source == edge.target:
             raise ValueError("a loop detected")
         else:
             self.color[edge] = None  # edge.source < edge.target
             self.m += 1
     if len(self.color) < self.m:
         raise ValueError("edges are not unique")
     # Tast czy graf jest dwudzielny.
     # Wlasciwie potem nie jest jawnie potrzebny podzial wierzcholkow.
     algorithm = Bipartite(self.graph)  # O(V+E) time
     algorithm.run()
     # dict with missing colors for nodes.
     self.missing = None
Exemplo n.º 8
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)
Exemplo n.º 9
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)
Exemplo n.º 10
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)
Exemplo n.º 11
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)