找到两个2跳邻域图?

问题描述 投票:0回答:1

我有感兴趣的节点,并且能够提取子图(下面的代码),但我相信 NetworkX

subgraph()
induced_subgraph()
只能提取节点的 1 跳邻域。

## Extract subgraph using nodes of interest (list)
G_S = induced_subgraph(G, S) # S is nodes of interest (list)

我查看了Ego Graph,但它只提取单个节点上的图。 如何提取 2 或 3 跳(甚至更多)子图?如果无法使用 NetworkX,graph-tool 是否有可以提取 k 跳子图的功能?

尝试 2 跳子图:

def extend_subgraph(G, S):
    # Start with the original set of nodes of interest
    extended_nodes = set(S)
    
    # Loop over each node in the current subgraph
    for node in S:
        # Get 1-hop neighbors of this node
        one_hop = set(nx.neighbors(G, node))
        
        # For each 1-hop neighbor, get its 1-hop neighbors
        for neighbor in one_hop:
            if neighbor not in extended_nodes:
                extended_nodes.add(neighbor)
                neighbors_of_neighbor = set(nx.neighbors(G, neighbor))
                
                # Add the 1-hop neighbors of the neighbor if they haven't been seen
                extended_nodes.update(neighbors_of_neighbor - extended_nodes)

    # Create a new subgraph with the extended set of nodes
    G_extended_subgraph = G.subgraph(extended_nodes)
    
    return G_extended_subgraph

G2 = extend_subgraph(G, S)
python networkx graph-theory graph-tool
1个回答
0
投票

如果格式灵活,您可以使用 dgl 来表示您的图,并使用 khop_out_subgraph 函数提取所需的 k 跳子图。然后根据需要使用方法 to_networkx 转换回 networkx。

© www.soinside.com 2019 - 2024. All rights reserved.