import csvfrom collections import defaultdictimport randomimport pandas as pdimport networkx as nximport matplotlib.pyplot as pltdef find_unique(df): s1 = set(df”V1″) s2 = set(df”V2″) return len(s1.union(s2))# Read the graph from csvgraph = pd.read_csv(‘roadmap12.csv’)# spacing in row V1graph.rename(columns={‘ V1’: ‘V1’}, inplace=True)# Get unique nodes from the graphnodes = list(graph”V1″.unique())# Get the number of nodes from the usernum_samples = input(“Enter the number of nodes you want? “)num_samples = int(num_samples)# Pick a random node to start withrandom = random.sample(nodes, 1)start = graphgraph”V1″ == random# sample will contain the subgraph, initialize it with random node and its neighborssample = startsample = sample.reset_index(drop=True) #reset the index in dataframedouble_check = set(sample”V1”) #continue adding nodes until not_found is Falsenot_found = find_unique(sample) <= num_samples# add more nodes and edges (with the constraint that the total nodes should be less than N)for i in range(num_samples): if i >= len(sample): break if not_found : to_node = sample.loci”V2″ neighbors = graphgraph”V1″ == to_node if len(neighbors): from_nodes = neighbors”V1″.unique() if set(from_nodes).intersection(double_check): continue sample = pd.concat(sample, neighbors) sample = sample.reset_index(drop=True) double_check = double_check.union(set(from_nodes)) not_found = find_unique(sample) <= num_samples else: double_check = double_check.union(set(from_nodes))# remove extra nodes if addedwhile(find_unique(sample) > num_samples): sample = sample:-1#Creates an empty graph with no nodes and no edgesG=nx.Graph()#adding nodes and edges to the subgraphfor idx,row in sample.iterrows() : G.add_node(row”V1″) G.add_node(row”V2″) G.add_edge(row”V1″,row”V2″)print(sample.to_string(index=False, header=False))#Draw the graph G using Matplotlibnx.draw(G,with_labels=True)plt.savefig(“subgraph.png”)