def multi_sampling(): f = FaceData() G = f.create_graph() # 获取原始数据 _degree = dict() # 分析重组数据 chushu = 0 for i in xrange(38): BFS = common.BFS(G, 10000) for i in BFS: _degree[G.degree(i)] = _degree.get(G.degree(i), 0) + 1 chushu += len(BFS) print "step ", i # 处理数据,取平均的度分布 x = sorted(_degree.iterkeys()) #生成x轴序列,从1到最大度 num = chushu y = [] for i in x: y.append(float(_degree[i]) / num) # 保存数据 f = open("../compare/degree_plot/Epin_BFS.txt", "w") try: for i in x: f.write(str(i) + " " + str(float(_degree[i]) / num) + "\n") finally: f.close()
def sub_degree(): f = FaceData() G = f.create_graph() # 获取原始数据 BFS = common.BFS(G, 10000) _plt = degree.degree(G, plt, "Original", "k-") # 绘制原始网络的度分布 _plt = degree.ego_degree(G, BFS, _plt, "BFS", "rh-") # 随机爬虫子网的度分布 _plt.legend(loc="upper right") # 加入图例 _plt.show()
def ori_avg_degree(): f = FaceData() G = f.create_graph() # 获取原始数据 rw = common.random_walk(G, None, 16000, "unique") mhrw = common.metropolis_hastings_random_walk(G, None, 16000, "unique") bfs = common.BFS(G, None, 16000, "unique") print "原始网络的平均度为: ", degree.avg_degree(G) print "RW抽样得到的平均度为: ", degree.avg_degree(G, rw) print "MHRW抽样得到的平均度为: ", degree.avg_degree(G, mhrw) print "BFS抽样得到的平均度为: ", degree.avg_degree(G, bfs)
def record_con(self, sample="MHRW", lengend=False): # 在G中抽取节点 if sample == "MHRW": nodes = common.metropolis_hastings_random_walk(self.G, None, 10000, "total") elif sample == "RW": nodes = common.random_walk(self.G, None, 10000, "total") elif sample == "BFS": nodes = common.BFS(self.G, 10000) else: nodes = improve_MH.impore_03(self.G, None, 10000, "total") x = range(self.x) # 横坐标 y = [] numerator = 0 # 分子 denominator = 0 # 分母 for i in nodes[0:self.x]: denominator += 1 if self.G.degree(i) <= self.limit: numerator += 1 y.append(float(numerator)/denominator) if sample == "MHRW": if lengend is True: self.plt.plot(x, y, "b--", label="MHRW", linewidth=3.0) else: self.plt.plot(x, y, "b--", linewidth=3.0) elif sample == "RW": if lengend is True: self.plt.plot(x, y, "g:", label="RW", linewidth=3.0) else: self.plt.plot(x, y, "g:", linewidth=3.0) elif sample == "BFS": if lengend is True: self.plt.plot(x, y, "c-.", label="BFS", linewidth=3.0) else: self.plt.plot(x, y, "c-.", linewidth=3.0) else: if lengend is True: self.plt.plot(x, y, "r-", label="UD", linewidth=3.0) else: self.plt.plot(x, y, "r-", linewidth=3.0)
def record_con(self, sample="MHRW", lengend=False): # 在G中抽取节点 if sample == "MHRW": nodes = common.metropolis_hastings_random_walk(self.G, None, self.x, "total") elif sample == "RW": nodes = common.random_walk(self.G, None, self.x, "total") elif sample == "BFS": nodes = common.BFS(self.G, self.x) else: nodes = improve_MH.impore_03(self.G, None, self.x, "total") x = range(self.x) # 横坐标 y = [] list_x = [] for i in nodes: list_x.append(self.G.degree(i)) x,y = self.Z_test(list_x) if sample == "MHRW": if lengend is True: self.plt.plot(x, y, c='b',marker='v', label="MHRW", linewidth=1.0) else: self.plt.plot(x, y, c='b',marker='v', linewidth=1.0) elif sample == "RW": if lengend is True: self.plt.plot(x, y, c='m',marker='*', label="RW", linewidth=1.0) else: self.plt.plot(x, y, c='m',marker='*', linewidth=1.0) elif sample == "BFS": if lengend is True: self.plt.plot(x, y, c='g',marker='s', label="BFS", linewidth=1.0) else: self.plt.plot(x, y, c='g',marker='s', linewidth=1.0) else: if lengend is True: self.plt.plot(x, y, c='r',marker='o', label="UD", linewidth=1.0) else: self.plt.plot(x, y, c='r',marker='o', linewidth=1.0)
def bfs_text(): f = FaceData() G = f.create_graph() # 获取原始数据 bfs = common.BFS(G, None, 5000, "total") print len(bfs)