1 import random
2 import networkx as nx
3 import matplotlib.pyplot as plt
4
5
6 def ba_bip_graph(n):
7 g = nx.DiGraph()
8 f = open("start.txt")
9 line = f.readline()
10 repeated_user_nodes = []
11 repeated_item_nodes = []
12 while line:
13 repeated_user_nodes.append(line.strip('
').split(" ")[0])
14 repeated_item_nodes.append(line.strip('
').split(" ")[1])
15 line = f.readline()
16 users = 5
17 user_line = 0
18 item_line = 0
19 while users < n:
20 m1 = random.randint(1, 10) #新增一个user,添加随机0-9个边
21 # print("m1:", m1)
22 item_target = set()
23 while len(item_target) < m1:
24 x = random.choice(repeated_item_nodes)
25 item_target.add(x)
26 label = []
27 f = open("user_name.txt")
28 line = f.readline()
29 while line:
30 label.append(line.strip('
').split(" ")[0])
31 line = f.readline()
32 # print([label[user_line]] * m1)
33 # print(item_target)
34 g.add_edges_from(zip([label[user_line]]*m1, item_target))
35 repeated_item_nodes.extend(item_target)
36 repeated_user_nodes.extend([label[user_line]]*m1)
37 user_line += 1
38 f.close()
39 #新增一个user,新增4个item
40 repeat = 0
41 while repeat < 4:
42 m2 = random.randint(1, 5)
43 # print("m2:", m2)
44 user_target = set()
45 while len(user_target) < m2:
46 x = random.choice(repeated_user_nodes)
47 user_target.add(x)
48 label = []
49 f_item = open("item_name.txt")
50 line = f_item.readline()
51 while line:
52 label.append(line.strip('
').split(" ")[0])
53 line = f_item.readline()
54 g.add_edges_from(zip([label[item_line]]*m2, user_target))
55 repeated_user_nodes.extend(user_target)
56 repeated_item_nodes.extend([label[item_line]] * m2)
57 item_line += 1
58 f.close()
59 repeat += 1
60 users = users+1
61 return g
62
63
64 BA = ba_bip_graph(10)
65 pos = nx.circular_layout(BA)
66 nx.draw(BA, pos, with_labels=True, node_size=20)
67 plt.show()
68 for edge in BA.edges:
69 print(edge)
程序中使用的文件下载地址 https://files-cdn.cnblogs.com/files/xianyuxianyuxian/StartGraph.rar
说明:
在本文中,以start.txt中的数据为起始图,左边添加一个user,右边添加4个item(可以修改).同时,添加一个点user,随机产生n(0<n<10)条edge,添加一个点item,随机产生n(0<n<5)条edge.函数的形参n是程序结束时user的数目。