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

📅  最后修改于: 2021-04-17 09:59:40             🧑  作者: Mango

先决条件:社交网络简介

K-shell分解是一种方法,其中我们可以根据节点的度数(例如,在一个存储桶中具有度1的节点)对节点进行划分。

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

具有7个节点的初始图

首先在上图中,我们将等级1的节点放入存储桶1中,即节点3和7。之后,我们将删除节点3和7,并检查是否还剩下等级1的节点,即节点6。删除节点6并检查是否剩下节点1的度1,即节点5。因此,我们将删除节点5并再次检查,但节点1没有剩余,所以现在我们将检查节点2的度2。 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个节点的图