重建DJIKSTRA算法的最短路径树的复杂性

发布于 2025-02-12 16:05:15 字数 1276 浏览 0 评论 0原文

我目前正在阅读 python中的数据结构和算法第669页。这本书说重建最短的路径树需要O(n+m)时间。但是,鉴于代码,我不明白为什么是O(n+m)而不是O(n*m)(因为我们有两个嵌套在一个顶点上,而另一个则在传入的边缘上。)

这本书说:

在本节中,我们证明了植根于 源S可以在O(N + M)时间中重建,给定D [V]集合。 Dijkstra的算法使用S作为源产生的值。像我们 在表示DFS和BFS树时,我们将映射每个顶点v =/= s to partent u(可能是u = s),使得u是在v之前的顶点,从s到v。如果u是 在从s到v的最短路径上v之前的顶点,一定是 D [U] + W(U,V)= D [V]。相反,如果满足上述方程式, 然后,从s到u的最短路径,然后是边缘(u,v)是一个 v。我们实施的最短路径将基于 此逻辑,测试每个顶点V的所有传入边缘,寻找一个 (u,v)满足密钥方程式。运行时间为o(n + m), 当我们考虑每个顶点和所有传入的边缘时。

这是实施的代码:

def shortest_path_tree(g, s, d):
    """Reconstruct shortest-path tree rooted at vertex s, given distance map d.
    
    Return tree as a map from each reachable vertex v (other than s) 
    to the edge e=(u,v) that is used to reach v from its parent u in the tree.
    """
    tree = {}
    for v in d:
        if v is not s:
            for e in g.incident_edges(v, False):      # consider INCOMING edges
                u = e.opposite(v)
                wgt = e.element()
                if d[v] == d[u] + wgt:
                    tree[v] = e                       # edge e is used to reach v
    return tree

谢谢!

I am currently reading Data Structures and Algorithms in Python and I am in Chapter 14 page 669. The book says that reconstructing a shortest path tree takes O(n+m) time. but given the code I couldn't understand why it's O(n+m) and not O(n*m) (because we have two nested for loops one over the vertices and the other over the incoming edges.)

The book says:

In this section, we demonstrate that the shortest-path tree rooted at
source s can be reconstructed in O(n + m) time, given the set of d[v]
values produced by Dijkstra’s algorithm using s as the source. As we
did when representing the DFS and BFS trees, we will map each vertex v
=/= s to a parent u (possibly, u = s), such that u is the vertex immediately before v on a shortest path from s to v. If u is the
vertex just before v on the shortest path from s to v, it must be that
d[u] + w(u, v) = d[v]. Conversely, if the above equation is satisfied,
then the shortest path from s to u, followed by the edge (u, v) is a
shortest path to v. Our implementation reconstructs the tree based on
this logic, testing all incoming edges to each vertex v, looking for a
(u, v) that satisfies the key equation. The running time is O(n + m),
as we consider each vertex and all incoming edges to those vertices.

and here is the code of the implementation:

def shortest_path_tree(g, s, d):
    """Reconstruct shortest-path tree rooted at vertex s, given distance map d.
    
    Return tree as a map from each reachable vertex v (other than s) 
    to the edge e=(u,v) that is used to reach v from its parent u in the tree.
    """
    tree = {}
    for v in d:
        if v is not s:
            for e in g.incident_edges(v, False):      # consider INCOMING edges
                u = e.opposite(v)
                wgt = e.element()
                if d[v] == d[u] + wgt:
                    tree[v] = e                       # edge e is used to reach v
    return tree

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2025-02-19 16:05:15

让我们考虑您显示的代码的伪代码:

for each node 
      visit node
      get its edges
        visit the node connected

您可以看到每个边缘再访问一个节点。

想象一下m = 0,确切的访问数为节点的数量,

想象M = 1,确切的访问数为节点 +(通过边缘连接的两个节点每次多1个时间)

访问量=节点 +边数
因此,o(n + m)的复杂性

Lets consider pseudocode for the code you showed:

for each node 
      visit node
      get its edges
        visit the node connected

You can see that each edge adds one more visit to a node.

imagine m = 0, the exact number of visits is number of nodes

imagine m = 1, the exact number of visits is number of nodes + (1 more time each for the two nodes connected by the edge)

therefore number of visits = number of nodes + number of edges
and hence the complexity of O(n + m)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文