先决条件– 社交网络简介,Barabasi Albert Graph
在社交网络中,有一种现象叫做富人越来越富,也被称为偏好依恋。在偏好依恋中,一个已经富有的人得到的越来越多,而一个拥有更少的人得到的更少。这被称为富人越来越富现象或偏好依恋。
例如,假设班上有一些学生,每个学生都与一些学生成为朋友,这称为其学位,即学生的学位是其拥有的朋友数量。现在学历高的学生富,学历低的学生穷。现在假设班上来了一个新学生,他/她要交m个朋友,那么他/她会选择学历更高的学生,并与他们成为朋友,从而增加富裕程度。这就是所谓的“富得更富”或“优先依恋” 。
Barabasi Albert 模型是优先依恋的实现。
逻辑——以下是 Barabasi Albert 模型背后的逻辑:
- 取一个具有 n 0 个节点的随机图,并以每个节点至少有 1 个链接的条件随机连接它们。
- 每次我们添加一个新节点 n ,它小于或等于 n 0 个链接,将新节点连接到网络中已有的 n 个节点。
- 现在一个节点连接到一个特定节点的概率将取决于它的程度。 (优先附件)。
方法 –以下是实施 Barabasi Albert 模型的步骤:
- 取一个有 n 个节点的图。
- 从用户处获取 m,即要连接到新节点的边数。
- 取m0,即初始节点数,使得m<=m0。
- 现在添加 n-m0 节点。
- 现在根据优先附件向这些 n-m0 节点添加边。
下面是 Barabasi Albert 模型的实现。
Python3
import networkx as nx
import random
import matplotlib.pyplot as plt
def display(g, i, ne):
pos = nx.circular_layout(g)
if i == '' and ne == '':
new_node = []
rest_nodes = g.nodes()
new_edges = []
rest_edges = g.edges()
else:
new_node = [i]
rest_nodes = list(set(g.nodes()) - set(new_node))
new_edges = ne
rest_edges = list(set(g.edges()) - set(new_edges) - set([(b, a) for (a, b) in new_edges]))
nx.draw_networkx_nodes(g, pos, nodelist=new_node, node_color='g')
nx.draw_networkx_nodes(g, pos, nodelist=rest_nodes, node_color='r')
nx.draw_networkx_edges(g, pos, edgelist=new_edges, style='dashdot')
nx.draw_networkx_edges(g, pos, edgelist=rest_edges,)
plt.show()
def barabasi_add_nodes(g, n, m0):
m = m0 - 1
for i in range(m0 + 1, n + 1):
g.add_node(i)
degrees = nx.degree(g)
node_prob = {}
s = 0
for j in degrees:
s += j[1]
print(g.nodes())
for each in g.nodes():
node_prob[each] = (float)(degrees[each]) / s
node_probabilities_cum = []
prev = 0
for n, p in node_prob.items():
temp = [n, prev + p]
node_probabilities_cum.append(temp)
prev += p
new_edges = []
num_edges_added = 0
target_nodes = []
while (num_edges_added < m):
prev_cum = 0
r = random.random()
k = 0
while (not (r > prev_cum and r <= node_probabilities_cum[k][1])):
prev_cum = node_probabilities_cum[k][1]
k = k + 1
target_node = node_probabilities_cum[k][0]
if target_node in target_nodes:
continue
else:
target_nodes.append(target_node)
g.add_edge(i, target_node)
num_edges_added += 1
new_edges.append((i, target_node))
print(num_edges_added, ' edges added')
display(g, i, new_edges)
return g
def plot_deg_dist(g):
all_degrees = []
for i in nx.degree(g):
all_degrees.append(i[1])
unique_degrees = list(set(all_degrees))
unique_degrees.sort()
count_of_degrees = []
for i in unique_degrees:
c = all_degrees.count(i)
count_of_degrees.append(c)
print(unique_degrees)
print(count_of_degrees)
plt.plot(unique_degrees, count_of_degrees, 'ro-')
plt.xlabel('Degrees')
plt.ylabel('Number of Nodes')
plt.title('Degree Distribution')
plt.show()
N = 10
m0 = random.randint(2, N / 5)
g = nx.path_graph(m0)
display(g, '', '')
g = barabasi_add_nodes(g, N, m0)
plot_deg_dist(g)
输出:
Enter the value of n: 10
3
[0, 1, 3]
1 edges added
[0, 1, 3, 4]
1 edges added
[0, 1, 3, 4, 5]
1 edges added
[0, 1, 3, 4, 5, 6]
1 edges added
[0, 1, 3, 4, 5, 6, 7]
1 edges added
[0, 1, 3, 4, 5, 6, 7, 8]
1 edges added
[0, 1, 3, 4, 5, 6, 7, 8, 9]
1 edges added
[0, 1, 3, 4, 5, 6, 7, 8, 9, 10]
1 edges added
[1, 2, 3, 6]
[7, 1, 1, 1]