def testEvenHoleFree(self): g = make_cycle(4) self.assertEqual(even_hole_free(g), False) g = make_cycle(5) self.assertEqual(even_hole_free(g), True) g = make_cycle(10) self.assertEqual(even_hole_free(g), False)
def testColoringHardGraph(self): # C5 + 5 Xi g = make_cycle(5) index = 0 for i in range(5, 10): g.add_node(i) # make it a two vertex g.add_edge(i, (index+0) % 5) # xi g.add_edge(i, (index+1) % 5) index += 1 g.add_edge(5, 6) g.add_edge(5, 8) g.add_edge(6, 8) g.add_edge(7, 9) self.dcolor = Dcolor(g) color = self.dcolor.color() expect = [[1, 8, 9], [5, 4, 7], [2], [0, 3, 6]] self.assertEqual(self.dcolor.valid_coloring(color), True) self.assertEqual(color, expect) # C5 + 2 Yi g = make_cycle(5) g.add_node(5) g.add_edge(0,5) g.add_edge(1,5) g.add_edge(2,5) g.add_node(6) g.add_edge(0,6) g.add_edge(3,6) g.add_edge(4,6) self.dcolor = Dcolor(g) color = self.dcolor.color() expect = [[0, 3], [1, 4], [5, 6], [2]] self.assertEqual(self.dcolor.valid_coloring(color), True) self.assertEqual(color, expect)
def go(): forbidden = [make_clique(4), make_cycle(4), make_clique(6), make_cycle(8)] onlyfiles = [ f for f in listdir(MYPATH) if isfile(join(MYPATH,f)) ] for f in onlyfiles: print("Working on:") print(f) g = File(MYPATH,file=f).load(f) check_graph(DROPS, g, forbidden) return
def go(): forbidden = [make_co_cricket(), make_cycle(4), make_cycle(6), make_cycle(8), make_wheel(6)] g = make_cycle(7) check_graph(DROPS, g, forbidden) return
def go(): forbidden = [make_kite(), make_cycle(4), make_cycle(6), make_cycle(8), make_wheel(6)] g = make_cycle(5) check_graph(DROPS, g, forbidden) return
def testOddHoleFree(self): g = make_cycle(4) self.assertEqual(odd_hole_free(g), True) g = make_cycle(5) self.assertEqual(odd_hole_free(g), False) g = make_cycle(10) self.assertEqual(odd_hole_free(g), True) g = make_cycle(11) self.assertEqual(odd_hole_free(g), False)
def testEvenHoleFree(self): g = make_cycle(4) self.assertEqual(even_hole_free(g).edges(), g.edges()) self.assertEqual(even_hole_free(g).nodes(), g.nodes()) g = make_cycle(5) self.assertEqual(even_hole_free(g), None) g = make_cycle(10) self.assertEqual(even_hole_free(g).edges(), g.edges()) self.assertEqual(even_hole_free(g).nodes(), g.nodes())
def TheAlgorithm(G): dColor = Dcolor(G) partialColoring = list() #Compute chi(G) (using brute force) k = len(dColor.color()) hasStrongStableSet = False thisStableSet = FindStrongStableSet(G) if thisStableSet != None: hasStrongStableSet = True while hasStrongStableSet: #thisStableSet = FindStrongStableSet(G) partialColoring.append(list(thisStableSet)) #Remove this stable set from the graph for thisStableVertex in thisStableSet: G.remove_node(thisStableVertex) thisStableSet = FindStrongStableSet(G) if thisStableSet == None: hasStrongStableSet = False #check for induced C7 graphToTest = convert_node_labels_to_integers(G, 0, ordering='default', label_attribute = None) if induced_subgraph(graphToTest, make_cycle(CYCLE_LENGTH)) == None: stillHasInducedC7 = False else: stillHasInducedC7 = True graphToTest.clear() while stillHasInducedC7 == True: thisStableSet = FindSimpleStableSet(G) partialColoring.append(thisStableSet) for thisStableVertex in thisStableSet: G.remove_node(thisStableVertex) graphToTest = convert_node_labels_to_integers(G, 0, ordering='default', label_attribute = None) if induced_subgraph(graphToTest, make_cycle(CYCLE_LENGTH)) == None: stillHasInducedC7 = False graphToTest.clear() """ At this point, there does not exist a strong stable set of size 3, because there is no C7. This means that G is now a perfect graph. """ t = chromatic_number(G) #Find the chromatic number of our partial graph of stable sets s = len(partialColoring) if k == (s + t): result = True else: result = False return result
def testOddHoleFree(self): g = make_cycle(4) self.assertEqual(odd_hole_free(g), None) g = make_cycle(5) self.assertEqual(odd_hole_free(g).edges(), g.edges()) self.assertEqual(odd_hole_free(g).nodes(), g.nodes()) g = make_cycle(10) self.assertEqual(odd_hole_free(g), None) g = make_cycle(11) self.assertEqual(odd_hole_free(g).edges(), g.edges()) self.assertEqual(odd_hole_free(g).nodes(), g.nodes())
def testForbidden(self): claw = make_claw() c4 = make_cycle(4) cok4 = make_cok4() g = make_cycle(5) not_allowed = [claw, c4, cok4] gen = Generator2(g, 1, forbidden=not_allowed) for graph in gen.iterate(): for h in not_allowed: if induced_subgraph(graph, h) is not None: print(graph.edges()) print(h.edges()) self.assertEqual(True, False ,"Failed to forbid a graph")
def testColoringCritical(self): self.dcolor = Dcolor(make_cycle(5)) color = self.dcolor.color() expect = [[0], [1, 3], [2, 4]] self.assertEqual(len(color), 3) self.assertEqual(color, expect) k1 = nx.Graph() k1.add_node(0) g = join(make_cycle(5), k1) self.dcolor = Dcolor(g) color = self.dcolor.color() self.assertEqual(len(color), 4) expect = [[5], [0], [1, 3], [2, 4]] self.assertEqual(expect, color)
def testColoringClique(self): g = make_cycle(3) self.dcolor = Dcolor(g) color = self.dcolor.color() expect = [[0], [1], [2]] self.assertEqual(len(color), 3) self.assertEqual(color, expect)
def Process(): for thisXConfiguration in product(range(MAX_X_CARDINALITY), repeat = BASE_CYCLE_SIZE): baseGraph = make_cycle(7) baseGraph, x_sets = ConstructGraph(thisXConfiguration, baseGraph) thisGraph = deepcopy(baseGraph) print(thisGraph.edges()) #Try all possible combos of X and X_i+2 edges for xSetIndex in range(0,BASE_CYCLE_SIZE): s1 = x_sets[xSetIndex] s2 = x_sets[(xSetIndex + 2) % BASE_CYCLE_SIZE] for i in range(0,4): for thisEdgeCombo in combinations(product(s1,s2),i): for thisEdge in thisEdgeCombo: thisGraph.add_edge(thisEdge[0], thisEdge[1]) WriteToLogFile(FinalProcessGraph(thisGraph)) #for each of these combos, try it with all combos of X and X_i+5 thisGraphMoreJoins = deepcopy(thisGraph) s1 = x_sets[xSetIndex] s2 = x_sets[(xSetIndex + 5) % BASE_CYCLE_SIZE] for i in range(0,4): for thisEdgeCombo in combinations(product(s1,s2),i): for thisEdge in thisEdgeCombo: thisGraphMoreJoins.add_edge(thisEdge[0], thisEdge[1]) WriteToLogFile(FinalProcessGraph(thisGraphMoreJoins)) thisGraph = deepcopy(baseGraph) baseGraph.clear() return
def testC5DiamondCoDiamondFree(self): g = make_cycle(5) subgraphs = [make_diamond(), make_co_diamond()] k_vertexes = k_vertex(g, subgraphs) expect = [False, False, True, True, False, False] for index, k in enumerate(k_vertexes): self.assertEqual(k['has_k_vertex'], expect[index], '''K Vertex says (diamond,co-diamond)- free Graph %d - vertex:%r but should be %r''' %(index, k['has_k_vertex'], expect[index])) set_2_vertex = [(0, 1), (0, 4), (1, 2), (2, 3), (3, 4)] for check in set_2_vertex: self.assertEqual(check in k_vertexes[2]['combinations'], True, ''' K vertex missing 2 Vertex set (%d, %d) on (diamond, co-diamond)-free Grpah ''' %(check[0],check[1])) set_3_vertex = [(0, 1, 3), (0, 2, 3), (0, 2, 4), (1, 2, 4), (1, 3, 4)] for check in set_3_vertex: self.assertEqual(check in k_vertexes[3]['combinations'], True, ''' K vertex missing 3 Vertex set (%d, %d, %d) on (diamond, co-diamond)-free Grpah ''' %(check[0],check[1], check[2]))
def process(): index = 0 #Generate an array corresponding to the k-vertices we want to add to c7 for add in combinations_with_replacement(range(4), 7): print(add) #We can have at most 3 Z's and 2 Y's, making 5 possible vertices we can add if count(add) > 5: break for thisPermutation in permutations(add): #copy initial graph (c7) and add vertices #self.g = BASE.copy() g = make_cycle(7) add_vertices(g, thisPermutation) check = True #we want our graph to remain {4k1,c4,c6}-free for H in FORBIDDEN: if induced_subgraph(g, H): check = False break if check: #log it f = File(DIRECTORY, G=g, logger=LOGGER, base="C5-") fp = f.save() if fp is not None: index += 1 print("Created Graph: %d" % index)
def Construct(): resultGraph = make_cycle(CYCLE_LENGTH) x_sets = [ [] for i in range(0,CYCLE_LENGTH)] #Add the nodes for vertexIndex in range(0, NUMBER_OF_LAYERS * CYCLE_LENGTH): #add the new node vertexToAddNum = resultGraph.number_of_nodes() resultGraph.add_node(vertexToAddNum) x_sets[vertexIndex % CYCLE_LENGTH].append(vertexToAddNum) #add its edges resultGraph.add_edge( (vertexToAddNum - 1) % CYCLE_LENGTH, vertexToAddNum ) resultGraph.add_edge( vertexToAddNum % CYCLE_LENGTH, vertexToAddNum) resultGraph.add_edge( (vertexToAddNum + 1) % CYCLE_LENGTH, vertexToAddNum) #x_i forms a clique for thisXSet in x_sets: if(len(thisXSet) > 1): for thisCliqueEdge in product(thisXSet, thisXSet): if(thisCliqueEdge[0] != thisCliqueEdge[1]): resultGraph.add_edge(thisCliqueEdge[0], thisCliqueEdge[1]) #X_i joins its neighbours for thisXSetIndex in range(0,CYCLE_LENGTH): x1 = x_sets[thisXSetIndex] x2 = x_sets[(thisXSetIndex+1) % CYCLE_LENGTH] for vertexI in x1: for vertexJ in x2: resultGraph.add_edge(vertexI,vertexJ) return resultGraph
def TestAdjacentToVertices(self): expect = adjacent_to_vertices(-1, [0, 1], make_claw()) self.assertEqual(expect, False) expect = adjacent_to_vertices(0, [1, 2, 3], make_claw()) self.assertEqual(expect, True) expect = adjacent_to_vertices(0, [2, 3], make_cycle(5)) self.assertEqual(expect, False)
def ConstructBaseGraph(): global CURRENT_X_SETS, CURRENT_Y_SETS CURRENT_X_SETS = [ [i] for i in range(0, CYCLE_LENGTH)] CURRENT_Y_SETS = [ [] for i in range(0, CYCLE_LENGTH)] result = make_cycle(CYCLE_LENGTH) return deepcopy(result)
def testColorCycle(self): g = make_cycle(5) for i in range(5, 100): g.add_node(i) g.add_edge(i - 1, i) self.dcolor = Dcolor(g) color = self.dcolor.color() self.assertEqual(len(color), 3) self.assertEqual(self.dcolor.valid_coloring(color), True)
def testTriangleFree(self): G = make_cycle(4) self.assertEqual(triangle_free(G), True) G = make_claw() self.assertEqual(triangle_free(G), True) G = make_co_claw() self.assertEqual(triangle_free(G), False) G = make_diamond() self.assertEqual(triangle_free(G), False)
def testTotalGraphs(self): G = nx.Graph() G.add_node(0) self.gen = Generator(G, 2, []) result = self.gen.total_graphs() self.assertEqual(10, result) G = make_cycle(5) self.gen = Generator(G, 5, []) result = self.gen.total_graphs() self.assertEqual(34427111456, result)
def even_hole_free(G): i_set = graph_clique_number(complement(G)) free = True i = 4 while i <= i_set * 2 and free: g = make_cycle(i) if induced_subgraph(G, g): free = False i += 2 return free
def testLoad(self): claw = make_claw() c7 = make_cycle(7) co_claw = make_co_claw() tests = {"test1.txt": claw, "test2.txt": c7, "test3.txt": co_claw} directory = os.path.join(self.directory, "test") for file, expect in tests.items(): file_obj = File(directory, file=file) self.assertEqual(expect.nodes(), file_obj.G.nodes(), "Load Failed Nodes: %s" % file) self.assertEqual(expect.edges(), file_obj.G.edges(), "Load Failed :Edges %s" % file)
def testCheckCondition(self): test = check_condition_holds(make_clique(5)) expect = False self.assertEqual(test, expect) test = check_condition_holds(make_clique(3)) expect = False self.assertEqual(test, expect) test = check_condition_holds(make_cycle(5)) expect = True self.assertEqual(test, expect)
def critical_cycle(n): g = make_cycle(n) v1 = n v2 = n+1 g.add_node(v1) g.add_node(v2) for i in range(0, n // 2 + 1): g.add_edge(i, v1) g.add_edge(n-i-1, v2) return g
def even_hole_free(G): i_set = graph_clique_number(complement(G)) free = None i = 4 while i <= i_set * 2 and free is None: g = make_cycle(i) induced = induced_subgraph(G, g) if induced is not None: free = induced i += 2 return free
def testColoringCritical(self): c5 = make_cycle(5) color = coloring(c5) expect = [[4, 1], [3, 0], [2]] self.assertEqual(len(color), 3) self.assertEqual(color, expect) k1 = nx.Graph() k1.add_node(0) g = join(c5, k1) color = coloring(g) self.assertEqual(len(color), 4) expect = [[4], [3, 1], [2, 0], [5]] self.assertEqual(expect, color)
def testGetName(self): g = make_claw() f = File(self.directory, G=g, logger=self.logger) self.assertEqual(f.get_name(), "has_K2_edges-3_nodes-4.txt") g = make_cycle(5) f = File(self.directory, G=g, logger=self.logger) self.assertEqual(f.get_name(), "has_K2_edges-5_nodes-5.txt") self.assertEqual(f.get_name(1), "has_K2_edges-5_nodes-5--1.txt") self.assertEqual(f.get_name(10), "has_K2_edges-5_nodes-5--10.txt") f = File(self.directory, G=g, logger=self.logger, base="C5") self.assertEqual(f.get_name(), "C5has_K2_edges-5_nodes-5.txt") self.assertEqual(f.get_name(1), "C5has_K2_edges-5_nodes-5--1.txt") self.assertEqual(f.get_name(10), "C5has_K2_edges-5_nodes-5--10.txt")
def testCompare(self): claw = make_claw() co_claw = make_co_claw() f = File(self.directory, G=claw, logger=self.logger) g = File(self.directory, G=claw, logger=self.logger) self.assertEqual(g.compare(f), True) self.assertEqual(f.compare(g), True) g = File(self.directory, G=co_claw, logger=self.logger) self.assertEqual(g.compare(f), False) self.assertEqual(f.compare(g), False) h = File(self.directory, G=make_cycle(4), logger=self.logger) self.assertEqual(h.compare(f), False) self.assertEqual(f.compare(h), False)
def setUp(self): self.graph = make_cycle(5) x = 5 y = 6 w = 7 self.graph.add_node(x) self.graph.add_node(y) self.graph.add_node(w) self.graph.add_edge(1, x) self.graph.add_edge(2, y) self.graph.add_edge(3, y) for i in range(0, 4): self.graph.add_edge(i, w)