def get_min_max_span(graph, node): """ Get minimum and maximum indices covered by the subgraph rooted at a specific node @type graph: graph_wrapper.GraphWrapper @param graph: a graph in which the node reside @type node: node @param node: root of the given subgraph @rtype: tuple [int] @return: (min,max) """ minInd = NO_INDEX maxInd = NO_INDEX for curNode in traversal(graph, node, 'pre'): curMin = curNode.minIndex() curMax = curNode.maxIndex() maxInd = max(maxInd, curMax) if curMin != NO_INDEX: if minInd == NO_INDEX: minInd = curMin else: minInd = min(minInd, curMin) return (minInd, maxInd)
def remove_dependents(plugin_id): for node in traversal(graph, plugin_id, 'pre'): for dependent in graph[node]: edge = node, dependent problems[dependent].append(IndirectDependency(dependent, graph.get_edge_properties(edge)['dependency'])) graph.del_node(node)
def traversal(self, node, order='pre'): """ Graph traversal iterator. @type node: node @param node: Node. @type order: string @param order: traversal ordering. Possible values are: 2. 'pre' - Preordering (default) 1. 'post' - Postordering @rtype: iterator @return: Traversal iterator. """ for each in traversal.traversal(self, node, order): yield each
def transitive_edges(graph): """ Return a list of transitive edges. Example of transitivity within graphs: A -> B, B -> C, A -> C in this case the transitive edge is: A -> C @attention: This function is only meaningful for directed acyclic graphs. @type graph: digraph @param graph: Digraph @rtype: List @return: List containing tuples with transitive edges (or an empty array if the digraph contains a cycle) """ #if the graph contains a cycle we return an empty array if not len(find_cycle(graph)) == 0: return [] tranz_edges = [] # create an empty array that will contain all the tuples #run trough all the nodes in the graph for start in topological_sorting(graph): #find all the successors on the path for the current node successors = [] for a in traversal(graph, start, 'pre'): successors.append(a) del successors[ 0] #we need all the nodes in it's path except the start node itself for next in successors: #look for an intersection between all the neighbors of the #given node and all the neighbors from the given successor intersect_array = _intersection(graph.neighbors(next), graph.neighbors(start)) for a in intersect_array: if graph.has_edge((start, a)): ##check for the detected edge and append it to the returned array tranz_edges.append((start, a)) return tranz_edges # return the final array
def calc_fitch_cost(tree,seqs,start_node=None): sites = len(seqs.values()[0]) if not start_node: start_node = seqs.keys()[0] #now calculate the cost totalcost = 0 nucleotides = ['A', 'C', 'T', 'G'] def cost (x,y): if x == y: return 0 else: return 1 for i in range(sites): #loop every node from tip to root seen = set([]) z = {} for v in traversal(tree, start_node, 'post'): #add the cost of the site to the total n = tree.neighbors(v) children = filter(lambda x: x in seen, n) if len(children) != 0: z[v] = {} for t in nucleotides: costs = sum([min([cost(t,t_0) + z[c][t_0] for t_0 in nucleotides]) for c in children]) z[v][t] = costs else: z[v] = {} the_sequence = seqs[v] the_character = the_sequence[i] for t in nucleotides: if (the_character == t): z[v][t] = 0 else: z[v][t] = 9E8 seen.add(v) totalcost += min(z[start_node].values()) return totalcost
def transitive_edges(graph): """ Return a list of transitive edges. Example of transitivity within graphs: A -> B, B -> C, A -> C in this case the transitive edge is: A -> C @attention: This function is only meaningful for directed acyclic graphs. @type graph: digraph @param graph: Digraph @rtype: List @return: List containing tuples with transitive edges (or an empty array if the digraph contains a cycle) """ #if the LoopGraph contains a cycle we return an empty array if not len(find_cycle(graph)) == 0: return [] tranz_edges = [] # create an empty array that will contain all the tuples #run trough all the nodes in the LoopGraph for start in topological_sorting(graph): #find all the successors on the path for the current node successors = [] for a in traversal(graph,start,'pre'): successors.append(a) del successors[0] #we need all the nodes in it's path except the start node itself for next in successors: #look for an intersection between all the neighbors of the #given node and all the neighbors from the given successor intersect_array = _intersection(graph.neighbors(next), graph.neighbors(start) ) for a in intersect_array: if graph.has_edge((start, a)): ##check for the detected edge and append it to the returned array tranz_edges.append( (start,a) ) return tranz_edges # return the final array
for i in xrange(1, number_of_classes+1): line_as_ints = map(int, raw_input().split()) for edge_dest in line_as_ints[1:]: #print "Add", i, edge_dest g.add_edge((i, edge_dest)) result = '' for node in g.nodes(): if result != '': break #print node l = [] try: for neighbor in g.neighbors(node): l.append( set (list(traversal.traversal(g, neighbor, 'pre')) ) ) except KeyError: pass r = set([]) for s in l: # print '-------' # print "r=", r # print "s=", s if r.intersection(s): result = 'Yes' break else: r = r.union(s) else: