-
Notifications
You must be signed in to change notification settings - Fork 2
/
source_codes.py
47 lines (37 loc) · 1.2 KB
/
source_codes.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
import networkx as nx
def create_edges(file):
# 创建有向边列表
edges = []
for line in file.readlines():
if not line.startswith('#'):
edges.append(line.rstrip('\n').split())
return edges
# # 创建有向边列表
with open('web-Google.txt', 'r') as f:
edges = create_edges(f)
def create_group(edges):
# 创建有向图
G = nx.DiGraph()
for edge in edges:
G.add_edge(edge[0], edge[1])
return G
# 创建有向图
G = create_group(edges)
def calculate_pangrank(G, tax):
# 计算网页的pangrank值,tax为税值
pr = nx.pagerank(G, alpha=tax)
return pr
# 计算网页的pangrank值,tax为税值
pr = calculate_pangrank(G, 0.85)
def pr_descending_order(pr):
# 转换成按pangrank值为key的降序列表
pangrank_list = []
descending_order_pangrank = []
for webpage, pangrank in pr.items():
pangrank_list.append([webpage, pangrank])
descending_order_pangrank = sorted(pangrank_list, key=lambda x:x[1], reverse=True)
return descending_order_pangrank
# 转换成按pangrank值为key的降序列表
descending_order_pangrank = pr_descending_order(pr)
# 查看前15行
print(descending_order_pangrank[0:15])