def set_graphs(self): self.original_graph = nx.Graph() dataset = open(self.dataset) for line in dataset.readlines(): line = line.strip() col = line.split(';') iid = "p%s" %(int(col[0])) self.original_graph.add_node(iid, {'bipartite': 0}) pid = int(col[1]) self.original_graph.add_node(pid, {'bipartite': 1}) self.original_graph.add_edge(iid, pid) dataset.close() nodes = set(n for n,d in self.original_graph.nodes(data=True) if d['bipartite'] == 1) self.graph_training = nx.Graph() for node in nodes: for intermediate_node in list(nx.all_neighbors(self.original_graph, node)): for second_node in list(set(nx.all_neighbors(self.original_graph, intermediate_node)) - set([node])): self.graph_training.add_edge(node, second_node) self.graph_test = self.graph_training.copy() if self.task == "missing_link_prediction" else self.get_future_link_graph() edges = set() if (self.task == "missing_link_prediction") and (self.sample_dataset != None): for line in [line for line in self.sample_dataset if line[2] == 1]: node_1, node_2 = int(line[0]), int(line[1]) try: self.graph_training.remove_edge(node_1, node_2) except: print node_1, node_2 pass
def cluster(G, E, x, y, j, WI): partition = [] while len(G.nodes()) > 0: vertices = set(G.nodes()) # G.nodes() currDegs = G.degree(G.nodes()) root = max(currDegs.iterkeys(), key=(lambda key: currDegs[key])) vertices.remove(root) V = { 0: [ root ] } El = { i: {} for i in E.keys() } # E_i[l] expanding = True l = 1 while expanding and len(vertices) > 0: V[l] = set() # [] for i in El.keys(): El[i][l] = set() # [] # find next layer of nodes for v in V[l-1]: for u in nx.all_neighbors(G, v): if u in vertices: V[l].add(u) vertices.remove(u) # find edges from V[l] to V[l] U v[l-1] for v in V[l]: for u in nx.all_neighbors(G, v): if u in V[l] or u in V[l-1]: # add edge to E_i[l] i = get_index_for_weight(G[u][v][0]['weight'], y, WI) El[i][l].add((u,v)) # check expansion expanding = False for i in El.keys(): if i > j: # ensure i <= j continue # index here by l (l+1 in paper) new_len = len(El[i][l]) # find the length of E_i[1] U ... U E_i[l-1] curr_len = len([ e for ell in xrange(1,l) for e in El[i][ell] ]) # if for any i, new_len > (1/x) * curr_len, # then we're still expanding if new_len > (1.0 / x) * curr_len: expanding = True break if expanding: l += 1 # create cluster cluster_nodes = tuple([ e for ell in xrange(l) for e in V[ell] ]) G.remove_nodes_from(cluster_nodes) partition.append(cluster_nodes) return partition
def get_sample(self): """ Extracts the whole sample from the graph representing the original dataset. """ Graph = nx.Graph() dataset = open(self.dataset) for line in dataset.readlines(): line = line.strip() col = line.split(';') iid = int(col[0]) Graph.add_node(iid, {'bipartite': 0}) pid = "p%s" %(int(col[1])) Graph.add_node(pid, {'bipartite': 1}) Graph.add_edge(iid, pid) dataset.close() self.original_graph = Graph.copy() nodes = set(n for n,d in Graph.nodes(data=True) if d['bipartite'] == 1) Gt = nx.Graph() for node in nodes: for intermediate_node in list(nx.all_neighbors(Graph, node)): for second_node in list(nx.all_neighbors(Graph, intermediate_node)): Gt.add_edge(node, second_node) self.graph_training = Gt.copy() self.graph_test = Gt.copy() if self.task == 1 else self.get_future_links_graph() self.get_positive_examples() self.get_negative_examples()
def get_graph_as_bow (g, h): ''' Get subgraph2vec sentences from the goven graph :param g: networkx graph :param h: WL kernel height :return: sentence of the format <target> <context1> <context2> ... ''' for n,d in g.nodes_iter(data=True): for i in xrange(0, h+1): center = d['relabel'][i] neis_labels_prev_deg = [] neis_labels_next_deg = [] if -1 != i-1: neis_labels_prev_deg = [g.node[nei]['relabel'][i-1] for nei in nx.all_neighbors(g, n)] + [d['relabel'][i-1]] NeisLabelsSameDeg = [g.node[nei]['relabel'][i] for nei in nx.all_neighbors(g,n)] if not i+1 > h: neis_labels_next_deg = [g.node[nei]['relabel'][i+1] for nei in nx.all_neighbors(g,n)] + [d['relabel'][i+1]] nei_list = NeisLabelsSameDeg + neis_labels_prev_deg + neis_labels_next_deg try: nei_list.append(d['relabel'][i-1]) #prev degree subgraph from the current node except: pass try: nei_list.append(d['relabel'][i+1]) #next degree subgraph from the current node except: pass nei_list = ' '.join (nei_list) sentence = center + ' ' + nei_list yield sentence
def computeNumberOfPathsOfLengthK(G): print 'Computing number of paths of length k for k=1 to number_of_nodes. This might take a while...' paths = set() for e in G.edges_iter(): paths.add(e) result = {} result[1] = G.number_of_edges() newPaths = set() length = 2 while len(paths) > 0: for path in paths: for neighb in nx.all_neighbors(G, path[0]): if neighb not in path: if neighb < path[-1]: newPaths.add((neighb,)+path) else: newPaths.add( ((neighb,)+path)[::-1] ) for neighb in nx.all_neighbors(G, path[-1]): if neighb not in path: if neighb < path[0]: newPaths.add( (path + (neighb,))[::-1] ) else: newPaths.add(path + (neighb,)) result[length] = len(newPaths) length += 1 paths = newPaths newPaths = set() return result
def get_graph_from_period(graph, t0,t0_): print "Starting generating graph from period", datetime.today() papers = list([n,d] for n,d in graph.nodes(data=True) if d['node_type'] == 'E' and d['time'] >= t0 and d['time'] <= t0_) print "Total Papers: ", len(papers), datetime.today() new_graph = networkx.Graph() new_graph.add_nodes_from(papers) autores = list([n,d] for n,d in graph.nodes(data=True) if d['node_type'] == 'N') new_graph.add_nodes_from(autores) element = 0 for paper in papers: element = element+1 FormatingDataSets.printProgressofEvents(element, len(papers), "Adding paper to new graph: ") authors = networkx.all_neighbors(graph, paper[0]) for author in authors: new_graph.add_edge(paper[0], author) element = 0 qtyautors= len(autores) for autor in autores: element = element+1 FormatingDataSets.printProgressofEvents(element,qtyautors, "Cleaning authors from new graph: ") if len(list(networkx.all_neighbors(new_graph,autor[0]))) == 0: new_graph.remove_node(autor[0]) print "Generating graph from period finished", datetime.today() return new_graph
def sort_weight_by_both(G_hu, G_origin): vm_active_weight_hu = {} vm_active_weight_origin = {} for vm in G_hu.nodes(): weight = 0 for edge in nx.all_neighbors(G_hu, vm): weight += G_hu[vm][edge]['weight'] vm_active_weight_hu[int(vm)] = weight weight = 0 vm = int(vm) for edge in nx.all_neighbors(G_origin, vm): weight += G_origin[vm][edge]['weight'] vm_active_weight_origin[vm] = weight #nodes_weight_hu = sorted(vm_active_weight_hu.iteritems(), key=lambda vm_active_weight_hu:vm_active_weight_hu[0], reverse=False) nodes_weight_origin = sorted(vm_active_weight_origin.iteritems(), key=lambda vm_active_weight_origin:vm_active_weight_origin[1], reverse=True) #print nodes_weight_hu nodes_weight = {} for node in nodes_weight_origin: if node[1] == 0: weight = 0 else: #print node[0] weight = vm_active_weight_hu[node[0]] / node[1] #print vm_active_weight_hu[node[0]], node[1] nodes_weight[node[0]] = weight nodes = sorted(nodes_weight.iteritems(), key=lambda nodes_weight:nodes_weight[1], reverse=True) print nodes return nodes
def bn(G, d1, d2): bns = set() for n in d1: for ne1 in nx.all_neighbors(G, n): if ne1 in d2: bns.add(n) bns.add(ne1) for ne2 in nx.all_neighbors(G, ne1): if ne2 in d2: bns.add(ne1) return bns
def edge_clustering(G): clusteringcoeffs = {} for s,t in G.edges(): c = 0 ns = set(nx.all_neighbors(G,s)) nt = set(nx.all_neighbors(G,t)) commons = ns.intersection(nt) ds = G.degree(s) dt = G.degree(t) c = (len(commons)+1)/float(min([ds,dt])) clusteringcoeffs[(s,t)] = c return clusteringcoeffs
def start(self, G): (parity, odds) = self.checkGraph(G) if parity != self.HALF and parity != self.FULL: return H = G.copy() euler_path = [] start_node = None if parity == self.HALF: start_node = odds[0] elif parity == self.FULL: start_node = H.nodes()[0] euler_path.append(start_node) neighbors = networkx.all_neighbors(H, start_node) last_node = start_node i = 1000 while H.number_of_edges() > 0 and i > 0: bridges = self.find_bridges(H) chosenNode = None neighbors = networkx.all_neighbors(H, last_node) wasNonBridge = False for node in neighbors: isBridge = self.isBridge(last_node, node, bridges) if isBridge is False: chosenNode = node wasNonBridge = True break if wasNonBridge is False: neighbors = networkx.all_neighbors(H, last_node) first = None for node in neighbors: first = node break chosenNode = first euler_path.append(chosenNode) H.remove_edge(last_node, chosenNode) if H.degree(last_node) == 0: H.remove_node(last_node) last_node = chosenNode i -= 1 return euler_path
def __edge_intersection_test(self, new_edge, join): a, b = new_edge for edge in self.graph.edges_iter(): if util.segment_intersection(edge, new_edge): if join and (edge[0] not in nx.all_neighbors(self.graph, a)) and (edge[1] not in nx.all_neighbors(self.graph, a)): proj = util.project_on_line(edge, b) c, d = edge self.graph.remove_edge(c, d) self.graph.add_edge(c, proj) self.graph.add_edge(proj, d) self.graph.add_edge(a, proj) return False return True
def neighbor_count_comorbid(network, alteration_type, icd_gene_clinical, cancer_info, comorbid_only = False, comorb_perm = False, weighted=False): tumor_data_list = alteration_type.keys() list2 = set() for icd in icd_gene_clinical: for cancer in tumor_data_list: rr = [icd_gene_clinical[icd]['cancer_assoc'][cancer_icd] for cancer_icd in cancer_info if cancer in cancer_info[cancer_icd]['TCGA']] if len(rr)>0 and rr[0] > 1: list2 |= set([cancer]) cancers = list2 mend_disease = mendelian_code.get_mendelian_diseases(icd_gene_clinical) #neighbor_count_mat = scipy.zeros([len(mend_disease), len(cancers)]) all_mend_gn = mendelian_code.get_mendelian_genes(icd_gene_clinical) #gn_count_mat = scipy.zeros([len(all_mend_gn), len(cancers)]) disease_score = pd.DataFrame(scipy.zeros([len(mend_disease), len(cancers)]), index = mend_disease, columns = cancers) gene_score = pd.DataFrame(scipy.zeros([len(all_mend_gn), len(cancers)]), index = all_mend_gn, columns = cancers) gene_connection = [['']*len(cancers) for i in range(len(all_mend_gn))] v = zip(*tuple([(c, canc) for canc in cancers for c in cancer_info if canc in cancer_info[c]['TCGA'] ])) canc_tab = dict(zip(v[1],v[0])) BIG_COUNT = 0 i = 0 for (m, md) in enumerate(mend_disease): mend_gn = icd_gene_clinical[md]['gene_omim'].keys() md_score = [0]*len(cancers) if len(mend_gn)==0: continue canc_gn = set() for (c, canc) in enumerate(cancers): if not canc in canc_tab: continue if comorbid_only and (not canc in canc_tab or icd_gene_clinical[md]['cancer_assoc'][canc_tab[canc]] < 1): continue i += 1 canc_gn |= alteration_type[canc][1] #pdb.set_trace() md_subgr = networkx.subgraph(network, set(mend_gn) | set(canc_gn)) for gn in mend_gn: if gn in md_subgr: if not weighted: gene_result = [neighbor for neighbor in networkx.all_neighbors(md_subgr,gn) if neighbor in canc_gn] BIG_COUNT += len(gene_result) else: BIG_COUNT += sum([md_subgr[gn][neighbor]['weight'] for neighbor in networkx.all_neighbors(md_subgr,gn) if neighbor in canc_gn]) #print 'tot pair: ' + str(i) + ' tot count: ' + str(BIG_COUNT) return BIG_COUNT
def __is_2_clun(self, nodes_pair): graph = self.graph for node_self in nodes_pair: # NOTE: 2clunにのみ対応. 2pathのみ検索 in_2_path_node_set = set() in_2_path_node_set.update(set(nx.all_neighbors(graph, node_self))) for neighbor in nx.all_neighbors(graph, node_self): in_2_path_node_set.update(set(nx.all_neighbors(graph, neighbor))) for node_target in nodes_pair: if node_target not in in_2_path_node_set and node_target != node_self: return False return True
def makeMobile( T, root, labels = 0, color = True ): ''' makeMobile( T, root, labels ) In: T is a tree with 'root' as root. labels is an integer (default 0). color is a boolean (default True). Out: T has been changed into a labeled mobile with root as root and the labels are... ... all 0 if labels == 0. ... selected deterministically if labels == 1. ... selected randomly if labels == 2. If color == True, then T has been colored by treeToMobile.colorMobile. ''' if color: # Initialise Square = dict(zip( T.nodes(), [ 'square' ] * len(T) )) Black = dict(zip( T.nodes(), [ 'black' ] * len(T) )) nx.set_node_attributes( T, 'shape', Square ) nx.set_node_attributes( T, 'color', Black ) # Make the root special. T.node[root][ 'color' ] = 'red' T.node[root][ 'shape' ] = 'circle' colorMobile( T, root ) # Initialise all labels to zero. Zero = dict(zip( T.nodes(), [0] * len(T) )) nx.set_node_attributes( T, 'label', Zero ) if labels == 1: for v in nx.all_neighbors( T, root ): labelMobileDet( T, v, root ) elif labels == 2: for v in nx.all_neighbors( T, root ): labelMobileRand( T, v, root )
def opinion_update(g): # Update opinions for i in g.nodes(): Ni = nx.all_neighbors(g, i) # summation of neighbors weighted_opinion = 0 weights = 0 for j in Ni: weighted_opinion = weighted_opinion + w[i][j]* g.node[j]['x'] weights = weights + w[i][j] # Only update opinion if not a curmudgeon if(g.node[i]['c'] == False): g.node[i]['x'] = (g.node[i]['x'] * w[i][i] + weighted_opinion)/(w[i][i] + weights) # Update weights e = g.edges() for i in e: exp_value = math.pow((g.node[i[0]]['x'] - g.node[i[1]]['x']),2) T = math.exp(-exp_value/g.node[i[0]]['h']) w[i[0]][i[1]] = (w[i[0]][i[1]] + r*T)/(1 + r) # If a curmudegeon then weight is always 0 if(g.node[i[0]]['c'] == True): w[i[0]][i[1]] = 0
def related_degree(self,node,tag): count = 0 for nd in nx.all_neighbors(self.G,node): if self.G.node[nd]['tag'] == tag: count = count + 1 return count
def walk(G, start): global step, maxSteps, degree_dict, path; #print type(nx.all_neighbors(G, start)) ; node_dict = {} ; for node in nx.all_neighbors(G, start): #degree = str(G.degree(node)) ; node_dict[str(node)] = 1 ; step = step + 1 ; #if( step >= maxSteps or start == end_node ): return ; if( step >= maxSteps ): provenance_dict[",".join(path)] = 1 ; return ; stop_walk_pro = np.random.random_sample() ; if( stop_walk_pro <= 0.1 ): walk(G, start) ; # it has the 10% rate to stop else: node = random.choice(node_dict.keys()) ; #node = select_random(node_dict, 1)[0] ; path.append(node) ; #print "add edge %s %s" % (start, node) ; walk(G, node) ;
def get_random_path(graph, K=100): """Pick one of top 10 most-connected nodes as start, get a random neighbor, repeat up to K times to build a random path. Returns path nodes (as ids), path edges (as id tuples), all explored nodes and all explored edges.""" nodes = graph.nodes() nodes = sorted(nodes, key=graph.degree, reverse=True) start_node = nodes[randint(0,min(10,len(nodes)))] print('start node has degree %d'%(graph.degree(start_node))) path_nodes = [] explored_nodes = set([start_node]) last_node = start_node explored_nodes.add(start_node) K = min(K, len(nodes)) for i in range(1,K): neighbors = [n for n in all_neighbors(graph, last_node)] new_nodes = set(neighbors).difference(set(path_nodes)) if not new_nodes: print('finish with node %s with neighbors %s and explored_nodes %s'% (last_node, str(neighbors), str(path_nodes))) break explored_nodes = explored_nodes.union(new_nodes) new_nodes = sorted(list(new_nodes), key=graph.degree, reverse=True) neighbor = new_nodes[randint(0,min(2,len(new_nodes)-1))] path_edges.append((last_node, neighbor)) last_node = neighbor path_nodes.append(last_node) return path_nodes, explored_nodes
def get_cluster_coefficients(G): """ Computes the cluster coefficient of the nodes of the given digraph """ nodes=G.nodes() coefs=[] for node in nodes: #COMPUTE CLUSTER COEFFICIENT #Get number of connections between neighbours neighbors=[neig for neig in nx.all_neighbors(G, node)] edges=G.out_edges(neighbors) nConn=0 for e in edges: if (e[1] in neighbors): nConn+=1 #Get max possible numeber of connections maxConn=(len(neighbors))*(len(neighbors)-1) #Get coef if(maxConn!=0): coef=float(nConn)/float(maxConn) #ADD TO THE LIST coefs.append(coef) return coefs
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""" nodes = nx.nodes(game.network) nodes.sort(key=lambda x : nx.degree(game.network, x), reverse=True) selections = set() for node in nodes: adjacents = list(nx.all_neighbors(game.network, node)) for adj_node in adjacents[:2]: selections.add(adj_node) if len(selections) == game.num_seeds: break if len(selections) == game.num_seeds: break assert len(selections) == game.num_seeds return list(selections)
def handlePacketIn(self, rpc): packet = rpc.Extensions[PacketInRequest.msg].packet srcV = rpc.Extensions[PacketInRequest.msg].srcV cookie = rpc.Extensions[PacketInRequest.msg].cookie eth = dpkt.ethernet.Ethernet( packet ) ip6 = eth.data srcAddrInt = int((ip6.src).encode('hex'), 16) dstAddrInt = int((ip6.dst).encode('hex'), 16) paths = bytearray(ip6.dst) nextHop = paths.pop(0) print 'next hop:', nextHop eth.data.dst = str(paths) self.mapping[ip6.src] = srcV self.rMapping[srcV] = srcAddrInt dstAddr = IPv6Address( dstAddrInt ) rpc = RPC() rpc.type = RPC.DataPush update = rpc.Extensions[DataPush.msg] try: fwdV_it = networkx.all_neighbors(self.graph, str(nextHop)) fwdV = fwdV_it.next() except: return update.srcV = int(fwdV) update.dstV = int(nextHop) update.data = str(eth) self.sendToController( rpc.SerializeToString() )
def add_commonality(graf): commonality = {} print "Adding commonality", a for edge in graf.edges_iter(): node1 = edge[0] node2 = edge[1] neighbaours1 = nx.all_neighbors(graf, node1) neighbaours2 = nx.all_neighbors(graf, node2) set1 = set(neighbaours1) set2 = set(neighbaours2) k = len(set1.intersection(set2)) n = len(set1) m = len(set2) com = (k + 1) / (sqrt(n * m)) commonality[edge] = com nx.set_edge_attributes(graf, "com", commonality)
def estimate_joint_dist(graph, nsteps): assert(not nx.is_directed(graph)) assert('labeled' in graph.graph and graph.graph['labeled']) n = nsteps #total num seen n_iod = {} #total seen with indeg i, outdeg o, deg d # random initial node; don't include in estimator node = random.choice(graph.nodes()) # rw for i in xrange(nsteps): node = random.choice(list(nx.all_neighbors(graph, node))) iod_tuple = (graph.node[node]['in-degree'], graph.node[node]['out-degree'], graph.node[node]['degree']) n_iod[iod_tuple] = n_iod.get(iod_tuple,0) + 1 # degree distribution parameters max_indeg = max([graph.node[k]['in-degree'] for k in graph.node.keys()]) max_outdeg = max([graph.node[k]['out-degree'] for k in graph.node.keys()]) deg_par = np.zeros((max_indeg + 1, max_outdeg + 1)) for (indeg, outdeg, deg) in n_iod.keys(): val = n_iod[(indeg, outdeg, deg)] deg_par[indeg, outdeg] += float(val) / float(n * deg) #deg_par[indeg, outdeg] += float(val) / float(deg) # normalize deg_par /= deg_par.sum() np.savetxt("deg_par.csv", deg_par, delimiter=",") return deg_par
def calculate(g, voltage): edges_num = nx.number_of_edges(g) # sort nodes in edges edges = [edge if edge[0] < edge[1] else (edge[1], edge[0]) for edge in nx.edges(g)] a = np.zeros((edges_num, edges_num)) b = np.zeros((edges_num, 1)) i = 0 # first law for node in [node for node in nx.nodes(g) if node != 0]: for neighbor in nx.all_neighbors(g, node): edge = tuple(sorted((node, neighbor))) a[i][edges.index(edge)] = 1 if neighbor < node else -1 i += 1 # second law cycles = nx.cycle_basis(g, 0) for cycle in cycles: for j in range(0, len(cycle)): node = cycle[j] next_node = cycle[(j + 1) % len(cycle)] edge = tuple(sorted((node, next_node))) resistance = g[node][next_node]['weight'] a[i][edges.index(edge)] = resistance if node < next_node else -resistance if 0 in cycle: b[i] = voltage i += 1 # solve x = np.linalg.solve(a, b) for (x1, x2), res in zip(edges, x): g[x1][x2]['current'] = res[0]
def biggestBlueChild( node, T, M ): ''' Used by treeBijection blueChild = biggestBlueChild( node, T, M ) In: T is a tree with integer vertices and M is a forest with the same set of vertices. node is a vertex in T and M. If node has a parent in T, then that parent is red in M. Out: blueChild is the largest child of node that has its 'color' attribute not set to 'red' ''' # Note that None < x for all numbers x. blueChild = None for v in nx.all_neighbors( T, node ): if M.node[v][ 'color' ] != 'red' and blueChild < v: blueChild = v return blueChild
def ramsey_R2(graph): r"""Approximately computes the Ramsey number `R(2;s,t)` for graph. Parameters ---------- graph : NetworkX graph Undirected graph Returns ------- max_pair : (set, set) tuple Maximum clique, Maximum independent set. """ if not graph: return (set([]), set([])) node = next(graph.nodes_iter()) nbrs = nx.all_neighbors(graph, node) nnbrs = nx.non_neighbors(graph, node) c_1, i_1 = ramsey_R2(graph.subgraph(nbrs)) c_2, i_2 = ramsey_R2(graph.subgraph(nnbrs)) c_1.add(node) i_2.add(node) return (max([c_1, c_2]), max([i_1, i_2]))
def show_connections(G,pos,neg): for x in pos: try: tmp = nx.all_neighbors(G,x[0]) for y in tmp: print y except: print "Not Found" for y in neg: try: tmp = nx.all_neighbors(G,x[0]) for y in tmp: print y except: print "Not Found"
def colorMobile( M, node ): ''' colorMobile( M, node ) In: M is a tree and node is a vertex in M. Either M.node[node][ 'shape' ] == 'circle' or M.node[node][ 'shape' ] == 'point' Out: The 'shape' attributes of all descendants of node have been altered, such that every second generation has the same 'shape' as node and the other generations have the "opposite" 'shape'. Ironically, the 'color' attribute has not been changed. ''' if M.node[node][ 'shape' ] == 'point': shape = 'circle' else: shape = 'point' for v in nx.all_neighbors( M, node ): # If we have not already, we color v and all its descendants. if not M.node[v][ 'shape' ] == shape: M.node[v][ 'shape' ] = shape colorMobile( M, v )
def child_explore(node, current_path): global done neighbours = list(nx.all_neighbors(G, node)) # print "Child starting node", node current_path.append(node) for node in current_path: visited.add(node) #this will include the current node since it just got appended # print "Child", rank, "is seeing visited as", visited # print "Child", rank, "is seeing current path as", current_path for n in neighbours: if n not in visited: if (child_explore(n, current_path)): return True if len(current_path) == nx.number_of_nodes(G) and original_node in neighbours: # comm.barrier() return True current_path.remove(node) visited.remove(node) #backtracking # comm.barrier() if done: print "Process",rank,"exiting" Finalize() return False
def search_motifs_with_n(self, n): graph = self.graph all_nodes = nx.nodes(graph) motif_counter = defaultdict(int) # 全組み合わせ探索 for nodes_pair in list(itertools.combinations(all_nodes, n)): degrees_in_pair = [] for node_self in nodes_pair: degree_in_pair = 0 for node_target in nodes_pair: if node_self == node_target: continue if node_target in nx.all_neighbors(graph, node_self): degree_in_pair += 1 degrees_in_pair.append(degree_in_pair) # 孤立ノードがあれば飛ばす if 0 in degrees_in_pair: continue # エッジが足りてなければ飛ばす if sum(degrees_in_pair) / 2 < n -1: continue # NOTE: 飛ばしきれてない. 本当は繋がってる判定が必要 score = sum([v * v for v in degrees_in_pair]) motif_counter[score] += 1 return motif_counter
def GWMIN2(Graph): start_time = time.time() G = copy.deepcopy(Graph) MIS = list() while G.number_of_nodes() != 0: remove = list() if len(G.edges()) == 0: for node in G.nodes(): MIS.append(node) remove.append(node) for node in remove: G.remove_node(node) else: weight = dict() for node in G.nodes(): w = G.nodes[node]['weight'] neighbors = nx.all_neighbors(G, node) s = 0 for neighbor in neighbors: s += G.nodes[neighbor]['weight'] if s == 0: MIS.append(node) else: weight[node] = w / s target = sorted(weight.items(), key=lambda x: x[1], reverse=True)[0][0] MIS.append(target) neighbors = nx.all_neighbors(G, target) G.remove_node(target) for i in neighbors: G.remove_node(i) for node in MIS: if node in G: G.remove_node(node) s = 0 for i in MIS: s += Graph.nodes[i]['weight'] end_time = time.time() all_time = round(end_time - start_time, 2) return MIS, s, all_time
def processPrd(G, radius): prm_cur = "prm" + str(radius - 1) prdId_nxt = "prd" + str(radius) prevPrm = nx.get_node_attributes(G, prm_cur) # claculate prime product prdList = {v : reduce(lambda x,y:x*y, map(lambda x: prevPrm[x], \ nx.all_neighbors(G, v)),1)*prevPrm[v]**2 for v in G.nodes()} nx.set_node_attributes(G, prdId_nxt, prdList) all_features = set(prdList.values()) return all_features, G
def _prepare_pce_input_files(graph, realId_to_pceId_map, pceId_to_realId_map): with open(PCE_INPUT_PATH, 'w') as out_file, open(REVERSE_ID_PATH, 'w') as reverse_id_out_file: for node in sorted(graph.nodes()): reverse_id_out_file.write(str(node)) line_to_write = ','.join([ str(realId_to_pceId_map[neighbor]) for neighbor in nx.all_neighbors(graph, node) ]) out_file.write(line_to_write + '\n') reverse_id_out_file.write('\n')
def update_winner(self, curnode): """.""" # find nearest unit and second nearest unit winner1, winner2 = self.determine_2closest_vertices(curnode) winnernode = winner1[0] winnernode2 = winner2[0] win_dist_from_node = winner1[1] errorvectors = nx.get_node_attributes(self.graph, 'error') error1 = errorvectors[winner1[0]] # update the new error newerror = error1 + win_dist_from_node**2 self.graph.add_node(winnernode, error=newerror) # move the winner node towards current node self.pos = nx.get_node_attributes(self.graph, 'pos') newposition = self.get_new_position(self.pos[winnernode], curnode) self.graph.add_node(winnernode, pos=newposition) # now update all the neighbors distances and their ages neighbors = nx.all_neighbors(self.graph, winnernode) age_of_edges = nx.get_edge_attributes(self.graph, 'age') for n in neighbors: newposition = self.get_new_position_neighbors(self.pos[n], curnode) self.graph.add_node(n, pos=newposition) key = (int(winnernode), n) if key in age_of_edges: newage = age_of_edges[(int(winnernode), n)] + 1 else: newage = age_of_edges[(n, int(winnernode))] + 1 self.graph.add_edge(winnernode, n, age=newage) # no sense in what I am writing here, but with algorithm it goes perfect # if winnner and 2nd winner are connected, update their age to zero if (self.graph.get_edge_data(winnernode, winnernode2) is not None): self.graph.add_edge(winnernode, winnernode2, age=0) else: # else create an edge between them self.graph.add_edge(winnernode, winnernode2, age=0) # if there are ages more than maximum allowed age, remove them age_of_edges = nx.get_edge_attributes(self.graph, 'age') for edge, age in iteritems(age_of_edges): if age > self.max_age: self.graph.remove_edge(edge[0], edge[1]) # if it causes isolated vertix, remove that vertex as well for node in self.graph.nodes(): if not self.graph.neighbors(node): self.graph.remove_node(node)
def new_WS(N, K, p): """Watts-Strogatz network.""" # It must satisfy that N >> K >> log(N) >> 1 G = _new_empty_G(N) _make_ring_lattice(G, K) nodes = nx.nodes(G) for i in nodes: neighbors = nx.all_neighbors(G, i) forbidden = list() forbidden.append(i) [forbidden.append(j) for j in neighbors] for j in nx.all_neighbors(G,i): if i < j: prob = np.random.random() if prob > 1 - p: new_j = np.random.randint(0, N) while new_j in forbidden: new_j = np.random.randint(0, N) G = _update_G(G, (i, j, new_j)) break return G
def get_t_neighbors(g, x, y): """Returns possible members of conditioning set for x---y edge consideration""" t = set([]) all_y_neighbors = set(nx.all_neighbors(g, y)) for z in all_y_neighbors: if has_undir_edge(g, z, y): if adjacent(g, z, x): continue t.add(z) return t
def run(self,action ,g , car =[]): map = g #speed = car[3] time = 0 neighbor = [] left_distance = car[5] check = False car_position = car[2] action = int(action) for i in nx.all_neighbors(map,car_position): neighbor.append(i) neighbor = list(sorted(set(neighbor))) current_position = car_position if action == 0: #up if ((car_position-neighbor[0])>1): check = True next_position = neighbor[0] time,left_distance = road().time(car,next_position,g) car_position = next_position else: check = False if action == 1: #down if ((car_position-neighbor[-1])<-1) : next_position = neighbor[-1] time,left_distance = road().time(car,next_position,g) car_position = next_position check =True else: check = False if action == 2: #left if ((car_position-1) in neighbor) : next_position = car_position -1 time,left_distance = road().time(car,next_position,g) car_position = next_position check =True else: check = False if action == 3: #right if ((car_position+1) in neighbor): next_position = car_position+1 time,left_distance = road().time(car,next_position,g) car_position = next_position check = True else: check = False car[2] = car_position car[5] = left_distance return check,car,current_position,time
def neighbors_subgraph(self, node_list): '''subgraph including node_list and ANY neighbors this function returns a subgraph of itself, consisting of the nodes in node_list AND any node that is a neighbor of ANY of them.''' neighbors = list() for node in node_list: neighbors.append(nx.all_neighbors(self, node)) return nx.subgraph(self, neighbors)
def idea2(G): """ Use betweenness centrality sum of a-p-s-t that pass through v Concept: The higher the b-c is for a vertex, the more weight it contributes Idea: Greedily remove vertices that have high b-c but retain the connectedness of the graph Drawback: May leave unconnected - then still need to remove vertices """ n = G.number_of_nodes() # True if the node is in the Tree or touching a node in the tree satisfied = [False for i in range(n)] used = [False for i in range(n)] bc = nx.betweenness_centrality(G, n) # Nodes sorted by their betweenness centrality list_bc = [[k, v] for k, v in bc.items()] bc_order = list(sorted(list_bc, key=lambda x: x[1], reverse=True)) H = G.copy() curr = 0 no_used = 1 while not all(satisfied): curr_node = bc_order[curr] neighbors = list(nx.all_neighbors(G, curr_node[0])) neighbor_used = 0 for neighbor in neighbors: if used[neighbor]: neighbor_used += 1 used[curr_node[0]] = True bc_order.remove(curr_node) satisfied[curr_node[0]] = True for neighbor in neighbors: satisfied[neighbor] = True curr -= 1 curr += 1 if curr >= len(bc_order): curr = 0 no_used += 1 H = G.copy() for index, val in enumerate(used): if not val: F = H.copy() F.remove_node(index) if nx.is_connected(F): H.remove_node(index) assert nx.is_connected(H), "H is not connected" return mwrc_approx(H)
def neighbors(self, exclusion=None): """Get all neighbors with a given exclusion. Return iterator over all neighboring nodes without the given exclusion node. Positional arguments: exclusion -- the exclusion node """ if exclusion is None: return nx.all_neighbors(self.graph, self) else: # Build iterator set iterator = (exclusion,) \ if not isinstance(exclusion, list) else exclusion # Return neighbors excluding iterator set return (n for n in nx.all_neighbors(self.graph, self) if n not in iterator)
def change_set_to_roles(files, git_dir, roles_dirs, playbooks_dirs, graph): """ Converts change set consisting of a number of files to the roles that they represent/contain. Input: files: A list of files modified by a commit range. git_dir: A path to the top-most directory in the local git repository tool is to be run in. roles_dirs: A list of relative paths to directories in which Ansible roles reside. playbook_dirs: A list of relative paths to directories in which Ansible playbooks reside. graph: A networkx digraph that is used to map Ansible dependencies. """ # set of roles items = set() # for all directories containing roles for role_dir in roles_dirs: role_dir_path = pathlib2.Path(git_dir, role_dir) # get all files in the directories containing roles (i.e. all the roles in that directory) candidate_files = {f for f in role_dir_path.glob("**/*")} # for all the files in the change set for f in files: file_path = pathlib2.Path(git_dir, f) # if the change set file is in the set of role files if file_path in candidate_files: # get name of role and add it to set of roles of the change set items.add(_get_resource_name(file_path, "roles")) # for all directories containing playbooks for play_dir in playbooks_dirs: play_dir_path = pathlib2.Path(git_dir, play_dir) # get all files in directory containing playbook that end with yml extension # (i.e. all playbooks in that directory) candidate_files = {f for f in play_dir_path.glob("*.yml")} # for all filse in the change set for f in files: file_path = pathlib2.Path(git_dir, f) # if the change set file is in teh set of playbook files if file_path in candidate_files: # gets first level of children of playbook in graph, which represents # all roles the playbook uses descendants = nx.all_neighbors(graph, (file_path.stem, "aws_playbook")) # adds all the roles that a playbook uses to set of roles of the change set items |= {desc.name for desc in descendants} return items
def get_subgraph(node, G): neighbors = nx.all_neighbors(G, node) sg = nx.DiGraph() sg.add_node(node, **G.nodes[node]) for n in neighbors: sg.add_node(n, **G.nodes[n]) if G.has_edge(node, n): sg.add_edge(node, n) else: sg.add_edge(n, node) return sg
def get_neighbors(self, node: str, dist=1, type_blacklist=None): if node not in self.g.nodes: return [] neighbors = set([node]) for _ in range(dist): for n in list(neighbors): for m in nx.all_neighbors(self.g, n): if type_blacklist is not None and self.get_node_attr( m, 'type') in type_blacklist: continue neighbors.add(m) return neighbors
def mostUniqueFriends(graph, adjacents): max = 0 maxNode = None for node in graph.nodes: uniqueFriends = 0 for v in list(nx.all_neighbors(graph, node)): if v not in adjacents: uniqueFriends += 1 if uniqueFriends > max: max = uniqueFriends maxNode = node return maxNode
def write_routers_to_interfaces(self): in_routers = nx.get_node_attributes(self.graph, 'in_routers') num_inputs = len(in_routers) self.file_handler.write("\n// Send out packets to Interfaces\n") tmp_list = list(set(self.in_routers)) bandwidth = self.args.bw delay = self.args.delay drop = self.args.loss queue_length = 1000 for i in range(num_inputs): neighs = list(nx.all_neighbors(self.graph, str(tmp_list[i]))) neighs.sort(key=lambda x: int(re.search('[0-9]+', x).group(0))) counter = 0 for neigh in neighs: if re.match("[oe][0-9]+", neigh): if not self.arp_less: inputs = in_routers[str(tmp_list[i])] iface = int( re.search('[0-9]+', inputs[counter]).group(0)) if self.args.in_constraints: self.file_handler.write( "link_out_%d_queue :: ThreadSafeQueue(%d);\n" % (iface, queue_length)) self.file_handler.write( "link_out_%d_bw :: LinkUnqueue(%s, %s);\n" % (iface, delay, bandwidth)) self.file_handler.write( "link_out_%d_loss :: RandomSample(DROP %s);\n" % (iface, drop)) self.file_handler.write( "router%d[%d] -> r%dttl_out_%s -> link_out_%d_queue -> CoDel() " "-> link_out_%d_loss -> link_out_%d_bw -> arpq%d;\n" % (tmp_list[i], neighs.index(neigh), tmp_list[i], neigh, iface, iface, iface, iface)) else: self.file_handler.write( "router%d[%d] -> r%dttl_out_%s -> arpq%d;\n" % (tmp_list[i], neighs.index(neigh), tmp_list[i], neigh, int( re.search('[0-9]+', inputs[counter]).group(0)))) counter = counter + 1 else: # fix this later, see above self.file_handler.write( "router%d[%d] -> r%dttl_out_%s -> al%d -> out%d;\n" % (self.in_routers[i], neighs.index(neigh), self.in_routers[i], neigh, i + 1, i + 1))
def get_vicinities(self): """ returns all the substructures that consists of a center atom and the attaching atoms as the data strucure as: [(0, [1, 2, 3]), (1, [3, 4, 5]), (2, [3, 4, 5]), ... ] """ return [(j, [i for i in nx.all_neighbors(self.graph, j)]) for j in self.graph.nodes()]
def neighbors(self, node, depth, visited=[]): vis = visited.copy() if depth >= 0: for i in self.stations: if node == i: yield i break vis.append(node) for i in nx.all_neighbors(self.stations, node): if i not in vis: for j in self.neighbors(i, depth - 1, vis): yield j
def approximation_greedy_vc(g): v = nx.number_of_nodes(g) e = nx.number_of_edges(g) edge_set = set() vertex_set = set() degree_of_node = dict({}) for node in nx.nodes(g): deg = nx.degree(g, node) if deg in degree_of_node: degree_of_node[deg].append(node) else: degree_of_node[deg] = [node] # print ("degree of %d is %d" % (node, nx.degree(g, node))) """ for key in degree_of_node: print ("degree %d : " % key) for node in degree_of_node[key]: print ",", node """ sorted_degree = [0] for i in range(1, v + 1): sorted_degree.append(nx.degree(g, i)) # print 'node ', i # print 'degree', sorted_degree[i] while len(edge_set) != 2 * e: current_max = find_max(sorted_degree) if degree_of_node[current_max] is None or len(degree_of_node[current_max]) == 0: current_max -= 1 continue nodes_with_current_degree = degree_of_node[current_max] random_choice = random.randint(0, len(nodes_with_current_degree) - 1) # [0, len-1] chosen_node = nodes_with_current_degree[random_choice] vertex_set.add(chosen_node) for neighbor in nx.all_neighbors(g, chosen_node): if neighbor not in vertex_set: edge_set.update([(chosen_node, neighbor), (neighbor, chosen_node)]) degree_of_node[sorted_degree[neighbor]].remove(neighbor) # remove sorted_degree[neighbor] -= 1 if sorted_degree[neighbor] not in degree_of_node: degree_of_node[sorted_degree[neighbor]] = [] degree_of_node[sorted_degree[neighbor]].append(neighbor) # add degree_of_node[current_max].remove(chosen_node) sorted_degree[chosen_node] = 0 return vertex_set
def diam(network): neighbors = [] for i in nx.all_neighbors(network,'newcomer'): neighbors.append(('newcomer',i)) Gc = max(nx.connected_component_subgraphs(network), key=len) diam_before = nx.diameter(Gc) network.remove_node('newcomer') Gc = max(nx.connected_component_subgraphs(network), key=len) diam_after = nx.diameter(Gc) network.add_node('newcomer') network.add_edges_from(neighbors) return [diam_before,diam_after]
def myHeuristic(graph, budget): hosts = [] # a set of nodes that are adjacent to a host adjacents = set([]) while len(hosts) < budget: v = mostUniqueFriends(graph, adjacents) if v not in hosts: for neighbor in list(nx.all_neighbors(graph, v)): adjacents.add(neighbor) adjacents.add(v) hosts.append(v) return hosts
def updateCost(nodev, G, NodeCluster, cost, Cluster, maxcluster): #更新节点的cost ci = set() #记录nodev可能连接的集团的ID集合 for vj in nx.all_neighbors(G, nodev): if NodeCluster[vj] != 0: #vj已属于某个集团 ci.add(NodeCluster[vj]) sumc = 1 for name in ci: sumc = sumc + len(Cluster[name]) if sumc < maxcluster: sumc = maxcluster cost[nodev] = sumc return cost[nodev]
def computeOneFile(file, aln_net): global max_clique_dict global full_score_dict file_root = file.split('/')[-1].split('.')[0] score_dict = prepareScoreDict(file_root) coev_net = nx.read_graphml(file) cliques = list(nx.find_cliques(coev_net)) max_clique_dict[file_root] = nx.graph_number_of_cliques(coev_net) # print file_root # print max_clique_dict[file_root] for clique in cliques: flag = False res_count_dict = {} for res in clique: if res in list(nx.nodes(aln_net)): for neighbor in list(nx.all_neighbors(aln_net, res)): prot = neighbor.split('-')[0] if prot not in res_count_dict.keys(): res_count_dict[prot] = 1 else: res_count_dict[prot] += 1 else: flag = True break if flag: max_clique_dict[file_root] -= 1 continue for key, value in res_count_dict.items(): if value == len(clique): if np.isnan(score_dict[key]): score_dict[key] = 1 else: score_dict[key] += 1 full_score_dict[file_root] = score_dict
def compute_num_triangles(g): # This is Prob. 3-e. ''' Read a NetworkX graph to count the total number of triangles. Parameters ---------- g: `NetworkX graph` A parsed NetworkX graph. Returns ------- triangle: `int` The total number of triangles(transitive triad) of the NetworkX graph. ''' import networkx as nx import numpy as np k = g.copy() count = 0 pathlist = [] component_size = [] k.remove_nodes_from(list(nx.isolates(k))) connected_components = nx.connected_component_subgraphs(k) total_list = [] for component in connected_components: if nx.number_of_nodes(component) >= 3: for items1 in component: for items2 in nx.all_neighbors(component, items1): for items3 in nx.all_neighbors(component, items2): if items1 in list(nx.all_neighbors(component, items3)): three_list = [] three_list.append(int(items1)) three_list.append(int(items2)) three_list.append(int(items3)) three_list.sort() total_list.append(three_list) total_list = np.array(total_list) total_new_list = np.unique(total_list, axis=0) triangle = total_new_list.shape[0] return triangle
def write_links(self): self.file_handler.write("\n// Links\n") if __NX_VERSION__ > 1: nodes = list(nx.nodes(self.graph).keys()) else: nodes = list(nx.nodes(self.graph)) nodes.sort() use_codel = self.args.use_codel for node in nodes: if re.match("[oe][0-9]+", node): continue neighbors = list(nx.all_neighbors(self.graph, node)) neighbors.sort(key=lambda x: int(re.search('[0-9]+', x).group(0))) for neigh in neighbors: if re.match("[oe][0-9]+", neigh): continue pull_elements = self.graph[node][neigh]['l_elements'] push_elements = self.graph[node][neigh]['s_elements'] push_str = "->" pull_str = "->" tee_str = "->" codel = "->" if use_codel: codel = "-> CoDel() ->" for element in pull_elements: tokens = element.split("(") element_short = get_capital_letters(tokens[0]) pull_str = "%s link_%s_%s_%s ->" % (pull_str, node, neigh, element_short) for element in push_elements: tokens = element.split("(") element_short = get_capital_letters(tokens[0]) push_str = "%s link_%s_%s_%s ->" % (push_str, node, neigh, element_short) if 'tee' in self.graph[node][neigh]: tee_str = "-> link_%s_%s_tee ->" % (node, neigh) self.file_handler.write( "router%s[%d] -> r%sttl_%s %s SetTimestamp(FIRST true) -> link_%s_%s_queue " "%s link_%s_%s_loss %s link_%s_%s_bw %s router%s\n" % (node, neighbors.index(neigh), node, neigh, push_str, node, neigh, codel, node, neigh, pull_str, node, neigh, tee_str, neigh))
def _get_motif4_sub_tree(self, root): visited_vertices = {root: 0} # visited_index = 1 # variation == (1, 1, 1) neighbors_first_deg = set(nx.all_neighbors(self._gnx, root)) # neighbors_first_deg, visited_neighbors, len_a = tee(neighbors_first_deg, 3) neighbors_first_deg = visited_neighbors = list(neighbors_first_deg) for n1 in visited_neighbors: visited_vertices[n1] = 1 for n1, n2, n3 in combinations(neighbors_first_deg, 3): yield [root, n1, n2, n3] for n1 in neighbors_first_deg: neighbors_sec_deg = set(nx.all_neighbors(self._gnx, n1)) # neighbors_sec_deg, visited_neighbors, len_b = tee(neighbors_sec_deg, 3) neighbors_sec_deg = visited_neighbors = list(neighbors_sec_deg) for n in visited_neighbors: if n not in visited_vertices: visited_vertices[n] = 2 for n2 in neighbors_sec_deg: for n11 in neighbors_first_deg: if visited_vertices[n2] == 2 and n1 != n11: yield [root, n1, n11, n2] for comb in combinations(neighbors_sec_deg, 2): if 2 == visited_vertices[comb[0]] and visited_vertices[comb[1]] == 2: yield [root, n1, comb[0], comb[1]] for n2 in neighbors_sec_deg: for n3 in set(nx.all_neighbors(self._gnx, n2)): if n3 not in visited_vertices: visited_vertices[n3] = 3 if visited_vertices[n2] == 2: yield [root, n1, n2, n3] else: if visited_vertices[n3] == 3 and visited_vertices[n2] == 2: yield [root, n1, n2, n3]
def sort_weight_by(G_x): vm_active_weight = {} for vm in G_x.nodes(): weight = 0 for edge in nx.all_neighbors(G_x, vm): weight += G_x[vm][edge]['weight'] vm_active_weight[vm] = weight nodes_weight = sorted(vm_active_weight.iteritems(), key=lambda vm_active_weight: vm_active_weight[1], reverse=True) return nodes_weight
def discount_neighbor(G, v, centrality_dict, discount_scale): ''' Discount all neighbors of v if v is chosen into the seed set, given by the neighborhood centrality. ''' updated_centralities = centrality_dict.copy() seen = set() seen.add(v) for neighbor in nx.all_neighbors(G, v): updated_centralities[neighbor] -= (discount_scale * 0.3 * centrality_dict[v]) seen.add(neighbor) for neighbor in nx.all_neighbors(G, v): for deg_2_neighbor in nx.all_neighbors(G, neighbor): if deg_2_neighbor not in seen: updated_centralities[deg_2_neighbor] -= (discount_scale * 0.3 * 0.3 * centrality_dict[v]) seen.add(deg_2_neighbor) return updated_centralities
def get_neighbours_by_layer(self, node_id, nlayer): """ Return all neighbours within a specific layer. :param node_id: Id for a node :param ntype: Layer the neighbour node should have :return: The neighbours. """ neighbours = [] for relation in nx.all_neighbors(self, node_id): if self.node[relation].get('layer', None) == nlayer: neighbours.append(relation) return neighbours
def get_neighbours_by_type(self, node_id, ntype): """ Return all neighbours of a certain type. :param node_id: Id for a node :param ntype: Type the neighbour node should have :return: The neighbours. """ neighbours = [] for relation in nx.all_neighbors(self, node_id): if self.node[relation].get('type') == ntype: neighbours.append(relation) return neighbours
def constraint(H, nodes=None, weight=None): if nodes is None: nodes = H constraint = {} for v in Selection_Algorithm.sel_subgraphs(): # Constraint is not defined for isolated nodes if len(H[v]) == 0: constraint[v] = float('nan') continue constraint[v] = sum( local_constraint(H, v, n, weight) for n in set(nx.all_neighbors(H, v))) return constraint