📜  社交网络上的 K-shell 分解

📅  最后修改于: 2021-09-03 13:38:37             🧑  作者: Mango

先决条件:社交网络简介

K-shell分解是一种我们可以根据节点的度数来划分节点的方法,例如一个桶中度数为1的节点等。

考虑一个例子,假设有 n 个节点,你在其中应用 k-shell 分解。因此,度数为 1 的节点将在bucket1 中,那么我们将看到在断开这些节点的连接后,是否还有度数为 1 的节点,如果是,那么我们将它们添加到桶 1 中,并再次检查并重复这些步骤的度数 2、3 和依此类推,然后将它们放入bucket2bucket3等中。

具有 7 个节点的初始图

在上图中,我们首先将度数为 1 的节点放入桶 1,即节点 3 和 7。之后,我们将删除节点 3 和 7,并检查是否还有度数为 1 的节点,即节点 6。现在我们将删除节点 6 并检查剩下的任何度数为 1 的节点,即节点 5。所以我们将删除节点 5 并再次检查,但没有剩余度数为 1 的节点,所以现在我们将检查度数为 2 的节点,即节点 1, 2 和 4,现在图中还有一个节点。所以bucket1 = [3, 7, 6, 5]bucket2 = [1, 2, 4]

以下是社交网络上 K-shell 分解的实现:

Python3
# Import required modules
import networkx as nx
import matplotlib.pyplot as plt
  
  
# Check if there is any node left with degree d
def check(h, d):
    f = 0  # there is no node of deg <= d
    for i in h.nodes():
        if (h.degree(i) <= d):
            f = 1
            break
    return f
  
  
# Find list of nodes with particular degree
def find_nodes(h, it):
    set1 = []
    for i in h.nodes():
        if (h.degree(i) <= it):
            set1.append(i)
    return set1
  
  
# Create graph object and add nodes
g = nx.Graph()
g.add_edges_from(
    [(1, 2), (1, 9), (3, 13), (4, 6),
     (5, 6), (5, 7), (5, 8), (5, 9), 
     (5, 10), (5, 11), (5, 12), (10, 12), 
     (10, 13), (11, 14), (12, 14), 
     (12, 15), (13, 14), (13, 15), 
     (13, 17), (14, 15), (15, 16)])
  
  
# Copy the graph
h = g.copy()
it = 1
  
  
# Bucket being filled currently
tmp = []
  
  
# list of lists of buckets
buckets = []
while (1):
    flag = check(h, it)
    if (flag == 0):
        it += 1
        buckets.append(tmp)
        tmp = []
    if (flag == 1):
        node_set = find_nodes(h, it)
        for each in node_set:
            h.remove_node(each)
            tmp.append(each)
    if (h.number_of_nodes() == 0):
        buckets.append(tmp)
        break
print(buckets)
  
  
# Illustrate the Social Network 
# in the form of a graph
nx.draw(g, with_labels=1)
plt.show()


输出:

[[2, 3, 4, 7, 8, 17, 16, 1, 6, 9], [11, 5, 10, 13, 12, 14, 15]]

具有 17 个节点的图