Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
Sorting 非排序节点阵列的路径查找算法_Sorting_Graph Algorithm_Path Finding_Maze - Fatal编程技术网

Sorting 非排序节点阵列的路径查找算法

Sorting 非排序节点阵列的路径查找算法,sorting,graph-algorithm,path-finding,maze,Sorting,Graph Algorithm,Path Finding,Maze,情景: 我有一张连接在一起的节点图 我有所有节点的未排序数组 我有检查节点是否相互连接的功能(如果节点1连接到节点2) 函数arenedsconnected(node1、node2)返回真/假 请求: 我正在寻找算法(伪代码)来找到使用此函数的两个随机节点之间的路径 结果应该是从数组开始的节点1和数组结束的节点2对节点数组进行排序。如果两个节点之间没有路径,则返回null 注: 不需要有可能的最短路线 如果有更多路径,请选择第一个可能的路径 谢谢你的建议。 我不是要求提供完整的解决方案,

情景: 我有一张连接在一起的节点图

  • 我有所有节点的未排序数组
  • 我有检查节点是否相互连接的功能(如果节点1连接到节点2)
函数arenedsconnected(node1、node2)返回真/假

请求: 我正在寻找算法(伪代码)来找到使用此函数的两个随机节点之间的路径

结果应该是从数组开始的节点1和数组结束的节点2对节点数组进行排序。如果两个节点之间没有路径,则返回null

注:

  • 不需要有可能的最短路线
  • 如果有更多路径,请选择第一个可能的路径
谢谢你的建议。 我不是要求提供完整的解决方案,而是指出从哪里开始解决这个问题。

我们可以使用DFS(深度优先搜索)来找到答案

让我们维护一个堆栈和一个已访问的数组,以跟踪以前访问过的节点。我们还需要跟踪每个节点的父节点,以获得最终答案。如上所述,使用areNodesConnected(节点1、节点2)功能检查边

步骤

  • 创建堆栈、父数组和已访问数组
  • 将起始节点推送到堆栈
  • 堆栈不为空时:弹出堆栈并将连接到弹出节点的所有节点推送到堆栈上。(连接函数时)使用父数组跟踪节点的来源
  • 迭代父数组以获得最终答案
  • 伪代码

    function findPath(int start, int end, int nodes):
        stack = Stack
        parent = array of integers to keep track of parents, set every value to -1 to start with
        vis = visited array of booleans to keep track of previous nodes
    
        stack.push(start)
        parent[start] = start // setting the starts parent to itself so we know when to stop searching.
    
        while stack is not empty:
            int x = stack.pop()
            for y in range 0 to nodes:
                if areNodesConnected(x, y) and not vis[y]: // checking if there is an edge from x to y
                    stack.push(y)
                    parent[y] = x; // store the parent of y
            vis[x] = true
    
        // now we have to print our final answer
    
        if parent[i] == -1:
            return null // there is no path
    
        list = List for final answer    
    
        for (int i = end; parent[i] != i; i = parent[i]): // recursively set i to it's parent until it is the starting node.
            list.add(i)
    
        // now lets add start
        list.add(start)
    
        return list
    

    上述解决方案的唯一问题是列表与您想要的相反。

    如何先保存每个节点的所有连接节点,然后从第一个随机节点开始,开始移动到每个其他连接节点,并跟踪每个路径中遍历的节点。如果某个节点重复,请停止递归,否则对当前节点执行相同操作。当到达第二个随机节点时,返回路径。