class TestPlanarGraphFactory(unittest.TestCase): def setUp(self): self.N = 4 # number of nodes self.graph_factory = PlanarGraphFactory(Graph) def test_cyclic(self): G = self.graph_factory.make_cyclic(n=self.N, directed=False) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), self.N) self.assertEqual(G.e(), self.N) aset = set(edge.weight for edge in G.iteredges()) self.assertEqual(G.e(), len(aset)) faces = list(G.iterfaces()) self.assertEqual(G.f(), 2) self.assertEqual(len(faces), 2) #print "faces", faces self.assertRaises(ValueError, self.graph_factory.make_cyclic, 1) self.assertRaises(ValueError, self.graph_factory.make_cyclic, 2) def test_wheel(self): G = self.graph_factory.make_wheel(n=self.N, directed=False) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), self.N) self.assertEqual(G.e(), 2 * self.N - 2) self.assertTrue(is_wheel(G)) faces = list(G.iterfaces()) self.assertEqual(G.f(), self.N) self.assertEqual(len(faces), self.N) #print "faces", faces self.assertRaises(ValueError, self.graph_factory.make_wheel, 1) self.assertRaises(ValueError, self.graph_factory.make_wheel, 2) self.assertRaises(ValueError, self.graph_factory.make_wheel, 3) def tearDown(self): pass
class TestPlanarGraphFactory(unittest.TestCase): def setUp(self): self.N = 4 # number of nodes self.graph_factory = PlanarGraphFactory(Graph) def test_cyclic(self): G = self.graph_factory.make_cyclic(n=self.N) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), self.N) self.assertEqual(G.e(), self.N) aset = set(edge.weight for edge in G.iteredges()) self.assertEqual(G.e(), len(aset)) faces = list(G.iterfaces()) self.assertEqual(G.f(), 2) self.assertEqual(len(faces), 2) #print "faces", faces self.assertRaises(ValueError, self.graph_factory.make_cyclic, 1) # For n=1 and the Graph class we have # ValueError("loops are forbidden") self.assertRaises(ValueError, self.graph_factory.make_cyclic, 2) # For n=2 and the Graph class we have # ValueError("parallel edges are forbidden") def test_wheel(self): G = self.graph_factory.make_wheel(n=self.N) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), self.N) self.assertEqual(G.e(), 2 * self.N - 2) self.assertTrue(is_wheel(G)) faces = list(G.iterfaces()) self.assertEqual(G.f(), self.N) self.assertEqual(len(faces), self.N) #print "faces", faces self.assertRaises(ValueError, self.graph_factory.make_wheel, 1) self.assertRaises(ValueError, self.graph_factory.make_wheel, 2) self.assertRaises(ValueError, self.graph_factory.make_wheel, 3) def tearDown(self): pass
def setUp(self): self.N = 4 # number of nodes self.graph_factory = PlanarGraphFactory(Graph)
from graphtheory.structures.edges import Edge from graphtheory.structures.graphs import Graph from graphtheory.planarity.planarfactory import PlanarGraphFactory gf = PlanarGraphFactory(Graph) G = gf.make_cyclic(n=10) # cyclic topological graph G = gf.make_wheel(n=10) # wheel topological graph # G.show() print(G.v()) # the number of nodes print(G.e()) # the number of edges print(G.f()) # the number of faces for face in G.iterfaces(): print(face) from alltests import run_all_tests run_all_tests() ''' from graphtheory.structures.graphs import Graph from graphtheory.structures.factory import GraphFactory gf = GraphFactory(Graph) G = gf.make_random(n=10, directed=True) G.show() from pprint import pprint pprint(G) # adjacency matrix equivalent from graphtheory.shortestpaths.dijkstra import Dijkstra algo = Dijkstra(G) source = 5