在Javascript中的有向非循环图中遍历两个节点之间的路径

时间:2013-02-11 00:39:57

标签: javascript directed-acyclic-graphs

我在找到DAG中两个节点之间的所有路径时遇到问题。我确定之前已经介绍过,但是我遇到了代码问题,而且算法也很少。

我的DAG数据结构为:

{
    "graph": [
        {
            "source": "node-6",
            "target": "node-endpointsuccess",
            "type": "success"
        },
        {
            "source": "node-7",
            "target": "node-endpointsuccess",
            "type": "success"
        },
        {
            "source": "node-7",
            "target": "node-endpointfailure",
            "type": "failure"
        },
        {
            "source": "node-6",
            "target": "node-7",
            "type": "failure"
        },
        {
            "source": "node-2",
            "target": "node-6",
            "type": "success"            },
        {
            "source": "node-7",
            "target": "node-endpointsuccess",
            "type": "success"
        },
        {
            "source": "node-7",
            "target": "node-endpointfailure",
            "type": "failure"
        },
        {
            "source": "node-5",
            "target": "node-7",
            "type": "success"
        },
        {
            "source": "node-4",
            "target": "node-endpointsuccess",
            "type": "success"
        },
        {
            "source": "node-4",
            "target": "node-endpointfailure",
            "type": "failure"
        },
        {
            "source": "node-5",
            "target": "node-4",
            "type": "failure"
        },
        {
            "source": "node-2",
            "target": "node-5",
            "type": "failure"
        },
        {
            "source": "node-root",
            "target": "node-2",
            "type": "success"
        },
        {
            "source": "node-4",
            "target": "node-endpointsuccess",
            "type": "success"
        },
        {
            "source": "node-4",
            "target": "node-endpointfailure",
            "type": "failure"
        },
        {
            "source": "node-1",
            "target": "node-4",
            "type": "success"
        },
        {
            "source": "node-3",
            "target": "node-endpointfailure",
            "type": "failure"
        },
        {
            "source": "node-1",
            "target": "node-3",
            "type": "failure"
        },
        {
            "source": "node-root",
            "target": "node-1",
            "type": "failure"
        },
        {
            "source": "node-3",
            "target": "node-8",
            "type": "success"
        },
        {
            "source": "node-8",
            "target": "node-endpointfailure",
            "type": "failure"
        },
        {
            "source": "node-8",
            "target": "node-endpointsuccess",
            "type": "success"
        },
        {
            "source": "node-endpointsuccess"
        },
        {
            "source": "node-endpointfailure"
        }
    ]
}

position对象用于保存页面上节点的位置(此问题不需要),type是边缘类型(success或{{1 }})。

我需要从这些信息中得到的是“开始”(在数据中表示为failure)和失败('source': 'node-root')之间的所有路径。我希望在node-endpointfailurenode-root之间按边类型返回多维路径。这样的东西就是输出:

node-endpointfailure

我已经尝试了一些DFS算法,但是一旦我找到了我的端点,我就会陷入阻碍功能的路径。这是我到目前为止所得到的:

[
    ['failure', 'failure', 'failure'],
    ['failure', 'failure', 'success', 'failure'],
    ['failure', 'success', 'failure'],
    ['success', 'success', 'failure', 'failure'],
    ...etc
]

我在这里尝试过使用这个算法。看起来很不错,但我不确定我错过了什么:https://stackoverflow.com/a/13852674/881011

0 个答案:

没有答案