egocentric_network_2.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import networkx as nx
  2. import matplotlib.pyplot as plt
  3. import itertools
  4. def EgocentricNetwork(G,v):
  5. egocentric_network_edge_list = []
  6. egocentric_network_node_list = [v]
  7. for i in G.neighbors(v):
  8. egocentric_network_node_list.append(i)
  9. egocentric_network_edge_list.append((v,i))
  10. egocentric_network_node_list.sort()
  11. egocentric_network_edge_list = list(tuple(sorted(p)) for p in egocentric_network_edge_list)
  12. for i in list(itertools.combinations(egocentric_network_node_list, 2)): #generates all possible pairs of nodes
  13. if i in G.edges() and i not in egocentric_network_edge_list:
  14. egocentric_network_edge_list.append(i)
  15. temp = []
  16. temp.extend(egocentric_network_node_list)
  17. for i in temp:#print i," ",G.neighbors(i)
  18. for j in G.neighbors(i):
  19. if j not in egocentric_network_node_list:
  20. egocentric_network_node_list.append(j)
  21. if (j,i) in G.edges() and (j,i) not in egocentric_network_edge_list:
  22. egocentric_network_edge_list.append((j,i))
  23. elif (i,j) in G.edges() and (i,j) not in egocentric_network_edge_list:
  24. egocentric_network_edge_list.append((i,j))
  25. return egocentric_network_edge_list,egocentric_network_node_list
  26. #takes input from the file and creates a graph
  27. def CreateGraph():
  28. G = nx.Graph()
  29. f = open('input.txt')
  30. n = int(f.readline())
  31. for i in range(n):
  32. G.add_node(i+1)
  33. no_of_edges = int(f.readline())
  34. for i in range(no_of_edges):
  35. graph_edge_list = f.readline().split()
  36. G.add_edge(int(graph_edge_list[0]), int(graph_edge_list[1]))
  37. vert = int(f.readline())
  38. return G, vert
  39. #draws the graph and displays the weights on the edges
  40. def DrawGraph(G,egocentric_network_edge_list,egocentric_network_node_list):
  41. pos = nx.spring_layout(G)
  42. nx.draw(G, pos, with_labels = True, node_color = 'blue', alpha = 0.2) #with_labels=true is to show the node number in the output graph
  43. nx.draw_networkx_edges(G, pos, edgelist = egocentric_network_edge_list , width = 2.5, alpha = 0.8, edge_color = 'blue')
  44. nx.draw_networkx_nodes(G,pos, nodelist = egocentric_network_node_list, node_color = 'blue', alpha = 0.5)
  45. return pos
  46. def DrawGraph(G, egocentric_network_edge_list, egocentric_network_node_list, vert):
  47. pos = nx.spring_layout(G)
  48. nx.draw(G, pos, with_labels = True, node_color = 'blue', alpha = 0.8) #with_labels=true is to show the node number in the output graph
  49. nx.draw_networkx_edges(G, pos, edgelist = egocentric_network_edge_list , width = 2.5, alpha = 0.8, edge_color = 'red')
  50. nx.draw_networkx_nodes(G,pos, nodelist = egocentric_network_node_list, node_color = 'red', alpha = 0.5)
  51. nx.draw_networkx_nodes(G,pos,nodelist=[vert],node_color='green',node_size=500,alpha=0.8)
  52. return pos
  53. def CentralityMeasures(G):
  54. # Betweenness centrality
  55. bet_cen = nx.betweenness_centrality(G)
  56. # Closeness centrality
  57. clo_cen = nx.closeness_centrality(G)
  58. # Eigenvector centrality
  59. eig_cen = nx.eigenvector_centrality(G)
  60. # Degree centrality
  61. deg_cen = nx.degree_centrality(G)
  62. #print bet_cen, clo_cen, eig_cen
  63. #main function
  64. if __name__== "__main__":
  65. G,vert = CreateGraph()
  66. egocentric_network_edge_list,egocentric_network_node_list = EgocentricNetwork(G,vert)
  67. DrawGraph(G,egocentric_network_edge_list,egocentric_network_node_list, vert)
  68. CentralityMeasures(G)
  69. plt.show()