📜  鄂尔多斯-仁义模型在社交网络上的实现

📅  最后修改于: 2021-09-06 06:08:10             🧑  作者: Mango

先决条件:社交网络概论,鄂尔多斯-仁义模型

鄂尔多斯人一模型用于在社交网络上创建随机网络或图。在 Erdos Reny 模型中,每条边都有固定的存在和不存在概率,独立于网络中的边。

使用 Erdos-Renyi 模型实现社交网络:

步骤 1)导入必要的模块,如networkxmatplotlib.pyplotrandom模块。

Python3
# Import Required modules
import networkx as nx
import matplotlib.pyplot as plt
import random


Python3
# Distribution graph for Erdos_Renyi model
def distribution_graph(g):
    print(nx.degree(g))
    all_node_degree = list(dict((nx.degree(g))).values())
  
    unique_degree = list(set(all_node_degree))
    unique_degree.sort()
    nodes_with_degree = []
    for i in unique_degree:
        nodes_with_degree.append(all_node_degree.count(i))
  
    plt.plot(unique_degree, nodes_with_degree)
    plt.xlabel("Degrees")
    plt.ylabel("No. of nodes")
    plt.title("Degree distribution")
    plt.show()


Python3
# Take N number of nodes as input
print("Enter number of nodes")
N = int(input())


Python3
# Take P probability value for edges
print("Enter value of probability of every node")
P = float(input())


Python3
# Create an empty graph object
g = nx.Graph()
  
# Adding nodes
g.add_nodes_from(range(1, N + 1))


Python3
# Add edges to the graph randomly.
for i in g.nodes():
    for j in g.nodes():
        if (i < j):
              
            # Take random number R.
            R = random.random()
              
            # Check if R


Python3
# Display connection between nodes    
distribution_graph(g)


Python3
# Implementation of Erdos-Renyi Model on a Social Network
  
  
# Import Required modules
import networkx as nx
import matplotlib.pyplot as plt
import random
  
  
# Distribution graph for Erdos_Renyi model
def distribution_graph(g):
    print(nx.degree(g))
    all_node_degree = list(dict((nx.degree(g))).values())
  
    unique_degree = list(set(all_node_degree))
    unique_degree.sort()
    nodes_with_degree = []
    for i in unique_degree:
        nodes_with_degree.append(all_node_degree.count(i))
  
    plt.plot(unique_degree, nodes_with_degree)
    plt.xlabel("Degrees")
    plt.ylabel("No. of nodes")
    plt.title("Degree distribution")
    plt.show()
  
          
# Take N number of nodes from user
print("Enter number of nodes")
N = int(input())
  
  
# Take P probability value for edges
print("Enter value of probability of every node")
P = float(input())
  
  
# Create an empty graph object
g = nx.Graph()
  
  
# Adding nodes
g.add_nodes_from(range(1, N + 1))
  
  
# Add edges to the graph randomly.
for i in g.nodes():
    for j in g.nodes():
        if (i < j):
              
            # Take random number R.
            R = random.random()
              
            # Check if R


步骤 2)为模型创建分布图。

蟒蛇3

# Distribution graph for Erdos_Renyi model
def distribution_graph(g):
    print(nx.degree(g))
    all_node_degree = list(dict((nx.degree(g))).values())
  
    unique_degree = list(set(all_node_degree))
    unique_degree.sort()
    nodes_with_degree = []
    for i in unique_degree:
        nodes_with_degree.append(all_node_degree.count(i))
  
    plt.plot(unique_degree, nodes_with_degree)
    plt.xlabel("Degrees")
    plt.ylabel("No. of nodes")
    plt.title("Degree distribution")
    plt.show()

步骤 3)从用户那里取N即节点数。

蟒蛇3

# Take N number of nodes as input
print("Enter number of nodes")
N = int(input())

步骤 4)现在取P,即用户对边的概率。

蟒蛇3

# Take P probability value for edges
print("Enter value of probability of every node")
P = float(input())

步骤5)创建一个没有任何边的N个节点的图。

蟒蛇3

# Create an empty graph object
g = nx.Graph()
  
# Adding nodes
g.add_nodes_from(range(1, N + 1))

Step 6)将边随机加入图中,取一对节点,得到一个随机数R。如果R (概率),则添加一条边。对所有可能的节点对重复步骤 5 和 6,然后显示形成的整个社交网络(图)。

蟒蛇3

# Add edges to the graph randomly.
for i in g.nodes():
    for j in g.nodes():
        if (i < j):
              
            # Take random number R.
            R = random.random()
              
            # Check if R

步骤 7)显示连接节点。

蟒蛇3

# Display connection between nodes    
distribution_graph(g)

以下是上述逐步方法的完整程序:

蟒蛇3

# Implementation of Erdos-Renyi Model on a Social Network
  
  
# Import Required modules
import networkx as nx
import matplotlib.pyplot as plt
import random
  
  
# Distribution graph for Erdos_Renyi model
def distribution_graph(g):
    print(nx.degree(g))
    all_node_degree = list(dict((nx.degree(g))).values())
  
    unique_degree = list(set(all_node_degree))
    unique_degree.sort()
    nodes_with_degree = []
    for i in unique_degree:
        nodes_with_degree.append(all_node_degree.count(i))
  
    plt.plot(unique_degree, nodes_with_degree)
    plt.xlabel("Degrees")
    plt.ylabel("No. of nodes")
    plt.title("Degree distribution")
    plt.show()
  
          
# Take N number of nodes from user
print("Enter number of nodes")
N = int(input())
  
  
# Take P probability value for edges
print("Enter value of probability of every node")
P = float(input())
  
  
# Create an empty graph object
g = nx.Graph()
  
  
# Adding nodes
g.add_nodes_from(range(1, N + 1))
  
  
# Add edges to the graph randomly.
for i in g.nodes():
    for j in g.nodes():
        if (i < j):
              
            # Take random number R.
            R = random.random()
              
            # Check if R

输出:

在图中随机添加边

鄂尔多斯-仁义模型在上述程序上的实现分布图: