简体   繁体   English

获取从节点到自身的最短路径的算法—邻接矩阵— Java

[英]Algorithm to get shortest path from node to itself — Adjacency Matrix — Java

有向图

有向图

public int dijkstra(){
    boolean[] visited = new boolean[gSize];

    int src = 1;
    int dest = 1;
    int[] distance = new int[5];
    int[] part = new int[5];
    int min;
    int nextNode = 0;

    for(int i = 0; i < 5; i++)
    {
        visited[i] = false;
        part[i] = 0;

        for(int j = 0; j < 5; j++)
            if(arr[i][j] == -1)
                arr[i][j] = 999; //gives it a high value to ignore
    }

    distance = arr[src];
    distance[src] = 0;

    visited[src] = true;

    for(int i = 0; i < 5; i++)
    {
        min = 999;

        for(int j = 0; j < 5; j++)
            if(min > distance[j] && visited[j] != true)
            {
                min = distance[j];
                nextNode = j;
            }

        visited[nextNode] = true;

        for(int k = 0; k < 5; k++)
            if(visited[k] != true)
                if(min + arr[nextNode][k] < distance[k])
                {
                    distance[k] = min + arr[nextNode][k];
                    part[k] = nextNode;
                }
    }
    return distance[dest];
}

This Dijkstra algorithm works as it is supposed to. Dijkstra算法可以按预期工作。 However, it works only from vertex 'x' to vertex 'y'. 但是,它仅在顶点“ x”到顶点“ y”之间起作用。 I can't, for the life of me, figure out how to find the shortest path from vertex 'x' to vertex 'x'. 在我的一生中,我无法弄清楚如何找到从顶点“ x”到顶点“ x”的最短路径。

For example: 例如:

From B to B the shortest path should return 9 (B -> C -> E -> B). 从B到B的最短路径应返回9(B-> C-> E-> B)。 Am I taking a wrong approach by thinking that Dijkstra's algorithm can solve this problem? 我是否以为Dijkstra的算法可以解决此问题而采取了错误的方法? Thank you! 谢谢!

You can search the shortest path starting from nodes adjacent to x and finishing to the node x. 您可以搜索最短路径,该最短路径从与x相邻的节点开始,再到x的节点。

The shortest path will be the shortest sum of path length from x to an adjacent node plus the shortest path length from this adjacent node to x. 最短路径将是从x到相邻节点的路径长度的最短总和加上从此相邻节点到x的最短路径长度。

Basically in pseudocode: 基本上是伪代码:

// Note: The function neighbors(x) returns the list of neighbors of node x
// The function distance(x, y) returns distance between node x and y
// applying dijkstra algorithm

shortestDistance = 0;
for (Node neighbor : neighbors(x)) {
   currentDistance = distance(x, neighbor) + distance(neighbor, x);
   shortestDistance = min(currentDistance, shortestDistance);
}
return shortestDistance;

Run Dijkstra for each starting node to compute all pair shortest paths. 对每个起始节点运行Dijkstra,以计算所有对最短路径。 Then you could compute self-shortest paths by going over adjacent nodes and adding this edge weight .In some cases shortest path would be infinity depending upon in-degree of a node. 然后您可以通过遍历相邻节点并添加此边缘权重来计算自最短路径。在某些情况下,最短路径将是无穷大,具体取决于节点的入度。

Here is a tricky way to find shortest path from one node to itself for directed graph with nonnegative weight, separate that node(say s) into two nodes s and s',the extra one s' is considered as a virtual one, build bidirected edges with the same weight if s has self loop, and also copy all edges which involve s for s', ie replace s with s'. 这是一种为非负有向图找到从一个节点到其自身的最短路径的棘手方法,将该节点(例如s)分为两个节点s和s',多余的s'被视为虚拟节点,建立双向如果s具有自环,则具有相同权重的边也将所有包含s的边复制为s',即用s'替换s。 Then the problem becomes to find the shortest path from s' to s. 然后问题就变成了找到从s'到s的最短路径。 You only need to modify the Dijkstra's algorithm a little bit to implement this idea. 您只需稍微修改Dijkstra的算法即可实现此想法。 just change the initialization. 只需更改初始化。 instead of setting up d[s]=0 and all others be infinity, set d[u] = w(s,u) for each adjacent node of s. 而不是将d [s] = 0设置为其他所有值都为无穷大,而是为s的每个相邻节点设置d [u] = w(s,u)。

The shortest path between two nodes in a grid can be described by the Manhattan distance 网格中两个节点之间的最短路径可以用Manhattan distance来描述

Manhattan distance: 曼哈顿距离:
The distance between two points in a grid based on a strictly horizontal and/or vertical path (that is, along the grid lines), as opposed to the diagonal or "as the crow flies" distance. 网格中两点之间的距离基于严格的水平和/或垂直路径(即沿着网格线),与对角线或“乌鸦飞过”距离相反。 The Manhattan distance is the simple sum of the horizontal and vertical components, whereas the diagonal distance might be computed by applying the Pythagorean theorem. 曼哈顿距离是水平分量和垂直分量的简单总和,而对角线距离可以通过应用勾股定理来计算。
Source 资源

Now if you wish to apply this to finding the shortest path you should read up on Heuristics and more specifically the A* Algorithm . 现在,如果您希望将其应用于查找最短路径,则应该阅读Heuristics ,更具体地说是A* Algorithm

Perhaps this question and anwser might be of use to you: A* Algorithm with Manhattan Distance Heuristic 也许这个问题和答案可能对您有用: 具有曼哈顿距离启发式算法的A *算法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM