具体的概念网上均可搜到,仅展示代码实现
有向图.jpgclass Graph:
def __init__(self):
self.linked_node_map = {} # 邻接表\
self.linked_node_map_f ={}
self.PR_map_1 = {}
self.PR_map = {} # 存储重要性
def add_node(self, node_id):
if node_id not in self.linked_node_map:
self.linked_node_map[node_id] = set({})
if node_id not in self.linked_node_map_f:
self.linked_node_map_f[node_id] = set({})
self.PR_map[node_id] = 0
self.PR_map_1[node_id] = 0
# else:
# print("此节点已存在")
def add_link(self, node1, node2):
if node1 not in self.linked_node_map:
self.add_node(node1)
if node2 not in self.linked_node_map:
self.add_node(node2)
self.linked_node_map[node1].add(node2)
self.linked_node_map_f[node2].add(node1)
# 计算pr
def get_PR(self, epoch_num=100, d=0.85):
L=len(self.linked_node_map)
for j in range(epoch_num):
if j == 0:
for node in self.PR_map_1:
self.PR_map_1[node] = 1 / (len(self.linked_node_map))
else:
self.PR_map_1 = self.PR_map
sum1 = 0
for node in self.PR_map:
list1 = list(self.linked_node_map_f[node])
for i in list1:
sum1 += self.PR_map_1[i] / len(self.linked_node_map[i])
self.PR_map[node] = ((1 - d) / L) + d * sum1
sum1 = 0
print(self.PR_map)
edges = [[1, 2], [1, 3], [1, 4], [2, 1], [2, 4], [3, 1], [4, 2], [4, 3]]
if __name__ == "__main__":
graph = Graph()
for edge in edges:
graph.add_link(edge[0], edge[1]) # node1,node2
print("邻接表:", graph.linked_node_map)
print("表示入度的表:", graph.linked_node_map_f)
graph.get_PR()
运行结果
运行结果1.png对比networkx库中的 nx.pagerank(G, alpha=0.85,max_iter=100)
方法
import networkx as nx
# 创建有向图
G = nx.DiGraph()
# 有向图之间边的关系
edges = [("A", "B"), ("A", "C"), ("A", "D"), ("B", "A"), ("B", "D"), ("C", "A"), ("D", "B"), ("D", "C")]
for edge in edges:
G.add_edge(edge[0], edge[1])
page_rank_list = nx.pagerank(G, alpha=0.85,max_iter=100)
print("page_rank value:", page_rank_list)
运行结果
运行结果2.png
网友评论