def test_exceptions(self): self.assertRaises(ValueError, CompleteBipartiteGraphEdgeColoring, Graph(5, directed=True)) gf = GraphFactory(Graph) G = gf.make_bipartite(2, 2, False, 1) #G.show() G.del_edge(Edge(0, 2)) # nie bedzie pelny dwudzielny self.assertRaises(ValueError, CompleteBipartiteGraphEdgeColoring, G)
def test_exceptions(self): self.assertRaises(ValueError, BipartiteGraphEdgeColoring, Graph(5, directed=True)) gf = GraphFactory(Graph) G = gf.make_cyclic(4) #G.show() G.add_edge(Edge(0, 2)) # nie bedzie dwudzielny self.assertRaises(ValueError, BipartiteGraphEdgeColoring, G)
def test_cyclic_graph(self): N = 8 assert N % 2 == 0 gf = GraphFactory(Graph) G = gf.make_cyclic(N) algorithm = BipartiteGraphEdgeColoring(G) algorithm.run() for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge], None) for node in G.iternodes(): color_set = set() for edge in G.iteroutedges(node): if edge.source > edge.target: color_set.add(algorithm.color[~edge]) else: color_set.add(algorithm.color[edge]) self.assertEqual(len(color_set), G.degree(node)) #print algorithm.color #algorithm.show_colors() all_colors = set(algorithm.color[edge] for edge in G.iteredges()) self.assertEqual(len(all_colors), 2)
def test_Kn_even(self): N = 10 self.assertEqual(N % 2, 0) gf = GraphFactory(Graph) G = gf.make_complete(N) algorithm = CompleteGraphEdgeColoring(G) algorithm.run() for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge], None) for node in G.iternodes(): color_set = set() for edge in G.iteroutedges(node): if edge.source > edge.target: color_set.add(algorithm.color[~edge]) else: color_set.add(algorithm.color[edge]) self.assertEqual(len(color_set), G.degree(node)) #print algorithm.color #algorithm.show_colors() all_colors = set(algorithm.color[edge] for edge in G.iteredges()) self.assertEqual(len(all_colors), N - 1)
def test_bipartite(self): N1 = 10 N2 = 13 gf = GraphFactory(Graph) G = gf.make_bipartite(N1, N2) algorithm = BipartiteGraphEdgeColoring(G) algorithm.run() for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge], None) for node in G.iternodes(): color_set = set() for edge in G.iteroutedges(node): if edge.source > edge.target: color_set.add(algorithm.color[~edge]) else: color_set.add(algorithm.color[edge]) self.assertEqual(len(color_set), G.degree(node)) #print algorithm.color #algorithm.show_colors() all_colors = set(algorithm.color[edge] for edge in G.iteredges()) Delta = max(G.degree(node) for node in G.iternodes()) self.assertEqual(len(all_colors), Delta)
def test_Kpq(self): N1 = 15 N2 = 13 gf = GraphFactory(Graph) G = gf.make_bipartite(N1, N2, False, 1) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), N1 + N2) self.assertEqual(G.e(), N1 * N2) algorithm = BipartiteGraphEdgeColoring(G) algorithm.run() for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge], None) for node in G.iternodes(): color_set = set() for edge in G.iteroutedges(node): if edge.source > edge.target: color_set.add(algorithm.color[~edge]) else: color_set.add(algorithm.color[edge]) self.assertEqual(len(color_set), G.degree(node)) #print algorithm.color #algorithm.show_colors() all_colors = set(algorithm.color[edge] for edge in G.iteredges()) self.assertEqual(len(all_colors), max(N1, N2))
#!/usr/bin/python import unittest from graphtheory.structures.edges import Edge from graphtheory.structures.graphs import Graph from graphtheory.structures.factory import GraphFactory from graphtheory.seriesparallel.sptools import make_random_spgraph from graphtheory.seriesparallel.sptools import find_peo_spgraph1 from graphtheory.seriesparallel.sptools import find_peo_spgraph2 print("Testing random sp-graph ...") G = make_random_spgraph(15) #G.show() print("peo1 {}".format(find_peo_spgraph1(G))) print("peo2 {}".format(find_peo_spgraph2(G))) print("Testing complete graph ...") gf = GraphFactory(Graph) G = gf.make_complete(4) #G.show() #print ( "peo1 {}".format(find_peo_spgraph1(G)) ) # ValueError #print ( "peo2 {}".format(find_peo_spgraph2(G)) ) # ValueError print("Testing cyclic graph ...") G = gf.make_cyclic(10) #G.show() print("peo1 {}".format(find_peo_spgraph1(G))) print("peo2 {}".format(find_peo_spgraph2(G))) # EOF
#!/usr/bin/python import math import random import timeit from graphtheory.structures.edges import Edge from graphtheory.structures.graphs import Graph from graphtheory.structures.factory import GraphFactory from graphtheory.hamiltonian.tspbf import BruteForceTSPWithGraph from graphtheory.hamiltonian.tspnn import NearestNeighborTSPWithGraph from graphtheory.hamiltonian.tsprnn import RepeatedNearestNeighborTSPWithGraph from graphtheory.hamiltonian.tspse import SortedEdgeTSPWithGraph V = 8 graph_factory = GraphFactory(Graph) G = graph_factory.make_complete(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 "Delta:", max(G.degree(node) for node in G.iternodes()) print "Testing BruteForceTSPWithGraph ..." t1 = timeit.Timer(lambda: BruteForceTSPWithGraph(G).run()) print V, E, t1.timeit(1) # single run print "Testing NearestNeighborTSPWithGraph ..." t1 = timeit.Timer(lambda: NearestNeighborTSPWithGraph(G).run())
def setUp(self): self.graph_factory = GraphFactory(Graph)
#!/usr/bin/python import timeit from graphtheory.structures.edges import Edge from graphtheory.structures.graphs import Graph from graphtheory.structures.factory import GraphFactory from graphtheory.forests.treedset import BorieDominatingSet from graphtheory.forests.treedset import TreeDominatingSet1 from graphtheory.forests.treedset import TreeDominatingSet2 V = 10 E = V - 1 # tree graph_factory = GraphFactory(Graph) G = graph_factory.make_tree(V, False) #G.show() print "Calculate parameters ..." print "Nodes:", G.v(), V print "Edges:", G.e(), E print "Directed:", G.is_directed() algorithm = BorieDominatingSet(G) algorithm.run() print algorithm.dominating_set print "borie dset", algorithm.cardinality algorithm = TreeDominatingSet1(G) algorithm.run() print algorithm.dominating_set print "tree dset1", algorithm.cardinality
from graphtheory.independentsets.isetsf import SmallestFirstIndependentSet2 from graphtheory.independentsets.isetsf import SmallestFirstIndependentSet3 from graphtheory.independentsets.isetsf import SmallestFirstIndependentSet4 from graphtheory.independentsets.isetsf import SmallestFirstIndependentSet5 from graphtheory.independentsets.isetsf import SmallestFirstIndependentSet6 from graphtheory.independentsets.isetsf import SmallestFirstIndependentSet7 from graphtheory.independentsets.isetll import LargestLastIndependentSet1 from graphtheory.independentsets.isetll import LargestLastIndependentSet2 from graphtheory.independentsets.isetll import LargestLastIndependentSet3 from graphtheory.independentsets.isetll import LargestLastIndependentSet4 from graphtheory.independentsets.isetll import LargestLastIndependentSet5 from graphtheory.independentsets.isetll import LargestLastIndependentSet6 from graphtheory.independentsets.isetll import LargestLastIndependentSet7 V = 10 graph_factory = GraphFactory(Graph) G = graph_factory.make_random(V, False, edge_probability=0.5) 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("Testing UnorderedSequentialIndependentSet1 ...") t1 = timeit.Timer(lambda: UnorderedSequentialIndependentSet1(G).run()) print("{} {} {}".format(V, E, t1.timeit(1))) # single run print("Testing UnorderedSequentialIndependentSet2 ...") t1 = timeit.Timer(lambda: UnorderedSequentialIndependentSet2(G).run())
#!/usr/bin/python import math import random import timeit from graphtheory.structures.edges import Edge from graphtheory.structures.graphs import Graph from graphtheory.structures.factory import GraphFactory from graphtheory.hamiltonian.tspbf import BruteForceTSPWithGraph from graphtheory.hamiltonian.tspnn import NearestNeighborTSPWithGraph from graphtheory.hamiltonian.tsprnn import RepeatedNearestNeighborTSPWithGraph from graphtheory.hamiltonian.tspse import SortedEdgeTSPWithGraph V = 8 graph_factory = GraphFactory(Graph) G = graph_factory.make_complete(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("Delta: {}".format(max(G.degree(node) for node in G.iternodes()))) print("Testing BruteForceTSPWithGraph ...") t1 = timeit.Timer(lambda: BruteForceTSPWithGraph(G).run()) print("{} {} {}".format(V, E, t1.timeit(1))) # single run print("Testing NearestNeighborTSPWithGraph ...") t1 = timeit.Timer(lambda: NearestNeighborTSPWithGraph(G).run())
def setUp(self): self.N = 5 # number of nodes graph_factory = GraphFactory(Graph) self.G = graph_factory.make_complete(self.N, True)
class TestHalinGraph(unittest.TestCase): def setUp(self): self.graph_factory = GraphFactory(Graph) def test_k4(self): # graf K4 = W4 G = self.graph_factory.make_complete(n=4, directed=False) # Sa 4 mozliwosci narysowania K4 na plaszczyznie bez przeciec. algorithm = HalinGraph(G) algorithm.run() self.assertTrue(algorithm.is_outer_k4()) # 2---3 numeracja wg cyklu Hamiltona # |\ /| wheel graph W_5 # | 0 | # |/ \| # 1---4 def test_wheel5(self): N = 5 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 2), Edge(0, 3), Edge(0, 4), Edge(1, 2), Edge(2, 3), Edge(3, 4), Edge(4, 1)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinGraph(G) algorithm.run() #print "wheel5 outer", algorithm.outer self.assertEqual(algorithm.outer, set([1, 2, 3, 4])) self.assertTrue(algorithm.is_outer_k4()) # 2-------3 numeracja wg cyklu Hamiltona # |\ /| 3-prism graph # | 1---4 | cubic, planar # |/ \| # 0-------5 def test_3prism(self): N = 6 G = Graph(N, False) edges = [Edge(0, 1), Edge(1, 2), Edge(2, 3), Edge(3, 4), Edge(4, 5), Edge(0, 5), Edge(1, 4), Edge(2, 0), Edge(3, 5)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinGraph(G) algorithm.run() #print "3prism outer", algorithm.outer #self.assertEqual(algorithm.outer, set([0, 2, 3, 5])) self.assertEqual(algorithm.outer, set([0, 1, 4, 5])) #self.assertEqual(algorithm.outer, set([1, 2, 3, 4])) self.assertTrue(algorithm.is_outer_k4()) # 4-------5 4-prism graph # |\ /| non-Halin graph # | 0---1 | Hamiltonian [0, 1, 2, 3, 7, 6, 5, 4] # | | | | cubic, planar # | 3---2 | # |/ \| # 7-------6 def test_4prism(self): N = 8 G = Graph(N, False) edges = [Edge(0, 1), Edge(1, 2), Edge(2, 3), Edge(3, 0), Edge(4, 5), Edge(5, 6), Edge(6, 7), Edge(7, 4), Edge(0, 4), Edge(1, 5), Edge(2, 6), Edge(3, 7)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #G.show() algorithm = HalinGraph(G) self.assertRaises(ValueError, algorithm.run) # 2-------3 numeracja wg cyklu Hamiltona # |\ / \ heptahedral graph 2 # | 1---4---5 http://mathworld.wolfram.com/HalinGraph.html # |/ \ / # 0-------6 def test_halin7(self): N = 7 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 2), Edge(0, 6), Edge(1, 2), Edge(1, 4), Edge(2, 3), Edge(3, 4), Edge(3, 5), Edge(4, 5), Edge(4, 6), Edge(5, 6)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinGraph(G) algorithm.run() #print "halin7 outer", algorithm.outer self.assertEqual(algorithm.outer, set([0, 2, 3, 5, 6])) self.assertTrue(algorithm.is_outer_k4()) # 0-----7-----6 numeracja wg cyklu Hamiltona # |\ | /| # | 2---3---4 | # |/ \| # 1-----------5 def test_halin8a(self): N = 8 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 2), Edge(0, 7), Edge(1, 2), Edge(1, 5), Edge(2, 3), Edge(3, 4), Edge(3, 7), Edge(4, 5), Edge(4, 6), Edge(5, 6), Edge(6, 7)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinGraph(G) algorithm.run() #print "halin8a outer", algorithm.outer # Sa dwie mozliwosci narysowania tego grafu. self.assertEqual(algorithm.outer, set([0, 1, 5, 6, 7])) #self.assertEqual(algorithm.outer, set([1, 2, 3, 4, 5])) self.assertTrue(algorithm.is_outer_k4()) # 2-------3 numeracja wg cyklu Hamiltona # / \ / \ # 1---7---6---4 # \ / \ / # 0-------5 def test_halin8b(self): N = 8 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 5), Edge(0, 7), Edge(1, 2), Edge(1, 7), Edge(2, 3), Edge(2, 7), Edge(3, 4), Edge(3, 6), Edge(4, 5), Edge(4, 6), Edge(5, 6), Edge(6, 7)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinGraph(G) algorithm.run() #print "halin8b outer", algorithm.outer self.assertEqual(algorithm.outer, set([0, 1, 2, 3, 4, 5])) self.assertTrue(algorithm.is_outer_k4()) # 1---2---3 numeracja wg cyklu Hamiltona # |\ \ /| # | 7---6 | # |/ / \| # 0---5---4 def test_halin8c(self): N = 8 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 5), Edge(0, 7), Edge(1, 2), Edge(1, 7), Edge(2, 3), Edge(2, 6), Edge(3, 4), Edge(3, 6), Edge(4, 5), Edge(4, 6), Edge(5, 6), Edge(6, 7)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinGraph(G) algorithm.run() #print "halin8c outer", algorithm.outer self.assertEqual(algorithm.outer, set([0, 1, 2, 3, 4, 5])) self.assertTrue(algorithm.is_outer_k4()) # 1-----4---5-----8 numeracja wg cyklu Hamiltona # |\ | | /| graf kubiczny # | 2---3---6---7 | # |/ \| # 0---------------9 def test_halin10j(self): N = 10 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 2), Edge(0, 9), Edge(1, 2), Edge(1, 4), Edge(2, 3), Edge(3, 4), Edge(3, 6), Edge(4, 5), Edge(5, 6), Edge(5, 8), Edge(6, 7), Edge(7, 8), Edge(7, 9), Edge(8, 9)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinGraph(G) algorithm.run() #print "halin10j outer", algorithm.outer self.assertEqual(algorithm.outer, set([0, 1, 4, 5, 8, 9])) self.assertTrue(algorithm.is_outer_k4()) # 1-----4---------5 numeracja wg cyklu Hamiltona # |\ | /| graf kubiczny # | 2---3---8---7 | # |/ | \| # 0---------9-----6 def test_halin10k(self): N = 10 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 2), Edge(0, 9), Edge(1, 2), Edge(1, 4), Edge(2, 3), Edge(3, 4), Edge(3, 8), Edge(4, 5), Edge(5, 6), Edge(5, 7), Edge(6, 7), Edge(6, 9), Edge(7, 8), Edge(8, 9)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinGraph(G) algorithm.run() #print "halin10k outer", algorithm.outer self.assertEqual(algorithm.outer, set([0, 1, 4, 5, 6, 9])) self.assertTrue(algorithm.is_outer_k4()) # --4---5-- # / \ / \ # 0 3 6 numeracja wg cyklu Hamiltona # |\ | /| graf kubiczny # | 1---2---7 | # |/ \| # 9-----------8 def test_halin10l(self): N = 10 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 4), Edge(0, 9), Edge(1, 2), Edge(1, 9), Edge(2, 3), Edge(2, 7), Edge(3, 4), Edge(3, 5), Edge(4, 5), Edge(5, 6), Edge(6, 7), Edge(6, 8), Edge(7, 8), Edge(8, 9)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinGraph(G) algorithm.run() #print "halin10l outer", algorithm.outer self.assertEqual(algorithm.outer, set([0, 4, 5, 6, 8, 9])) self.assertTrue(algorithm.is_outer_k4()) # -----------11 # / / \ # 0-------1--10---9 numeracja wg cyklu Hamiltona # | | | cubic Frucht graph # 4---3---2---7---8 # \ / \ / # 5-----------6 def test_frucht12(self): N = 10 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 4), Edge(0, 11), Edge(1, 2), Edge(1, 10), Edge(2, 3), Edge(2, 7), Edge(3, 4), Edge(3, 5), Edge(4, 5), Edge(5, 6), Edge(6, 7), Edge(6, 8), Edge(7, 8), Edge(8, 9), Edge(9, 10), Edge(9, 11), Edge(10, 11)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinGraph(G) algorithm.run() #print "frucht12 outer", algorithm.outer self.assertEqual(algorithm.outer, set([0, 4, 5, 6, 8, 9, 11])) self.assertTrue(algorithm.is_outer_k4()) def tearDown(self): pass
class TestNodeColoring(unittest.TestCase): def setUp(self): self.graph_factory = GraphFactory(Graph) def test_k4(self): # graf K4 = W4 G = self.graph_factory.make_complete(n=4, directed=False) # Sa 4 mozliwosci narysowania K4 na plaszczyznie bez przeciec. algorithm = HalinNodeColoring(G, outer=set([1, 2, 3])) algorithm.run() parent = {0: None, 1: 0, 2: 0, 3: 0} self.assertEqual(algorithm.parent, parent) for node in G.iternodes(): self.assertNotEqual(algorithm.color[node], None) for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge.source], algorithm.color[edge.target]) all_colors = set(algorithm.color[node] for node in G.iternodes()) self.assertEqual(len(all_colors), 4) # 2---3 numeracja wg cyklu Hamiltona # |\ /| wheel graph W_5 # | 0 | # |/ \| # 1---4 def test_wheel5(self): N = 5 G = Graph(N, False) edges = [ Edge(0, 1), Edge(0, 2), Edge(0, 3), Edge(0, 4), Edge(1, 2), Edge(2, 3), Edge(3, 4), Edge(4, 1) ] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinNodeColoring(G, outer=set([1, 2, 3, 4])) algorithm.run() #print "wheel5 outer", algorithm.outer #print "wheel5 cycle", algorithm.cycle parent = {0: None, 1: 0, 2: 0, 3: 0, 4: 0} self.assertEqual(algorithm.parent, parent) for node in G.iternodes(): self.assertNotEqual(algorithm.color[node], None) for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge.source], algorithm.color[edge.target]) all_colors = set(algorithm.color[node] for node in G.iternodes()) self.assertEqual(len(all_colors), 3) # 2-------3 numeracja wg cyklu Hamiltona # |\ /| 3-prism graph # | 1---4 | cubic, planar # |/ \| # 0-------5 def test_3prism(self): N = 6 G = Graph(N, False) edges = [ Edge(0, 1), Edge(1, 2), Edge(2, 3), Edge(3, 4), Edge(4, 5), Edge(0, 5), Edge(1, 4), Edge(2, 0), Edge(3, 5) ] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "3prism" algorithm = HalinNodeColoring(G, outer=set([0, 2, 3, 5])) #algorithm = HalinNodeColoring(G, outer=set([0, 1, 4, 5])) #algorithm = HalinNodeColoring(G, outer=set([1, 2, 3, 4])) algorithm.run() #print "3prism outer", algorithm.outer parent = {0: 1, 1: None, 2: 1, 3: 4, 4: 1, 5: 4} self.assertEqual(algorithm.parent, parent) for node in G.iternodes(): self.assertNotEqual(algorithm.color[node], None) for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge.source], algorithm.color[edge.target]) all_colors = set(algorithm.color[node] for node in G.iternodes()) self.assertEqual(len(all_colors), 3) # 2-------3 numeracja wg cyklu Hamiltona # |\ / \ heptahedral graph 2 # | 1---4---5 http://mathworld.wolfram.com/HalinGraph.html # |/ \ / # 0-------6 def test_halin7(self): N = 7 G = Graph(N, False) edges = [ Edge(0, 1), Edge(0, 2), Edge(0, 6), Edge(1, 2), Edge(1, 4), Edge(2, 3), Edge(3, 4), Edge(3, 5), Edge(4, 5), Edge(4, 6), Edge(5, 6) ] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinNodeColoring(G, outer=set([0, 2, 3, 5, 6])) algorithm.run() #print "halin7 outer", algorithm.outer parent = {0: 1, 1: None, 2: 1, 3: 4, 4: 1, 5: 4, 6: 4} self.assertEqual(algorithm.parent, parent) for node in G.iternodes(): self.assertNotEqual(algorithm.color[node], None) for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge.source], algorithm.color[edge.target]) all_colors = set(algorithm.color[node] for node in G.iternodes()) self.assertEqual(len(all_colors), 3) # 0-----7-----6 numeracja wg cyklu Hamiltona # |\ | /| # | 2---3---4 | # |/ \| # 1-----------5 def test_halin8a(self): N = 8 G = Graph(N, False) edges = [ Edge(0, 1), Edge(0, 2), Edge(0, 7), Edge(1, 2), Edge(1, 5), Edge(2, 3), Edge(3, 4), Edge(3, 7), Edge(4, 5), Edge(4, 6), Edge(5, 6), Edge(6, 7) ] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinNodeColoring(G, outer=set([0, 7, 6, 5, 1])) algorithm.run() #print "halin8a outer", algorithm.outer parent = {0: 2, 1: 2, 2: None, 3: 2, 4: 3, 5: 4, 6: 4, 7: 3} self.assertEqual(algorithm.parent, parent) for node in G.iternodes(): self.assertNotEqual(algorithm.color[node], None) for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge.source], algorithm.color[edge.target]) all_colors = set(algorithm.color[node] for node in G.iternodes()) self.assertEqual(len(all_colors), 3) # 2-------3 numeracja wg cyklu Hamiltona # / \ / \ # 1---7---6---4 # \ / \ / # 0-------5 def test_halin8b(self): N = 8 G = Graph(N, False) edges = [ Edge(0, 1), Edge(0, 5), Edge(0, 7), Edge(1, 2), Edge(1, 7), Edge(2, 3), Edge(2, 7), Edge(3, 4), Edge(3, 6), Edge(4, 5), Edge(4, 6), Edge(5, 6), Edge(6, 7) ] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinNodeColoring(G, outer=set([0, 1, 2, 3, 4, 5])) algorithm.run() #print "halin8b outer", algorithm.outer parent = {0: 7, 1: 7, 2: 7, 3: 6, 4: 6, 5: 6, 6: None, 7: 6} self.assertEqual(algorithm.parent, parent) for node in G.iternodes(): self.assertNotEqual(algorithm.color[node], None) for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge.source], algorithm.color[edge.target]) all_colors = set(algorithm.color[node] for node in G.iternodes()) self.assertEqual(len(all_colors), 3) # 1---2---3 numeracja wg cyklu Hamiltona # |\ \ /| # | 7---6 | # |/ / \| # 0---5---4 def test_halin8c(self): N = 8 G = Graph(N, False) edges = [ Edge(0, 1), Edge(0, 5), Edge(0, 7), Edge(1, 2), Edge(1, 7), Edge(2, 3), Edge(2, 6), Edge(3, 4), Edge(3, 6), Edge(4, 5), Edge(4, 6), Edge(5, 6), Edge(6, 7) ] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinNodeColoring(G, outer=set([0, 1, 2, 3, 4, 5])) algorithm.run() #print "halin8c outer", algorithm.outer parent = {0: 7, 1: 7, 2: 6, 3: 6, 4: 6, 5: 6, 6: None, 7: 6} self.assertEqual(algorithm.parent, parent) for node in G.iternodes(): self.assertNotEqual(algorithm.color[node], None) for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge.source], algorithm.color[edge.target]) all_colors = set(algorithm.color[node] for node in G.iternodes()) self.assertEqual(len(all_colors), 3) # 1-----4---5-----8 numeracja wg cyklu Hamiltona # |\ | | /| graf kubiczny # | 2---3---6---7 | # |/ \| # 0---------------9 def test_halin10j(self): N = 10 G = Graph(N, False) edges = [ Edge(0, 1), Edge(0, 2), Edge(0, 9), Edge(1, 2), Edge(1, 4), Edge(2, 3), Edge(3, 4), Edge(3, 6), Edge(4, 5), Edge(5, 6), Edge(5, 8), Edge(6, 7), Edge(7, 8), Edge(7, 9), Edge(8, 9) ] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinNodeColoring(G, outer=set([0, 1, 4, 5, 8, 9])) algorithm.run() #print "halin10j outer", algorithm.outer parent = { 0: 2, 1: 2, 2: None, 3: 2, 4: 3, 5: 6, 6: 3, 7: 6, 8: 7, 9: 7 } self.assertEqual(algorithm.parent, parent) for node in G.iternodes(): self.assertNotEqual(algorithm.color[node], None) for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge.source], algorithm.color[edge.target]) all_colors = set(algorithm.color[node] for node in G.iternodes()) self.assertEqual(len(all_colors), 3) # 1-----4---------5 numeracja wg cyklu Hamiltona # |\ | /| graf kubiczny # | 2---3---8---7 | # |/ | \| # 0---------9-----6 def test_halin10k(self): N = 10 G = Graph(N, False) edges = [ Edge(0, 1), Edge(0, 2), Edge(0, 9), Edge(1, 2), Edge(1, 4), Edge(2, 3), Edge(3, 4), Edge(3, 8), Edge(4, 5), Edge(5, 6), Edge(5, 7), Edge(6, 7), Edge(6, 9), Edge(7, 8), Edge(8, 9) ] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinNodeColoring(G, outer=set([0, 1, 4, 5, 6, 9])) algorithm.run() #print "halin10k outer", algorithm.outer parent = { 0: 2, 1: 2, 2: None, 3: 2, 4: 3, 5: 7, 6: 7, 7: 8, 8: 3, 9: 8 } self.assertEqual(algorithm.parent, parent) for node in G.iternodes(): self.assertNotEqual(algorithm.color[node], None) for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge.source], algorithm.color[edge.target]) all_colors = set(algorithm.color[node] for node in G.iternodes()) self.assertEqual(len(all_colors), 3) # --4---5-- # / \ / \ # 0 3 6 numeracja wg cyklu Hamiltona # |\ | /| graf kubiczny # | 1---2---7 | # |/ \| # 9-----------8 def test_halin10l(self): N = 10 G = Graph(N, False) edges = [ Edge(0, 1), Edge(0, 4), Edge(0, 9), Edge(1, 2), Edge(1, 9), Edge(2, 3), Edge(2, 7), Edge(3, 4), Edge(3, 5), Edge(4, 5), Edge(5, 6), Edge(6, 7), Edge(6, 8), Edge(7, 8), Edge(8, 9) ] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinNodeColoring(G, outer=set([0, 4, 5, 6, 8, 9])) algorithm.run() #print "halin10l outer", algorithm.outer parent = { 0: 1, 1: None, 2: 1, 3: 2, 4: 3, 5: 3, 6: 7, 7: 2, 8: 7, 9: 1 } self.assertEqual(algorithm.parent, parent) for node in G.iternodes(): self.assertNotEqual(algorithm.color[node], None) for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge.source], algorithm.color[edge.target]) all_colors = set(algorithm.color[node] for node in G.iternodes()) self.assertEqual(len(all_colors), 3) # 10---8---7 wachlarz nieparzysty # / \ | / \ # / 9 \ # / | \ # 0---2---3---4---6 # \ / \ / # 1-----------5 def test_halin11(self): N = 11 G = Graph(N, False) edges = [ Edge(0, 1), Edge(0, 2), Edge(0, 10), Edge(1, 2), Edge(1, 5), Edge(2, 3), Edge(3, 4), Edge(3, 9), Edge(4, 5), Edge(4, 6), Edge(5, 6), Edge(6, 7), Edge(7, 8), Edge(7, 9), Edge(8, 9), Edge(8, 10), Edge(9, 10) ] # E=17 for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinNodeColoring(G, outer=set([0, 1, 5, 6, 7, 8, 10])) algorithm.run() #print "halin11 outer", algorithm.outer parent = { 0: 2, 1: 2, 2: None, 3: 2, 4: 3, 5: 4, 6: 4, 7: 9, 8: 9, 9: 3, 10: 9 } self.assertEqual(algorithm.parent, parent) for node in G.iternodes(): self.assertNotEqual(algorithm.color[node], None) for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge.source], algorithm.color[edge.target]) all_colors = set(algorithm.color[node] for node in G.iternodes()) self.assertEqual(len(all_colors), 3) # -----------11 # / / \ # 0-------1--10---9 numeracja wg cyklu Hamiltona # | | | cubic Frucht graph # 4---3---2---7---8 # \ / \ / # 5-----------6 def test_frucht12(self): N = 10 G = Graph(N, False) edges = [ Edge(0, 1), Edge(0, 4), Edge(0, 11), Edge(1, 2), Edge(1, 10), Edge(2, 3), Edge(2, 7), Edge(3, 4), Edge(3, 5), Edge(4, 5), Edge(5, 6), Edge(6, 7), Edge(6, 8), Edge(7, 8), Edge(8, 9), Edge(9, 10), Edge(9, 11), Edge(10, 11) ] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "frucht12" algorithm = HalinNodeColoring(G, outer=set([0, 4, 5, 6, 8, 9, 11])) algorithm.run() #print "frucht12 outer", algorithm.outer parent = { 0: 1, 1: None, 2: 1, 3: 2, 4: 3, 5: 3, 6: 7, 7: 2, 8: 7, 9: 10, 10: 1, 11: 10 } self.assertEqual(algorithm.parent, parent) for node in G.iternodes(): self.assertNotEqual(algorithm.color[node], None) for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge.source], algorithm.color[edge.target]) all_colors = set(algorithm.color[node] for node in G.iternodes()) self.assertEqual(len(all_colors), 3) # 0---------15--------14 cubic # / \ | / \ # 2---1---6---7---8---13--12 # \ / \ / # 3---5 9---11 # \ / \ / # 4---------------10 def test_halin16(self): N = 16 G = Graph(N, False) edges = [ Edge(0, 1), Edge(0, 2), Edge(0, 15), Edge(1, 2), Edge(1, 6), Edge(2, 3), Edge(3, 4), Edge(3, 5), Edge(4, 5), Edge(4, 10), Edge(5, 6), Edge(6, 7), Edge(7, 8), Edge(7, 15), Edge(8, 9), Edge(8, 13), Edge(9, 10), Edge(9, 11), Edge(10, 11), Edge(11, 12), Edge(12, 13), Edge(12, 14), Edge(13, 14), Edge(14, 15) ] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "halin16" algorithm = HalinNodeColoring(G, outer=set( [0, 2, 3, 4, 10, 11, 12, 14, 15])) algorithm.run() #print "halin16 outer", algorithm.outer parent = { 0: 1, 1: None, 2: 1, 3: 5, 4: 5, 5: 6, 6: 1, 7: 6, 8: 7, 9: 8, 10: 9, 11: 9, 12: 13, 13: 8, 14: 13, 15: 7 } self.assertEqual(algorithm.parent, parent) for node in G.iternodes(): self.assertNotEqual(algorithm.color[node], None) for edge in G.iteredges(): self.assertNotEqual(algorithm.color[edge.source], algorithm.color[edge.target]) all_colors = set(algorithm.color[node] for node in G.iternodes()) self.assertEqual(len(all_colors), 3) def tearDown(self): pass
from graphtheory.connectivity.connected import is_connected from graphtheory.algorithms.acyclic import is_acyclic from graphtheory.bipartiteness.bipartite import is_bipartite from graphtheory.connectivity.cutnodes import is_biconnected from graphtheory.spanningtrees.boruvka import BoruvkaMST from graphtheory.spanningtrees.prim import PrimMST from graphtheory.spanningtrees.prim import PrimMSTWithEdges from graphtheory.spanningtrees.prim import PrimMatrixMST from graphtheory.spanningtrees.prim import PrimMatrixMSTWithEdges from graphtheory.spanningtrees.prim import PrimConnectedMST from graphtheory.spanningtrees.prim import PrimTrivialMST 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())
class TestHalinGraphTreeDecomposition(unittest.TestCase): def setUp(self): self.graph_factory = GraphFactory(Graph) def test_k4(self): # graf K4 = W4 G = self.graph_factory.make_complete(n=4, directed=False) # Sa 4 mozliwosci narysowania K4 na plaszczyznie bez przeciec. #print "k4 ..." algorithm = HalinGraphTreeDecomposition(G, outer=set([1, 2, 3])) algorithm.run() parent = {0: None, 1: 0, 2: 0, 3: 0} self.assertEqual(algorithm.parent, parent) order = [1, 2, 3, 0] self.assertEqual(algorithm.order, order) self.assertEqual(len(algorithm.cliques), G.v()-3) # 2---3 numeracja wg cyklu Hamiltona # |\ /| wheel graph W_5 # | 0 | # |/ \| # 1---4 def test_wheel5(self): N = 5 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 2), Edge(0, 3), Edge(0, 4), Edge(1, 2), Edge(2, 3), Edge(3, 4), Edge(4, 1)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "wheel5 ..." algorithm = HalinGraphTreeDecomposition(G, outer=set([1, 2, 3, 4])) algorithm.run() #print "wheel5 outer", algorithm.outer #print "wheel5 cycle", algorithm.cycle parent = {0: None, 1: 0, 2: 0, 3: 0, 4: 0} self.assertEqual(algorithm.parent, parent) order = [1, 2, 3, 4, 0] self.assertEqual(algorithm.order, order) self.assertEqual(len(algorithm.cliques), G.v()-3) # 2-------3 numeracja wg cyklu Hamiltona # |\ /| 3-prism graph # | 1---4 | cubic, planar # |/ \| # 0-------5 def test_3prism(self): N = 6 G = Graph(N, False) edges = [Edge(0, 1), Edge(1, 2), Edge(2, 3), Edge(3, 4), Edge(4, 5), Edge(0, 5), Edge(1, 4), Edge(2, 0), Edge(3, 5)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "3prism ..." algorithm = HalinGraphTreeDecomposition(G, outer=set([0, 2, 3, 5])) #algorithm = HalinGraphTreeDecomposition(G, outer=set([0, 1, 4, 5])) #algorithm = HalinGraphTreeDecomposition(G, outer=set([1, 2, 3, 4])) algorithm.run() #print "3prism outer", algorithm.outer parent = {0: 1, 1: None, 2: 1, 3: 4, 4: 1, 5: 4} self.assertEqual(algorithm.parent, parent) #order = [1, 0, 2, 3, 5, 4] order = [4, 0, 2, 3, 5, 1] self.assertEqual(algorithm.order, order) self.assertEqual(len(algorithm.cliques), G.v()-3) # 2-------3 numeracja wg cyklu Hamiltona # |\ / \ heptahedral graph 2 # | 1---4---5 http://mathworld.wolfram.com/HalinGraph.html # |/ \ / # 0-------6 def test_halin7(self): N = 7 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 2), Edge(0, 6), Edge(1, 2), Edge(1, 4), Edge(2, 3), Edge(3, 4), Edge(3, 5), Edge(4, 5), Edge(4, 6), Edge(5, 6)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "halin7 ..." algorithm = HalinGraphTreeDecomposition(G, outer=set([0, 2, 3, 5, 6])) algorithm.run() #print "halin7 outer", algorithm.outer parent = {0: 1, 1: None, 2: 1, 3: 4, 4: 1, 5: 4, 6: 4} self.assertEqual(algorithm.parent, parent) #order = [1, 0, 2, 3, 5, 6, 4] order = [5, 4, 0, 2, 3, 6, 1] self.assertEqual(algorithm.order, order) self.assertEqual(len(algorithm.cliques), G.v()-3) # 0-----7-----6 numeracja wg cyklu Hamiltona # |\ | /| # | 2---3---4 | # |/ \| # 1-----------5 def test_halin8a(self): N = 8 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 2), Edge(0, 7), Edge(1, 2), Edge(1, 5), Edge(2, 3), Edge(3, 4), Edge(3, 7), Edge(4, 5), Edge(4, 6), Edge(5, 6), Edge(6, 7)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "halin8a ..." algorithm = HalinGraphTreeDecomposition(G, outer=set([0, 7, 6, 5, 1])) algorithm.run() #print "halin8a outer", algorithm.outer parent = {0: 2, 1: 2, 2: None, 3: 2, 4: 3, 5: 4, 6: 4, 7: 3} self.assertEqual(algorithm.parent, parent) #order = [2, 4, 0, 1, 5, 6, 7, 3] order = [4, 2, 0, 1, 5, 6, 7, 3] self.assertEqual(algorithm.order, order) self.assertEqual(len(algorithm.cliques), G.v()-3) # 2-------3 numeracja wg cyklu Hamiltona # / \ / \ # 1---7---6---4 # \ / \ / # 0-------5 def test_halin8b(self): N = 8 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 5), Edge(0, 7), Edge(1, 2), Edge(1, 7), Edge(2, 3), Edge(2, 7), Edge(3, 4), Edge(3, 6), Edge(4, 5), Edge(4, 6), Edge(5, 6), Edge(6, 7)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "halin8b ..." algorithm = HalinGraphTreeDecomposition(G, outer=set([0, 1, 2, 3, 4, 5])) algorithm.run() #print "halin8b outer", algorithm.outer parent = {0: 7, 1: 7, 2: 7, 3: 6, 4: 6, 5: 6, 6: None, 7: 6} self.assertEqual(algorithm.parent, parent) #order = [1, 7, 0, 2, 3, 4, 5, 6] order = [4, 6, 0, 1, 2, 3, 5, 7] self.assertEqual(algorithm.order, order) self.assertEqual(len(algorithm.cliques), G.v()-3) # 1---2---3 numeracja wg cyklu Hamiltona # |\ \ /| # | 7---6 | # |/ / \| # 0---5---4 def test_halin8c(self): N = 8 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 5), Edge(0, 7), Edge(1, 2), Edge(1, 7), Edge(2, 3), Edge(2, 6), Edge(3, 4), Edge(3, 6), Edge(4, 5), Edge(4, 6), Edge(5, 6), Edge(6, 7)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "halin8c ..." algorithm = HalinGraphTreeDecomposition(G, outer=set([0, 1, 2, 3, 4, 5])) algorithm.run() #print "halin8c outer", algorithm.outer parent = {0: 7, 1: 7, 2: 6, 3: 6, 4: 6, 5: 6, 6: None, 7: 6} self.assertEqual(algorithm.parent, parent) #order = [7, 0, 1, 2, 3, 4, 5, 6] # wersja z indeksami order = [3, 4, 6, 0, 1, 2, 5, 7] # wersja z linkami self.assertEqual(algorithm.order, order) self.assertEqual(len(algorithm.cliques), G.v()-3) # 1-----4---5-----8 numeracja wg cyklu Hamiltona # |\ | | /| graf kubiczny # | 2---3---6---7 | # |/ \| # 0---------------9 def test_halin10j(self): N = 10 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 2), Edge(0, 9), Edge(1, 2), Edge(1, 4), Edge(2, 3), Edge(3, 4), Edge(3, 6), Edge(4, 5), Edge(5, 6), Edge(5, 8), Edge(6, 7), Edge(7, 8), Edge(7, 9), Edge(8, 9)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "halin10j ..." algorithm = HalinGraphTreeDecomposition(G, outer=set([0, 1, 4, 5, 8, 9])) algorithm.run() #print "halin10j outer", algorithm.outer parent = {0: 2, 1: 2, 2: None, 3: 2, 4: 3, 5: 6, 6: 3, 7: 6, 8: 7, 9: 7} self.assertEqual(algorithm.parent, parent) order = [7, 2, 8, 6, 0, 1, 4, 5, 9, 3] self.assertEqual(algorithm.order, order) self.assertEqual(len(algorithm.cliques), G.v()-3) # 1-----4---------5 numeracja wg cyklu Hamiltona # |\ | /| graf kubiczny # | 2---3---8---7 | # |/ | \| # 0---------9-----6 def test_halin10k(self): N = 10 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 2), Edge(0, 9), Edge(1, 2), Edge(1, 4), Edge(2, 3), Edge(3, 4), Edge(3, 8), Edge(4, 5), Edge(5, 6), Edge(5, 7), Edge(6, 7), Edge(6, 9), Edge(7, 8), Edge(8, 9)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "halin10k ..." algorithm = HalinGraphTreeDecomposition(G, outer=set([0, 1, 4, 5, 6, 9])) algorithm.run() #print "halin10k outer", algorithm.outer parent = {0: 2, 1: 2, 2: None, 3: 2, 4: 3, 5: 7, 6: 7, 7: 8, 8: 3, 9: 8} self.assertEqual(algorithm.parent, parent) order = [7, 2, 6, 8, 0, 1, 4, 5, 9, 3] self.assertEqual(algorithm.order, order) self.assertEqual(len(algorithm.cliques), G.v()-3) # --4---5-- # / \ / \ # 0 3 6 numeracja wg cyklu Hamiltona # |\ | /| graf kubiczny # | 1---2---7 | # |/ \| # 9-----------8 def test_halin10l(self): N = 10 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 4), Edge(0, 9), Edge(1, 2), Edge(1, 9), Edge(2, 3), Edge(2, 7), Edge(3, 4), Edge(3, 5), Edge(4, 5), Edge(5, 6), Edge(6, 7), Edge(6, 8), Edge(7, 8), Edge(8, 9)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "halin10l ..." algorithm = HalinGraphTreeDecomposition(G, outer=set([0, 4, 5, 6, 8, 9])) algorithm.run() #print "halin10l outer", algorithm.outer parent = {0: 1, 1: None, 2: 1, 3: 2, 4: 3, 5: 3, 6: 7, 7: 2, 8: 7, 9: 1} self.assertEqual(algorithm.parent, parent) #order = [3, 1, 7, 0, 4, 5, 6, 8, 9, 2] order = [3, 7, 1, 0, 4, 5, 6, 8, 9, 2] self.assertEqual(algorithm.order, order) self.assertEqual(len(algorithm.cliques), G.v()-3) # 10---8---7 wachlarz nieparzysty # / \ | / \ # / 9 \ # / | \ # 0---2---3---4---6 # \ / \ / # 1-----------5 def test_halin11(self): N = 11 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 2), Edge(0, 10), Edge(1, 2), Edge(1, 5), Edge(2, 3), Edge(3, 4), Edge(3, 9), Edge(4, 5), Edge(4, 6), Edge(5, 6), Edge(6, 7), Edge(7, 8), Edge(7, 9), Edge(8, 9), Edge(8, 10), Edge(9, 10)] # E=17 for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) algorithm = HalinGraphTreeDecomposition(G, outer=set([0, 1, 5, 6, 7, 8, 10])) algorithm.run() #print "halin11 ..." #print "halin11 outer", algorithm.outer parent = {0: 2, 1: 2, 2: None, 3: 2, 4: 3, 5: 4, 6: 4, 7: 9, 8: 9, 9: 3, 10: 9} self.assertEqual(algorithm.parent, parent) order = [4, 8, 9, 2, 0, 1, 5, 6, 7, 10, 3] self.assertEqual(algorithm.order, order) self.assertEqual(len(algorithm.cliques), G.v()-3) # -----------11 # / / \ # 0-------1--10---9 numeracja wg cyklu Hamiltona # | | | cubic Frucht graph # 4---3---2---7---8 # \ / \ / # 5-----------6 def test_frucht12(self): N = 10 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 4), Edge(0, 11), Edge(1, 2), Edge(1, 10), Edge(2, 3), Edge(2, 7), Edge(3, 4), Edge(3, 5), Edge(4, 5), Edge(5, 6), Edge(6, 7), Edge(6, 8), Edge(7, 8), Edge(8, 9), Edge(9, 10), Edge(9, 11), Edge(10, 11)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "frucht12 ..." algorithm = HalinGraphTreeDecomposition(G, outer=set([0, 4, 5, 6, 8, 9, 11])) algorithm.run() #print "frucht12 outer", algorithm.outer parent = {0: 1, 1: None, 2: 1, 3: 2, 4: 3, 5: 3, 6: 7, 7: 2, 8: 7, 9: 10, 10: 1, 11: 10} self.assertEqual(algorithm.parent, parent) #order = [10, 3, 7, 11, 1, 0, 4, 5, 6, 8, 9, 2] order = [10, 7, 3, 11, 1, 0, 4, 5, 6, 8, 9, 2] self.assertEqual(algorithm.order, order) self.assertEqual(len(algorithm.cliques), G.v()-3) # 0---------15--------14 cubic # / \ | / \ # 2---1---6---7---8---13--12 # \ / \ / # 3---5 9---11 # \ / \ / # 4---------------10 def test_halin16(self): N = 16 G = Graph(N, False) edges = [Edge(0, 1), Edge(0, 2), Edge(0, 15), Edge(1, 2), Edge(1, 6), Edge(2, 3), Edge(3, 4), Edge(3, 5), Edge(4, 5), Edge(4, 10), Edge(5, 6), Edge(6, 7), Edge(7, 8), Edge(7, 15), Edge(8, 9), Edge(8, 13), Edge(9, 10), Edge(9, 11), Edge(10, 11), Edge(11, 12), Edge(12, 13), Edge(12, 14), Edge(13, 14), Edge(14, 15)] for node in range(N): G.add_node(node) for edge in edges: G.add_edge(edge) #print "halin16 ..." algorithm = HalinGraphTreeDecomposition(G, outer=set([0, 2, 3, 4, 10, 11, 12, 14, 15])) algorithm.run() #print "halin16 outer", algorithm.outer parent = {0: 1, 1: None, 2: 1, 3: 5, 4: 5, 5: 6, 6: 1, 7: 6, 8: 7, 9: 8, 10: 9, 11: 9, 12: 13, 13: 8, 14: 13, 15: 7} self.assertEqual(algorithm.parent, parent) order = [5, 9, 13, 1, 11, 12, 8, 2, 3, 6, 0, 4, 10, 14, 15, 7] self.assertEqual(algorithm.order, order) self.assertEqual(len(algorithm.cliques), G.v()-3) def tearDown(self): pass
#!/usr/bin/python import timeit from graphtheory.structures.edges import Edge from graphtheory.structures.graphs import Graph from graphtheory.structures.factory import GraphFactory from graphtheory.independentsets.isetbt import BacktrackingIndependentSet V = 10 graph_factory = GraphFactory(Graph) probability = 0.5 G = graph_factory.make_random(V, False, probability) #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 ( "Testing BacktrackingIndependentSet ..." ) t1 = timeit.Timer(lambda: BacktrackingIndependentSet(G).run()) print ( "{} {} {}".format(V, E, t1.timeit(1)) ) # single run # EOF
from graphtheory.independentsets.isetsf import SmallestFirstIndependentSet2 from graphtheory.independentsets.isetsf import SmallestFirstIndependentSet3 from graphtheory.independentsets.isetsf import SmallestFirstIndependentSet4 from graphtheory.independentsets.isetsf import SmallestFirstIndependentSet5 from graphtheory.independentsets.isetsf import SmallestFirstIndependentSet6 from graphtheory.independentsets.isetsf import SmallestFirstIndependentSet7 from graphtheory.independentsets.isetll import LargestLastIndependentSet1 from graphtheory.independentsets.isetll import LargestLastIndependentSet2 from graphtheory.independentsets.isetll import LargestLastIndependentSet3 from graphtheory.independentsets.isetll import LargestLastIndependentSet4 from graphtheory.independentsets.isetll import LargestLastIndependentSet5 from graphtheory.independentsets.isetll import LargestLastIndependentSet6 from graphtheory.independentsets.isetll import LargestLastIndependentSet7 V = 100 graph_factory = GraphFactory(Graph) probability = 0.5 G = graph_factory.make_random(V, False, probability) E = G.e() #G.show() print "Calculate parameters ..." print "Nodes:", G.v(), V print "Edges:", G.e(), E print "Directed:", G.is_directed() print "Testing UnorderedSequentialIndependentSet1 ..." t1 = timeit.Timer(lambda: UnorderedSequentialIndependentSet1(G).run()) print V, E, t1.timeit(1) # pojedyncze wykonanie print "Testing UnorderedSequentialIndependentSet2 ..."
from graphtheory.structures.edges import Edge from graphtheory.structures.graphs import Graph from graphtheory.structures.factory import GraphFactory from graphtheory.connectivity.connected import is_connected from graphtheory.algorithms.acyclic import is_acyclic from graphtheory.bipartiteness.bipartite import is_bipartite from graphtheory.connectivity.cutnodes import is_biconnected from graphtheory.spanningtrees.boruvka import BoruvkaMST from graphtheory.spanningtrees.prim import PrimMST from graphtheory.spanningtrees.prim import PrimMatrixMST from graphtheory.spanningtrees.prim import PrimTrivialMST 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())
#!/usr/bin/python import timeit import random from graphtheory.structures.edges import Edge from graphtheory.structures.graphs import Graph from graphtheory.structures.factory import GraphFactory from graphtheory.planarity.halintd import HalinGraphTreeDecomposition from graphtheory.planarity.halintools import make_halin_outer from graphtheory.planarity.halintools import make_halin_cubic_outer V = 10 graph_factory = GraphFactory(Graph) G = graph_factory.make_necklace(n=V) # V even outer = set(range(0,V,2)) | set([V-1]) # necklace #G, outer = make_halin_outer(V) #G, outer = make_halin_cubic_outer(V) # always finishing with 7-wheel E = G.e() #G.show() print ( "Testing HalinGraphTreeDecomposition ..." ) t1 = timeit.Timer(lambda: HalinGraphTreeDecomposition(G, outer).run()) print ( "{} {} {}".format(V, E, t1.timeit(1)) ) # single run # EOF
def setUp(self): self.N = 10 # number of nodes self.graph_factory = GraphFactory(Graph)
#!/usr/bin/python import unittest import timeit from graphtheory.structures.edges import Edge from graphtheory.structures.graphs import Graph from graphtheory.forests.treecenter import TreeCenter from graphtheory.structures.factory import GraphFactory V = 1000 # liczba wierzcholkow E = V-1 # liczba krawedzi graph_factory = GraphFactory(Graph) G = graph_factory.make_tree(V) #G.show() print ( "Calculate parameters ..." ) print ( "Nodes: {} {}".format( G.v(), V )) print ( "Edges: {} {}".format( G.e(), E )) assert G.v() == V assert G.e() == E print ( "Directed: {}".format( G.is_directed() )) print ( "Delta: {}".format( max(G.degree(node) for node in G.iternodes()) )) print ( "Testing TreeCenter ..." ) t1 = timeit.Timer(lambda: TreeCenter(G).run()) print ( "{} {} {}".format(V, E, t1.timeit(1)) ) # single run # EOF
class TestGraphFactory(unittest.TestCase): def setUp(self): self.N = 10 # number of nodes self.graph_factory = GraphFactory(Graph) def test_complete(self): G = self.graph_factory.make_complete(n=self.N, directed=False) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), self.N) self.assertEqual(G.e(), self.N * (self.N-1) // 2) aset = set(edge.weight for edge in G.iteredges()) self.assertEqual(G.e(), len(aset)) def test_complete_directed(self): G = self.graph_factory.make_complete(n=self.N, directed=True) self.assertTrue(G.is_directed()) self.assertEqual(G.v(), self.N) self.assertEqual(G.e(), self.N * (self.N-1) // 2) aset = set(edge.weight for edge in G.iteredges()) self.assertEqual(G.e(), len(aset)) 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)) self.assertRaises(ValueError, self.graph_factory.make_cyclic, 1) self.assertRaises(ValueError, self.graph_factory.make_cyclic, 2) def test_cyclic_directed(self): G = self.graph_factory.make_cyclic(n=self.N, directed=True) self.assertTrue(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)) self.assertRaises(ValueError, self.graph_factory.make_cyclic, 1) self.assertRaises(ValueError, self.graph_factory.make_cyclic, 2) def test_sparse(self): m_edges = 2 * self.N G = self.graph_factory.make_sparse(n=self.N, directed=False, m=m_edges) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), self.N) self.assertEqual(G.e(), m_edges) aset = set(edge.weight for edge in G.iteredges()) self.assertEqual(G.e(), len(aset)) def test_tree(self): G = self.graph_factory.make_tree(n=self.N) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), self.N) self.assertEqual(G.e(), self.N-1) aset = set(edge.weight for edge in G.iteredges()) self.assertEqual(G.e(), len(aset)) def test_connected(self): m_edges = 2 * self.N G = self.graph_factory.make_connected(n=self.N, m=m_edges) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), self.N) self.assertEqual(G.e(), m_edges) aset = set(edge.weight for edge in G.iteredges()) self.assertEqual(G.e(), len(aset)) def test_random(self): G = self.graph_factory.make_random( n=self.N, directed=False, edge_probability=0.1) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), self.N) aset = set(edge.weight for edge in G.iteredges()) self.assertEqual(G.e(), len(aset)) def test_random_directed(self): G = self.graph_factory.make_random( n=self.N, directed=True, edge_probability=0.1) self.assertTrue(G.is_directed()) self.assertEqual(G.v(), self.N) aset = set(edge.weight for edge in G.iteredges()) self.assertEqual(G.e(), len(aset)) def test_bipartite(self): N1, N2 = 5, 6 G = self.graph_factory.make_bipartite(N1, N2, directed=False, edge_probability=0.1) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), N1 + N2) aset = set(edge.weight for edge in G.iteredges()) self.assertEqual(G.e(), len(aset)) # We use the fact that nodes are in two sets # [0 ... N1-1], [N1 ... N1+N2-1]. for edge in G.iteredges(): self.assertEqual(edge.source < N1, edge.target >= N1) def test_grid(self): size = 4 G = self.graph_factory.make_grid(size) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), size * size) self.assertEqual(G.e(), 2 * size * (size-1)) self.assertRaises(ValueError, self.graph_factory.make_grid, 2) def test_grid_periodic(self): size = 4 G = self.graph_factory.make_grid_periodic(size) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), size * size) self.assertEqual(G.e(), 2 * size * size) self.assertRaises( ValueError, self.graph_factory.make_grid_periodic, 2) def test_triangle(self): size = 4 G = self.graph_factory.make_triangle(size) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), size * size) self.assertEqual(G.e(), 3 * size * size -4 * size + 1) self.assertRaises(ValueError, self.graph_factory.make_triangle, 2) def test_triangle_periodic(self): size = 4 G = self.graph_factory.make_triangle_periodic(size) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), size * size) self.assertEqual(G.e(), 3 * size * size) self.assertRaises( ValueError, self.graph_factory.make_triangle_periodic, 2) def test_ladder(self): size = 4 G = self.graph_factory.make_ladder(size) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), 2 * size) self.assertEqual(G.e(), 3 * size -2) self.assertRaises(ValueError, self.graph_factory.make_ladder, 2) def test_prism(self): size = 4 G = self.graph_factory.make_prism(size) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), 2 * size) self.assertEqual(G.e(), 3 * size) self.assertRaises( ValueError, self.graph_factory.make_prism, 2) def test_antiprism(self): size = 4 G = self.graph_factory.make_antiprism(size) self.assertFalse(G.is_directed()) self.assertEqual(G.v(), 2 * size) self.assertEqual(G.e(), 4 * size) self.assertRaises( ValueError, self.graph_factory.make_antiprism, 2) def test_flow_network(self): G = self.graph_factory.make_flow_network(self.N) self.assertTrue(G.is_directed()) self.assertEqual(G.v(), self.N) self.assertTrue(G.e() > self.N - 2) def test_necklace(self): G = self.graph_factory.make_necklace(n=self.N, directed=False) self.assertEqual(G.v(), self.N) self.assertEqual(G.e(), 3 * self.N // 2) self.assertRaises(ValueError, self.graph_factory.make_necklace, 1) self.assertRaises(ValueError, self.graph_factory.make_necklace, 2) self.assertRaises(ValueError, self.graph_factory.make_necklace, 3) self.assertRaises(ValueError, self.graph_factory.make_necklace, 5) #G = self.graph_factory.make_necklace(n=6, directed=False) #G.show() 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)) 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 test_fake_wheel(self): G = self.graph_factory.make_fake_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.assertFalse(is_wheel(G)) self.assertRaises(ValueError, self.graph_factory.make_fake_wheel, 1) self.assertRaises(ValueError, self.graph_factory.make_fake_wheel, 2) self.assertRaises(ValueError, self.graph_factory.make_fake_wheel, 3) self.assertRaises(ValueError, self.graph_factory.make_fake_wheel, 4) self.assertRaises(ValueError, self.graph_factory.make_fake_wheel, 5) self.assertRaises(ValueError, self.graph_factory.make_fake_wheel, 6) def tearDown(self): pass
class TestWheel(unittest.TestCase): def setUp(self): self.graph_factory = GraphFactory(Graph) def test_k4(self): # complete graph K4 = W4 G = self.graph_factory.make_complete(n=4, directed=False) self.assertTrue(is_wheel(G)) algorithm = WheelGraph(G) algorithm.run() self.assertEqual(algorithm.hub, 0) def test_wheel_true(self): v = 10 G = Graph(v, False) for node in range(v): G.add_node(node) hub = 0 for node in range(1, v): G.add_edge(Edge(hub, node)) G.add_edge(Edge(node, node+1 if node < v-1 else 1)) self.assertTrue(is_wheel(G)) algorithm = WheelGraph(G) algorithm.run() self.assertEqual(algorithm.hub, hub) def test_is_wheel(self): G = self.graph_factory.make_complete(n=3, directed=False) self.assertFalse(is_wheel(G)) self.assertRaises(ValueError, lambda: WheelGraph(G).run()) def test_wheel_false(self): # 0---+ +---5 windmill graph # |\ \ / /| https://en.wikipedia.org/wiki/Windmill_graph # | 1---3---4 | # |/ / \ \| # 2---+ +---6 v = 7 G = Graph(v, False) for node in range(v): G.add_node(node) edges = [ Edge(0, 1), Edge(1, 2), Edge(0, 2), Edge(4, 5), Edge(5, 6), Edge(4, 6), Edge(0, 3), Edge(1, 3), Edge(2, 3), Edge(3, 5), Edge(3, 4), Edge(3, 6)] for edge in edges: G.add_edge(edge) self.assertFalse(is_wheel(G)) self.assertRaises(ValueError, lambda: WheelGraph(G).run()) def test_wheel_false2(self): # 1---2 missing Edge(0, 3) or Edge(1, 3) # |\ /| # | 0 | # |/ | # 4---3 v = 5 G = Graph(v, False) for node in range(v): G.add_node(node) edges = [ Edge(0, 1), Edge(0, 2), Edge(0, 4), Edge(1, 2), Edge(2, 3), Edge(3, 4), Edge(1, 4)] for edge in edges: G.add_edge(edge) G.add_edge(Edge(2, 4)) # bad edge self.assertFalse(is_wheel(G)) self.assertRaises(ValueError, lambda: WheelGraph(G).run()) def tearDown(self): pass
#!/usr/bin/python import timeit import random from graphtheory.structures.edges import Edge from graphtheory.structures.graphs import Graph from graphtheory.structures.factory import GraphFactory from graphtheory.planarity.halin import HalinGraph from graphtheory.planarity.halintools import make_halin_outer from graphtheory.planarity.halintools import make_halin_cubic_outer V = 10 graph_factory = GraphFactory(Graph) G = graph_factory.make_necklace(n=V) # V even outer = set(range(0,V,2)) | set([V-1]) # necklace #G, outer = make_halin_outer(V) #G, outer = make_halin_cubic_outer(V) # always finishing with 7-wheel E = G.e() #G.show() print ( "Testing HalinGraph ..." ) t1 = timeit.Timer(lambda: HalinGraph(G).run()) print ( "{} {} {}".format(V, E, t1.timeit(1)) ) # single run # EOF
#!/usr/bin/python import timeit from graphtheory.structures.edges import Edge from graphtheory.structures.graphs import Graph from graphtheory.structures.factory import GraphFactory from graphtheory.forests.treeiset import BorieIndependentSet from graphtheory.forests.treeiset import TreeIndependentSet1 from graphtheory.forests.treeiset import TreeIndependentSet2 V = 10 E = V-1 # tree graph_factory = GraphFactory(Graph) G = graph_factory.make_tree(V, False) #G.show() print ( "Calculate parameters ..." ) print ( "Nodes: {} {}".format( G.v(), V )) print ( "Edges: {} {}".format( G.e(), E )) print ( "Directed: {}".format( G.is_directed() )) algorithm = BorieIndependentSet(G) algorithm.run() print ( algorithm.independent_set ) print ( "borie iset []".format( algorithm.cardinality )) algorithm = TreeIndependentSet1(G) algorithm.run() print ( algorithm.independent_set ) print ( "tree iset1 []".format( algorithm.cardinality ))
#!/usr/bin/python import timeit from graphtheory.structures.edges import Edge from graphtheory.structures.graphs import Graph from graphtheory.structures.factory import GraphFactory from graphtheory.flow.fordfulkerson import FordFulkerson from graphtheory.flow.fordfulkerson import FordFulkersonSparse from graphtheory.flow.fordfulkerson import FordFulkersonWithEdges from graphtheory.flow.fordfulkerson import FordFulkersonRecursive # very slow from graphtheory.flow.edmondskarp import EdmondsKarp from graphtheory.flow.edmondskarp import EdmondsKarpSparse V = 20 graph_factory = GraphFactory(Graph) G = graph_factory.make_flow_network(V) E = G.e() #G.show() print "Calculate parameters ..." print "Nodes:", G.v(), V print "Edges:", G.e(), E print "Directed:", G.is_directed() print "Testing FordFulkerson ..." t1 = timeit.Timer(lambda: FordFulkerson(G).run(0, V - 1)) print V, E, t1.timeit(1) # single run print "Testing FordFulkersonSparse ..." t1 = timeit.Timer(lambda: FordFulkersonSparse(G).run(0, V - 1)) print V, E, t1.timeit(1) # single run