def remove_val_two_vertex(graph): '''Given a graph, this function removes a valence-two vertex if one exists. ''' vertex = find_val_two_vertex(graph) if vertex is not None: edge1 = fh.find_edge(graph, vertex[0][0]) edge2 = fh.find_edge(graph, vertex[1][0]) ht1, ht2 = vertex[0][1], vertex[1][1] graph.nodes.append(make_merged_edge(edge1, ht1, edge2, ht2)) fix_turns_merge(graph, edge1, ht1, edge2, ht2) fh.delete_folded_edges(graph.nodes, [edge1.name, edge2.name]) graph.update_matrix else: return
def partial_fold(old_graph, edge1, ht1, edge2, ht2): '''Given a graph, two edges to be folded, and the directions the folding is happening, this function outputs a graph that is the original graph with the fold partially performed on it. ''' graph = copy.deepcopy(old_graph) # alias the edges to those in the new graph edge1, edge2 = fh.find_edge(graph, edge1.name), fh.find_edge(graph, edge2.name) new_edge_name = next_edge_name(graph.nodes) fix_turns(new_edge_name, graph.nodes, edge1, edge2, ht1, ht2) graph.nodes.append(make_new_edge(new_edge_name, edge1, edge2, ht1, ht2)) fix_folded_edges(new_edge_name, edge1, edge2, ht1, ht2) for edge in graph.nodes: fh.remove_self_connect(edge) mleg.remove_val_two_vertex(graph) graph.update_matrix() return graph
def diff_length_fold(old_graph, edge_long, ht_long, edge_short, ht_short): '''Given a graph, two edges to be folded, and the directions the folding is happening, this function outputs a graph that is the original graph with the fold performed in it as if edge_long is longer than edge_short. ''' graph = copy.deepcopy(old_graph) edge_long = f.find_edge(graph, edge_long.name) # Set the variables to the nodes in edge_short = f.find_edge(graph, edge_short.name) # the new graph fix_turns(graph.nodes, edge_long, ht_long, edge_short, ht_short) graph.nodes.append(make_new_edge(edge_long, ht_long, edge_short, ht_short)) fix_short_edge(edge_long, ht_long, edge_short, ht_short) add_turns(graph.nodes, edge_long, ht_long, edge_short, ht_short) f.delete_folded_edges(graph.nodes, [edge_long.name]) for edge in graph.nodes: f.remove_self_connect(edge) mleg.remove_val_two_vertex(graph) graph.update_matrix() return graph
def perform_fold(self): '''Given a Fold, performs the fold on the graph specified in the object with the longer edge as specified. Returns the new graph. ''' edge1 = fh.find_edge(self.graph_from, self.fold_name[0]) edge2 = fh.find_edge(self.graph_from, self.fold_name[2]) ht1, ht2 = self.fold_name[1], self.fold_name[3] if self.longer == "same": new = fs.same_length_fold(self.graph_from, edge1, ht1, edge2, ht2) return new elif self.longer == self.fold_name[0]: new = fd.diff_length_fold(self.graph_from, edge1, ht1, edge2, ht2) return new elif self.longer == self.fold_name[2]: new = fd.diff_length_fold(self.graph_from, edge2, ht2, edge1, ht1) return new elif self.longer == "partial": new = fp.partial_fold(self.graph_from, edge1, ht1, edge2, ht2) return new
def is_connected(self, starting = None, seen = None): '''A recursive function that checks if a graph is connected. Code adapted from: https://www.python-course.eu/graphs_python.php ''' if starting is None: starting = self.nodes[0] if seen is None: seen = [] seen.append(starting.name) if len(seen) != len(self.nodes): # We haven't seen every edge for turn in starting.head: if turn[0] not in seen: if self.is_connected(fh.find_edge(self, turn[0]), seen): return True for turn in starting.tail: if turn[0] not in seen: if self.is_connected(fh.find_edge(self, turn[0]), seen): return True else: return True return False
def find_legal_folds(self, partial = False, same = True): '''Given a graph, this function determines what the legal folds on this graph are. It returns the folds as a list of Fold objects. ''' names = [] folds = [] for edge in self.nodes: for turn in edge.head: fold_on = fh.find_edge(self, turn[0]) fold_name = fh.sort_to_str(edge.name, "h", turn[0], turn[1]) if fold_name[0] == fold_name[2]: # This is only a legal partial fold if partial: folds.append(f.Fold(self, fold_name, "partial")) else: pass elif fold_name not in names and turn[2] == False: names.append(fold_name) if same and fh.same_length_fold_allowed(edge, "h", fold_on, turn[1]): folds.append(f.Fold(self, fold_name, 'same')) folds.append(f.Fold(self, fold_name, edge.name)) folds.append(f.Fold(self, fold_name, turn[0])) if partial: folds.append(f.Fold(self, fold_name, "partial")) # Same thing, but in tail for turn in edge.tail: fold_on = fh.find_edge(self, turn[0]) fold_name = fh.sort_to_str(edge.name, "t", turn[0], turn[1]) if fold_name[0] == fold_name[2]: # legal as a partial fold, but that would be accounted for in the # head section pass elif fold_name not in names and turn[2] == False: names.append(fold_name) if same and fh.same_length_fold_allowed(edge, "t", fold_on, turn[1]): folds.append(f.Fold(self, fold_name, 'same')) folds.append(f.Fold(self, fold_name, edge.name)) folds.append(f.Fold(self, fold_name, turn[0])) if partial: folds.append(f.Fold(self, fold_name, "partial")) return folds
## while fio.graph_file(i, folder) != fio.next_graph(folder): ## print(i) ## graph = fio.read_graph(i, folder) ## can_get_to = [] ## folds = fh.fold_obj_to_name(graph.find_legal_folds()) ## ## for fold in folds: ## new = partial_fold(graph, fh.find_edge(graph, fold[0]), fold[1], ## fh.find_edge(graph, fold[2]), fold[3]) ## ind = new.find_in_list(folder) ## if ind is None: ## file_name = fio.next_graph(folder) ## fio.write_file(new, file_name) ## can_get_to.append([fold, "p", fio.graph_index(file_name)]) ## else: ## can_get_to.append([fold, "p", fio.graph_index(ind)]) ## graph.can_get_to = can_get_to ## i += 1 ## folder = "partial_fold_test" ## graph = fio.read_graph(1, folder) ## fold = fh.fold_obj_to_name(graph.find_legal_folds())[0] ## new = partial_fold(graph, fh.find_edge(graph, fold[0]), fold[1], ## fh.find_edge(graph, fold[2]), fold[3]) ## print(graph.is_equal(new)) folder = "from_tri_reduced_partial" graph = fio.read_graph(6, folder) new = partial_fold(graph, fh.find_edge(graph, "b"), "t", fh.find_edge(graph, "d"), "h")