forked from MohimenulRafi/Network_Dismantling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
107 lines (86 loc) · 3.73 KB
/
Main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import FileParser as fp
import Graph as gp
import networkx as nx
filename='node-edge-pairs.json'
data=fp.loadData(filename)
edges=fp.getEdges(data)
nodes=fp.getNodes(edges)
'''filename2='email-Eu-core-temporal.txt'
fileObject=fp.getFileObject(filename2)
edges=fp.getEdges2(fileObject)
nodes=fp.getNodes(edges)'''
graph=gp.BuildGraph(nodes, edges)
'''import json
n=[]
f=open('nodes.json')
d=json.load(f)
for k, v in d.items():
n.append(int(k))
print(len(n))
print(len(nodes))
diff=list(set(n)-set(nodes))
print(diff)'''
#nodes=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
#edges=[(1,6),(2,4),(3,4),(4,6),(5,6),(6,5),(6,1),(6,4),(6,7),(7,8),(7,10),(8,9),(8,10),(10,11),(10,12),(10,13),(10,14),(11,12),(11,13),(12,13),(12,15),(13,15),(15,16),(16,17)]
print("Number of nodes in the initial graph: "+str(len(nodes)))
print("Number of edges in the initial graph: "+str(len(edges)))
dismantled_graph=nx.Graph()
import NetworkDismantling as ND
nd=ND.Dismantle(graph)
largest_component_fraction=[]
#efficiency_list=[]
#average_clustering_coefficient=[]
#transitivity=[]
number_of_deletion=[]
deleted_nodes=[]
dismantled_graph, largest_component_fraction, number_of_deletion, deleted_nodes=nd.dismantle()
import json
filewriter=open("Result_BBRoad_DFBC.txt", "a")
filewriter.write("Largest Component Fraction\n")
json.dump(largest_component_fraction, filewriter)
#filewriter.write("\n\nEfficiency\n")
#json.dump(efficiency_list, filewriter)
#filewriter.write("\n\nAverage Clustering Coefficient\n")
#json.dump(average_clustering_coefficient, filewriter)
#filewriter.write("\n\nTransitivity\n")
#json.dump(transitivity, filewriter)
filewriter.write("\n\nNumber of Deletion\n")
json.dump(number_of_deletion, filewriter)
filewriter.write("\n\nDeleted Nodes\n")
json.dump(deleted_nodes, filewriter)
#Reinsert
import NodeReinsert as NR
print("Node reinserting started...")
reinserted_nodes=[]
nr=NR.Reinsert(dismantled_graph, graph.adjacency_list, deleted_nodes, graph.N)
dismantled_graph_node_reinserted, reinserted_nodes=nr.reinsertNode()
print("Node reinserting finished")
filewriter.write("\n\nReinserted Nodes\n")
json.dump(reinserted_nodes, filewriter)
#efficiency_after_reinsert=nx.global_efficiency(dismantled_graph_node_reinserted)
#avg_cc_after_reinsert=nx.average_clustering(dismantled_graph_node_reinserted)
#transitivity_after_reinsert=nx.transitivity(dismantled_graph_node_reinserted)
#filewriter.write("\n\nEfficiency After Reinsert\n")
#json.dump([efficiency_after_reinsert], filewriter)
#filewriter.write("\n\nAvg CC After Reinsert\n")
#json.dump([avg_cc_after_reinsert], filewriter)
#filewriter.write("\n\nTransitivity After Reinsert\n")
#json.dump([transitivity_after_reinsert], filewriter)
#Draw Network
pos=nx.spring_layout(dismantled_graph)
gcc=sorted(nx.connected_components(dismantled_graph_node_reinserted), key=len, reverse=True)
largest_component=dismantled_graph_node_reinserted.subgraph(gcc[0])
print("Largest component size: "+str(largest_component.number_of_nodes()))
filewriter.write("\n\nLargest Component Size\n")
filewriter.write(str(largest_component.number_of_nodes()))
for sg in nx.connected_components(dismantled_graph_node_reinserted):
component=dismantled_graph_node_reinserted.subgraph(sg)
if component.number_of_nodes()==largest_component.number_of_nodes():
nx.draw_networkx(component, pos=pos, with_labels=False, node_size=30, font_weight='bold', node_color='red')
else:
nx.draw_networkx(component, pos=pos, with_labels=False, node_size=30, font_weight='bold', node_color='purple')
#nx.draw_networkx(dismantled_graph, with_labels=False, node_size=20, font_weight='bold', node_color='red')
filewriter.close()
import matplotlib.pyplot as plt
plt.title("Network After Dismantling and Node Reinserting (BB Road)")
plt.savefig('BB_Road_DFBC.png')