I have an adjacency matrix 5000X5000 and I would like to create a network graph . The requirement is that the user will input the node , and the output would be a graph ( 1st and 2nd degree ) for that particular input node.
I have already tried using Gephi but as the adjacency matrix is huge I am not able to focus on each and every node. So I would like if I could create a graph for specific nodes ( as I am only interested in 1st and 2nd degree connections for each node and not beyond that )
Gephi is UI based so I don't have the code.
Input will be a node_id and output will be a graph corresponding to that node_id ( 1st and 2nd degree connections )
Here is an implementation using networkx
:
import networkx as nx
import numpy as np
# make dummy adjacency matrix
a = np.random.rand(100,100)
a = np.tril(a)
a = a>0.95
# make graph from adjaceny matrix
G = nx.from_numpy_matrix(a)
def neigh(G, node, depth):
""" given starting node, recursively find neighbours
until desired depth is reached
"""
node_list = []
if depth==0:
node_list.append(node)
else:
for neighbor in G.neighbors(node):
node_list.append(node)
node_list += neigh(G, neighbor, depth-1)
return list(set(node_list)) # intermediate conversion to set to lose duplicates.
# a bit more compressed:
def neigh_short(G, node, depth):
""" given starting node, recursively find neighbours
until desired depth is reached
"""
node_list = [node]
if depth>0:
for neighbor in G.neighbors(node)
node_list += neigh_short(G, neighbor, depth-1)
return list(set(node_list)) # intermediate conversion to set to lose duplicates.
# example:
# find all neighbours with distance 2 from node 5:
n = neigh(G, node=5, depth=2)
# extract the respective subgraph from G and store in H
H = G.subgraph(n)