Ejemplo 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")
Ejemplo n.º 2
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
Ejemplo n.º 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.distance = dict()
     self.cardinality = 0
     algorithm = Bipartite(self.graph)
     algorithm.run()
     self.v1 = set()
     self.v2 = set()
     for node in self.graph.iternodes():
         if algorithm.color[node] == 1:
             self.v1.add(node)
         else:
             self.v2.add(node)
     self.Q = Queue()   # for nodes from self.v1