简体   繁体   English

Python:深度优先搜索(DFS)输出后序数

[英]Python: Depth-First-Search (DFS) output post-order number

I want to find out which tuple-node with the same tuple elements (eg (1,1), (2,2) or (i, i)) is the source in a particular graph, ie which node has the highest post-order number.我想找出具有相同元组元素(例如 (1,1)、(2,2) 或 (i, i))的哪个元组节点是特定图中的源,即哪个节点具有最高的 post-订单号。 I want to find the source by applying DFS to it and take the number with the highest post-order number as my source node for further usage.我想通过对它应用 DFS 来找到源,并将具有最高后序编号的编号作为我的源节点以供进一步使用。 Assume, you have the following graph:假设您有以下图表:

graph={
    (1,1): [(1,2),(2,2)],
    (1,2): [(1,3)],
    (1,3): [(1,2),(2,3)],
    (2,2): [(3,3)],
    (2,3): [],
    (3,3): [(2,2)],
}

Now I have this iterative dfs function (I have to do it iteratively because I have a massive stack).现在我有了这个迭代 dfs 函数(我必须迭代地执行它,因为我有一个巨大的堆栈)。 I was not sure how to extend it to return the node with the highest post-order number.我不确定如何扩展它以返回具有最高后序编号的节点。

def dfs_iterative_starting(graph, n):
    # n is the number different numbers (e.g. (1,1), (2,2) or (i,i))
    # array in which I'll save the post-order numbers. The index indicates the node, e.g. index 1 -> (1,1)
    arr = [0]*(n+1)
    # starting node is (1,1)
    stack, path = [(1,1)], []
    # counter for the post-order number
    counter = 1
    while stack:
        vertex = stack.pop()
        if vertex in path:
            continue
        path.append(vertex)

        # counting post-order number????
        i, j = vertex
        if i == j:
            arr[i] = counter
        for neighbor in graph[vertex]:
            stack.append(neighbor)

            # counting post-order number????
            k, j = neighbor
            counter += 1
            if k == j:
                arr[k] = counter
    print(arr)
    return arr.index(max(arr))

For the above-mentioned example, it returns 2 even though the correct answer would be 1. If I print arr, I get the following list [0, 1, 5, 4]对于上述示例,即使正确答案是 1,它也会返回 2。如果我打印 arr,我会得到以下列表 [0, 1, 5, 4]

In the recursive implementation, we'd have a subsequent operation to add to the post-order list the node whose neighbours we first explore.在递归实现中,我们将有一个后续操作将我们首先探索其邻居的节点添加到后序列表中。 This has to be pushed to the stack first.这必须首先被推入堆栈。 It might help to distinguish what to do with the stack element.它可能有助于区分如何处理堆栈元素。 Here's one example:下面是一个例子:

JavaScript code (sorry, on smartphone and have no access to Python): JavaScript 代码(抱歉,在智能手机上无法访问 Python):

 function f(graph){ let stack = [['(1,1)', 'explore']]; let path = new Set(); let post_order = []; while (stack.length){ let [vertex, action] = stack.pop(); if (action == 'mark'){ post_order.push(vertex); continue; } path.add(vertex); stack.push([vertex, 'mark']); for (let neighbour of graph[vertex]){ if (!path.has(neighbour)) stack.push([neighbour, 'explore']); } } console.log('Path: ' + JSON.stringify(Array.from(path))); return post_order; } var g = { '(1,1)': ['(1,2)','(2,2)'], '(1,2)': ['(1,3)'], '(1,3)': ['(1,2)','(2,3)'], '(2,2)': ['(3,3)'], '(2,3)': [], '(3,3)': ['(2,2)'], } /* (1,1) -> (1,2) <-> (1,3) -> (2,3) \\ (2,2) <-> (3,3) */ console.log(JSON.stringify(f(g)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM