先决条件-社交网络简介,Barabasi Albert Graph
在社交网络中,有一种现象称为“变得越来越富有”,也称为“优先依恋”。在“优先依恋”中,已经富裕的人变得越来越多,而富有的人的收入却越来越少。这称为“越来越丰富的现象”或“优先依恋”。
例如,假设班上有一些学生,每个学生都是与某些学生的朋友,这被称为学位,即学生的学历就是其所拥有的朋友的数量。现在,高学历的学生是有钱的,而低学历的学生是穷的。现在假设班上有一个新学生,他/她必须结交m个朋友,因此他/她将选择学位更高的学生并与他们成为朋友,从而增加了有钱人的程度。这称为“变得越来越富有”或“优先依恋” 。
Barabasi Albert模型是特惠附件的实现。
逻辑–以下是Barabasi Albert模型背后的逻辑:
- 拍摄一个具有n个0节点的随机图,并以每个节点至少具有1条链接的条件将它们随机连接。
- 每次我们添加一个小于或等于n 0个链接的新节点n,它将新节点连接到网络中已有的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]