python retrieve unconnected node pairs

Solutions on MaxInterview for python retrieve unconnected node pairs by the best coders in the world

showing results for - "python retrieve unconnected node pairs"
Gabriele
01 Apr 2017
1nodelist = list(G0.nodes(data=True))                        # From G to list
2vertex = [a for a, b in nodelist]                           # Get attribute vertex
3node_df = pd.DataFrame({'vertex': vertex})                  # Get DF with vertex
4node_list = node_df["vertex"].tolist()                      # From df to list
5
6node_list = list(dict.fromkeys(node_list))                  # Remove duplicate items from the list
7adj_G = nx.to_numpy_matrix(G0, nodelist = node_list)        # Build adjacency matrix
8
9
10all_unconnected_pairs = []                                                  
11offset = 0                                                                  
12for i in tqdm(range(adj_G.shape[0])):                                       # Take only above diagonal of the adjacency matrix                                         
13  for j in range(offset, adj_G.shape[1]):
14    if i != j:
15        # if G0[node_list[i], node_list[j]]['weight'] <=2:                  # Take only edges with weight 1 or 2
16            if adj_G[i,j] ==0:                                              # Take only unconnected node pairs                                     
17                all_unconnected_pairs.append([node_list[i], node_list[j]])
18
19  offset = offset + 1
20
21len(all_unconnected_pairs)  
22
23
similar questions
queries leading to this page
python retrieve unconnected node pairs