简体   繁体   中英

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. However, it works only from vertex 'x' to vertex 'y'. I can't, for the life of me, figure out how to find the shortest path from vertex 'x' to vertex 'x'.

For example:

From B to B the shortest path should return 9 (B -> C -> E -> B). Am I taking a wrong approach by thinking that Dijkstra's algorithm can solve this problem? Thank you!

You can search the shortest path starting from nodes adjacent to x and finishing to the node 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.

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. 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'. Then the problem becomes to find the shortest path from s' to s. You only need to modify the Dijkstra's algorithm a little bit to implement this idea. 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.

The shortest path between two nodes in a grid can be described by the 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 .

Perhaps this question and anwser might be of use to you: A* Algorithm with Manhattan Distance Heuristic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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