├── README.md ├── aver_shpathl.py ├── clustering_co.py ├── coreness_ca.py ├── degree_dis.py ├── guite.py ├── netgraph.py ├── random_attack.py └── webmodel.py /README.md: -------------------------------------------------------------------------------- 1 | # Complex-Network-Model 2 | - 本系统采用python语言开发,导入pycharm即可使用 3 | - 导入后直接运行guite.py文件即可得到可视化界面 4 | 5 | ## Finished Items 6 | 7 | | Attribute | finished | 8 | | :--------------------------------: | :------: | 9 | | UI | √ | 10 | | clustering coefficient | √ | 11 | | coreness | √ | 12 | | degree | √ | 13 | | shortest path | √ | 14 | | attention attack test | √ | 15 | | random attack test | √ | 16 | | node number/edge number statistics | √ | 17 | | size of the largest subgraph | √ | 18 | | average path length | √ | 19 | 20 | -------------------------------------------------------------------------------- /aver_shpathl.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import sys 4 | 5 | 6 | reload(sys) 7 | sys.setdefaultencoding('utf8') 8 | import webmodel 9 | import copy 10 | 11 | def calculationtwo(flag): 12 | lenthma = [] 13 | lenthma = webmodel.readArff('E:/write.xls',flag) 14 | Num = lenthma.__len__() # 节点个数 15 | lenthmat=copy.deepcopy(lenthma) 16 | for m in range(0,Num): 17 | for n in range(0, Num): 18 | if lenthmat[m][n]==0: 19 | lenthmat[m][n]='INF' 20 | for m in range(0, Num): 21 | lenthmat[m][m]=0 22 | 23 | for k in range(0,Num): 24 | for i in range(0, Num): 25 | for j in range(0, Num): 26 | if lenthmat[i][k] != 'INF' and lenthmat[k][j] != 'INF': 27 | if lenthmat[i][j] > lenthmat[i][k] + lenthmat[k][j]: 28 | lenthmat[i][j] = lenthmat[i][k] + lenthmat[k][j] 29 | sumlen=0 30 | for i in range(0,Num): 31 | for j in range(0,Num): 32 | if lenthmat[i][j]!='INF': 33 | sumlen = sumlen + lenthmat[i][j] 34 | ave=sumlen/1.0/(Num*(Num-1)) 35 | print("aver_path lengh:", ave) 36 | return ave 37 | 38 | 39 | def calculation(lenthma, Num): 40 | lenthmat=copy.deepcopy(lenthma) 41 | for m in range(0,Num): 42 | for n in range(0, Num): 43 | if lenthmat[m][n]==0: 44 | lenthmat[m][n]='INF' 45 | for m in range(0, Num): 46 | lenthmat[m][m]=0 47 | 48 | for k in range(0,Num): 49 | for i in range(0, Num): 50 | for j in range(0, Num): 51 | if lenthmat[i][k] != 'INF' and lenthmat[k][j] != 'INF': 52 | if lenthmat[i][j] > lenthmat[i][k] + lenthmat[k][j]: 53 | lenthmat[i][j] = lenthmat[i][k] + lenthmat[k][j] 54 | 55 | sumlen=0 56 | 57 | for i in range(0,Num): 58 | for j in range(0,Num): 59 | if lenthmat[i][j]!='INF': 60 | sumlen = sumlen + lenthmat[i][j] 61 | ave=sumlen/1.0/(Num*(Num-1)) 62 | return ave 63 | 64 | 65 | if __name__ == '__main__': 66 | lenthmat = [] 67 | lenthmat = webmodel.readArff('E:/write.xls',0) 68 | Num = lenthmat.__len__()#节点个数 69 | #print namemat 70 | print Num 71 | #a = [[0, 1, 1, 0, 1], [1, 0, 1, 1, 0], [1, 1, 0, 0, 0], [0, 1, 0, 0, 0], [1, 0, 0, 0, 0]] 72 | # n=a.__len__() 73 | ave=calculation(lenthmat, Num) 74 | print("aver_path lengh:", ave) 75 | 76 | -------------------------------------------------------------------------------- /clustering_co.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import sys 4 | 5 | #计算E[i],C[i]后使用公式计算cluster 6 | reload(sys) 7 | sys.setdefaultencoding('utf8') 8 | import webmodel 9 | 10 | def getclustering_co(flag): 11 | namemat = [] 12 | EEE = [] 13 | CCC = [] 14 | 15 | namemat = webmodel.readArff('E:/write.xls', flag) 16 | Num = namemat.__len__() # 节点个数 17 | # print namemat 18 | #print Num 19 | 20 | for i in range(0, 63): 21 | EEE.append(0) 22 | CCC.append(0) 23 | for i in range(0, 63): 24 | neighbornum = 0 25 | neighbor = [] 26 | for j in range(0, 63): 27 | if i != j and namemat[i][j] == 1: 28 | neighbornum += 1 29 | neighbor.append(j) 30 | for linea in neighbor: 31 | for lineb in neighbor: 32 | if namemat[linea][lineb] == 1: 33 | EEE[i] += 1 34 | EEE[i] = EEE[i] / 2 35 | # print EEE[i] 36 | if (EEE[i] == 0): 37 | CCC[i] = 0 38 | else: 39 | CCC[i] = 2 * EEE[i] / 1.0 / (neighbornum * (neighbornum - 1)) 40 | aver = sum(CCC) / 1.0 / Num 41 | print aver 42 | 43 | if __name__ == '__main__': 44 | getclustering_co(0) -------------------------------------------------------------------------------- /coreness_ca.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import sys 4 | 5 | # 计算节点和整个图的coreness 6 | reload(sys) 7 | sys.setdefaultencoding('utf8') 8 | import webmodel 9 | 10 | def corenessca(flagg): 11 | namemat = [] 12 | coreness = [] 13 | Degreedis = [] 14 | namemat = webmodel.readArff('E:/write.xls',flagg) 15 | Num = namemat.__len__() # 节点个数 16 | print namemat 17 | for i in range(0, Num): 18 | coreness.append(0) 19 | Degreedis.append(0) 20 | flag = 0 21 | while flag < 63: 22 | v = 0 23 | for line in namemat: 24 | Degreedis[v] = sum(line) 25 | v += 1 26 | for j in range(0, Num): 27 | if Degreedis[j] == flag: 28 | print('Number %d node has coreness: ' % (j + 1) ,flag) 29 | #print flag 30 | for k in range(0, Num): 31 | namemat[k][j] = 0 32 | namemat[j][k] = 0 33 | v = 0 34 | for line in namemat: 35 | Degreedis[v] = sum(line) 36 | v += 1 37 | flag2 = 0 38 | for l in range(0, Num): 39 | if Degreedis[l] == flag and flag!=0: 40 | flag2 = 1 41 | if flag2 == 0: 42 | flag += 1 43 | 44 | 45 | if __name__ == '__main__': 46 | corenessca(2) 47 | 48 | -------------------------------------------------------------------------------- /degree_dis.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import sys 4 | 5 | reload(sys) 6 | sys.setdefaultencoding('utf8') 7 | import webmodel 8 | import matplotlib.pyplot as plt 9 | from matplotlib.font_manager import * 10 | from matplotlib import mlab 11 | from matplotlib import rcParams 12 | 13 | 14 | def showdegree(flag): 15 | namemat = [] 16 | Degreedis = [] 17 | namemat = webmodel.readArff('E:/write.xls', flag) 18 | Num = namemat.__len__() # 节点个数 19 | for i in range(0, 63): 20 | Degreedis.append(0) 21 | i = 0 22 | for line in namemat: 23 | Degreedis[i] = sum(line) 24 | i += 1 25 | # print Degreedis 26 | temp = [] 27 | 28 | for j in range(0, 63): 29 | temp.append(j) 30 | fig1 = plt.figure(2) 31 | rects = plt.bar(left=(temp[0:63]), height=(Degreedis[0:63]), width=1, align="center", yerr=0.000001) 32 | plt.title('The degree of these nodes') 33 | plt.xlabel('Number of nodes') 34 | plt.ylabel('Degree') 35 | plt.show() 36 | 37 | def showdistribution(flag): 38 | namemat = [] 39 | Degreedis = [] 40 | namemat = webmodel.readArff('E:/write.xls', flag) 41 | Num = namemat.__len__() # 节点个数 42 | for i in range(0, 63): 43 | Degreedis.append(0) 44 | i = 0 45 | for line in namemat: 46 | Degreedis[i] = sum(line) 47 | i += 1 48 | # print Degreedis 49 | temp = [] 50 | for j in range(0, 63): 51 | temp.append(j) 52 | aver_degree = sum(Degreedis) / Num 53 | M = max(Degreedis) # 最大的度 54 | print M 55 | Degreedistribution = [] 56 | dnum = [] 57 | for m in range(0, M + 1): 58 | Degreedistribution.append(0) 59 | dnum.append(0) 60 | 61 | for item in Degreedis: 62 | for n in range(0, M + 1): 63 | if item == n: 64 | dnum[n] += 1 65 | for k in range(0, M + 1): 66 | Degreedistribution[k] = dnum[k] / 1.0 / Num 67 | fig1 = plt.figure(2) 68 | rects = plt.bar(left=(temp[0:M + 1]), height=(Degreedistribution[0:M + 1]), width=1, align="center", yerr=0.000001) 69 | plt.title('The degreedistribution of these nodes') 70 | plt.xlabel('Number of nodes') 71 | plt.ylabel('Degreedistribution') 72 | plt.show() 73 | 74 | 75 | if __name__ == '__main__': 76 | showdegree(0) 77 | showdistribution(0) 78 | 79 | -------------------------------------------------------------------------------- /guite.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | from Tkinter import * 3 | import easygui 4 | import netgraph 5 | import degree_dis 6 | import aver_shpathl 7 | import clustering_co 8 | import coreness_ca 9 | import random_attack 10 | import matplotlib.pyplot as plt 11 | 12 | 13 | root = Tk() 14 | root.title('welcome! ! !') 15 | root.geometry('500x400+500+300') 16 | v = IntVar() 17 | 18 | 19 | def callback(): 20 | if (str(var.get()) == '1'): 21 | netgraph.drawnetgraph(0) 22 | 23 | if (str(var.get()) == '2'): 24 | netgraph.drawnetgraph(1) 25 | 26 | if (str(var.get()) == '3'): 27 | netgraph.drawnetgraph(2) 28 | 29 | 30 | def callbackone(): 31 | if (str(var.get()) == '1'): 32 | degree_dis.showdegree(0) 33 | 34 | if (str(var.get()) == '2'): 35 | degree_dis.showdegree(1) 36 | 37 | if (str(var.get()) == '3'): 38 | degree_dis.showdegree(2) 39 | 40 | 41 | def callbacktwo(): 42 | if (str(var.get()) == '1'): 43 | degree_dis.showdistribution(0) 44 | 45 | if (str(var.get()) == '2'): 46 | degree_dis.showdistribution(1) 47 | 48 | if (str(var.get()) == '3'): 49 | degree_dis.showdistribution(2) 50 | 51 | 52 | def callbackaver(): 53 | if (str(var.get()) == '1'): 54 | aver_shpathl.calculationtwo(0) 55 | 56 | if (str(var.get()) == '2'): 57 | aver_shpathl.calculationtwo(1) 58 | 59 | if (str(var.get()) == '3'): 60 | aver_shpathl.calculationtwo(2) 61 | 62 | 63 | def callbackclustering(): 64 | if (str(var.get()) == '1'): 65 | clustering_co.getclustering_co(0) 66 | 67 | if (str(var.get()) == '2'): 68 | clustering_co.getclustering_co(1) 69 | 70 | if (str(var.get()) == '3'): 71 | clustering_co.getclustering_co(2) 72 | 73 | 74 | def callbackcoreness(): 75 | if (str(var.get()) == '1'): 76 | coreness_ca.corenessca(0) 77 | 78 | if (str(var.get()) == '2'): 79 | coreness_ca.corenessca(1) 80 | 81 | if (str(var.get()) == '3'): 82 | coreness_ca.corenessca(2) 83 | 84 | 85 | def callbacklargestgraph(): 86 | if (str(var.get()) == '1'): 87 | random_attack.largestsubgraph(0) 88 | 89 | if (str(var.get()) == '2'): 90 | random_attack.largestsubgraph(1) 91 | 92 | if (str(var.get()) == '3'): 93 | random_attack.largestsubgraph(2) 94 | 95 | 96 | def attack_averpathl(): 97 | if (str(var.get()) == '1'): 98 | random_attack.aver_pathle(0) 99 | 100 | if (str(var.get()) == '2'): 101 | random_attack.aver_pathle(1) 102 | 103 | if (str(var.get()) == '3'): 104 | random_attack.aver_pathle(2) 105 | 106 | 107 | group = LabelFrame(root, text='welcome!') # 基于root 制定一个框架 . 108 | group.pack(padx=50) 109 | 110 | var = IntVar() 111 | R1 = Radiobutton(root, text="name", variable=var, value=1, ) 112 | R1.pack(anchor=W) 113 | 114 | R2 = Radiobutton(root, text="hometown", variable=var, value=2) 115 | R2.pack(anchor=W) 116 | 117 | R3 = Radiobutton(root, text="dialect", variable=var, value=3) 118 | R3.pack(anchor=W) 119 | webgraphButton = Button(root, text='显示网络图', command=callback) 120 | 121 | webgraphButton.pack(pady=0) 122 | 123 | nodedisButton = Button(root, text='node degree show', command=callbackone) 124 | nodedisButton.pack(pady=0) 125 | 126 | nodedisButton2 = Button(root, text='node-de_distribution ', command=callbacktwo) 127 | nodedisButton2.pack(pady=0) 128 | 129 | getaverpath = Button(root, text='aver_shpathlength ', command=callbackaver) 130 | getaverpath.pack(pady=0) 131 | 132 | clustering = Button(root, text='clustering_coefficient ', command=callbackclustering) 133 | clustering.pack(pady=0) 134 | 135 | coreness = Button(root, text='coreness ', command=callbackcoreness) 136 | coreness.pack(pady=0) 137 | 138 | attack_largestgraph = Button(root, text='attack_largestgraph ', command=callbacklargestgraph) 139 | attack_largestgraph.pack(pady=0) 140 | 141 | attack_averpathl = Button(root, text='attack_averpathlength ', command=attack_averpathl) 142 | attack_averpathl.pack(pady=0) 143 | 144 | mainloop() 145 | -------------------------------------------------------------------------------- /netgraph.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import networkx as nx 3 | import numpy as np 4 | import sys 5 | 6 | reload(sys) 7 | sys.setdefaultencoding('utf8') 8 | import webmodel 9 | import matplotlib.pyplot as plt 10 | 11 | 12 | def drawnetgraph(Num): 13 | lenthmat = webmodel.readArff('E:/write.xls',Num) 14 | G = nx.Graph() 15 | Matrix = np.array([]) 16 | Matrix = np.array(lenthmat) 17 | #print len(Matrix) 18 | for i in range(0,63): 19 | for j in range(0,63): 20 | if Matrix[i][j] == 1: 21 | G.add_edge(i, j) 22 | del lenthmat 23 | nx.draw(G, node_size=20) 24 | plt.show() 25 | 26 | 27 | if __name__ == '__main__': 28 | drawnetgraph(1) 29 | -------------------------------------------------------------------------------- /random_attack.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import sys 4 | import matplotlib.pyplot as plt 5 | 6 | import aver_shpathl 7 | import random 8 | import copy 9 | 10 | import numpy as np 11 | 12 | # 模拟attack 13 | reload(sys) 14 | sys.setdefaultencoding('utf8') 15 | import webmodel 16 | 17 | def getfloyd(namemat): 18 | lenthmat = copy.deepcopy(namemat) 19 | Num=len(lenthmat) 20 | for m in range(0,Num): 21 | for n in range(0, Num): 22 | if lenthmat[m][n]==0: 23 | lenthmat[m][n]='INF' 24 | for m in range(0, Num): 25 | lenthmat[m][m]=0 26 | 27 | for k in range(0,Num): 28 | for i in range(0, Num): 29 | for j in range(0, Num): 30 | if lenthmat[i][k] != 'INF' and lenthmat[k][j] != 'INF': 31 | if lenthmat[i][j] > lenthmat[i][k] + lenthmat[k][j]: 32 | lenthmat[i][j] = lenthmat[i][k] + lenthmat[k][j] 33 | return lenthmat 34 | 35 | 36 | def largestsubgraph(flagg): 37 | namemat = [] 38 | namemat = webmodel.readArff('E:/write.xls', flagg) 39 | Num = namemat.__len__() # 节点个数 40 | scatterpr = [] 41 | scatterpi = [] 42 | Degreedis = [] 43 | tempa=[] 44 | tempb = [] 45 | largesta=[] 46 | largestb=[] 47 | for i in range(0,64): 48 | largesta.append(0) 49 | largestb.append(0) 50 | aa = copy.deepcopy(namemat) 51 | bb = copy.deepcopy(namemat) 52 | Num = namemat.__len__() 53 | for line in namemat: 54 | Degreedis.append(sum(line)) 55 | #print Degreedis 56 | 57 | 58 | # intentional_attack后 59 | tempa = getfloyd(aa) 60 | print tempa 61 | k = 0 62 | for line in tempa: 63 | for j in line: 64 | 65 | if j != 0 and j != 'INF': 66 | largesta[k] += 1 67 | k += 1 68 | terma = max(largesta) 69 | scatterpi.append(terma) 70 | print terma 71 | 72 | for j in range(0, Num): 73 | k = 0 74 | for i in range(0, 63): 75 | largesta[i]=0 76 | maxnode = Degreedis.index(max(Degreedis)) 77 | Degreedis[maxnode] = 0 78 | for m in range(0, Num): 79 | aa[m][maxnode] = 0 80 | aa[maxnode][m] = 0 81 | tempa=getfloyd(aa) 82 | for line in tempa: 83 | for j in line: 84 | if j!=0 and j!='INF': 85 | largesta[k]+=1 86 | k+=1 87 | terma=max(largesta) 88 | scatterpi.append(terma) 89 | # random_attack后 90 | tempb = getfloyd(bb) 91 | k = 0 92 | for line in tempb: 93 | for j in line: 94 | if j != 0 and j != 'INF': 95 | largestb[k] += 1 96 | k += 1 97 | termb = max(largestb) 98 | scatterpr.append(termb) 99 | 100 | b_list = range(0, 63) 101 | blist_webId = random.sample(b_list, 63) 102 | for j in range(0, Num): 103 | k = 0 104 | for i in range(0, 63): 105 | largestb[i]=0 106 | temp = blist_webId[j] 107 | for m in range(0, Num): 108 | bb[m][temp] = 0 109 | bb[temp][m] = 0 110 | tempb = getfloyd(bb) 111 | for line in tempb: 112 | for j in line: 113 | if j != 0 and j != 'INF': 114 | largestb[k] += 1 115 | k += 1 116 | termb = max(largestb) 117 | scatterpr.append(termb) 118 | 119 | print scatterpi 120 | print scatterpr 121 | x = np.arange(0, 64) 122 | y = x 123 | fig = plt.figure() 124 | ax1 = fig.add_subplot(111) 125 | # 设置标题 126 | ax1.set_title('Attack!!!') 127 | # 设置X轴标签 128 | plt.xlabel('Number') 129 | # 设置Y轴标签 130 | plt.ylabel('Maximum Connected Subgraph') 131 | # 画散点图 132 | ax1.scatter(x, scatterpi, s=12, c='r', marker='o') 133 | ax1.scatter(x, scatterpr, s=10, c='b', marker='o') 134 | # 设置图标 135 | label = ["intentional", "random"] 136 | plt.legend(label, loc=0, ncol=2) 137 | # 显示所画的图 138 | plt.show() 139 | 140 | 141 | def aver_pathle(flagg): 142 | 143 | namemat = [] 144 | namemat = webmodel.readArff('E:/write.xls', flagg) 145 | Num = namemat.__len__() # 节点个数 146 | 147 | scatterpr = [] 148 | scatterpi = [] 149 | Degreedis = [] 150 | Num = namemat.__len__() 151 | aa = copy.deepcopy(namemat) 152 | bb = copy.deepcopy(namemat) 153 | for line in namemat: 154 | Degreedis.append(sum(line)) 155 | 156 | # intentional_attack后 157 | ava = aver_shpathl.calculation(bb, Num) 158 | scatterpi.append(ava) 159 | jin = Num 160 | for j in range(0, Num): 161 | maxnode = Degreedis.index(max(Degreedis)) 162 | Degreedis[maxnode] = 0 163 | for k in range(0, Num): 164 | aa[k][maxnode] = 0 165 | aa[maxnode][k] = 0 166 | jin -= 1 167 | if jin == 0 or jin == 1: 168 | scatterpi.append(0) 169 | else: 170 | ave = aver_shpathl.calculation(bb, jin) 171 | scatterpi.append(ave) 172 | 173 | # random_attack后 174 | b_list = range(0, 63) 175 | blist_webId = random.sample(b_list, 63) 176 | ave = aver_shpathl.calculation(bb, Num) 177 | scatterpr.append(ave) 178 | 179 | jim = Num 180 | for j in range(0, Num): 181 | temp = blist_webId[j] 182 | for k in range(0, Num): 183 | bb[k][temp] = 0 184 | bb[temp][k] = 0 185 | jim -= 1 186 | if jim == 0 or jim == 1: 187 | scatterpr.append(0) 188 | else: 189 | ave = aver_shpathl.calculation(bb, jim) 190 | scatterpr.append(ave) 191 | x = np.arange(0, 64) 192 | y = x 193 | fig = plt.figure() 194 | ax1 = fig.add_subplot(111) 195 | # 设置标题 196 | ax1.set_title('Attack!!!') 197 | # 设置X轴标签 198 | plt.xlabel('Number') 199 | # 设置Y轴标签 200 | plt.ylabel('Average Path Length') 201 | # 画散点图 202 | ax1.scatter(x, scatterpi,s=12, c='r', marker='o') 203 | ax1.scatter(x, scatterpr,s=10, c='b', marker='o') 204 | # 设置图标 205 | label = ["intentional", "random"] 206 | plt.legend(label, loc=0, ncol=2) 207 | # 显示所画的图 208 | plt.show() 209 | 210 | 211 | if __name__ == '__main__': 212 | largestsubgraph(0) 213 | aver_pathle(0) 214 | -------------------------------------------------------------------------------- /webmodel.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import xlrd 3 | import sys 4 | 5 | 6 | reload(sys) 7 | sys.setdefaultencoding('utf-8') 8 | 9 | def readArff(fileName,Num): 10 | aa = [] 11 | bb = [] 12 | data = xlrd.open_workbook('E:/write.xls') 13 | table = data.sheets()[Num] 14 | nrows = table.nrows # 行数 15 | ncols = table.ncols # 列数 16 | for i in xrange(0, nrows): 17 | if i == 0: 18 | continue 19 | rowValues = table.row_values(i) # 某一行数据 20 | # print rowValues 21 | j = 0 22 | for item in rowValues: 23 | a = item.encode("utf-8") 24 | if a != 'm' and a != 'y' and a != 'n': 25 | continue 26 | if a == 'm' or a == 'n': 27 | bb.append(0) 28 | if a == 'y': 29 | bb.append(1) 30 | j += 1 31 | aa.append(bb) 32 | bb = [] 33 | return aa 34 | 35 | --------------------------------------------------------------------------------