参数“弱”或“强”对scipy.sparse.csgraph.connected_components有什么作用?

时间:2019-06-17 19:48:41

标签: python numpy graph scipy graph-algorithm

下面的代码演示了有向图:

Nodes: 0, 1, 2
Edges: [0 -> 1], [2 -> 1]

被认为是一个弱连接的组件,是三个强连接的组件。

assert(scipy.sparse.csgraph.connected_components(np.array([[0,1,0], [0,0,0], [0,1,0]]), directed=True, connection='weak', return_labels=True) == (1, array([0, 0, 0], dtype=int32)))

assert(scipy.sparse.csgraph.connected_components(np.array([[0,1,0], [0,0,0], [0,1,0]]), directed=True, connection='strong', return_labels=True) == (3, array([1, 0, 2], dtype=int32)))

我理解为什么强连接的组件返回值有意义-我无法从0遍历到2,也不能从1遍历到1或2。

但是根据文档:

directedbool, optional
    If True (default), then operate on a directed graph: only move
    from point i to point j along paths csgraph[i, j]. If False,
    then find the shortest path on an undirected graph: the
    algorithm can progress from point i to j along csgraph[i, j]
    or csgraph[j, i].

connectionstr, optional    
    [‘weak’|’strong’]. For directed graphs, the type of connection
    to use. Nodes i and j are strongly connected if a path exists
    both from i to j and from j to i. Nodes i and j are weakly
    connected if only one of these paths exists. If directed ==
    False, this keyword is not referenced.

'弱'连接的组件不应该存在,因为2不能通过1到达,而1不能通过2到达。

这是怎么回事?文档正确吗?

1 个答案:

答案 0 :(得分:1)

该文档不正确,将在Scipy的将来版本中进行更新。参见https://github.com/scipy/scipy/issues/9861

将来的文档将显示为:

connection : str, optional
    ['weak'|'strong'].  For directed graphs, the type of connection to
    use.  Nodes i and j are strongly connected if a path exists both
    from i to j and from j to i. A directed graph is weakly connected
    if replacing all of its directed edges with undirected edges produces
    a connected (undirected) graph. If directed == False, this keyword
    is not referenced.

这是弱连接​​的标准定义。