Python networkx将子图与松散节点连接

Python networkx将子图与松散节点连接,python,networkx,Python,Networkx,假设我们有下图(请参见下图): 在某一时刻,有一个子图由几个相互连接的节点和一个没有边的节点组成: nx.draw(G.subgraph([1,2,3,11]), with_labels=True) 由于不完整子图的所有节点来自同一个完整图,因此它们可以连接(例如,通过短路径),如下图所示: nx.draw(G.subgraph([1,2,3,4,5,6,10,11]), with_labels=True) 我想知道是否存在一个networkx函数,该函数在给定一组(或任意一个可数个节

假设我们有下图(请参见下图):

在某一时刻,有一个子图由几个相互连接的节点和一个没有边的节点组成:

nx.draw(G.subgraph([1,2,3,11]), with_labels=True)

由于不完整子图的所有节点来自同一个完整图,因此它们可以连接(例如,通过短路径),如下图所示:

nx.draw(G.subgraph([1,2,3,4,5,6,10,11]), with_labels=True)

我想知道是否存在一个
networkx
函数,该函数在给定一组(或任意一个可数个节点)的情况下返回一个子图,以确保所有节点都已连接。换句话说(具体来说),我希望将列表
[1,2,3,4,5,6,10,11]
作为函数的输出

澄清编辑 既然我似乎被误解了,我就再举几个例子

第一个例子 输出节点是使子图连接起来的节点,即子图中没有松散的端点

第二个例子 例3 通常,有一个函数基于
选择的\u节点创建一个子图,其中所有节点都以某种方式连接(边取自
图形


实现目标的最简单方法是计算所选节点之间的所有最短路径,然后确定形成这些路径的节点集

#!/usr/bin/env python
"""
Find the subgraph G' induced on G, that
1) contain all nodes in a set of nodes V', and
2) is a connected component.

See also:
https://stackoverflow.com/questions/58076592/python-networkx-connect-subgraph-with-a-loose-node
"""
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
import itertools

def get_connected_subgraph(graph, v_prime):
    """Given a graph G=(V,E), and a vertex set V', find the V'', that
    1) is a superset of V', and
    2) when used to induce a subgraph on G forms a connected component.

    Arguments:
    ----------
    G : networkx.Graph object
        The full graph.
    v_prime : list
        The chosen vertex set.

    Returns:
    --------
    v_prime_prime : set
        The set of nodes fullfilling criteria 1) and 2).

    """
    vpp = set()
    for source, target in itertools.combinations(v_prime, 2):
        paths = nx.all_shortest_paths(graph, source, target)
        for path in paths:
            vpp = vpp.union(path)
    return vpp

def test_1():
    G1 = nx.Graph()
    G1.add_edges_from([(1,2), (1,3), (2,3), (2,4), (3,4), (4,5), (5,6), (6,7), (5,7), (5,8), (8,9), (9,10), (10,12), (10,11), (11,12)])
    chosen_nodes = [1,2,3,12] # construct a subgraph based on those nodes
    output_nodes = [1,2,3,4,5,8,9,10,12]
    returned_nodes = get_connected_subgraph(G1, chosen_nodes)
    assert set(output_nodes) == returned_nodes

def test_2():
    G2 = nx.Graph()
    G2.add_edges_from([(1,2), (2,3), (3,4), (4,5), (5,6), (4,6)])
    chosen_nodes = [1,2,5]
    output_nodes = [1,2,3,4,5]
    returned_nodes = get_connected_subgraph(G2, chosen_nodes)
    assert set(output_nodes) == returned_nodes

def test_3():
    G3 = nx.Graph()
    G3.add_edges_from([(1,2), (2,3), (3,5), (2,4), (4,5)])
    chosen_nodes = [1,2,3]
    output_nodes = [1,2,3]
    returned_nodes = get_connected_subgraph(G3, chosen_nodes)
    assert set(output_nodes) == returned_nodes

您正在寻找连接的组件;对于给定的图
g
networkx.connected_components(g)
返回所有连接组件的迭代器,首先是最大组件。在我的例子中
networkx.connected_components(g)
返回一个只有一个元素的生成器(我只能使用
next()
一次,然后出现
IterationError
)。此元素由
G
中的所有节点组成。输出应该是这些元素的子集。您是在子图上调用了
connected_components
,还是在原始图上调用了
connected_components
。在子图上调用它会生成一个包含两个元素的生成器:
[{11},{1,2,3}]
。这就是您要查找的吗?
G1 = nx.Graph()

G1.add_edges_from([(1,2), (1,3), (2,3), (2,4), (3,4), (4,5), (5,6), (6,7), (5,7), (5,8), (8,9), (9,10), (10,12), (10,11), (11,12)])

chosen_nodes = [1,2,3,12] # construct a subgraph based on those nodes

output_nodes = [1,2,3,4,5,8,9,10,12]
G2 = nx.Graph()

G2.add_edges_from([(1,2), (2,3), (3,4), (4,5), (5,6), (4,6)])

chosen_nodes = [1,2,5]

output_nodes = [1,2,3,4,5]
G3 = nx.Graph()

G3.add_edges_from([(1,2), (2,3), (3,5), (2,4), (4,5)])

chosen_nodes = [1,2,3]

output_nodes = [1,2,3]
def get_connected_subgraph(graph, chosen_nodes):
    return output_nodes
#!/usr/bin/env python
"""
Find the subgraph G' induced on G, that
1) contain all nodes in a set of nodes V', and
2) is a connected component.

See also:
https://stackoverflow.com/questions/58076592/python-networkx-connect-subgraph-with-a-loose-node
"""
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
import itertools

def get_connected_subgraph(graph, v_prime):
    """Given a graph G=(V,E), and a vertex set V', find the V'', that
    1) is a superset of V', and
    2) when used to induce a subgraph on G forms a connected component.

    Arguments:
    ----------
    G : networkx.Graph object
        The full graph.
    v_prime : list
        The chosen vertex set.

    Returns:
    --------
    v_prime_prime : set
        The set of nodes fullfilling criteria 1) and 2).

    """
    vpp = set()
    for source, target in itertools.combinations(v_prime, 2):
        paths = nx.all_shortest_paths(graph, source, target)
        for path in paths:
            vpp = vpp.union(path)
    return vpp

def test_1():
    G1 = nx.Graph()
    G1.add_edges_from([(1,2), (1,3), (2,3), (2,4), (3,4), (4,5), (5,6), (6,7), (5,7), (5,8), (8,9), (9,10), (10,12), (10,11), (11,12)])
    chosen_nodes = [1,2,3,12] # construct a subgraph based on those nodes
    output_nodes = [1,2,3,4,5,8,9,10,12]
    returned_nodes = get_connected_subgraph(G1, chosen_nodes)
    assert set(output_nodes) == returned_nodes

def test_2():
    G2 = nx.Graph()
    G2.add_edges_from([(1,2), (2,3), (3,4), (4,5), (5,6), (4,6)])
    chosen_nodes = [1,2,5]
    output_nodes = [1,2,3,4,5]
    returned_nodes = get_connected_subgraph(G2, chosen_nodes)
    assert set(output_nodes) == returned_nodes

def test_3():
    G3 = nx.Graph()
    G3.add_edges_from([(1,2), (2,3), (3,5), (2,4), (4,5)])
    chosen_nodes = [1,2,3]
    output_nodes = [1,2,3]
    returned_nodes = get_connected_subgraph(G3, chosen_nodes)
    assert set(output_nodes) == returned_nodes