def plotArbitraryGraph(self, graphToDraw): """function to plot graphs""" self.HEdgeList = copy.deepcopy(self.GEdgeList) self.H = copy.deepcopy(self.G) graphToDraw = nx.to_dict_of_lists(graphToDraw) #print "HEY" #print type(graphToDraw) edges = 0 for node, degree in graphToDraw.iteritems(): edges += len(degree) print(type(self.G)) print(self.G) GD = nx.Graph(graphToDraw) pos = nx.spring_layout(GD) print("\nArbitrary Graph: " + str(self.G)) print("\nNo. of edges in the Arbitrary Graph: " + str(edges / 2)) #plt.title("Arbitrary Graph") nx.draw(GD, pos, width=8.0, alpha=0.5, with_labels=True) plt.draw() plt.show() #plt.savefig('Arbitrary_Graph.png') self.H = nx.to_dict_of_lists(self.H) #self.H = nx.Graph(self.H) #print "see graph det",self.H #self.ChordalCheck(self.G) self.createChrdG()
def test_to_dict_of_lists_equivalence(): return True # this shouldn't change very much _Q = test_.fetch_query("prototype", "English") G = wikt_api.graph(_Q) wikt_api.draw_graph(G, pause=True) dl = nx.to_dict_of_lists(G) print(dl) G2 = nx.from_dict_of_lists(dl, create_using=nx.DiGraph) wikt_api.draw_graph(G2, pause=True) dl2 = nx.to_dict_of_lists(G2) assert dl == dl2
def multiplex_network(nodes_number, physical_network, hidden_network): layer_network = {} physical_dict = nx.to_dict_of_lists(physical_network) hidden_dict = nx.to_dict_of_lists(hidden_network) for node in range(nodes_number): layer_network[node] = { 'hidden': hidden_dict[node], 'physical': physical_dict[node] } return layer_network
def get_random_connected(nodes, nr_edges): """ Constructs a dictionary describing a random connected graph with a specified number of edges, with the name of the vertices are taken from the 'nodes' :param nodes: list of str Name of the nodes to be used :param nr_edges: int The number of edges that the graph should have. :return: dct keys are the names of the nodes and values their neighbors """ nn = len(nodes) min_edges = nn - 1 max_edges = nn * (nn - 1) / 2 if (nr_edges < min_edges) or (nr_edges > max_edges): raise ValueError("Number of edges cannot be less than #vertices-1 or greater then #vertices * (#vertices-1)/2") G = nx.random_tree(nn) non_edges = list(nx.non_edges(G)) for _ in range(min_edges, nr_edges): random_edge = random.choice(non_edges) G.add_edge(random_edge[0], random_edge[1]) non_edges.remove(random_edge) # Construct mapping to relabel nodes mapping = {i: nodes[i] for i in range(len(nodes))} nx.relabel_nodes(G=G, mapping=mapping, copy=False) # Get the dictionary from the graph adjacency_dct = nx.to_dict_of_lists(G) return adjacency_dct
def __init__(self, filename): self.filepath = filename self.filename = filename # if self.filename.endswith(".pdbqt"): # PDBQT_to_PDB(filename) # self.filename = self.filename + ".pdb" self.lines = [] with open(filename, "r") as f: self.lines = f.readlines() self.filename = self.filename.split("/")[-1] self.filename = self.filename[0:-4] self.atoms = {} self.ring_names = [] self.graph = nx.Graph() self.setAtomsAndConnections() self.connections = nx.to_dict_of_lists(self.graph) self.connections = {k: v for k, v in self.connections.items() if v} self.rings = nx.cycle_basis(self.graph) self.hash_to_ring_atoms = {} self.setHashToRingAtoms() self.nameRings()
def as_json(size, type='randomreg', degree=3, seed=42, repr_way='dict_of_lists'): """ Return a JSON representation of a graph. repr_way: 'dict_of_lists' or 'dict_of_dicts' """ types_allowed = ['randomreg', 'grid', 'rectgrid', 'randomgnp'] if type in types_allowed: args = dict(S=size, type=type, degree=degree, seed=seed) G = utils_qaoa.get_test_graph(**args) #dict_ = nx.to_dict_of_dicts(G) if repr_way == 'dict_of_dicts': dict_ = nx.to_dict_of_dicts(G) elif repr_way == 'dict_of_lists': dict_ = nx.to_dict_of_lists(G) to_db = {} # TODO: generator of id should be separate to_db['_id'] = f"S{size}_{type}_d{degree}_s{seed}" # Note: mongodb will try to use all the nested dicts, # so store the graph as string to_db['graph'] = json.dumps(dict_) to_db['n_edges'] = G.number_of_edges() to_db['n_nodes'] = G.number_of_nodes() to_db['extra'] = args to_db['tags'] = ['qaoa', 'maxCut'] str = json.dumps(to_db) print(str) else: raise Exception( f"Invalid graph type {type}, should be one of {types_allowed}")
def createGraph(degree, normal, testing, validation): # number of nodes n = normal # mean degree deg = degree # number of abberant nodes numtestnodes = testing numvalnodes = validation nodes = range(n) valnodes = range(n, n + numvalnodes) testnodes = range(n + numvalnodes, n + numvalnodes + numtestnodes) total = n + numvalnodes + numtestnodes # number of edges m = total * deg / 2 # normal nodes G = nx.gnm_random_graph(total, m) # parameters for each node's queuing and processing times expParameters = np.zeros(total) unifParameters = np.zeros(total) for i in range(total): # exponential delay parameter from Gamma(20,5) expParameters[i] = np.random.gamma(20, 5) # uniform processing parameter from Pareto(1.25,20) unifParameters[i] = (np.random.pareto(1.25) + 1) * 20 return (G, nx.to_dict_of_lists(G), expParameters, unifParameters)
def non_bool_where(partition): """Return the number of non-connected assignment subgraphs. :param partition: Instance of Partition; contains connected components. :return: number of contiguous districts :rtype: int """ returns = 0 # Generates a subgraph for each district and perform a BFS on it # to check connectedness. for part in partition.parts: subgraph = partition.subgraphs[part] adj = nx.to_dict_of_lists() if _bfs(adj): returns += 1 else: print(part) nx.draw(subgraph) plt.show() print(subgraph.nodes) for subdistrict in nx.connected_components(subgraph): nx.draw(partition.graph.subgraph(subdistrict), with_labels=True) plt.show() print(subdistrict) return returns
def non_bool_fast_connected(partition): """ Checks that a given partition's components are connected using a simple breadth-first search. :partition: Instance of Partition; contains connected components. :return: int: number of contiguous districts """ assignment = partition.assignment # Inverts the assignment dictionary so that lists of VTDs are keyed # by their congressional districts. districts = collections.defaultdict(set) returns = 0 for vtd in assignment: districts[assignment[vtd]].add(vtd) # Generates a subgraph for each district and perform a BFS on it # to check connectedness. for district in districts: adj = nx.to_dict_of_lists(partition.graph, districts[district]) if bfs(adj): returns += 1 return returns
def test_create_complete_dag_graph( fake_workbench: Workbench, fake_workbench_complete_adjacency: Dict[str, List[str]], ): dag_graph = create_complete_dag(fake_workbench) assert nx.is_directed_acyclic_graph(dag_graph) assert nx.to_dict_of_lists(dag_graph) == fake_workbench_complete_adjacency
def make_class_hierarchy(n, n_intermediate=None, n_leaf=None): """Create a mock class hierarchy for testing purposes. Parameters ---------- n : int Number of nodes in the returned graph n_intermediate : int Number of intermediate (non-root, non-terminal) nodes in the returned graph n_leaf : int Number of leaf (terminal) nodes in the returned graph Returns ------- G : dict of lists adjacency matrix format representing the class hierarchy """ if n_leaf is None and n_intermediate is None: # No specific structure specified, use a general purpose graph generator G = gn_graph(n=n, create_using=DiGraph()) if n_intermediate == 0: # No intermediate nodes, build a 1-level rooted tree if n_leaf is None: n_leaf = n - 1 G = DiGraph(data=product((ROOT, ), range(n_leaf))) return to_dict_of_lists(G)
def gen_and_save_u2u_dict_and_split(num_user, num_item): u2ui = np.load('datasets/Yelp/noiso_reid_u2ui.npz') print('Building u2u graph...') g = nx.Graph() g.add_nodes_from(list(range(num_user))) g.add_edges_from(u2ui['u2u']) g.remove_node(0) print('To undirected graph...') g.to_undirected() # g.add_edges_from([[u, u] for u in g.nodes]) u2u_dict = nx.to_dict_of_lists(g) df = pd.DataFrame(data=u2ui['u2i'], columns=['user', 'item', 'ts']) print('Raw u2i =', df.shape) df.drop_duplicates(subset=['user', 'item', 'ts'], keep='first', inplace=True) df = df.sort_values(['user', 'ts'], kind='mergesort').reset_index(drop=True) print('Processed u2i =', df.shape) user_train, user_valid, user_test, eval_users, valid_items, test_items = data_partition( df) save_path = 'datasets/Yelp/u2u_split_dicts.pkl' save_pkl(save_path, [ u2u_dict, user_train, user_valid, user_test, eval_users, valid_items, test_items ]) print('saved at', save_path)
def stats(self): ''' Return all other stats Params: None Returns: dictionary of stats with keys(stats supported): num_connections - total number of connections max_degree - degree of highest degree node mean_degree - average degree empty - number of nodes with no connections variance - variance in degree distribution odd_length - odd length cycle exist? - Not implemented num_connected_components - number of connected components any_frac - fraction of nodes with connection(s) big_frac - fraction of nodes in the largest connected group ''' output = {} degrees = self.degree_dist() output['num_connections'] = int(sum([i * degrees[i] for i in degrees]) / 2.0) output['max_degree'] = max(degrees.keys()) output['mean_degree'] = sum([i * degrees[i] for i in degrees]) / float(self.size) if self.size else 0 output['empty'] = not output['max_degree'] output['variance'] = sum([degrees[degree] * (degree - output['mean_degree'])**2 for degree in degrees]) / float(self.size) if self.size else 0 # output['odd_length'] = 'Not implemented' output['num_connected_components'] = nx.number_connected_components(self.G) output['any_frac'] = sum([degrees[i] for i in degrees if i != 0]) / float(self.size) if self.size else 0 if not output['empty']: Gcc = nx.connected_component_subgraphs(self.G) num_in_greatest = len(nx.to_dict_of_lists(self.G)) output['big_frac'] = num_in_greatest / float(self.size) if self.size else 0 else: output['big_frac'] = 0 return output
def generate_random_graphs(numberOfNodes, outputFile, graphType, degree=None): sparseResult = open(outputFile, "w") #first writing the number of nodes if (graphType == "degreeBased"): G = nx.random_regular_graph( degree, numberOfNodes, numberOfNodes * int(math.sqrt(numberOfNodes))) if (graphType == "completeChaos"): G = nx.gnm_random_graph(numberOfNodes, numberOfNodes * int(math.sqrt(numberOfNodes))) if (graphType == "dense"): G = nx.dense_gnm_random_graph( numberOfNodes, numberOfNodes * int(math.sqrt(numberOfNodes))) sparseResult.write( str(numberOfNodes) + " " + str(nx.number_of_edges(G)) + "\n") semiSparseRep = nx.to_dict_of_lists(G) #print semiSparseRep for element in semiSparseRep: if len(semiSparseRep[element]) == 0: return 0 for j in semiSparseRep[element]: sparseResult.write(str(j + 1) + " ") sparseResult.write("\n") return 1
def dfs(graph, start, goal=None): """ Implements depth-first search from start node to goal(if given). :param graph: networkx.Graph() :param start: vertex to start search from :param goal: (optional) vertex where search ends, default: None if default search stops when all vertices are visited :return: generator that gives vertices in dfs-order :raise KeyError: if 'start' is not in 'graph' or 'goal' is provided and is not in 'graph' """ if start not in graph: raise KeyError('dfs: Start vertex not in graph') if goal is not None and goal not in graph: raise KeyError('dfs: Given goal vertex not in graph') graph_dict = nx.to_dict_of_lists(graph) queue = [start] visited = [] while queue: v = queue.pop(0) if goal is not None and v == goal: yield v break if v in visited: continue visited.append(v) queue[0:0] = [i for i in graph_dict[v] if i not in visited] yield v
def get_graph_adjacency(self, G): ''' 获取图中的邻接矩阵的字典形式 ''' adj_list = nx.to_dict_of_lists(G) return adj_list
def bfs(graph, start, goal=None): """ Implements breadth-first search from start node to goal(if given). :param graph: networkx.Graph() :param start: vertex to start search from :param goal: (optional) vertex where search ends, default: None if default search stops when all vertices are visited :return: generator that gives vertices in bfs-order :raise KeyError: if 'start' is not in 'graph' or 'goal' is provided and is not in 'graph' """ if start not in graph: raise KeyError('bfs: Start vertex not in graph') if goal is not None and goal not in graph: raise KeyError('bfs: Given goal vertex not in graph') graph_dict = nx.to_dict_of_lists(graph) queue = [start] if goal is None: for v in queue: queue.extend([i for i in graph_dict[v] if i not in queue]) yield v else: for v in queue: if v == goal: yield v break queue.extend([i for i in graph_dict[v] if i not in queue]) yield v
def __init__(self, ): self.agentGroupList = [] self.agentList = [] self.baseMetrics = [] #エージェントを全員作る for i in range(Setting.AGENT_NUM_IN_ONE_GROUP * Setting.AGENT_GROUP_NUM): self.agentList.append(Agent(i)) #ベースとなる評価指標を作る for i in range(Setting.GYOMU_NUM - Setting.GROUP_DIFFERENCE): self.baseMetrics.append(Metrics()) #評価指標が異なるエージェントグループを作る for i in range(Setting.AGENT_GROUP_NUM): self.agentGroupList.append(AgentGroup(i, self.baseMetrics)) #エージェントグループにエージェントを入れる for agentGroup in self.agentGroupList: for i in range(Setting.AGENT_NUM_IN_ONE_GROUP): agentGroup.addAgent( self.agentList[i + agentGroup.id * Setting.AGENT_NUM_IN_ONE_GROUP]) #ネットワークを作り、エージェントを繋げる self.G = nx.relaxed_caveman_graph(Setting.AGENT_GROUP_NUM, Setting.AGENT_NUM_IN_ONE_GROUP, Setting.BETA) relations = nx.to_dict_of_lists(self.G) for i in range(len(relations)): self.agentList[i].connect(relations[i], self.agentList) self.Alldata = []
def fast_connected(partition): """ Checks that a given partition's components are connected using a simple breadth-first search. :partition: Instance of Partition; contains connected components. :flips: Dictionary of proposed flips. :return: Boolean; Are the components of this partition connected? """ assignment = partition.assignment # Inverts the assignment dictionary so that lists of VTDs are keyed # by their congressional districts. districts = collections.defaultdict(set) for vtd in assignment: districts[assignment[vtd]].add(vtd) # Generates a subgraph for each district and perform a BFS on it # to check connectedness. for district in districts: adj = nx.to_dict_of_lists(partition.graph, districts[district]) if bfs(adj) is False: return False return True
def proposed_changes_still_contiguous(partition): """ Checks whether the districts that are altered by a proposed change (stored in partition.flips) remain contiguous under said changes. :parition: Current :class:`.Partition` object. :returns: True if changed districts are contiguous, False otherwise. """ # Check whether this is the initial partition (parent=None) # or one with proposed changes (parent != None). districts_of_interest = set(partition.assignment.values()) if partition.parent: if partition.flips.keys is not None: districts_of_interest = set(partition.flips.values()).union( set(map(partition.parent.assignment.get, partition.flips))) else: districts_of_interest = [] # Inverts the assignment dictionary so that lists of VTDs are keyed # by their congressional districts. assignment = partition.assignment districts = collections.defaultdict(set) for vtd in assignment: districts[assignment[vtd]].add(vtd) for key in districts_of_interest: adj = nx.to_dict_of_lists(partition.graph, districts[key]) if _bfs(adj) is False: return False return True
def init_setup(): data = Dataset(root='/tmp/', name=args.dataset, setting='gcn') data.features = normalize_feature(data.features) adj, features, labels = data.adj, data.features, data.labels StaticGraph.graph = nx.from_scipy_sparse_matrix(adj) dict_of_lists = nx.to_dict_of_lists(StaticGraph.graph) idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test device = torch.device('cuda') if args.ctx == 'gpu' else 'cpu' # black box setting adj, features, labels = preprocess(adj, features, labels, preprocess_adj=False, sparse=True, device=device) victim_model = load_victim_model(data, device=device, file_path=args.saved_model) setattr(victim_model, 'norm_tool', GraphNormTool(normalize=True, gm='gcn', device=device)) output = victim_model.predict(features, adj) loss_test = F.nll_loss(output[idx_test], labels[idx_test]) acc_test = accuracy(output[idx_test], labels[idx_test]) print("Test set results:", "loss= {:.4f}".format(loss_test.item()), "accuracy= {:.4f}".format(acc_test.item())) return features, labels, idx_val, idx_test, victim_model, dict_of_lists, adj
def generate_seeds(num_players, num_seeds, G): # Initialize see array to zeros seeds = np.zeros(num_seeds, dtype=np.int) neighbors = nx.to_dict_of_lists(G).values() m = nx.closeness_centrality(G); centralities = m.values(); # Initialize seed array to zeros seeds = np.zeros(num_seeds, dtype=np.int) sum_centralities = [(None, None)] * len(neighbors) # tuple of (node_id, degree) # Assign sum of centralities to each node in the graph (over all neighbors) for i in range(len(neighbors)): sum = 0.0 this_nodes_neighbors = neighbors[i] for j in range(len(neighbors[i])): sum = sum + centralities[this_nodes_neighbors[j]] sum_centralities[i] = ( i, sum ) sorted_centralities = sorted(sum_centralities, key=itemgetter(1), reverse=True) for i in range(num_seeds): seeds[i] = sorted_centralities[i][0] return seeds
def sage_format_to_node2vec_format(sage_prefix='ppi', root_dir='data'): train_data = utils_sage.load_data(sage_prefix, root_dir='data') G = train_data[0] features = train_data[1] id_map = train_data[2] class_map = train_data[4] try: all_labels = np.fromiter(class_map.values(), dtype=int) except Exception: print("Not support multiple classes") return graph = nx.to_dict_of_lists(G) edges = [] for key, value in graph.items(): if isinstance(value, int): edges.append([key, value]) else: for v in value: edges.append([key, v]) np.savetxt("{}/{}.edgelist".format(root_dir, sage_prefix), edges, delimiter=' ', fmt='%s') return None
def test_lil_bfs(): n = 10 r = 1 g1 = nx.random_regular_graph(d=3, n=10) g1_lil = nx.adjacency_matrix(g1, nodelist=range(n)).tolil() tree_gt = nx.bfs_tree(g1, source=r) tree = bfs_lil(g1_lil, r=r) print("\n", tree) print(nx.to_dict_of_lists(tree_gt)) pos = nx.spring_layout(g1) plt.subplot(131) plt.title("graph") nx.draw(g1, pos=pos, node_color=set_color(g1, r, 1), with_labels=True) plt.subplot(132) plt.title("bfs_nx_tree") nx.draw(tree_gt, pos=pos, node_color=set_color(tree_gt, r, 1), with_labels=True) plt.subplot(133) plt.title("bfs_my_tree") nx.draw( nx.from_dict_of_lists(tree, create_using=nx.DiGraph), pos=pos, node_color=set_color(tree_gt, r, 1), with_labels=True, ) plot()
def run_er_algo(): """ run homework """ nodes = 10000 prob = 0.1 # er_graph= er_algorithm(nodes, prob) time0 = time.time() G = nx.fast_gnp_random_graph(nodes, prob, directed=True) digraph = nx.to_dict_of_lists(G) time1 = time.time() print('NetworkX took {} second'.format(time1 - time0)) time0 = time.time() digraph2 = er_algorithm(nodes, prob) time1 = time.time() print('Mine took {} second'.format(time1 - time0)) norm_dist1 = normal_degree_distribution(digraph, 'in') norm_dist2 = normal_degree_distribution(digraph2, 'in') plt.plot(list(norm_dist1.keys()), list(norm_dist1.values()), 'o', list(norm_dist2.keys()), list(norm_dist2.values()), 'g^') plt.show()
async def upsert_pipeline( self, project_id: ProjectID, dag_graph: nx.DiGraph, publish: bool, ) -> None: pipeline_at_db = CompPipelineAtDB( project_id=project_id, dag_adjacency_list=nx.to_dict_of_lists(dag_graph), state=RunningState.PUBLISHED if publish else RunningState.NOT_STARTED, ) insert_stmt = insert(comp_pipeline).values(**pipeline_at_db.dict(by_alias=True)) # FIXME: This is not a nice thing. this part of the information should be kept in comp_runs. update_exclusion_policy = set() if not dag_graph.nodes(): update_exclusion_policy.add("dag_adjacency_list") on_update_stmt = insert_stmt.on_conflict_do_update( index_elements=[comp_pipeline.c.project_id], set_=pipeline_at_db.dict( by_alias=True, exclude_unset=True, exclude=update_exclusion_policy ), ) async with self.db_engine.acquire() as conn: await conn.execute(on_update_stmt)
def generate_seeds(num_players, num_seeds, G): # Initialize see array to zeros seeds = np.zeros(num_seeds, dtype=np.int) neighbors = nx.to_dict_of_lists(G).values() m = nx.closeness_centrality(G) centralities = m.values() degrees = np.zeros(len(neighbors), dtype=np.int) # tuple of (node_id, degree) for i in range(len(neighbors)): degrees[i] = len(neighbors[i]) scores = [(None, None)] * len(neighbors) degree_max = max(degrees) cent_max = max(centralities) for i in range(len(neighbors)): norm_degree = float(degrees[i]) / degree_max norm_cent = float(centralities[i]) / cent_max scores[i] = (i, norm_degree * DEGREE_WEIGHT + norm_cent * CENT_WEIGHT) sorted_scores = sorted(scores, key=itemgetter(1), reverse=True) for i in range(num_seeds): seeds[i] = sorted_scores[i][0] return seeds
def delete_step(): reaction = request.form['reaction'] task_id = request.form['task_id'] data = json.loads(current_app.redis.get(task_id)) graph_dict = json.loads(data['graph_dict']) attr_dict = json.loads(data['attr_dict']) target_smiles = data['target_smiles'] network_options = json.loads(data['network_options']) graph = nx.from_dict_of_lists(graph_dict, create_using=nx.DiGraph) network = Network(graph=graph, target_smiles=target_smiles, print_log=not current_app.config['PRODUCTION']) network.update_settings(network_options) network.add_attributes(attr_dict) to_delete = network.delete_reaction_node(reaction) nodes = [] edges = [] data['graph_dict'] = json.dumps(nx.to_dict_of_lists(network.graph)) data['attr_dict'] = json.dumps(network.attributes_dict()) nodes = add_new(data['nodes'], nodes) edges = add_new(data['edges'], edges) nodes, edges = delete_nodes_and_edges(to_delete, nodes, edges) data['nodes'] = nodes data['edges'] = edges current_app.redis.mset({task_id: json.dumps(data)}) time_to_expire = 15 * 60 # 15 mins * 60 seconds current_app.redis.expire(task_id, time_to_expire) result = {'to_delete': to_delete} return jsonify(result=result)
async def compute_pipeline_details( complete_dag: nx.DiGraph, pipeline_dag: nx.DiGraph, comp_tasks: List[CompTaskAtDB]) -> PipelineDetails: try: # FIXME: this problem of cyclic graphs for control loops create all kinds of issues that must be fixed # first pass, traversing in topological order to correctly get the dependencies, set the nodes states await _set_computational_nodes_states(complete_dag) except nx.NetworkXUnfeasible: # not acyclic pass return PipelineDetails( adjacency_list=nx.to_dict_of_lists(pipeline_dag), node_states={ node_id: NodeState( modified=node_data.get(kNODE_MODIFIED_STATE, False), dependencies=node_data.get(kNODE_DEPENDENCIES_TO_COMPUTE, set()), currentStatus=next( (task.state for task in comp_tasks if str(task.node_id) == node_id), RunningState.UNKNOWN, ), ) for node_id, node_data in complete_dag.nodes.data() if _is_node_computational(node_data.get("key", "")) }, )
def generate_random_graph(): """ generates random graphs: external loop - number of repeated generations; inner loop-number of different probabilities :return: saves generated graphs to json and as networkx adjlist """ for i in range(1, 100): for prob in range(1, 10): prob = float(prob / float(nodes * 10)) # den=nodes*10 G = nx.fast_gnp_random_graph(nodes, prob).to_undirected() res = {} try: res = nx.to_dict_of_lists(G) except TypeError: # Python 3.x sys.stdout("Error") size = len(list(nx.bridges(G))) # number of bridges file_name_adj = "random_{}_{}_{}.adj_list".format( nodes, prob, size) file_name_json = "random_{}_{}_{}.json".format(nodes, prob, size) file_path_adj = os.path.join(os.getcwd(), '..', 'res', file_name_adj) file_path_json = os.path.join(os.getcwd(), '..', 'res', file_name_json) # writing to .json with open(file_path_json, "w") as f: json.dump(res, f, indent=4) # writing to .adjlist fh = open(file_path_adj, 'wb+') nx.write_adjlist(G, fh)
def get_isomorphic_signature(graph: DiGraph) -> str: """ Generate unique isomorphic id with pynauty """ nauty_graph = pynauty.Graph(len(graph.nodes), directed=True, adjacency_dict=nx.to_dict_of_lists(graph)) return hashlib.md5(pynauty.certificate(nauty_graph)).hexdigest()
def save_graph(self, G: nx.Graph, savePath, graphName, **kwargs): savePath, graphName = self.format_path(G, savePath, graphName, **kwargs) G_d = nx.to_dict_of_lists(G) print("Saving graph to {}".format( os.path.join(savePath, graphName + ".graph"))) pickle.dump(G_d, open(os.path.join(savePath, graphName + ".graph"), "wb"))
def write_gph(G, filename): edges = [] for key, values in nx.to_dict_of_lists(G).items(): for value in values: edges.append('{},{}\n'.format(key,value)) with open(filename, 'w') as f: f.writelines(edges)
def from_edgelist(filename): #to read from .edgelist file G = Graph() t = nx.read_edgelist(filename) t = nx.to_dict_of_lists(t) t = {int(k): [int(i) for i in v] for k, v in t.items()} for key, value in t.items(): G[key].extend(value) return G
def generate_seeds(num_players, num_seeds, G): DEGREE_WEIGHT = 1.0 CENTRALITY_WEIGHT = 7.0 NEIGHBOR_WEIGHT = 3.0 NEIGHBOR_WEIGHT2 = 3.0 # Initialize see array to zeros seeds = np.zeros(num_seeds, dtype=np.int) neighbors = nx.to_dict_of_lists(G).values() m = nx.closeness_centrality(G) centralities = m.values() sum_degrees = np.zeros(len(neighbors), dtype=np.int) sum_centralities = np.zeros(len(neighbors), dtype=np.float32) degrees = np.zeros(len(neighbors), dtype=np.int) # tuple of (node_id, degree) for i in range(len(neighbors)): degrees[i] = len(neighbors[i]) # Assign sum of degrees to each node in the graph (over all neighbors) for i in range(len(neighbors)): sum = 0 this_nodes_neighbors = neighbors[i] for j in range(len(neighbors[i])): sum = sum + degrees[this_nodes_neighbors[j]] sum_degrees[i] = sum # Assign sum of centralities to each node in the graph (over all neighbors) for i in range(len(neighbors)): sum = 0.0 this_nodes_neighbors = neighbors[i] for j in range(len(neighbors[i])): sum = sum + centralities[this_nodes_neighbors[j]] sum_centralities[i] = sum scores = [(None, None)] * len(neighbors) degree_max = max(degrees) cent_max = max(centralities) neighbors_max = max(sum_degrees) neighbors_max2 = max(sum_centralities) for i in range(len(neighbors)): norm_degree = float(degrees[i]) / degree_max norm_cent = float(centralities[i]) / cent_max norm_neighbors = float(sum_degrees[i]) / neighbors_max norm_neighbors2 = float(sum_centralities[i]) / neighbors_max2 scores[i] = (i, norm_degree * DEGREE_WEIGHT + norm_cent * CENTRALITY_WEIGHT + norm_neighbors * NEIGHBOR_WEIGHT + norm_neighbors2 * NEIGHBOR_WEIGHT2) sorted_scores = sorted(scores, key=itemgetter(1), reverse=True) for j in range(50): for i in range(num_seeds): seeds[i] = sorted_scores[i][0] print seeds[i] return seeds
def generate_random_graphs(numberOfNodes, outputFile): sparseResult = open(outputFile, "w") #first writing the number of nodes G=nx.gnm_random_graph(numberOfNodes, numberOfNodes*int(math.sqrt(numberOfNodes))) sparseResult.write(str(numberOfNodes) + " " + str(nx.number_of_edges(G)) + "\n"); semiSparseRep = nx.to_dict_of_lists(G) #print semiSparseRep for element in semiSparseRep: if len(semiSparseRep[element]) ==0: return 0 for j in semiSparseRep[element]: sparseResult.write(str(j+1) + " ") sparseResult.write("\n") return 1
def generate_seeds(num_players, num_seeds, G): # Initialize seed array to zeros seeds = np.zeros(num_seeds, dtype=np.int) neighbors = nx.to_dict_of_lists(G).values() degrees = [(None, None)] * len(neighbors) # tuple of (node_id, degree) for i in range(len(neighbors)): degrees[i] = ( i, len(neighbors[i]) ) sorted_degrees = sorted(degrees, key=itemgetter(1), reverse=True) for i in range(num_seeds): seeds[i] = sorted_degrees[i][0] return seeds
def run_pymetis(J, nparts): ''' ''' # print('running {0}-way partitioning...'.format(nparts)) # run pymetis partitioning adj_list = nx.to_dict_of_lists(nx.Graph(J)) adj_list = [adj_list[k] for k in range(len(adj_list))] ncuts, labels = part_graph(nparts, adjacency=adj_list) # get indices of each partition parts = [[] for _ in range(nparts)] for i, p in enumerate(labels): parts[p].append(i) return parts
def w1_q2(): """ question2 """ nx_graph = nx.fast_gnp_random_graph(5000, 0.1, directed=True) nx_graph = nx.to_dict_of_lists(nx_graph) norm_dist = normal_degree_distribution(nx_graph, 'in') plt.loglog(list(norm_dist.keys()), list(norm_dist.values()), 'co', label='n:5000 p:0.1') plt.title('Log10 chart\nDistribution in-degrees ER Algorithm') plt.xlabel('degree') plt.ylabel('frequency') plt.legend() plt.grid(True) plt.show()
def random_range(cls, n_guests_range, n_seats_range, n_tables_range, n_groups_range, pref_weight_range, p=0.1, min_group_size=2, max_group_size=None): n_guests = rnd.randint(*n_guests_range) n_seats = rnd.randint(*n_seats_range) n_tables = rnd.randint(*n_tables_range) n_groups = rnd.randint(*n_groups_range) pref_weight = rnd.randint(*pref_weight_range) G = nx.fast_gnp_random_graph(n_guests, p, directed=True) G = nx.convert_node_labels_to_integers(G, first_label=1) if max_group_size is None: max_group_size = n_seats groups = disjoint_subsets(G.nodes(), n_groups, min_size=min_group_size, max_size=max_group_size) pref = nx.to_dict_of_lists(G).values() return cls(n_guests, n_seats, n_tables, n_groups, pref_weight, groups, pref)
def play(game, playerlist): assert game.num_players == len(playerlist) graph = nx.to_dict_of_lists(game.network) nodes = {} for i, player in enumerate(playerlist): nodes["strategy" + str(i)] = player.give_output_list(game) assert len(nodes["strategy" + str(i)]) == game.num_seeds #print nodes["strategy" + str(i)] #print result = sim.run(graph, nodes) return result
def compare_several_distr(): """ debugging """ node = 5000 prob = 0.1 step = 0.1 result = {} while prob <= 0.6: nx_graph = nx.fast_gnp_random_graph(node, prob, directed=True) digraph = nx.to_dict_of_lists(nx_graph) norm_dist = normal_degree_distribution(digraph, 'in') result[prob] = norm_dist prob += step prob = round(prob, 1) print(prob) # charting x1 = list(result[0.1].keys()) y1 = list(result[0.1].values()) x2 = list(result[0.2].keys()) y2 = list(result[0.2].values()) x3 = list(result[0.3].keys()) y3 = list(result[0.3].values()) x4 = list(result[0.4].keys()) y4 = list(result[0.4].values()) x5 = list(result[0.5].keys()) y5 = list(result[0.5].values()) x6 = list(result[0.6].keys()) y6 = list(result[0.6].values()) plt.loglog(x1, y1, 'ro', x2, y2, '^g', x3, y3, 'sr', x4, y4, 'r', x5, y5, 'bo', x6, y6, 'yo') plt.show()
def minimize_u_distance_to_incoming(H, E, U0, W): rH = nx.to_dict_of_lists(H) nU = np.copy(U0) nodes = list(rH) random.shuffle(nodes) stats = np.zeros((len(rH), 8)) for u in nodes: nei = rH[u] local_E, local_wc = [], [] w = 0 for v in nei: nu, nv = (u, v) if u < v else (v, u) local_E.append((nu, nv)) local_wc.append(E[(nu, nv)]) w += W[local_wc[-1], :] nw = w / np.linalg.norm(w) def m(x): return np.array([x * nU[e[0] if e[0] != u else e[1], :] for e in local_E]) x = np.copy(U0[u, :]) prev_x = np.copy(x) score = (np.argmax(m(x)@W.T, 1) == local_wc).sum() if (np.argmax(m(nw)@W.T, 1) == local_wc).sum() >= score: nU[u, :] = np.copy(nw) continue new_score = score f, g = ((x - w)**2).sum(), 2 * (x - w) nb_iter = 0 eps = .1 costs = [f, ] while nb_iter < 100 and new_score >= score: prev_x = np.copy(x) x -= eps * g x /= np.linalg.norm(x) f, ng = ((x - w)**2).sum(), 2 * (x - w) new_score = (np.argmax(m(x)@W.T, 1) == local_wc).sum() costs.append(f) g = ng nb_iter += 1 if new_score < score: x = np.copy(prev_x) stats[u, :] = (np.linalg.norm(U0[u, :] - nw), np.linalg.norm(x - nw), nb_iter, np.max(m(U0[u, :])@W.T, 1).sum(), np.max(m(x)@W.T, 1).sum(), np.linalg.norm(U0[u, :] - w), np.linalg.norm(x - w), np.linalg.norm(nw - w)) nU[u, :] = np.copy(x) return nU, stats
def create_blocks(nb_blocks, n, adj): # blocks = [] labeling = [] for i in range(nb_blocks): # blocks.append(nx.fast_gnp_random_graph(n, 4 / n, seed=seed + i)) # connect_graph(blocks[-1]) rG = nx.to_dict_of_lists(blocks[i]) best_labels, min_fail = None, 2e9 nb_iter = 0 while nb_iter < 7000 and min_fail > 0: nb_iter += 1 labels = create_random_labeling(rG, adj) new_failed = find_failed(rG, labels, adj) if len(new_failed) < min_fail: best_labels, min_fail = dict(labels), len(new_failed) labeling.append(dict(best_labels)) # print(i, nb_iter, min_fail) return blocks, labeling
def create_full_graph(nb_blocks=4, n=100): def intersect(a, b): return len(set(a) & set(b)) >= 1 adj = {} for p in pairs: nei = {q for q in pairs if q != p and intersect(p, q)} adj[p] = nei # adj[p].add(p) blocks, labeling = create_blocks(nb_blocks, n, adj) G, labels = assemble_blocks(blocks, labeling, adj) mapping = {v: i for i, v in enumerate(sorted(G))} inv_mapping = {v: k for k, v in mapping.items()} # H = G.copy() # nx.relabel_nodes(H, mapping, copy=False) num_failed = len(find_failed(nx.to_dict_of_lists(G), labels, adj)) # print(H.number_of_nodes(), H.number_of_edges(), num_failed) return G, labels, mapping, inv_mapping, num_failed
def generate_random_graphs(numberOfNodes, outputFile, graphType, degree=None): sparseResult = open(outputFile, "w") # first writing the number of nodes if graphType == "degreeBased": G = nx.random_regular_graph(degree, numberOfNodes, numberOfNodes * int(math.sqrt(numberOfNodes))) if graphType == "completeChaos": G = nx.gnm_random_graph(numberOfNodes, numberOfNodes * int(math.sqrt(numberOfNodes))) if graphType == "dense": G = nx.dense_gnm_random_graph(numberOfNodes, numberOfNodes * int(math.sqrt(numberOfNodes))) sparseResult.write(str(numberOfNodes) + " " + str(nx.number_of_edges(G)) + "\n") semiSparseRep = nx.to_dict_of_lists(G) # print semiSparseRep for element in semiSparseRep: if len(semiSparseRep[element]) == 0: return 0 for j in semiSparseRep[element]: sparseResult.write(str(j + 1) + " ") sparseResult.write("\n") return 1
def __init__(self, G=None, L0=None, C=None, epsilon=0.05): super(Cascade, self).__init__() # NB always using G.nodes() to access nodes so that the sort order # is the same for A, L0 and C self.G = G nodes = G.nodes() nodes_indices = {node: i for i, node in enumerate(nodes)} # Initialize A (adjacency list) # This is always determined from G (cannot be passed separately) a = nx.to_dict_of_lists(G) A, A_ptr, A_len = create_sparse_adjacency_list(nodes, nodes_indices, a) self.A = A self.A_ptr = A_ptr self.A_len = A_len self.n = len(nodes) # self.n is number of nodes # Initialize L and L0 (initial load) if L0 is not None: # L0 given, so use it rather than G assert len(L0) == self.n # Fail if L0 doesn't match G self.L0 = np.array(L0, dtype=DTYPE_INT) else: # L0 not given l = [G.node[i]['load'] for i in nodes] self.L0 = np.array(l, dtype=DTYPE_INT) self.L = self.L0.copy() # Initialize C and C0 (initial capacity) if C is not None: # C given, so use it rather than G if isinstance(C, int): self.C0 = C * np.ones(self.n, dtype=DTYPE_INT) else: assert len(C) == self.n self.C0 = np.array(C, dtype=DTYPE_INT) else: # C not given c = [G.node[i]['capacity'] for i in nodes] self.C0 = np.array(c, dtype=DTYPE_INT) self.C_max = int(self.C0.max()) self.C = self.C0.copy() self.epsilon = epsilon
def give_output_list(self, game): """ This returns a list of the selected nodes. The twin attack player finds the highest degree nodes, and for each, it selects two neighbors of that node and""" # First, find out what the TA is doing. if game in self.game_memo_dict: return self.game_memo_dict[game] nodes = nx.nodes(game.network) value_dict = nx.degree_centrality(game.network) nodes.sort(key=lambda x : value_dict[x], reverse=True) ta_output_list = nodes[:game.num_seeds] candidate_nodes = nodes[:25] i = 0 while i < 10000000: i += 1 selections = set() while len(selections) < game.num_seeds: selections.add(random.choice(candidate_nodes)) selections = list(selections) result = sim.run(nx.to_dict_of_lists(game.network), {"s0" : ta_output_list, "s1" : selections}) if result["s0"] < result["s1"]: break assert len(selections) == game.num_seeds self.game_memo_dict[game] = selections return list(selections)
def main(): #G_no_path = nx.fast_gnp_random_graph(12, 0.1, 142443) #graph_no_path = nx.to_dict_of_lists(G_no_path) random.seed() print("| algorithm \t| total nodes \t| nodes searched") for i in range(3, 10): print() G = nx.fast_gnp_random_graph(2**i, random.uniform(0.0, 0.3), 142443) graph = nx.to_dict_of_lists(G) tests1 = [] tests2 = [] tests3 = [] for n in range(100): while True: source = random.randrange(0, len(graph)) target = random.randrange(0, len(graph)) if source != target: break # test if path exists try: test = nx.shortest_path(G, source, target) path_exists = True except nx.NetworkXNoPath: path_exists = False if path_exists: tests1.append(bfs1(source, target, graph)) else: # path does not exist, bfs1 would go into endless loop tests1.append(len(graph)) # assume bfs1 traversed the whole graph tests2.append(bfs2(source, target, graph)) tests3.append(bfs3(source, target, graph)) print("| {0} \t\t| {1} \t\t| {2}".format("bfs1", 2**i, sum(tests1))) print("| {0} \t\t| {1} \t\t| {2}".format("bfs2", 2**i, sum(tests2))) print("| {0} \t\t| {1} \t\t| {2}".format("bfs3", 2**i, sum(tests3)))
def main(): #G_no_path = nx.fast_gnp_random_graph(12, 0.1, 142443) #graph_no_path = nx.to_dict_of_lists(G_no_path) random.seed() print("| algorithm \t| total nodes \t| nodes searched") for i in range(3, 10): print() G = nx.fast_gnp_random_graph(2**i, random.uniform(0.0, 0.5), 142443) graph = nx.to_dict_of_lists(G) tests1 = [] tests2 = [] for n in range(123): while True: source = random.randrange(0, len(graph)-1) target = random.randrange(0, len(graph)-1) if source != target: break tests1.append(dfs1(graph, source, target)) tests2.append(dfs2(graph, source, target, set())) print("| {0} \t\t| {1} \t\t| {2}".format("dfs1", 2**i, sum(tests1))) print("| {0} \t\t| {1} \t\t| {2}".format("dfs2", 2**i, sum(tests2)))
while len(q) > 0: n = q.pop() # print n for node in G[n]: # if len(visited.intersection([node])) == 0 : # print visited if node not in visited: q.appendleft(node) # visited = visited.union(node) visited.append(node) if __name__ == "__main__": import matplotlib.pyplot as plt times = list() for index in xrange(1, 500, 10): G = nx.complete_graph(index) G = nx.to_dict_of_lists(G) print index t1 = time.time() BFS1(G) t12 = time.time() - t1 t2 = time.time() BFS2(G) t22 = time.time() - t2 times.append((t12, t22)) plt.plot([x for x, y in times], "-or") plt.plot([y for x, y in times], "-ob") plt.show()
def mystery(digraph): """ Question 1 For Questions 1-3, we will consider the following pseudo-code of Algorithm Mystery: :param digraph: """ result = {} for key, values in digraph.items(): flag = True for item in values: if item == 1: flag = False break if flag == True: result[key] = values return result if __name__ == '__main__': nx_graph = nx.fast_gnp_random_graph(10, 0.5, directed=False) digraph = nx.to_dict_of_lists(nx_graph) pprint(digraph) print('--------------------') question1 = mystery(digraph) pprint(question1)
def formatNetworkOutput(graph, output_folder, analysis_name, candidate_tf_list): ''' takes the networkx graph returns all figures, tables, etc ''' # output the network as a .ntx dictionary of lists networkFilename = output_folder + analysis_name + '.ntx' networkFile = open(networkFilename, 'w') networkDictOfLists = nx.to_dict_of_lists(graph) pickle.dump(networkDictOfLists, networkFile) # output the adjacency list and nodelist nodeFile = output_folder + analysis_name + '_NODELIST.txt' if nx.__version__[0] == '1': nodeList = [ [n] for n in graph.nodes_iter()] elif nx.__version__[0] == '2': nodeList = [[n] for n in graph.nodes()] else: print('ERROR: UNSUPPORTED VERSION OF NETWORKX MODULE') sys.exit() utils.unParseTable(nodeList, nodeFile, '\t') adjFile = output_folder + analysis_name + '_ADJ_LIST.txt' if nx.__version__[0] == '1': adjList = graph.adjacency_list() elif nx.__version__[0] == '2': adjList = [n[1].keys() for n in graph.adjacency()] else: print('ERROR: UNSUPPORTED VERSION OF NETWORKX MODULE') sys.exit() utils.unParseTable(adjList, adjFile, '\t') edgesTable = [['From', 'To']] targetList = [] for i,gene in enumerate(nodeList): for j in adjList[i]: newline = [gene[0],j] edgesTable.append(newline) TFname = gene[0] edgeFile = output_folder + analysis_name + '_EDGE_LIST.txt' utils.unParseTable(edgesTable, edgeFile, '\t') # Make the degree table degTable = [['Tf', 'In_Degree', 'Out_Degree', 'Total_Connections' ]] degFile = output_folder + analysis_name + '_DEGREE_TABLE.txt' for node in graph.nodes(): #shouldn't we output the table for the TFs that have motifs only ? for canidateMotifs in graph.nodes().... newline = [node, graph.in_degree()[node], graph.out_degree()[node], graph.degree()[node]] degTable.append(newline) utils.unParseTable(degTable, degFile, '\t') print 'DEFINING THE CORE REGULATORY CIRCUIT' autoreg = graph.selfloop_edges() selfLoops = [x for x,y in autoreg] selfLoopFile = output_folder + analysis_name + '_SELF_LOOPS.txt' utils.unParseTable(selfLoops, selfLoopFile, '') #recover bidirectional edges pairs = [] for n in selfLoops: for m in selfLoops: if n != m: if graph.has_edge(n,m) and graph.has_edge(m,n): pairs.append([n,m]) unDirGraph = nx.from_edgelist(pairs) cliqueGen = find_cliques_recursive(unDirGraph) cliqueList = list(cliqueGen) utils.unParseTable(cliqueList, output_folder + analysis_name + '_CLIQUES_ALL.txt', '\t') cliqueRanking = [] outDegreeDict = graph.out_degree() for c in cliqueList: score = 0 for gene in c: score += outDegreeDict[gene] score = score/len(c) if score > 0 and len(c) > 2: cliqueRanking.append((c, score)) sortCliqueRanking = sorted(cliqueRanking, reverse=True, key=lambda x:x[1]) cliqueFile = output_folder + analysis_name + '_CLIQUE_SCORES_DEGREE.txt' utils.unParseTable(sortCliqueRanking, cliqueFile, '\t') factorEnrichmentDict = {} for factor in selfLoops: factorEnrichmentDict[factor] = 0 for pair in cliqueRanking: c = pair[0] for factor in c: factorEnrichmentDict[factor] += 1 factorRankingTable = [] for factor in selfLoops: newline = [factor, factorEnrichmentDict[factor]/float(len(cliqueRanking))] factorRankingTable.append(newline) factorRankingFile = output_folder + analysis_name + '_ENRICHED_CLIQUE_FACTORS.txt' utils.unParseTable(factorRankingTable, factorRankingFile, '\t') # Begin VSA scoring # Initiate the graph G=nx.Graph() #recover bidirectional edges bidirectionalEdges = pairs #fill up the graph G.add_nodes_from(selfLoops) G.add_edges_from(bidirectionalEdges) #find all the cliques cliques = find_cliques_recursive(G) cliqueList = list(cliques) print 'Number of cliques:' print len(cliqueList) #count the occurences of the TFs accross the loops dicoTFinloopsCounts={} for clique in cliqueList: for TF in clique: if dicoTFinloopsCounts.has_key(TF): dicoTFinloopsCounts[TF]+=1 else: dicoTFinloopsCounts[TF]=1 #calculate a score by loop cliqueRanking = [] cliqueNub = 0 for clique in cliqueList: cliqueScore=0 for TF in clique: cliqueScore = (float(cliqueScore) + (float(dicoTFinloopsCounts[TF]))) cliqueRanking.append((clique, cliqueScore/len(clique), len(clique))) #print(cliqueRanking) sortCliqueRanking = sorted(cliqueRanking, reverse=True, key=lambda x:x[1]) #print(sortCliqueRanking) cliqueFile = output_folder + analysis_name + '_CLIQUE_SCORES_VSA.txt' utils.unParseTable(sortCliqueRanking, cliqueFile, '\t') print 'Top CRC:' print sortCliqueRanking[0]
# identify largest connected component Gcc=nx.connected_component_subgraphs(G) G0=Gcc[0] nx.draw_networkx_edges(G0,pos, with_labels=False, edge_color='r', width=6.0 ) # show other connected components for Gi in Gcc[1:]: if len(Gi)>1: nx.draw_networkx_edges(Gi,pos, with_labels=False, edge_color='r', alpha=0.3, width=5.0 ) # dump graph in human-readable form print "Generated binomial graph (n=%d, p=%5.4f):" % (n,p) for n,ns in nx.to_dict_of_lists(G).iteritems(): print " %s\t%s" % (n, ns) print # as well as a pickled form pickle.dump(G, file("graph.pickle", "w")) print "Created graph.pickle" plt.savefig("giant_component.png") #plt.show() # display print "Created giant_component.png"
# print "-" * 20 # print "strategies:" d = nx.closeness_centrality(G) sorted_centrality_nodes = sorted(d.keys(), key=lambda k: d[k], reverse=True) deg_centrality_nodes = sorted_centrality_nodes[:N] # d = nx.communicability_centrality(G) # sorted_centrality_nodes = sorted(d.keys(), key=lambda k: d[k], reverse=True) # comm_centrality_nodes = sorted_centrality_nodes[:N] d = betweenness_centrality.betweenness_centrality_parallel(G) sorted_centrality_nodes = sorted(d.keys(), key=lambda k: d[k], reverse=True) btwn_centrality_nodes = sorted_centrality_nodes[:N] # for node in btwn_centrality_nodes: # print node graph = nx.to_dict_of_lists(G) nodes = {"btwn_centrality": btwn_centrality_nodes, "closeness_centrality": deg_centrality_nodes} s = sim.run(graph, nodes) print s #d = nx.betweenness_centrality(G) #btwn = heap.nlargest(N, d, key = lambda k: d[k])
def pathify(self): ''' makes a path out of graph ''' # build stack of unverified components. stack = nx.weakly_connected_components(self) # loop until stack is empty. while len(stack) > 0: print len(stack) # pop off stack. comp = stack.pop() # skip singeltons. if len(comp) < 3: continue # create proper subgraph. subg1 = self.di_subgraph(nbunch=comp) # make input for mst graph. successors = nx.to_dict_of_lists(subg1) # make the MstGraph. mst_graph = MstGraph(successors) # calculate the MST. mst = mst_graph.mst() # build graph from MST. subg2 = nx.DiGraph() for edge in mst.iteredges(): subg2.add_edge(edge[0], edge[1], {'weight':-1}) # ensure its acylclic. if nx.is_directed_acyclic_graph(subg2) == False: logging.error("pathify: cycle in graph") sys.exit(1) # use dikstra all pairs to find backbone. paths = nx.all_pairs_dijkstra_path(subg2, weight='weight') # determine longest. longest_a = -1 longest_b = -1 for path in paths: if len(paths[path]) > longest_a: longest_a = len(paths[path]) longest_b = path path = paths[longest_b] # remove edges 1 adjacent to path. to_remove = [] for e in subg2.edges(): test1 = e[0] in path test2 = e[1] in path if test1 + test2 == 1: to_remove.append(e) print "removing" if self._is_linear(subg=subg2) == False: print sys.exit() # remove edges. subg2.remove_edges_from(to_remove) # calculate components. comps = nx.weakly_connected_components(subg1) # add unverified to stack. for comp in comps: # build new graph. subg2 = self.di_subgraph(nbunch=comp) # verify. if self._is_linear(subg=subg2) == False: stack.append(comp)