python networkx.algorithms.approximation.steinertree 介绍

【未标明的翻译皆出自‘有道翻译’】

1、python 包 官方文档链接 :

Approximations and Heuristics — NetworkX 2.6rc1.dev0 documentation

复杂网络软件 — NetworkX 2.5 文档 (osgeo.cn)

2、官方文档里Steiner Tree所在位置如图:

3、Steiner Tree 函数的介绍

metric_closure(G[, weight])

Return the metric closure of a graph.

steiner_tree(G, terminal_nodes[, weight])

Return an approximation to the minimum Steiner tree of a graph.

  • networkx.algorithms.approximation.steinertree.metric_closure

metric_closure(Gweight='weight')[source]

Return the metric closure of a graph. 返回一个图的度量闭包

The metric closure of a graph G is the complete graph in which each edge is weighted by the shortest path distance between the nodes in G .

图G的度量闭包是一个完全图,其中每条边都被G中节点之间的最短路径距离加权。

Parameters

G :NetworkX graph

Returns :NetworkX graph    Metric closure of the graph G.

 

  • networkx.algorithms.approximation.steinertree.steiner_tree

steiner_tree(Gterminal_nodesweight='weight')[source]

Return an approximation to the minimum Steiner tree of a graph. 。返回图的最小斯坦纳树的近似值。

The minimum Steiner tree of G w.r.t a set of terminal_nodes is a tree within G that spans those nodes and has minimum size (sum of edge weights) among all such trees.

G的最小Steiner树(一组terminal_nodes)是G内部跨越这些节点且在所有这些树中具有最小大小(边权和)的树。

G的最小Steiner树是一组G内的树,该树跨越这些节点并且在所有此类树中具有最小的大小(边缘权重之和)。【谷歌翻译】

The minimum Steiner tree can be approximated by computing the minimum spanning tree of the subgraph of the metric closure of G induced by the terminal nodes, where the metric closure of G is the complete graph in which each edge is weighted by the shortest path distance between the nodes in G . This algorithm produces a tree whose weight is within a (2 - (2 / t)) factor of the weight of the optimal Steiner tree where t is number of terminal nodes.

最小Steiner树可以近似计算子图的最小生成树的度规关闭终端节点,引发的G, G的度量闭包是完全图中每条边的节点之间的最短路径距离加权的G。该算法生成的树的权值在最优Steiner树权值的(2 - (2 / t))因子之内,其中t为终端节点数。

可以通过计算由终端节点terminal nodes引起的G度量封闭的子图的最小生成树来近似最小Steiner树,其中G度量封闭是完整的图,其中每个边都由之间的最短路径距离加权 G中的节点 。该算法生成的树的权重在最佳Steiner树的权重的(2-(2 / t))因子之内,其中t是终端节点的数量。【谷歌翻译】

Parameters

G : NetworkX graph

terminal_nodes : list         A list of terminal nodes for which minimum steiner tree is to be found. 要为其找到最小斯坦纳树的终端节点列表。

Returns : NetworkX graph        Approximation to the minimum steiner tree of G induced by terminal_nodes . 由终端节点诱导的G最小steiner树的逼近。

  • Notes

For multigraphs, the edge between two nodes with minimum weight is the edge put into the Steiner tree.

对于多重图,权值最小的两个节点之间的边是放入Steiner树的边。

  • References

Steiner_tree_problem on Wikipedia. https://en.wikipedia.org/wiki/Steiner_tree_problem

4、函数源码 : networkx.algorithms.approximation.steinertree — NetworkX 2.6rc1.dev0 documentation

from itertools import chain

from networkx.utils import pairwise, not_implemented_for
import networkx as nx

__all__ = ["metric_closure", "steiner_tree"]


[docs]@not_implemented_for("directed")
def metric_closure(G, weight="weight"):
    """Return the metric closure of a graph.

    The metric closure of a graph *G* is the complete graph in which each edge
    is weighted by the shortest path distance between the nodes in *G* .

    Parameters
    ----------
    G : NetworkX graph

    Returns
    -------
    NetworkX graph
        Metric closure of the graph `G`.

    """
    M = nx.Graph()

    Gnodes = set(G)

    # check for connected graph while processing first node
    all_paths_iter = nx.all_pairs_dijkstra(G, weight=weight)
    u, (distance, path) = next(all_paths_iter)
    if Gnodes - set(distance):
        msg = "G is not a connected graph. metric_closure is not defined."
        raise nx.NetworkXError(msg)
    Gnodes.remove(u)
    for v in Gnodes:
        M.add_edge(u, v, distance=distance[v], path=path[v])

    # first node done -- now process the rest
    for u, (distance, path) in all_paths_iter:
        Gnodes.remove(u)
        for v in Gnodes:
            M.add_edge(u, v, distance=distance[v], path=path[v])

    return M
@not_implemented_for("directed")
def steiner_tree(G, terminal_nodes, weight="weight"):
    """Return an approximation to the minimum Steiner tree of a graph.

    The minimum Steiner tree of `G` w.r.t a set of `terminal_nodes`
    is a tree within `G` that spans those nodes and has minimum size
    (sum of edge weights) among all such trees.

    The minimum Steiner tree can be approximated by computing the minimum
    spanning tree of the subgraph of the metric closure of *G* induced by the
    terminal nodes, where the metric closure of *G* is the complete graph in
    which each edge is weighted by the shortest path distance between the
    nodes in *G* .
    This algorithm produces a tree whose weight is within a (2 - (2 / t))
    factor of the weight of the optimal Steiner tree where *t* is number of
    terminal nodes.

    Parameters
    ----------
    G : NetworkX graph

    terminal_nodes : list
         A list of terminal nodes for which minimum steiner tree is
         to be found.

    Returns
    -------
    NetworkX graph
        Approximation to the minimum steiner tree of `G` induced by
        `terminal_nodes` .

    Notes
    -----
    For multigraphs, the edge between two nodes with minimum weight is the
    edge put into the Steiner tree.


    References
    ----------
    .. [1] Steiner_tree_problem on Wikipedia.
       https://en.wikipedia.org/wiki/Steiner_tree_problem
    """
    # H is the subgraph induced by terminal_nodes in the metric closure M of G.
    M = metric_closure(G, weight=weight)
    H = M.subgraph(terminal_nodes)
    # Use the 'distance' attribute of each edge provided by M.
    mst_edges = nx.minimum_spanning_edges(H, weight="distance", data=True)
    # Create an iterator over each edge in each shortest path; repeats are okay
    edges = chain.from_iterable(pairwise(d["path"]) for u, v, d in mst_edges)
    # For multigraph we should add the minimal weight edge keys
    if G.is_multigraph():
        edges = (
            (u, v, min(G[u][v], key=lambda k: G[u][v][k][weight])) for u, v in edges
        )
    T = G.edge_subgraph(edges)
    return T

5、函数使用

(待补充)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值