def choose_initial_labeling(self): ''' Choose a labeling of the local graph that respects the n-hop decision rule''' source_neighbors = self.neighbors(self.source) # Choose the next virtual source weights = [] for neighbor in source_neighbors: subtree = dfs_tree(self, neighbor) leaves = [i for i in subtree.nodes() if (subtree.out_degree(i)==0 and \ not nx.has_path(subtree,self.source,i))] weights.append(len(leaves)) weights = [float(i)/sum(weights) for i in weights] virtual_source = np.random.choice(source_neighbors, p=weights) # Relabel the nodes so that only the infected nodes have positive values label = 1 mapping = {virtual_source: label} # Generate a directed tree emanating from the virtual source away from the source directed_self = dfs_tree(self, self.source) infected_subgraph = dfs_tree(directed_self, virtual_source) bfs_edges = list(nx.bfs_edges(infected_subgraph, virtual_source)) # In this first timestep, we only need the direct neighbors of V.S. to be infected bfs_edges = [item[1] for item in bfs_edges if (item[1] != self.source and item[0] == virtual_source)] for edge in bfs_edges: label += 1 mapping[edge] = label self = nx.relabel_nodes(self, mapping, copy=False)
def choose_initial_labeling(self): ''' Choose a labeling of the local graph that respects the n-hop decision rule''' source_neighbors = self.neighbors(self.source) # Choose the next virtual source weights = [] for neighbor in source_neighbors: subtree = dfs_tree(self, neighbor) leaves = [i for i in subtree.nodes() if (subtree.out_degree(i)==0 and \ not nx.has_path(subtree,self.source,i))] weights.append(len(leaves)) weights = [float(i) / sum(weights) for i in weights] virtual_source = np.random.choice(source_neighbors, p=weights) # Relabel the nodes so that only the infected nodes have positive values label = 1 mapping = {virtual_source: label} # Generate a directed tree emanating from the virtual source away from the source directed_self = dfs_tree(self, self.source) infected_subgraph = dfs_tree(directed_self, virtual_source) bfs_edges = list(nx.bfs_edges(infected_subgraph, virtual_source)) # In this first timestep, we only need the direct neighbors of V.S. to be infected bfs_edges = [ item[1] for item in bfs_edges if (item[1] != self.source and item[0] == virtual_source) ] for edge in bfs_edges: label += 1 mapping[edge] = label self = nx.relabel_nodes(self, mapping, copy=False)
def treeUnion(self, agglomeratedShot1, agglomeratedShot2): has_joined = False subtree_at_2 = None tree1 = None if (agglomeratedShot1.root.name in nx.to_dict_of_dicts( agglomeratedShot2.tree) and (agglomeratedShot1.pause > agglomeratedShot2.pause)): subtree_at_2 = dfs_tree(agglomeratedShot2.tree, agglomeratedShot1.root.name) children = nx.to_dict_of_dicts(subtree_at_2) tree1 = nx.to_dict_of_dicts(agglomeratedShot1.tree) tree1[agglomeratedShot1.root.name] = children tree1 = nx.from_dict_of_dicts(tree1) has_joined = True else: for key in nx.to_dict_of_dicts(agglomeratedShot1.tree)[ agglomeratedShot1.root.name].keys(): if key in nx.to_dict_of_dicts(agglomeratedShot2.tree): subtree_at_2 = dfs_tree(agglomeratedShot2.tree, key) children = nx.to_dict_of_dicts(subtree_at_2) tree1 = nx.to_dict_of_dicts(agglomeratedShot1.tree) tree1[key] = children tree1 = nx.from_dict_of_dicts(tree1) has_joined = True if (has_joined): ocr = '' ids = agglomeratedShot1.ids + agglomeratedShot2.ids transcript = agglomeratedShot1.transcript + ' ' + agglomeratedShot2.transcript if self.ocr_on: ocr = agglomeratedShot1.ocr + ' ' + agglomeratedShot2.ocr pitch = agglomeratedShot1.pitch volume = agglomeratedShot1.volume pause = agglomeratedShot1.pause root = agglomeratedShot1.root join = agglomeratedShot1.ids + agglomeratedShot2.ids self.boundaries.append(join[0]) T = nx.minimum_spanning_tree(tree1) a_node = AgglomeratedNode(0, ids, transcript, ocr, pitch, volume, pause, root, T) for id in a_node.ids: self.agglomerate_shots = [ s for s in self.agglomerate_shots if id not in s.ids ] self.agglomerate_shots.append(a_node) self.agglomerate_shots = sorted(self.agglomerate_shots, key=lambda x: x.ids) return self.agglomerate_shots, has_joined
def outputGraph(self, filename, _filter=None): if filename not in self._graphTime: self._graphTime[filename] = 0 if (time.time() - self._graphTime[filename]) > 10: # print 'pre-Update Graph: %s' % filename G = nx.DiGraph() plt.clf() for edge in self._edges: if _filter is None: G.add_edge(edge[0], edge[1]) elif _filter(edge[0]) or _filter(edge[1]): G.add_edge(edge[0], edge[1]) try: G1 = dfs_tree(G, u"0015") G2 = dfs_tree(G, u"0013") G3 = nx.compose(G1, G2) G4 = dfs_tree(G, u"0017") G = nx.compose(G3, G4) except: pass relabel = {} newToOld = {} for n in G.nodes(): if n in self._berths and self._berths[n] is not None: relabel[n] = self._berths[n] newToOld[self._berths[n]] = n nx.relabel_nodes(G, relabel, False) colours = [] for n in G.nodes(): if n in newToOld: n = newToOld[n] if n in self._berths and self._berths[n] is not None: colours.append("r") else: colours.append("g") pos = nx.graphviz_layout(G, prog="dot") nx.draw_networkx_nodes(G, pos, node_color=colours, node_shape="s", node_size=900) nx.draw_networkx_edges(G, pos) nx.draw_networkx_labels(G, pos) fig = matplotlib.pyplot.gcf() fig.set_size_inches(16.0, 25.0) plt.axis("off") self.outputFile(plt, filename) self._graphTime[filename] = time.time()
def generate_new_tree(self): self.tree.clear() self.tree.add_node(0, x=0, y=0) #this is a fake node self.tree.add_edge(0, 1, x=0, y=0) self.tree.add_node(1, x=0, y=0) next_node_number = 2 current_nodes = [1] for level in range(1, self.n + 1): next_nodes = [] node_to_split = -1 if level == self.n else rand.choice( current_nodes) for node in current_nodes: for i in range(1 + int(node == node_to_split)): self.add_child(node, next_node_number, next_nodes) next_node_number += 1 current_nodes = next_nodes self.leaves = self.tree.nodes()[-self.n:] # Pre-cache the leaves from each node self.node_leaves_lookup = {} for node in range(1, self.num_nodes + 1): subtree_nodes = dfs_tree(self.tree, node) self.node_leaves_lookup[node] = [ leaf for leaf in subtree_nodes if self.tree.out_degree(leaf) == 0 ]
def create_lineage_tree(self): self.tree.clear() self.tree.add_node(0) #this is a fake node self.tree.add_edge(0, 1) self.tree.add_node(1) next_node_number = 2 current_nodes = [1] for level in range(1, self.num_cells + 1): next_nodes = [] node_to_split = -1 if level == self.num_cells else rand.choice( current_nodes) for node in current_nodes: for i in range(1 + int(node == node_to_split)): self.add_child(node, next_node_number, next_nodes) next_node_number += 1 current_nodes = next_nodes self.leaves = self.tree.nodes()[-self.num_cells:] self.leave_genotypes = {} for leaf in self.leaves: self.leave_genotypes[leaf] = [0 for idx in range(self.num_muts)] # Pre-cache the leaves from each node self.node_leaves_lookup = {} for node in range(1, self.num_nodes + 1): subtree_nodes = dfs_tree(self.tree, node) self.node_leaves_lookup[node] = [ leaf for leaf in subtree_nodes if self.tree.out_degree(leaf) == 0 ]
def inverse_edge_function_holds(self): logging.info('=' * 80) logging.info('Inverse edge function property') logging.info('=' * 80) for u in self.tree.nodes_iter(): T_u = dfs_tree(self.tree, u) vertices_in_bags_below_u = set() for t in T_u.nodes(): vertices_in_bags_below_u.update(self.bags[t]) if not (vertices_in_bags_below_u & self._B(u) <= self.bags[u]): logging.error( 'Inverse edge function property does not hold for node "%s"' % u) logging.error( 'Bag of the subtree induced at "%s" contained "%s"' % (u, vertices_in_bags_below_u)) logging.error( 'Vertices returned from the edge function are "%s"' % self._B(u)) logging.error('Bag content is: %s' % self.bags[u]) logging.error( 'Hence, not (vertices_in_bags_below_u & self._B(u) <= self.bags[u]) does not hold for node %s.' % u) return False logging.info('Inverse edge function property *holds*.') logging.info('=' * 80) return True
def visualise_tree(root=None, exact_order=True): # tree = read_dendrogram_tree(PATH_DEND_JSON) dend = nomenclature_2_nodes_n_edges(PATH_NMN_TABLE) tree = generate_dendrogram_tree(dend) if root is not None: tree = dfs_tree(tree, root) marker_expressions = read_marker_file(PATH_MARKERS) add_label_metadata(tree) node_colors, node_labels, node_sizes, pos = decorate_nodes( marker_expressions, tree) all_leafs = [ x for x in tree.nodes(data=True) if tree.out_degree(x[0]) == 0 ] position_leaf_nodes(all_leafs, exact_order, pos) position_intermediate_nodes(all_leafs, pos, tree) # nx.draw_networkx(tree, pos, node_color=color_map, with_labels=False, arrows=False) nx.draw_networkx_nodes(tree, pos, node_color=node_colors, node_size=node_sizes) nx.draw_networkx_edges(tree, pos, arrows=True, connectionstyle="arc3,rad=0.1") text = nx.draw_networkx_labels(tree, pos, node_labels, font_size=7) rotate_leaf_labels(text) plt.show()
def get_snippet(G, start_node, dmin, dmax): snippet = None node_id = start_node while snippet is None and node_id < G.number_of_nodes(): hub_ego = dfs_tree(G, node_id) if hub_ego.number_of_nodes() > dmin and hub_ego.number_of_nodes( ) < dmax: snippet = hub_ego node_id += 1 return snippet, node_id
def exclude_subtree_from(self, package): sub = dfs_tree(self.G, package) seen = {package} for n in sub.nodes(): if n in seen: continue seen.add(n) if local_node_connectivity(self.G, self.root, n) > 1: sub.remove_nodes_from( DependencyGraph(sub).exclude_subtree_from(n)) return DependencyGraph(sub)
def exclude_subtree_from(self, package): sub = dfs_tree(self.G, package) root = nx.topological_sort(self.G)[0] seen = {package} for n in sub.nodes(): if n in seen: continue seen.add(n) if local_node_connectivity(self.G, root, n) > 1: sub.remove_nodes_from( InstalledGraph(sub).exclude_subtree_from(n)) return InstalledGraph(sub)
def build_tree_recursive(tree, parent, treegraph): # find children children = treegraph.neighbors(parent) # build a subtree for each child if children: tree["children"] = [] for i, child in enumerate(children): # start new subtree tree["children"].append({"name": child.split('.')[-1]}) # call recursively to build a subtree for current node build_tree_recursive(tree["children"][i], child, dfs_tree(treegraph, child))
def rand_code_snippets(G, n, last_node_id=0, dmin=10, dmax=64, mode='dfs'): snippets = [] node_id = last_node_id while len(snippets) < n: if mode == 'dfs': hub_ego = dfs_tree(G, node_id) else: hub_ego = nx.ego_graph(G, node_id, radius=3) neighbours = list(hub_ego.nodes()) if (len(neighbours) > dmin) and (len(neighbours) < dmax): snippets.append(neighbours) node_id += 1 return snippets, node_id
def get_subtrees(self, digestibles): d = {} frontier_digestibles = list(digestibles) # this is a shallow copy g = self.graph._g ## FIXME: should not access others' private vars assert self.state in g.edge for next_state in g.edge[self.state]: ## NOTE: even if digestible==edge_digestible, event_uuid can differ. Do NOT return edge_digestible. edge_digestible = g.edge[self.state][next_state]['digestible'] digestibles_matched = [digestible for digestible in digestibles if digestible == edge_digestible] if not digestibles_matched: continue digestible = digestibles_matched[0] frontier_digestibles.remove(digestible) subtree = dfs_tree(g, next_state) d[digestible] = subtree for digestible in frontier_digestibles: d[digestible] = None return d
def construct_true_mut_tree(self): muttree = nx.DiGraph() for idx, edge in enumerate(self.mutation_locations): parent = edge[0] child = edge[1] if idx == 0: muttree.add_node('gene' + str(idx)) else: muttree.add_node('gene' + str(idx)) indices = range(idx) for jdx in reversed(indices): subtree = dfs_tree(self.tree, self.mutation_locations[jdx][1]) if parent in subtree: muttree.add_edge('gene' + str(jdx), 'gene' + str(idx)) break elif self.mutation_locations[jdx][1] == child: muttree.add_edge('gene' + str(jdx), 'gene' + str(idx)) break return muttree
def format_single_verb(nx_graph, words, verb_ind): """ Get a single verb description. """ desc = words bio_tags = ["O" for _ in words] desc[verb_ind] = f"[V: {desc[verb_ind]}]" bio_tags[verb_ind] = "B-V" for dep in nx_graph[verb_ind]: label = nx_graph[verb_ind][dep]["label"] subtree = dfs_tree(nx_graph, dep) start_word_ind = min(subtree) start_word = desc[start_word_ind] end_word_ind = max(subtree) # Update description desc[start_word_ind] = f"[{label}: {start_word}" desc[end_word_ind] = f"{desc[end_word_ind]}]" # Update tags bio_tags[start_word_ind] = f"B-{label}" for cur_ind in range(start_word_ind + 1, end_word_ind + 1): bio_tags[cur_ind] = f"I-{label}" return desc, bio_tags
def ford_fulkerson(G, s, t, capacity='capacity'): """Find a maximum single-commodity flow using the Ford-Fulkerson algorithm. This algorithm uses Edmonds-Karp-Dinitz path selection rule which guarantees a running time of O(nm^2) for n nodes and m edges. Parameters ---------- G : NetworkX graph Edges of the graph are expected to have an attribute called 'capacity'. If this attribute is not present, the edge is considered to have infinite capacity. s : node Source node for the flow. t : node Sink node for the flow. capacity: string Edges of the graph G are expected to have an attribute capacity that indicates how much flow the edge can support. If this attribute is not present, the edge is considered to have infinite capacity. Default value: 'capacity'. Returns ------- flow_value : integer, float Value of the maximum flow, i.e., net outflow from the source. flow_dict : dictionary Dictionary of dictionaries keyed by nodes such that flow_dict[u][v] is the flow edge (u, v). Raises ------ NetworkXError The algorithm does not support MultiGraph and MultiDiGraph. If the input graph is an instance of one of these two classes, a NetworkXError is raised. NetworkXUnbounded If the graph has a path of infinite capacity, the value of a feasible flow on the graph is unbounded above and the function raises a NetworkXUnbounded. Examples -------- >>> import networkx as nx >>> G = nx.DiGraph() >>> G.add_edge('x','a', capacity=3.0) >>> G.add_edge('x','b', capacity=1.0) >>> G.add_edge('a','c', capacity=3.0) >>> G.add_edge('b','c', capacity=5.0) >>> G.add_edge('b','d', capacity=4.0) >>> G.add_edge('d','e', capacity=2.0) >>> G.add_edge('c','y', capacity=2.0) >>> G.add_edge('e','y', capacity=3.0) >>> flow, F = nx.ford_fulkerson(G, 'x', 'y') >>> flow 3.0 """ if G.is_multigraph(): raise nx.NetworkXError( 'MultiGraph and MultiDiGraph not supported (yet).') auxiliary, inf_capacity_flows = _create_auxiliary_digraph(G, capacity=capacity) flow_value = 0 # Initial feasible flow. # As long as there is an (s, t)-path in the auxiliary digraph, find # the shortest (with respect to the number of arcs) such path and # augment the flow on this path. while True: try: path_nodes = nx.bidirectional_shortest_path(auxiliary, s, t) except nx.NetworkXNoPath: break # Get the list of edges in the shortest path. path_edges = list(zip(path_nodes[:-1], path_nodes[1:])) # Find the minimum capacity of an edge in the path. try: path_capacity = min([auxiliary[u][v][capacity] for u, v in path_edges if capacity in auxiliary[u][v]]) except ValueError: # path of infinite capacity implies no max flow raise nx.NetworkXUnbounded( "Infinite capacity path, flow unbounded above.") flow_value += path_capacity # Augment the flow along the path. for u, v in path_edges: edge_attr = auxiliary[u][v] if capacity in edge_attr: edge_attr[capacity] -= path_capacity if edge_attr[capacity] == 0: auxiliary.remove_edge(u, v) else: inf_capacity_flows[(u, v)] += path_capacity if auxiliary.has_edge(v, u): if capacity in auxiliary[v][u]: auxiliary[v][u][capacity] += path_capacity else: auxiliary.add_edge(v, u, {capacity: path_capacity}) S = set([s]+dfs_tree(auxiliary, s).node.keys()) T = set(G.node.keys()).difference(S) return flow_value, (list(S), list(T))
def subtree(self, node): """Return the subtree where `node` is the root, as a ModelTree.""" subtree_nodes = dfs_tree(self, node).nodes return self.subgraph(subtree_nodes)
def nearest_drop_map( map_width, factory_loc, dropoff_locs, halite_map, halite_scale=100.0, avoidance_map=None, build_return_trees=False, ship_ind_arr=None, ): """For each tile, provide number of turns to nearest drop + halite cost / halite_scale. The Halite cost is to be used to distinguish between routes that are equally long. Use BFS. """ locs = [factory_loc] + dropoff_locs locs_copy = copy(locs) out_arr = np.full_like(halite_map, np.inf) locq = deque() if build_return_trees: import networkx # edges go from depos (root to leaf) digraph = networkx.DiGraph() for loc in locs: out_arr[loc[0], loc[1]] = 0 locq.append(((loc[0], loc[1]), None)) if build_return_trees: digraph.add_node((loc[0], loc[1])) del locs while locq: (loc, parent) = locq.popleft() if parent is not None: val = ( out_arr[parent[0], parent[1]] + 1 + int(halite_map[loc[0], loc[1]] / 10) / halite_scale ) if parent is None or (val < out_arr[loc[0], loc[1]]): if ( (avoidance_map is not None) and avoidance_map[loc[0], loc[1]] and ((loc[0], loc[1]) not in locs_copy) ): pass else: for d in [(0, 1), (1, 0), (-1, 0), (0, -1)]: new_loc = ((loc[0] + d[0]) % map_width, (loc[1] + d[1]) % map_width) locq.append((new_loc, loc)) if parent is not None: curr_val = out_arr[loc[0], loc[1]] if val < curr_val: out_arr[loc[0], loc[1]] = val if build_return_trees: digraph.add_edge((parent[0], parent[1]), (loc[0], loc[1])) else: out_arr[loc[0], loc[1]] = curr_val if not build_return_trees: return out_arr else: return_turn_arr = np.full_like(halite_map, hlt.constants.MAX_TURNS) depo_nodes = (n for n in digraph.nodes() if digraph.in_degree(n) == 0) depo_adj_nodes = [] for node in depo_nodes: successors = digraph.successors(node) for successor in successors: depo_adj_nodes.append(successor) from networkx.algorithms.traversal.depth_first_search import dfs_tree from networkx.algorithms import topological_sort for node in depo_adj_nodes: subtree = dfs_tree(digraph, node) root_out = list(topological_sort(subtree)) i = 0 for node in reversed(root_out): return_turn = hlt.constants.MAX_TURNS - i return_turn_arr[node[0], node[1]] = return_turn if ship_ind_arr[node[0], node[1]]: i += 1 return out_arr, return_turn_arr
x, y = pos[node] ax.text(x, y, label, bbox=bbox_props, **label_props) ax.xaxis.set_visible(False) ax.yaxis.set_visible(False) plt.show() subtrees_G=[] subtrees_H=[] for i,node in enumerate(G.nodes()): subtrees_G.append(dfs_tree(G,node)) for i,node in enumerate(H.nodes()): subtrees_H.append(dfs_tree(H,node)) def label_check(d1,d2): return d1['labels']==d2['labels'] for subtree in subtrees_H: for node in subtree.nodes(): subtree.node[node]['labels']=labels2[node] for subtree in subtrees_G: for node in subtree.nodes(): subtree.node[node]['labels']=labels[node]
with open('input/day07.txt') as f: lines = f.readlines() # Graph mapping from outer to inner G = nx.DiGraph() for line in lines: outside_color, contents = line.strip().split(' bags contain ') G.add_node(outside_color) for n, inside_color in parse_contents(contents): G.add_edge(outside_color, inside_color, weight=n) count = 0 for node in G.nodes(): if node != 'shiny gold' and 'shiny gold' in dfs_tree(G, node): count += 1 print('Part 1:', count) store = dict() def num_contained(G, bag) -> int: if bag not in store: store[bag] = sum([ G.edges[bag, child]['weight'] * (num_contained(G, child) + 1) for child in G[bag] ]) return store[bag]
def bfs_tree(graph, root): return dfs_tree(graph, root)
G.add_edge(fromnode.aktorid,tonode.aktorid) for t in aktoraktor.objects.filter(, rolleid=): from networkx.algorithms.traversal.depth_first_search import dfs_tree names = [] for o in coms: names.append((o.aktorid, o.navn)) na=dict((n,d) for n,d in names) for p in comsid: H= dfs_tree(G,p) I=G.subgraph(H.nodes()) plt.clf() plt.figure(figsize=(10, 10)) labels=dict((n,d['navn']) for n,d in I.nodes(data=True)) nx.draw_spring(I, with_labels=True, labels=labels, alpha=0.6, edge_color='g', style='dotted') plt.axis('off') plt.savefig("Udvalg-" + na[p] + "-" + str(p) + ".png") #concentric circles names = [] for o in coms: names.append((o.aktorid, o.navn)) na=dict((n,d) for n,d in names)
def subtree_from(self, package): return DependencyGraph(dfs_tree(self.G, package))
def processing(G=nx.MultiDiGraph(), cnt=Counter(100), phase=1): init = [x for x in G.nodes() if G.in_degree(x) == 0 or x == 1][0] for item in dfs_tree(G, init): #processing_step(G=nx.MultiDiGraph(), cnt=Counter(100),item=item,init=init) if item == 1: G.nodes[item].update({'isHandled': True}) continue try: if G.nodes[item]['isHandled'] and phase <= 3: continue except Exception as e: pass parent = G.nodes[item]['ppid'] if phase == 1 and G.nodes[parent]['sid'] == G.nodes[item]['sid'] and \ G.nodes[parent]['pgid'] == G.nodes[item]['pgid'] \ and G.nodes[item]['sid'] == G.nodes[item]['pgid']: G.add_edge(parent, item, "fork()") elif phase == 1 and (not G.nodes[item]['sid'] == G.nodes[item]['pid'] or not G.nodes[item]['pgid'] == G.nodes[item]['pid']): # upbranch! G.nodes[item].update({'isHandled': True}) #try: creatr_p = G.nodes[G.nodes[item]['sid']]['pid'] if creatr_p == 0: creatr_p = 1 upbranch(G, creatr_p, item, 'sid', cnt) elif phase == 2 and G.nodes[item]['pid'] == G.nodes[item][ 'sid'] == G.nodes[item]['pgid']: try: l = [ x for x, y in G.nodes(data=True) if y['pid'] == G.nodes[item]['pid'] and not y['sid'] == G.nodes[item]['pid'] ] # there may be error - check it after 3-5 impl intermediate_state = l[0] except: intermediate_state = cnt.inc() G.add_node(intermediate_state) G.nodes[intermediate_state].update(G.nodes[ G.nodes[item]['ppid']]) # maybe change to actual_parent? G.nodes[intermediate_state].update({ 'pid': G.nodes[item]['pid'], 'ppid': G.nodes[item]['ppid'], 'isHandled': False }) G.add_edge(G.nodes[item]['ppid'], intermediate_state, '*H*') G.add_edge(G.nodes[item]['ppid'], intermediate_state, 'fork()') G.add_edge(intermediate_state, item, '*H*') G.add_edge(intermediate_state, item, 'setsid()') # pstree pgroup(set new pgroup) reconstruction elif phase == 3 \ and G.nodes[item]['pgid'] == G.nodes[item]['pid'] \ and not G.nodes[item]['sid'] == G.nodes[item]['pid']: l = [ x for x, y in G.nodes(data=True) if y['pid'] == G.nodes[item]['ppid'] and y['sid'] == G.nodes[item]['sid'] ] # not empty, elsewise wrong tree try: actual_parent = l[0] except: actual_parent = G.nodes[item]['ppid'] #G.nodes[item].update({'isHandled':True}) l = [ x for x, y in G.nodes(data=True) if y['pid'] == G.nodes[item]['pid'] and y['sid'] == G.nodes[item]['sid'] ][0] # not empty, elsewise wrong tree try: intermediate_state = l[0] except: intermediate_state = cnt.inc() G.add_node(intermediate_state) G.nodes[intermediate_state].update(G.nodes[actual_parent]) G.nodes[intermediate_state].update({ 'pid': G.nodes[item]['pid'], 'ppid': G.nodes[actual_parent]['pid'] }) G.add_edge(intermediate_state, actual_parent, "pred") G.add_edge(actual_parent, intermediate_state, "fork()") G.add_edge(actual_parent, intermediate_state, "H#") G.add_edge(item, intermediate_state, "pred") G.add_edge(intermediate_state, item, "setpgid(0, 0)") G.add_edge(intermediate_state, item, "H$") G.nodes[item].update({'isHandled': True}) # intermediate pgroup(set new pgroup) reconstruction elif phase == 4 and \ not G.nodes[item]['pid'] == G.nodes[item]['sid'] and \ not G.nodes[item]['pid'] == G.nodes[item]['pgid']: get_pgroup_leader(G, G.nodes[item]['pgid'], G.nodes[item]['sid'], cnt) elif phase == 5 and not G.nodes[item]['pid'] == G.nodes[item]['sid']: if G.nodes[item]['pgid'] == G.nodes[item]['pid']: continue # find actual parent: l = [ x for x, y in G.nodes(data=True) if y['pid'] == G.nodes[item]['ppid'] and y['sid'] == G.nodes[item]['sid'] ] try: if G.nodes[item]['pgid'] == G.nodes[item]['sid']: parent = [ i for i in l if G.nodes[i]['pgid'] == G.nodes[item]['sid'] ][-1] else: parent = [ i for i in l if G.nodes[i]['pgid'] == G.nodes[parent]['pgid'] ][-1] # Перезагружаем родителя - более актуальное состояние { not empty, else incorrect tree } except: try: parent = l[-1] except: parent = G.nodes[item]['ppid'] if (not G.nodes[parent]['pgid'] == G.nodes[item]['pgid'] \ ) and \ G.nodes[parent]['sid'] == G.nodes[item]['sid']: flag = False try: intermediate_state = [ x for x, y in G.nodes(data=True) if y['pid'] == G.nodes[item]['pid'] and y['sid'] == G.nodes[item]['sid'] and not y['pgid'] == G.nodes[item]['pgid'] ][-1] # THIS IS HEURISTICS - handle it as well. except: flag = True intermediate_state = cnt.inc() G.add_node(intermediate_state) # создаётся G.nodes[intermediate_state].update({ 'pid': G.nodes[item]['pid'], 'ppid': G.nodes[item]['pid'], 'sid': G.nodes[parent]['sid'], 'pgid': G.nodes[parent]['pgid'], 'isHandled': True }) print("Added intermediate state", G.nodes[intermediate_state]) creator = [ x for x, y in G.nodes(data=True) if y['pid'] == y['pgid'] == G.nodes[item]['pgid'] ][0] print("aaa") G.add_edge(intermediate_state, item, "follow") G.add_edge(item, creator, "creator_pgroup") G.add_edge( creator, item, "setpgid(" + str(G.nodes[item]['pid']) + ", " + str(G.nodes[item]['pgid']) + ")") if flag: G.add_edge(parent, intermediate_state, "HHH") G.add_edge(parent, intermediate_state, "fork()") return G
#%% def depth_first_search(graph, target): S = set() empty = False print("start stack") stack = queue.LifoQueue() stack.put(target) #S.add(target) while (~(empty)): if stack.empty(): break target = stack.get() if target in S: continue S.add(target) print(target) for v in G.neighbors(target): if v in S: pass else: stack.put(v) print("end") #%% depth_first_search(G, 3) #%% print(list(depth_first_search1.dfs_tree(G, 2))) #%%
def longest_oxygen_path(self): return dag_longest_path_length( dfs_tree(self._paths, self.oxygen_position))
def preprocess_tree(T, attr_name, ctx, creators={}, cnt=Counter(100)): # test it before usage! # setting tree into consistent state (see reparent manual) init = [x for x in T.nodes() if T.in_degree(x) == 0 or x == 1][0] for p in list(T.nodes): try: if not T.nodes[p]['ppid'] in list(T.nodes): T.nodes[p]['ppid'] = init # init value except: print('catch', T.nodes[p], p) print('azaza') # now pstree is consistent subroots = list(T.successors(init)) creators[1] = {init: init} for subroot in subroots: for item_ptr in dfs_tree(T, subroot): attr_val = T.nodes[item_ptr][attr_name] if attr_val == T.nodes[item_ptr][ 'pid']: # creator criteria - replace if generalized creators[attr_val] = { item_ptr: subroot } # check correctness also for subroot in subroots: for item_ptr in dfs_tree(T, subroot): attr_val = T.nodes[item_ptr][attr_name] if not attr_val == T.nodes[item_ptr][ 'pid']: # handle holder - this node is not in current tree creator_location = creators.get(attr_val, None) if creator_location is None: creator = attr_val #cnt.inc() - must be checket to unduplication creators[attr_val] = {creator, creator} T.add_node(creator) T.nodes[creator].update(T.nodes[init]) T.nodes[creator].update({ 'ppid': init, 'pid': attr_val, }) # append entry if T.nodes[item_ptr]['sid'] == attr_val: T.nodes[creator].update({ 'pgid': attr_val, 'sid': attr_val }) else: T.nodes[creator].update({ 'pgid': attr_val, }) T.nodes[creator].update({'status': 0}) T.add_edge(init, creator, key='h-intermediate') try: T.remove_edge(T.nodes[subroot]['ppid'], subroot, key='h') except: pass T.nodes[subroot].update({'ppid': attr_val}) T.add_edge(creator, subroot, 'h-rev_reparent') # append node to root pass elif attr_name in ['sid', 'pgid'] and attr_val == 1: pass else: try: creator_subroot_val = creator_location[next( iter(creator_location))] except TypeError: creator_subroot_val = next(iter(creator_location)) if not creator_subroot_val == subroot: # not from this subtree - condition (*) from scratch creator = next( iter(creator_location )) # eject subtree which contains creator intermediate_node = cnt.inc() T.add_node(intermediate_node) T.nodes[intermediate_node].update(T.nodes[creator]) T.nodes[intermediate_node].update({ 'pid': intermediate_node, 'ppid': creator }) T.nodes[intermediate_node].update({'status': 0}) try: T.remove_edge(T.nodes[subroot]['ppid'], subroot, 'h') except: pass T.nodes[subroot].update( {'ppid': intermediate_node} ) # connect current_subroot_val node to creator via intermediate state T.add_edge(creator, intermediate_node, 'h-intermediate') T.add_edge(intermediate_node, subroot, 'h-stitching_to_subtree') break # dependency is handled now else: # everything is ok continue if ctx.per_step_show: save_and_draw_graph( T, num_palette=ctx.colors_dict, pic_name=ctx.compose_name("_preprocess"), show_graph=False) ctx.op_inc() return T
for substance_holonym in synset.substance_holonyms(): edges.add((synset.name(), substance_holonym.name(), "substance_holonym")) for part_holonym in synset.part_holonyms(): edges.add((synset.name(), part_holonym.name(), "part_holonym")) nouns = pd.DataFrame(list(edges), columns=['child', 'parent', 'type']) # Get Animal Subgraph G = nx.DiGraph() for child, parent, edge_type in edges: G.add_edge(parent, child, edge_type=edge_type) subgraph = dfs_tree(G, source="animal.n.01") subgraph = G.subgraph(subgraph.nodes) print("Number of edges in subgraph:", len(subgraph.edges)) print("Subgraph depth (longest path length): ", nx.dag_longest_path_length(subgraph)) print("Edge types:") all_nodes = set(subgraph.nodes) mammals = nouns[(nouns["child"].isin(all_nodes) & (nouns["parent"].isin(all_nodes)))] print(mammals.groupby("type").count()) # Save to disk with open(data_folder + "/mammal.csv", "w") as f: for parent, child in subgraph.edges: