python - Prim 算法生成的最小生成树的预序树遍历

标签 python algorithm tree preorder

我正在尝试实现一种近似算法来解决旅行商问题 (TSP),当三角不等式对边权重成立时可以使用该算法。正如 Cormen 等人在算法简介(第 3 3d.)中所述,伪代码是:

enter image description here

这是一个例子:

enter image description here

我苦恼的是如何在 Prim 算法生成的树上实现预序树遍历。由于这不是二叉搜索树,https://en.wikipedia.org/wiki/Tree_traversal#Pre-order_2 处给出的伪代码似乎不适用。

相反,节点具有 keyparent 属性,而不是具有 leftright 属性。下面是它们是如何在我的 Prim 算法实现中生成的(带有一个小测试用例):

import math
import copy
import pytest
import pandas as pd
from cached_property import cached_property


class Node(object):
    def __init__(self, key=math.inf, parent=None):
        self.key = key
        self.parent = parent

    def __lt__(self, other):
        return self.key < other.key


class Graph(object):
    def __init__(self, edges):
        self.edges = edges

    @cached_property
    def nodes(self):
        _nodes = set()
        for edge in self.edges:
            _nodes.add(edge[0])
            _nodes.add(edge[1])
        return {node: Node() for node in list(_nodes)}

    @cached_property
    def adj(self):
        A = {node: [] for node in self.nodes}
        for edge in self.edges:
            u, v, _ = edge
            A[u].append(v)
            A[v].append(u)
        return A

    @cached_property
    def w(self):
        N = len(self.nodes)
        none_array = [[None for _ in range(N)] for _ in range(N)]
        df = pd.DataFrame(none_array, index=sorted(self.nodes), columns=sorted(self.nodes))
        for edge in self.edges:
            u, v, weight = edge
            df.set_value(u, v, weight)
            df.set_value(v, u, weight)
        return df

    def mst_prim(self, root):
        r = self.nodes[root]
        r.key = 0
        Q = copy.copy(self.nodes)
        while Q:
            u = min(Q, key=Q.get)
            u_node = Q.pop(u)
            for v in self.adj[u]:
                if v in Q and self.w[u][v] < self.nodes[v].key:
                    self.nodes[v].parent = u
                    self.nodes[v].key = self.w[u][v]


@pytest.fixture
def edges_simple():
    return [('a', 'b', 4),
            ('a', 'h', 8),
            ('b', 'h', 11),
            ('h', 'i', 7),
            ('b', 'c', 8),
            ('h', 'g', 1),
            ('i', 'c', 2),
            ('i', 'g', 6),
            ('c', 'd', 7),
            ('g', 'f', 2),
            ('c', 'f', 4),
            ('d', 'f', 14),
            ('d', 'e', 9),
            ('f', 'e', 10)
            ]

def test_mst_prim(edges_simple):
    graph = Graph(edges_simple)
    graph.mst_prim(root='a')
    # print("\n")
    # for u, node in graph.nodes.items():
    #   print(u, node.__dict__)
    assert graph.nodes['a'].parent is None
    assert graph.nodes['i'].parent == 'c'
    assert graph.nodes['d'].parent == 'c'



if __name__ == "__main__":
    # pytest.main([__file__+"::test_mst_prim", "-s"])
    pytest.main([__file__, "-s"])

如何在此图上执行前序树遍历? (请注意,这个问题类似于 pre-order traversal of a Minimum spanning tree ,但我发现那里给出的答案相当高级)。

最佳答案

我建议您在 Node 类中添加一个名为 children 的新列表。

在您的 Prim 的 算法之后,您可以运行您获得的节点并将它们添加到它们父级的子级。复杂度为 O(n),所以没什么大不了的。之后,DFS 遍历就很容易了。

但同样,在 post 中您提到过,您必须为您的 child 选择一个顺序以进行 preorder 遍历。在你的情况下,当你只引用你的 parent 时,没有办法知道什么是 left-most child 。

关于python - Prim 算法生成的最小生成树的预序树遍历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46321226/

相关文章:

python - 减少 Django Web 应用程序中 ORM 查询的数量

python - 在聚类中向数据集添加条件

php - 使用 MySQL 从树中选择记录

c++ - 如何将音频字节转换为样本

c++ - 我们在 std::map 或 std::set 中有什么样的排序?

c++ - 如何修复我的 STL 样式容器以容纳不完整或抽象类型?

Haskell:二叉树深度的尾递归版本

python - ImportError : libpython3. 7m.so.1.0:无法打开共享对象文件:没有这样的文件或目录

python - 如何使用 type() 创建绑定(bind)方法?

java - 顺序搜索 Java