Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用具有父对象的给定javascript对象计算从节点X到其他节点的路径_Javascript_Jquery_Tree_Dijkstra - Fatal编程技术网

使用具有父对象的给定javascript对象计算从节点X到其他节点的路径

使用具有父对象的给定javascript对象计算从节点X到其他节点的路径,javascript,jquery,tree,dijkstra,Javascript,Jquery,Tree,Dijkstra,假设有一个图有7个节点T,U,V,X,W,Y,Z 我有一个Javascript对象,在计算了节点X的最短路径后,该对象包含每个节点的父节点 parents {"T":"V","U":"V","V":"X","W":"X","X":"X","Y":"X","Z":"X"} 例: 使用上面的对象,我需要从X计算每个节点的路径 例: 因此,我需要一个JAVASCRIPT中的简单程序,它将把路径输出到对象名path ex: path {"X":"X", "Y":"XY", "Z":"XZ", "T":

假设有一个图有7个节点T,U,V,X,W,Y,Z

我有一个Javascript对象,在计算了节点X的最短路径后,该对象包含每个节点的父节点

parents {"T":"V","U":"V","V":"X","W":"X","X":"X","Y":"X","Z":"X"}
例:

使用上面的对象,我需要从X计算每个节点的路径

例:

因此,我需要一个JAVASCRIPT中的简单程序,它将把路径输出到对象名path

ex:
path {"X":"X", "Y":"XY", "Z":"XZ", "T":"XVT", "U":"XVU", "W":"XW"}
如有任何帮助或建议,将不胜感激

谢谢你的阅读

总体思路是使用算法。这是非常基本的,唯一的事情是找到在每次递归调用中应该导航到的节点,下面是一些代码和解释:

let func = (parents) => {
   let ans = {}; // ans will contain all of our paths 
   let dfs = (X, path) => { // dfs function to run over tree
     ans[X] = path; // putting current path to current vertex
     let neighbours = [];
     Object.keys(parents).forEach(key => {
       if (parents[key] == X) { // looking for vertices where X is parent 
          neighbours.push(key);
       }
     });
     if (parents[X] != null) { // looking for parent of X
        neighbours.push(X);
     }
     neighbours.forEach(neighbour => { // iterating over all of the neighbour vetrices
       if (!ans[neighbour]) { // if it is not visited 
        dfs(neighbour, path + neighbour); // go into it with updated path
       }
     })
   };
   dfs("X", "X"); // run on initial node
   return ans; // don't forget to return calculated answer
}

帮什么忙?您还没有展示您自己试图解决这个问题的代码。Stackoverflow不是免费的代码编写服务。这里的想法是帮助您使用代码,而不是为您完成所有工作。再见,非常感谢@sreja Nagin。这很有魅力。
ex:
path {"X":"X", "Y":"XY", "Z":"XZ", "T":"XVT", "U":"XVU", "W":"XW"}
let func = (parents) => {
   let ans = {}; // ans will contain all of our paths 
   let dfs = (X, path) => { // dfs function to run over tree
     ans[X] = path; // putting current path to current vertex
     let neighbours = [];
     Object.keys(parents).forEach(key => {
       if (parents[key] == X) { // looking for vertices where X is parent 
          neighbours.push(key);
       }
     });
     if (parents[X] != null) { // looking for parent of X
        neighbours.push(X);
     }
     neighbours.forEach(neighbour => { // iterating over all of the neighbour vetrices
       if (!ans[neighbour]) { // if it is not visited 
        dfs(neighbour, path + neighbour); // go into it with updated path
       }
     })
   };
   dfs("X", "X"); // run on initial node
   return ans; // don't forget to return calculated answer
}