Пример #1
0
def is_biconnected(graph):
    """Test if the undirected graph is biconnected."""
    if graph.is_directed():
        raise ValueError("the graph is directed")
    if not is_connected(graph):
        return False
    algorithm = TarjanCutNode(graph)
    algorithm.run()
    return len(algorithm.cut_nodes) == 0
Пример #2
0
def is_biconnected(graph):
    """Test if the undirected graph is biconnected."""
    if graph.is_directed():
        raise ValueError("the graph is directed")
    if not is_connected(graph):
        return False
    algorithm = TarjanCutNode(graph)
    algorithm.run()
    return len(algorithm.cut_nodes) == 0
Пример #3
0
 def __init__(self, graph):
     """The algorithm initialization."""
     if graph.is_directed():
         raise ValueError("the graph is directed")
     if not is_connected(graph):
         raise ValueError("the graph is not connected")
     self.graph = graph
     self.color = dict((node, None) for node in self.graph.iternodes())
     self._color_list = [False] * self.graph.v()
Пример #4
0
 def __init__(self, graph):
     """The algorithm initialization."""
     if graph.is_directed():
         raise ValueError("the graph is directed")
     if not is_connected(graph):
         raise ValueError("the graph is not connected")
     self.graph = graph
     self.color = dict((node, None) for node in self.graph.iternodes())
     self._color_list = [False] * self.graph.v()
Пример #5
0
 def __init__(self, graph):
     """The algorithm initialization.
     
     Parameters
     ----------
     graph : undirected weighted graph or multigraph
     """
     if graph.is_directed():
         raise ValueError("the graph is directed")
     if not is_connected(graph):
         raise ValueError("the graph is not connected")
     self.graph = graph
     self.mst = self.graph.__class__(self.graph.v(), directed=False)
     self._in_queue = dict((node, True) for node in self.graph.iternodes())
     self._pq = PriorityQueue()
Пример #6
0
 def __init__(self, graph):
     """The algorithm initialization.
     
     Parameters
     ----------
     graph : undirected weighted graph or multigraph
     """
     if graph.is_directed():
         raise ValueError("the graph is directed")
     if not is_connected(graph):
         raise ValueError("the graph is not connected")
     self.graph = graph
     self.mst = self.graph.__class__(self.graph.v(), directed=False)
     self._in_queue = dict((node, True) for node in self.graph.iternodes())
     self._pq = PriorityQueue()
Пример #7
0
 def __init__(self, graph):
     """The algorithm initialization.
     
     Parameters
     ----------
     graph : undirected weighted graph or multigraph
     """
     if graph.is_directed():
         raise ValueError("the graph is directed")
     if not is_connected(graph):
         raise ValueError("the graph is not connected")
     self.graph = graph
     self.mst = self.graph.__class__(self.graph.v(), directed=False)
     for node in self.graph.iternodes():   # isolated nodes are possible
         self.mst.add_node(node)
     self._in_mst = dict((node, False) for node in self.graph.iternodes())
Пример #8
0
 def __init__(self, graph):
     """The algorithm initialization.
     
     Parameters
     ----------
     graph : undirected weighted graph or multigraph
     """
     if graph.is_directed():
         raise ValueError("the graph is directed")
     if not is_connected(graph):
         raise ValueError("the graph is not connected")
     self.graph = graph
     self.mst = self.graph.__class__(self.graph.v(), directed=False)
     for node in self.graph.iternodes():   # isolated nodes are possible
         self.mst.add_node(node)
     self._in_mst = dict((node, False) for node in self.graph.iternodes())
Пример #9
0
 def __init__(self, graph):
     """The algorithm initialization."""
     if graph.is_directed():
         raise ValueError("the graph is directed")
     if not is_connected(graph):
         raise ValueError("the graph is not connected")
     self.graph = graph
     self.color = dict((node, None) for node in self.graph.iternodes())
     self.order = list()
     self.parent = dict()
     self.m = 0  # graph.e() is slow
     for edge in self.graph.iteredges():
         self.m += 1
         if edge.source == edge.target:
             raise ValueError("a loop detected")
     if 2 * self.m == self.graph.v() * (self.graph.v() - 1):
         raise ValueError("complete graph detected")
Пример #10
0
 def __init__(self, graph):
     """The algorithm initialization."""
     if graph.is_directed():
         raise ValueError("the graph is directed")
     if not is_connected(graph):
         raise ValueError("the graph is not connected")
     self.graph = graph
     self.color = dict((node, None) for node in self.graph.iternodes())
     self.order = list()
     self.parent = dict()
     self.m = 0   # graph.e() is slow
     for edge in self.graph.iteredges():
         self.m += 1
         if edge.source == edge.target:
             raise ValueError("a loop detected")
     if 2 * self.m == self.graph.v() * (self.graph.v() - 1):
         raise ValueError("complete graph detected")
     self._color_list = [False] * self.graph.v()
Пример #11
0
from graphtheory.spanningtrees.kruskal import KruskalMST
from graphtheory.spanningtrees.kruskal import KruskalMSTSorted

V = 50
graph_factory = GraphFactory(Graph)
G = graph_factory.make_random(V, False, 0.5)
#G = graph_factory.make_complete(V, False)
#G = graph_factory.make_cyclic(V, False)
E = G.e()
#G.show()

print "Calculate parameters ..."
print "Nodes:", G.v(), V
print "Edges:", G.e(), E
print "Directed:", G.is_directed()
print "Connected:", is_connected(G)
print "Biconnected:", is_biconnected(G)
print "Acyclic:", is_acyclic(G)
print "Bipartite:", is_bipartite(G)
Delta = max(G.degree(node) for node in G.iternodes())
print "Delta:", Delta

print "Testing BoruvkaMST ..."
t1 = timeit.Timer(lambda: BoruvkaMST(G).run())
print V, E, t1.timeit(1)  # single run

print "Testing PrimMST ..."
t1 = timeit.Timer(lambda: PrimMST(G).run())
print V, E, t1.timeit(1)  # single run

print "Testing PrimMSTWithEdges ..."
Пример #12
0
from graphtheory.spanningtrees.kruskal import KruskalMST
from graphtheory.spanningtrees.kruskal import KruskalMSTSorted

V = 10
graph_factory = GraphFactory(Graph)
G = graph_factory.make_random(V, False, 0.5)
#G = graph_factory.make_complete(V, False)
#G = graph_factory.make_cyclic(V, False)
E = G.e()
#G.show()

print "Calculate parameters ..."
print "Nodes:", G.v(), V
print "Edges:", G.e(), E
print "Directed:", G.is_directed()
print "Connected:", is_connected(G)
print "Biconnected:", is_biconnected(G)
print "Acyclic:", is_acyclic(G)
print "Bipartite:", is_bipartite(G)
Delta = max(G.degree(node) for node in G.iternodes())
print "Delta:", Delta

print "Testing BoruvkaMST ..."
t1 = timeit.Timer(lambda: BoruvkaMST(G).run())
print V, E, t1.timeit(1)            # single run

print "Testing PrimMST ..."
t1 = timeit.Timer(lambda: PrimMST(G).run())
print V, E, t1.timeit(1)            # single run

print "Testing PrimMatrixMST ..."
Пример #13
0
from graphtheory.spanningtrees.kruskal import KruskalMST
from graphtheory.spanningtrees.kruskal import KruskalMSTSorted

V = 50
graph_factory = GraphFactory(Graph)
G = graph_factory.make_random(V, False, 0.5)
#G = graph_factory.make_complete(V, False)
#G = graph_factory.make_cyclic(V, False)
E = G.e()
#G.show()

print ( "Calculate parameters ..." )
print ( "Nodes: {} {}".format(G.v(), V) )
print ( "Edges: {} {}".format(G.e(), E) )
print ( "Directed: {}".format(G.is_directed()) )
print ( "Connected: {}".format(is_connected(G)) )
print ( "Biconnected: {}".format(is_biconnected(G)) )
print ( "Acyclic: {}".format(is_acyclic(G)) )
print ( "Bipartite: {}".format(is_bipartite(G)) )
Delta = max(G.degree(node) for node in G.iternodes())
print ( "Delta: {}".format(Delta) )

print ("Testing BoruvkaMST ..." )
t1 = timeit.Timer(lambda: BoruvkaMST(G).run())
print ( "{} {} {}".format(V, E, t1.timeit(1)) )   # single run

print ("Testing PrimMST ..." )
t1 = timeit.Timer(lambda: PrimMST(G).run())
print ( "{} {} {}".format(V, E, t1.timeit(1)) )   # single run

print ("Testing PrimMSTWithEdges ..." )
Пример #14
0
 def test_is_connected(self):
     self.assertFalse(is_connected(self.G))
     self.G.add_edge(Edge(5, 6))
     self.assertTrue(is_connected(self.G))
     self.assertRaises(ValueError, is_connected, Graph(1, True))
Пример #15
0
 def test_is_connected(self):
     self.assertFalse(is_connected(self.G))
     self.G.add_edge(Edge(5, 6))
     self.assertTrue(is_connected(self.G))
     self.assertRaises(ValueError, is_connected, Graph(1, True))