Javascript中有向无环图中两个节点之间的路径遍历
在DAG中查找两个节点之间的所有路径时遇到问题。我确信之前已经讨论过了,但是我在代码和算法方面遇到了问题 我有一个数据结构为:Javascript中有向无环图中两个节点之间的路径遍历,javascript,directed-acyclic-graphs,Javascript,Directed Acyclic Graphs,在DAG中查找两个节点之间的所有路径时遇到问题。我确信之前已经讨论过了,但是我在代码和算法方面遇到了问题 我有一个数据结构为: { "graph": [ { "source": "node-6", "target": "node-endpointsuccess", "type": "success" }, { "source": "node-7"
{
"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
要么failure
)
我需要从这些信息中得到的是“开始”(在数据中表示为“源”:“节点根”)和故障(节点端点故障
)之间的所有路径。我希望通过边类型返回节点根
和节点端点失败
之间的多维路径。类似这样的输出:
[
['failure', 'failure', 'failure'],
['failure', 'failure', 'success', 'failure'],
['failure', 'success', 'failure'],
['success', 'success', 'failure', 'failure'],
...etc
]
我尝试了一些DFS算法,但是一旦找到端点,我就无法从函数中提取路径。以下是到目前为止我得到的信息:
function getPaths(from, to, nodes, visited) {
results = [];
// for all nodes
for (var n in nodes) {
// get the nodes that are connected to this one
if (nodes[n].source == from) {
// check if we have found an endpoint
if (nodes[n].target == to) {
// found an endpoint, return the edge type
console.info('paydirt');
results.push(nodes[n].type);
return results;
}
// follow that path
results = getPaths(nodes[n].source, to, nodes, visited);
// add to visited list
for (var r in results)
visited.push(results[r].type);
}
}
return visited;
}
getPaths('node-root', 'node-endpointfailure', data, []);
我试过在这里使用算法。看起来是一个很好的例子,但我不确定我遗漏了什么:DAG表示有向无环图。你说你有一个DAG,但你说最终你想检测周期?DAG不能有循环。此外,如果类型和位置只是一些不相关的实现细节,则应该完全从您的帖子中删除