def bipartition(T, e): # make a copy of T and remove e, dfs on both end-vertex of e to find all leaves in each component T = T.copy() T.remove_edge(*e) A = set(dfs.dfs_preorder_nodes(T, e[0])) B = set(dfs.dfs_preorder_nodes(T, e[1])) # returns the bipartition if both sides are non-empty or None otherwise return (frozenset(A), frozenset(B)) if bipartition_is_valid( (A, B)) else None
def verify_graph(aut, roots, graph): from networkx.algorithms.traversal.depth_first_search import dfs_preorder_nodes for i, root in enumerate(roots): for node in dfs_preorder_nodes(graph, root): for source, target in graph.out_edges_iter(node): data = graph[source][target] assert aut.repeated_image(source.extend(data['start_tail']), data['power']) == target.extend(data['end_tail'])
def _potential_conjugators(self, other, roots, graph, choices, type_c, verbose=False): log = get_logger(verbose) #1. Flatten and glue the graph components together to form the ladder from networkx.algorithms.traversal.depth_first_search import dfs_preorder_nodes ladder = [] for root in roots: ladder.extend(dfs_preorder_nodes(graph, root)) log("ladder is:") log(*ladder, sep="\n") #2. Define the test function. See the docstring for maps_satisfying_choices deduce_images = partial(image_for_type_b, roots=roots, graph=graph, aut=other) #3. Now the hard part---the iteration. for images in maps_satisfying_choices(ladder, choices, deduce_images, verbose): domain = Generators(self.signature, sorted(images)) range = Generators(self.signature, (images[d] for d in domain)) log("deducing images for type C words") #Add in type C images to domain and range for word, data in type_c.items(): domain.append(word) img = image_for_type_c(word, data, images, other) range.append(img) try: rho = Automorphism(domain, range) except ValueError as e: log("This didn't define an automorphism") continue else: log("These images DID build an automorphism") rho.add_relabellers(self.domain_relabeller, other.range_relabeller) yield rho
def test_graph_shortest_path_method(self): """ Test Dijkstra shortest path method """ from networkx.algorithms.shortest_paths.generic import shortest_path from networkx.algorithms.traversal.depth_first_search import dfs_preorder_nodes print(shortest_path(self.nx, 8, 10)) print(list(dfs_preorder_nodes(self.nx, 8))) # In a mixed directed graph where 7 connects to 8 but not 8 to 7 self.assertEqual(dijkstra_shortest_path(self.graph, 8, 10), [8, 3, 4, 5, 6, 9, 10]) self.assertEqual(list(dfs_paths(self.graph, 8, 10)), [[8, 3, 4, 5, 6, 9, 10]]) self.assertEqual(list(dfs_paths(self.graph, 8, 10, method='bfs')), [[8, 3, 4, 5, 6, 9, 10]]) # Fully connect 7 and 8 self.graph.add_edge(8, 7, directed=True) self.assertEqual(dijkstra_shortest_path(self.graph, 8, 10), [8, 7, 6, 9, 10]) self.assertEqual(list(dfs_paths(self.graph, 8, 10)), [[8, 7, 6, 9, 10], [8, 3, 4, 5, 6, 9, 10]]) self.assertEqual(list(dfs_paths(self.graph, 8, 10, method='bfs')), [[8, 7, 6, 9, 10], [8, 3, 4, 5, 6, 9, 10]])
def merge_subgraph(self, subgraph=None, source=None): if type(subgraph) not in [Rag, Graph]: # input is node list subgraph = self.subgraph(subgraph) if len(subgraph) > 0: node_dfs = list(dfs_preorder_nodes(subgraph, source)) # dfs_preorder_nodes returns iter, convert to list source_node, other_nodes = node_dfs[0], node_dfs[1:] for current_node in other_nodes: self.merge_nodes(source_node, current_node)
def dump_graph(roots, graph): from networkx.algorithms.traversal.depth_first_search import dfs_preorder_nodes for i, root in enumerate(roots): print('component', i, 'with root', root) print('type B elements:') for node in dfs_preorder_nodes(graph, root): print('\tEdges out of', node) for source, target in graph.out_edges_iter(node): data = graph[source][target] print('\t\tto', target, 'with data\n\t\t\t', fmt_triple(data))
def get_dropoff_ordering_steiner(self): mst = self.get_steiner_tree() preorder_nodes = dfs_preorder_nodes(mst, source=self.start_loc) preorder_nodes = list(preorder_nodes) final_order = [n for n in preorder_nodes if n in self.homes] #print(final_order) return final_order
def calculate_all_micas(self): """ Calculate the MICA (Most Informative Common Ancestor) of every class-pair """ G = self.G ics = self._information_content_frame() classes = list(dfs.dfs_preorder_nodes(G)) #mica_df = pd.DataFrame(index=classes, columns=classes) #mica_ic_df = pd.DataFrame(index=classes, columns=classes) ncs = len(classes) ic_grid = np.empty([ncs, ncs]) mica_arrs = [] logging.info('Calculating ICs for {} x {} classes'.format(ncs, ncs)) for c1i in range(0, ncs): c1 = classes[c1i] logging.debug('Calculating ICs for {}'.format(c1)) ancs1r = self._ancestors(c1) | {c1} c2i = 0 mica_arr = [] for c2i in range(0, ncs): c2 = classes[c2i] # TODO: optimize; matrix is symmetrical #if c1i > c2i: # continue ancs2r = self._ancestors(c2) | {c2} common_ancs = ancs1r & ancs2r if len(common_ancs) > 0: ic_cas = ics.loc[common_ancs] max_ic = float(ic_cas.max()) ic_micas = ic_cas[ic_cas >= max_ic] micas = set(ic_micas.index) mica_arr.append(micas) #mica_grid[c1i, c2i] = micas #mica_grid[c2i, c1i] = micas ic_grid[c1i, c2i] = max_ic #ic_grid[c2i, c1i] = max_ic else: ic_grid[c1i, c2i] = 0 mica_arr.append(set()) #ic_grid[c2i, c1i] = 0 # TODO: consider optimization step; blank out all anc pairs mica_arrs.append(mica_arr) logging.info('DONE Calculating ICs for {} x {} classes'.format( ncs, ncs)) #self.mica_df = pd.DataFrame(mica_grid, index=classes, columns=classes) self.mica_df = pd.DataFrame(mica_arrs, index=classes, columns=classes) self.mica_ic_df = pd.DataFrame(ic_grid, index=classes, columns=classes)
def get_relaxed_solution(self, model): vars_names = model.get_variables_names() list_of_vars = [] obj = model.get_objective_coefficients() dvars = {"$1":0, "$2":0} for ii in range(len(vars_names)): list_of_vars.append([vars_names[ii], 0, 0]) # name, weight, times dvars[vars_names[ii]] = ii G = nx.DiGraph(self._tree) for node in dfs_preorder_nodes(G, self._tree.get_root()): m = self._tree.get_model_by_name(node) kvars, coeffs, krhs, w = AddOnsMPM().to_knapsack(m) # function, 4 - Sobj/Scon con_sum, obj_sum = 0, 0 for i in range(len(kvars)): k = dvars[kvars[i]] # dvars - dictionary: key - index obj_sum += obj[i] con_sum += coeffs[i] list_of_vars[k][1] += krhs*obj[k]/coeffs[i] #w*krhs*obj[k]/coeffs[i] list_of_vars[k][2] += 1 for v in list_of_vars: v[1] /= v[2] # making weights of shared_vars comparable to other vars list_of_vars.sort(key = it(1), reverse = True) res_list = [] mm = len(list_of_vars) num_rows = model.get_num_rows() lhs = [0]*num_rows mtrx = model.get_rows_coefficients().toarray() fbool = False for jj in range(mm): for ii in range(num_rows): lhs[ii] += mtrx[ii][dvars[list_of_vars[jj][0]]] if lhs[ii] > model.get_rows_rhs()[ii]: fbool = True break if fbool: break res_list.append(list_of_vars[jj][0]) return res_list
def bfs_check(graph, n, check_result = False): start = time.time() # Explore the graph DFS: traversed_n = list(dfs_preorder_nodes(graph, 0)) # Check connectivity: check = True if len(traversed_n) == n else False tot_time = time.time() - start if check_result: return [tot_time, 'Connected' if check else 'Disconnected'] else: return tot_time
def __increment_alien_nodes(g, depth, max_generation_gap): '''Try to satisfy parent depth >= child depth - max_generation_gap by appropriately incrementing the depth of alien nodes (people that married into the pedigree but have no parent information so look like founder nodes); this feature is disabled if max_generation_gap is set to 0.''' # Identify pairs: alien node (people with no parent info that married into the # pedigree) + the other parent of a child they had together def aliens(): for node in g.nodes_iter(): if (g.out_degree(node) > 0): children = g.successors(node) # Node is more than max_generation_gap apart from all of its children if (depth[node] < min(depth[child] for child in children) - max_generation_gap): # If at least one of the children has another parent from which we can infer # the proper depth, return the alien and that parent for child in children: if g.in_degree(child) > 1: for parent in g.predecessors(child): if (parent != node): yield (node, parent) # Decrement the depths of each alien and its ancestors to match the other parent's # depth whenever possible h = g.reverse(copy=True) for (alien, parent) in aliens(): # We have information to update the alien node; see if we can actually increment its depth increment = depth[parent] - depth[alien] if (increment > 0): # Visit ancestors in depth-first-search pre-ordering (each ancestors is # processed before its parent) for ancestor in dfs_preorder_nodes(h, alien): new_depth = depth[ancestor] + increment if (g.out_degree(ancestor) == 0) or (new_depth < min(depth[node] for node in g.successors(ancestor))): # Not violating topological order ==> update this ancestor depth[ancestor] = new_depth return depth
def solveSteinerTreeDTH(self): print("start node: ", self.start_loc) print("home nodes: ", self.homes) # print("Traversal actual with homes: ", traversal_ordering) leaf_homes = self.getLeafNodes() preorder_nodes = dfs_preorder_nodes(self.steiner_tree, source=self.start_loc) traversal_ordering = [n for n in preorder_nodes if n in leaf_homes] print("Traversal ordering of the leaf: ", traversal_ordering) # """Remove non-leaf nodes from the order.""" # for home in traversal_ordering: # if home not in leaf_homes: # print(home) # traversal_ordering.remove(home) # current_loc = self.start_loc # """ Needs to start and end at root""" # print("Traversal_ordering: ", traversal_ordering) # # traversal_ordering.insert(0, current_loc) # # traversal_ordering.append(current_loc) """Hash map of the dropoffs""" dropoffs = dict() for i in range(len(traversal_ordering) - 1): current_leaf_home = traversal_ordering[i] next_leaf_home = traversal_ordering[i + 1] """Shortest path between current and next leaf home on the graph""" shortest_path = netx.shortest_path(self.netx_graph, source=current_leaf_home, target=next_leaf_home) print("Shortest path between ", current_leaf_home, " and ", next_leaf_home, shortest_path) for node in shortest_path: """Check if any node in the shortest node is a part of the Steiner Tree """ if node != current_leaf_home and node != next_leaf_home and node in self.steiner_tree: #print("Node : ", node) """Shares a common ancestor.""" """Drop off curr_node at curr->parent""" #print("Steiner tree: ", set(self.steiner_tree)) print( "Dfs ", networkx.dfs_predecessors(self.steiner_tree, source=self.start_loc)) dropoffs[current_leaf_home] = networkx.dfs_predecessors( self.steiner_tree, source=self.start_loc)[current_leaf_home] print("Dropoffs ", dropoffs) new_candidate_dropoff = set() for home in self.homes: if home in dropoffs.keys(): new_candidate_dropoff.add(dropoffs[home]) else: new_candidate_dropoff.add(home) print("homes ", self.homes) new_candidate_dropoff = list(new_candidate_dropoff) """Add source to the candidate dropoff list to create the steiner tree.""" new_candidate_dropoff.append(self.start_loc) #print("Candidate_dropoffs ", new_candidate_dropoff) steiner_tree_candidate_dropoffs = steiner_tree(self.netx_graph, new_candidate_dropoff, weight='weight') preorder_nodes = dfs_preorder_nodes(steiner_tree_candidate_dropoffs, source=self.start_loc) preorder_nodes = list(preorder_nodes) final_order = [ n for n in preorder_nodes if n in steiner_tree_candidate_dropoffs ] #print("final order pre", final_order) for elem in final_order: if elem in dropoffs.values(): keys = [k for k, v in dropoffs.items() if v == elem] if elem in self.homes or elem == self.start_loc: for i in keys: final_order.insert( final_order.index(elem) + 1, elem + " " + i) else: index = final_order.index(elem) for i in keys: final_order[index] = elem + " " + i index = index + 1 print("final order ", final_order) return self.get_cost_params(final_order)
def getOrdering(self): steiner_tree = self.steiner_tree preorder_nodes = dfs_preorder_nodes(steiner_tree, source=self.start_loc) final_order = [n for n in preorder_nodes if n in self.homes] return final_order
for n in g.node: g.node[n]['name'] = n outputs = [ # 'science-pack-1', # 'science-pack-2', # 'science-pack-3', # 'fast-inserter', # 'filter-inserter', # 'fast-transport-belt', # 'long-handed-inserter', # 'assembling-machine-2', # 'rail', 'solar-panel', 'laser-turret', 'gun-turret', ] nodes = set() for o in outputs: for n in dfs.dfs_preorder_nodes(g, o): nodes.add(n) factory = g.subgraph(nodes).reverse() nx.write_graphml(factory, os.path.join(basedir, 'factory.graphml'), prettyprint=True) try: nx.drawing.nx_agraph.view_pygraphviz(factory) # , prog='fdp') except ImportError: log.warn