def free(self): ''' a method to determine if the graph is ISK4-free Parameters: None Returns: sub: list of vertices that form a ISK4 is None if does not contain (list) ''' # check if omega is greater than four sub = None if nx.graph_clique_number(self.g) > 3: sub = induced_subgraph(self.g, make_clique(4)) else: # loop through all possible subdivisions between 4 and n n = len(self.g.nodes()) i = 0 while i < n - 3 and sub is None: for ball in unlabeled_balls_in_unlabeled_boxe(i, [i]*6): graph = self.create_subdivions(ball) sub = induced_subgraph(self.g, graph) if sub is not None: break i += 1 return sub
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 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 compare_graphs(self,g , h): same = False if (len(g.nodes()) == len(h.nodes())): induced = induced_subgraph(g, h) if induced is not None: same = True return same
def GIsHFree(G, H): result = True for thisForbiddenInducedSubgraph in H: if induced_subgraph(G, thisForbiddenInducedSubgraph): result = False break return result
def IsHFreeGraph(H,G): result = True for thisForbiddenGraph in H: if induced_subgraph(G, thisForbiddenGraph): result = False break return 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 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 compare(self, B): """ a method use to compare the two graphs of the File objects given Parameter: B: the object to compare to (File) Returns: True if the two graphs are the same (isomorphic) False otherwise """ self.logger.debug("Comparing two graphs") same = False cond1 = len(self.G.nodes()) == len(B.G.nodes()) cond2 = len(self.G.edges()) == len(B.G.edges()) self.logger.debug("same edges: %r Same nodes: %r" % (cond1, cond2)) if cond1 and cond2: induced = induced_subgraph(self.G, B.G) induced_2 = induced_subgraph(B.G, self.G) if induced is not None or induced_2 is not None: same = True self.logger.debug("The two graphs are the same: %r" % same) return same
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 iterate(self): ''' a generator for all the graphs starting with G adding up to N vertices not containing any H as a induced subgraph Parameters: Returns: yields: a graph h (networx) ''' self.logger.info("Generating graphs up to %d" % self.n) index = len(self.G.nodes()) - 1 add_nodes = 0 l_nodes = len(self.G.nodes()) g = self.G.copy() while index < (self.n + l_nodes - 1): # add a node # print("----------------") # print("Index: %d of %d" % (index, self.n + l_nodes - 1)) # print("----------------") self.logger.info("Index: %d" % index) index += 1 add_nodes += 1 g.add_node(index) max_edges = int(l_nodes * add_nodes + ((add_nodes)*(add_nodes-1)) /2) self.logger.debug("Max Edges %d" % max_edges) for x in range(0, 2**max_edges): bitstring = self.to_bitstring(x, max_edges) h = g.copy() # add the edges for i, bit in enumerate(bitstring): self.logger.debug(i) if bit == "1": target, source = self.determine(i, l_nodes, add_nodes) self.logger.debug("Target: %d " % target) self.logger.debug("Source: %d" % source) h.add_edge(target, source) allowed = True for not_allowed in self.forbidden: if induced_subgraph(h, not_allowed) is not None: allowed = False break if allowed: self.logger.info("Graph was not forbidden") yield h
def special_graph(g): valid = False if induced_subgraph(g, make_diamond()) is not None: if clique_cutset(g) is None: valid = True return valid
for thisAVertex in A_VERTICES: for thisBVertex in B_VERTICES: G.add_edge(thisAVertex, thisBVertex) #Populate the C vertices and create the necessary edges for i in range(0,rows): for j in range(0,cols): vertexToAdd = G.number_of_nodes() G.add_node(vertexToAdd) C_VERTICES.append(vertexToAdd) for thisAVertex in A_VERTICES: if thisAVertex >= i: G.add_edge(vertexToAdd, thisAVertex) for thisBVertex in B_VERTICES: if (thisBVertex - 2) >= j: G.add_edge(vertexToAdd, thisBVertex) return G G = constructGn(3, 3) #Check for forbidden subgraphs if not induced_subgraph(G, make_cycle(5)): print("G has no induced C5!!!") if not induced_subgraph(G, make_cycle(6)): print("G has no induced C6!!!")