def showdegree(flag): namemat = [] Degreedis = [] namemat = webmodel.readArff('E:/write.xls', flag) Num = namemat.__len__() # 节点个数 for i in range(0, 63): Degreedis.append(0) i = 0 for line in namemat: Degreedis[i] = sum(line) i += 1 # print Degreedis temp = [] for j in range(0, 63): temp.append(j) fig1 = plt.figure(2) rects = plt.bar(left=(temp[0:63]), height=(Degreedis[0:63]), width=1, align="center", yerr=0.000001) plt.title('The degree of these nodes') plt.xlabel('Number of nodes') plt.ylabel('Degree') plt.show()
def getclustering_co(flag): namemat = [] EEE = [] CCC = [] namemat = webmodel.readArff('E:/write.xls', flag) Num = namemat.__len__() # 节点个数 # print namemat #print Num for i in range(0, 63): EEE.append(0) CCC.append(0) for i in range(0, 63): neighbornum = 0 neighbor = [] for j in range(0, 63): if i != j and namemat[i][j] == 1: neighbornum += 1 neighbor.append(j) for linea in neighbor: for lineb in neighbor: if namemat[linea][lineb] == 1: EEE[i] += 1 EEE[i] = EEE[i] / 2 # print EEE[i] if (EEE[i] == 0): CCC[i] = 0 else: CCC[i] = 2 * EEE[i] / 1.0 / (neighbornum * (neighbornum - 1)) aver = sum(CCC) / 1.0 / Num print aver
def corenessca(flagg): namemat = [] coreness = [] Degreedis = [] namemat = webmodel.readArff('E:/write.xls', flagg) Num = namemat.__len__() # 节点个数 print namemat for i in range(0, Num): coreness.append(0) Degreedis.append(0) flag = 0 while flag < 63: v = 0 for line in namemat: Degreedis[v] = sum(line) v += 1 for j in range(0, Num): if Degreedis[j] == flag: print('Number %d node has coreness: ' % (j + 1), flag) #print flag for k in range(0, Num): namemat[k][j] = 0 namemat[j][k] = 0 v = 0 for line in namemat: Degreedis[v] = sum(line) v += 1 flag2 = 0 for l in range(0, Num): if Degreedis[l] == flag and flag != 0: flag2 = 1 if flag2 == 0: flag += 1
def calculationtwo(flag): lenthma = [] lenthma = webmodel.readArff('E:/write.xls',flag) Num = lenthma.__len__() # 节点个数 lenthmat=copy.deepcopy(lenthma) for m in range(0,Num): for n in range(0, Num): if lenthmat[m][n]==0: lenthmat[m][n]='INF' for m in range(0, Num): lenthmat[m][m]=0 for k in range(0,Num): for i in range(0, Num): for j in range(0, Num): if lenthmat[i][k] != 'INF' and lenthmat[k][j] != 'INF': if lenthmat[i][j] > lenthmat[i][k] + lenthmat[k][j]: lenthmat[i][j] = lenthmat[i][k] + lenthmat[k][j] sumlen=0 for i in range(0,Num): for j in range(0,Num): if lenthmat[i][j]!='INF': sumlen = sumlen + lenthmat[i][j] ave=sumlen/1.0/(Num*(Num-1)) print("aver_path lengh:", ave) return ave
def drawnetgraph(Num): lenthmat = webmodel.readArff('E:/write.xls', Num) G = nx.Graph() Matrix = np.array([]) Matrix = np.array(lenthmat) #print len(Matrix) for i in range(0, 63): for j in range(0, 63): if Matrix[i][j] == 1: G.add_edge(i, j) del lenthmat nx.draw(G, node_size=20) plt.show()
def showdistribution(flag): namemat = [] Degreedis = [] namemat = webmodel.readArff('E:/write.xls', flag) Num = namemat.__len__() # 节点个数 for i in range(0, 63): Degreedis.append(0) i = 0 for line in namemat: Degreedis[i] = sum(line) i += 1 # print Degreedis temp = [] for j in range(0, 63): temp.append(j) aver_degree = sum(Degreedis) / Num M = max(Degreedis) # 最大的度 print M Degreedistribution = [] dnum = [] for m in range(0, M + 1): Degreedistribution.append(0) dnum.append(0) for item in Degreedis: for n in range(0, M + 1): if item == n: dnum[n] += 1 for k in range(0, M + 1): Degreedistribution[k] = dnum[k] / 1.0 / Num fig1 = plt.figure(2) rects = plt.bar(left=(temp[0:M + 1]), height=(Degreedistribution[0:M + 1]), width=1, align="center", yerr=0.000001) plt.title('The degreedistribution of these nodes') plt.xlabel('Number of nodes') plt.ylabel('Degreedistribution') plt.show()
def largestsubgraph(flagg): namemat = [] namemat = webmodel.readArff('E:/write.xls', flagg) Num = namemat.__len__() # 节点个数 scatterpr = [] scatterpi = [] Degreedis = [] tempa = [] tempb = [] largesta = [] largestb = [] for i in range(0, 64): largesta.append(0) largestb.append(0) aa = copy.deepcopy(namemat) bb = copy.deepcopy(namemat) Num = namemat.__len__() for line in namemat: Degreedis.append(sum(line)) #print Degreedis # intentional_attack后 tempa = getfloyd(aa) print tempa k = 0 for line in tempa: for j in line: if j != 0 and j != 'INF': largesta[k] += 1 k += 1 terma = max(largesta) scatterpi.append(terma) print terma for j in range(0, Num): k = 0 for i in range(0, 63): largesta[i] = 0 maxnode = Degreedis.index(max(Degreedis)) Degreedis[maxnode] = 0 for m in range(0, Num): aa[m][maxnode] = 0 aa[maxnode][m] = 0 tempa = getfloyd(aa) for line in tempa: for j in line: if j != 0 and j != 'INF': largesta[k] += 1 k += 1 terma = max(largesta) scatterpi.append(terma) # random_attack后 tempb = getfloyd(bb) k = 0 for line in tempb: for j in line: if j != 0 and j != 'INF': largestb[k] += 1 k += 1 termb = max(largestb) scatterpr.append(termb) b_list = range(0, 63) blist_webId = random.sample(b_list, 63) for j in range(0, Num): k = 0 for i in range(0, 63): largestb[i] = 0 temp = blist_webId[j] for m in range(0, Num): bb[m][temp] = 0 bb[temp][m] = 0 tempb = getfloyd(bb) for line in tempb: for j in line: if j != 0 and j != 'INF': largestb[k] += 1 k += 1 termb = max(largestb) scatterpr.append(termb) print scatterpi print scatterpr x = np.arange(0, 64) y = x fig = plt.figure() ax1 = fig.add_subplot(111) # 设置标题 ax1.set_title('Attack!!!') # 设置X轴标签 plt.xlabel('Number') # 设置Y轴标签 plt.ylabel('Maximum Connected Subgraph') # 画散点图 ax1.scatter(x, scatterpi, s=12, c='r', marker='o') ax1.scatter(x, scatterpr, s=10, c='b', marker='o') # 设置图标 label = ["intentional", "random"] plt.legend(label, loc=0, ncol=2) # 显示所画的图 plt.show()
def aver_pathle(flagg): namemat = [] namemat = webmodel.readArff('E:/write.xls', flagg) Num = namemat.__len__() # 节点个数 scatterpr = [] scatterpi = [] Degreedis = [] Num = namemat.__len__() aa = copy.deepcopy(namemat) bb = copy.deepcopy(namemat) for line in namemat: Degreedis.append(sum(line)) # intentional_attack后 ava = aver_shpathl.calculation(bb, Num) scatterpi.append(ava) jin = Num for j in range(0, Num): maxnode = Degreedis.index(max(Degreedis)) Degreedis[maxnode] = 0 for k in range(0, Num): aa[k][maxnode] = 0 aa[maxnode][k] = 0 jin -= 1 if jin == 0 or jin == 1: scatterpi.append(0) else: ave = aver_shpathl.calculation(bb, jin) scatterpi.append(ave) # random_attack后 b_list = range(0, 63) blist_webId = random.sample(b_list, 63) ave = aver_shpathl.calculation(bb, Num) scatterpr.append(ave) jim = Num for j in range(0, Num): temp = blist_webId[j] for k in range(0, Num): bb[k][temp] = 0 bb[temp][k] = 0 jim -= 1 if jim == 0 or jim == 1: scatterpr.append(0) else: ave = aver_shpathl.calculation(bb, jim) scatterpr.append(ave) x = np.arange(0, 64) y = x fig = plt.figure() ax1 = fig.add_subplot(111) # 设置标题 ax1.set_title('Attack!!!') # 设置X轴标签 plt.xlabel('Number') # 设置Y轴标签 plt.ylabel('Average Path Length') # 画散点图 ax1.scatter(x, scatterpi, s=12, c='r', marker='o') ax1.scatter(x, scatterpr, s=10, c='b', marker='o') # 设置图标 label = ["intentional", "random"] plt.legend(label, loc=0, ncol=2) # 显示所画的图 plt.show()
lenthmat[m][m]=0 for k in range(0,Num): for i in range(0, Num): for j in range(0, Num): if lenthmat[i][k] != 'INF' and lenthmat[k][j] != 'INF': if lenthmat[i][j] > lenthmat[i][k] + lenthmat[k][j]: lenthmat[i][j] = lenthmat[i][k] + lenthmat[k][j] sumlen=0 for i in range(0,Num): for j in range(0,Num): if lenthmat[i][j]!='INF': sumlen = sumlen + lenthmat[i][j] ave=sumlen/1.0/(Num*(Num-1)) return ave if __name__ == '__main__': lenthmat = [] lenthmat = webmodel.readArff('E:/write.xls',0) Num = lenthmat.__len__()#节点个数 #print namemat print Num #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]] # n=a.__len__() ave=calculation(lenthmat, Num) print("aver_path lengh:", ave)