FIG learning shortest path (the Dijkstra)

1: Dijkstra algorithm

  The algorithm can be realized greedy algorithm, by starting from the starting point to find the shortest distance from other points, find the vertex of the shortest distance, and then in the vertex as a transit point, to find

Starting with transit point to other vertices closer distance, in order to find the optimal path step by step through choice!

2: algorithmic process

 

As shown in FIG flow, the distance to other vertex v0 is set to an array path [0,1,5,65535,65535,65535,65535,65535,65535] where v0 65535 substituting the distance between the point to be infinite. First, find the nearest one vertex in the path to v1, v1 then we as a transit point to update the array, how to update Nepal? V0 to v1 through joining path and reaches the other vertex v0 is smaller than the path directly to the other vertex, instead of directly using the values ​​in the array smaller path. In turn continue the cycle come and find v0 to v2, to v3, to v4, to v5, to v6, to v7, v8 of the distance. On it.

 

3: Code Example

 

package nearstWay;

import sun.security.util.Length;

/**
 * @Author: dazhu
 * @date :Created in 2020/3/18 11:37
 * @Description: Shortest Path
 * @modified By:
 * @version: $
 * / 
Public  class the Main {
     public  static  void main (String [] args) {
         // create a vertex array, the adjacency matrix 
        int INF = 65535 ;
        Vertex[] vertexList = new Vertex[]{
                new Vertex("v0"),
                new Vertex("v1"),
                new Vertex("v2"),
                new Vertex("v3"),
                new Vertex("v4"),
                new Vertex("v5"),
                new Vertex("v6"),
                new Vertex("v7"),
                new Vertex("v8")
        };
        int[][] adjMat = new int[][]{
                {   0 ,   1 ,   5 , inf inf inf inf inf inf},
                {   1 ,   0 ,   3 ,   7 ,   5 , inf inf inf inf},
                {   5 ,   3 ,   0 , inf,   1 ,   7 , inf inf inf},
                {inf,   7 , inf,   0 ,   2 , inf,   3 , inf inf},
                {inf,   5 ,   1 ,   2 ,   0 ,   3 ,   6 ,   9 , inf},
                {inf inf,   7 , inf,   3 ,   0 , inf,   5 , inf},
                {inf inf inf,   3 ,   6 , inf,   0 ,   2 ,   7 },
                {inf inf inf inf,   9 ,   5 ,   2 ,   0 ,   4 },
                {inf inf inf inf inf inf,   7 ,   4 ,   0 },
        };
        Graph g = new Graph(vertexList,adjMat);
        Dijkstra djk = new Dijkstra(g);
        djk.findNearestWay(0);
    }
}
class the Dijkstra {
     public Graph G = null ;
     public  int nVertex;
     public  int [] pathMatirx; // for the shortest path table storing array 
    public  int [] shortPathTable; // weight value for storing shortest path points 
    public  int [] markArr; // markArr [K] = v0. 1 representative of the shortest path to obtain vk of the 
    public the Dijkstra (Graph G) {
         the this .g = G;
         the this .nVertex = g.vertexList.length;
         // the output initialize the array to the number of vertices graph 
        the this .pathMatirx = new new  int[nVertex];
        this.shortPathTable = new int[nVertex];
        this.markArr = new int[nVertex];
    }

    public  void findNearestWay ( int V0) {
         int V = 0 , W = 0 , K = 0 ;
         int min;
         // initialize the data 
        for ( int I = 0 ; I <nVertex; I ++ ) {
             the this .markArr [I] = 0 ;
             // initializes to shotPathTable other vertex v0 is the distance 
            the this .shortPathTable [I] = g.adjMat [v0] [I];
             // initialize array of paths is 0 
            the this .pathMatirx [I] = 0 ;
        }
        // do not need to claim on behalf of a path between v0 to v0! ! 
        the this .markArr [ 0 ] = . 1 ;
         // start the main loop, each cycle of v0 to find the shortest path between a vertex v, until all of the vertices are seeking! ! ! 
        for (V = . 1 ; V <nVertex; V ++ ) {
            min = 65535 ; // current knowledge of the distance from the nearest v0, initialized to a suitable value larger
             // Find the nearest vertex v0 in 
            for (W = 0 ; W <nVertex; W ++ ) {
                 // if and only if the point and find the shortest path is not less than min the path 
                IF ((markArr [W] == 0 ) && shortPathTable [W] < min) {
                     // save the shortest path to the vertex K 
                    K = W;
                     // be replaced by the min the shortest path 
                    min = shortPathTable [W];
                }
            }
            // to find the nearest vertex currently set to 1, vk to v0 represents the shortest path has been found 
            the this .markArr [K] = 1 ;

            // correction of the current route and the shortest distance! !
            // If v0 through the previously determined distance reaches another vertex vk, v0 is smaller than the distance from the other straight lines in vertex
             // update shortPathTable v0 in [] is the shortest distance 
            for (W = 0 ; W <nVertex; W ++ ) {
                 IF ( markArr [W] == 0 && g.adjMat + min [K] [W] < shortPathTable [W]) {
                    short path table [w] = min + g.adjMat [k] [w];
                    pathMatirx [i] = k;
                }
            }
        }
    }
}

class Graph{
    public  Vertex[] vertexList;
    public int adjMat[][];
    public int nVerts;

    public Graph (Vertex [] VertexList, int [] [] adjMat) {
         // initialization, the vertex array 
        the this .vertexList = VertexList;
         // Create the matrix side 
        the this .adjMat = adjMat;
         // initial vertex ordering 
        the this .nVerts = 0 ;

    }
    // added vertex to the vertex array 
    public  void addVertex (String Lab) {
        vertexList[nVerts++] = new Vertex(lab);
    }

    // add edge to edge matrix
     // weight 0: a weight of 0 are connected
     // weight k: k is connected to the right
     // weight infinity: not connected to 
    public  void addEdge ( int Start, int End, int weight) {
        adjMat[start][end] = weight;
        adjMat[end][start] = weight;
    }

    // display all vertices 
    public  void showVertexs () {
         for ( int I = 0 ; I <vertexList.length; I ++ ) {
            System.out.println(vertexList[i]);
        }
    }
    // print the specified vertices 
    public  void printVertex ( int V) {
        System.out.println(vertexList[v].label);
    }

}


// structure vertex class
 // vertex can be present in the array, denoted by index,
 // course also be present in the list 
class Vertex {
     public String label; // label 
    public Boolean wasVisited; // if traversed indicia. 
    public Vertex (String label) {
         the this .label = label;
        wasVisited = false;
    }
}

 

Guess you like

Origin www.cnblogs.com/dazhu123/p/12519611.html