Answers for "python - retrieve unconnected node pairs"

0

python - retrieve unconnected node pairs

nodelist = list(G0.nodes(data=True))                        # From G to list
vertex = [a for a, b in nodelist]                           # Get attribute vertex
node_df = pd.DataFrame({'vertex': vertex})                  # Get DF with vertex
node_list = node_df["vertex"].tolist()                      # From df to list

node_list = list(dict.fromkeys(node_list))                  # Remove duplicate items from the list
adj_G = nx.to_numpy_matrix(G0, nodelist = node_list)        # Build adjacency matrix


all_unconnected_pairs = []                                                  
offset = 0                                                                  
for i in tqdm(range(adj_G.shape[0])):                                       # Take only above diagonal of the adjacency matrix                                         
  for j in range(offset, adj_G.shape[1]):
    if i != j:
        # if G0[node_list[i], node_list[j]]['weight'] <=2:                  # Take only edges with weight 1 or 2
            if adj_G[i,j] ==0:                                              # Take only unconnected node pairs                                     
                all_unconnected_pairs.append([node_list[i], node_list[j]])

  offset = offset + 1

len(all_unconnected_pairs)
Posted by: Guest on November-26-2020

Python Answers by Framework

Browse Popular Code Answers by Language