在整数堆栈或数组的图中查找路径

时间:2017-10-16 12:42:06

标签: python graph stack dfs

给出使用邻接矩阵表示的图(有序DAG)

g = [
        [0,1,1,0,0,0,0],
        [0,0,0,1,0,0,0],
        [0,0,0,1,0,0,0],
        [0,0,0,0,1,1,0],
        [0,0,0,0,0,0,1],
        [0,0,0,0,0,0,1],
        [0,0,0,0,0,0,0]
    ]

我编写了以下内容,以便从第一个节点(节点0)开始计算出图中所有最长的路径。

from collections import deque
stack = deque()
stack.append([0])
current_longest = 1
paths = []
N = len(g[0])

while stack:
    cur_path = stack.pop()
    print(cur_path)
    last_node = cur_path[-1]

    any_n = False
    for new_node in range(last_node + 1, N):
        if g[last_node][new_node] == 1:
            any_n = True
            stack.append(cur_path + [new_node])

    if any_n == False:
        if len(cur_path) > current_longest:
            nmb_paths = 1
            paths = []
            paths.append(cur_path) 
            current_longest = len(cur_path)
        elif len(cur_path) == current_longest:
            nmb_paths += 1
            paths.append(cur_path)    
print(f"paths = {paths}")

输出:

[0]
[0, 2]
[0, 2, 3]
[0, 2, 3, 5]
[0, 2, 3, 5, 6]
[0, 2, 3, 4]
[0, 2, 3, 4, 6]
[0, 1]
[0, 1, 3]
[0, 1, 3, 5]
[0, 1, 3, 5, 6]
[0, 1, 3, 4]
[0, 1, 3, 4, 6]
paths = [[0, 2, 3, 5, 6], [0, 2, 3, 4, 6], [0, 1, 3, 5, 6], [0, 1, 3, 4, 6]]

但我现在正试图弄清楚我是否可以在没有数组堆栈的情况下做到这一点。因此,如果我不能将数组存储为堆栈的元素,我只能使用整数堆栈或普通数组。我坚持这个,我认为这是跟踪列表长度的问题,因为它增长,存储每条路径,如果它没有增长,存储路径,但有点难倒atm,任何提示?

1 个答案:

答案 0 :(得分:0)

没什么兴趣,但是我觉得无论如何我都会提出我的解决方案,以防它帮助任何人(我在C中实现这一点,因此编写一个ArrayStack很痛苦)这是我的原型:

"events" : [ {
    "image" : "http://via.placeholder.com/512x800",
    "title" : "Easter Weekend Celebration",
    "day" : "10 April 2017",
    "photo" : "http://via.placeholder.com/512x512/000",
    "address" : "4 Pybus Rd, Sandton, New York“,
    "times" : "Sundays, From 08h00 - 11h00",
    "info" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod.”,
    "RegisterURL" : "https://www.google.com",
    "calendar" : "some-data"

路径的进展:

g = [
        [0,1,1,0,0,0,0],
        [0,0,0,1,0,0,0],
        [0,0,0,1,0,0,0],
        [0,0,0,0,1,1,0],
        [0,0,0,0,0,0,1],
        [0,0,0,0,0,0,1],
        [0,0,0,0,0,0,0]
    ]

N = len(g[0])

nmb_paths = 1
current_longest = 1
paths = []
array = [0]
current_length = 1
visited = [0] * N
visited[0] = 1
pred = [-1] * N


while current_length > 0:
    last_node = array[current_length-1]

    any_n = False
    new_node = last_node + 1

    print('paths = ', paths)

    while not any_n and new_node < N:
        if g[last_node][new_node] == 1 and visited[new_node] != 1:
            if pred[new_node] != last_node:
                pred[new_node] = last_node
                any_n = True

                array.append(new_node)
                visited[new_node] = 1
                current_length += 1
                last_node = new_node

                if current_length > current_longest:
                    paths = []
                    app = array.copy()
                    paths.append(app)
                    current_longest = current_length
                    nmb_paths = 1
                elif current_length == current_longest:
                    app = array.copy()
                    paths.append(app)
                    nmb_paths += 1

        new_node+=1

    if any_n == False:
        final = array[current_length - 1]
        del array[current_length-1]
        visited[final] = 0
        for j in range(final+1, N):
            pred[j] = -1
        current_length -= 1