Dijstra algorithm to find the shortest distance from the source point to each vertex

Source: Internet
Author: User

The 1:dijstra algorithm often asks for the shortest distance,

Dijstra each time never found node n[], found the shortest point from the source node m, the shortest node, the M is added to the discovered node y[], with the node m to update the other nodes n[]-m the shortest distance . Until all nodes are found

Proof: M is the shortest distance from the source point S,

Because the node is the shortest distance in the discovery node, there is no distance from S to n[]-m to M and less than S to M.

In the discovered node y[], from S to y[] to M distance and, if there is less than s to M distance, then in the shortest distance to obtain s>y[i], it has been used (S>y[i]) + (Y[I]>M) to replace the distance of s>m. So there's no such situation.

Code:

 PackageCom.li.chapter24.mydijstra;ImportJava.io.InputStream;ImportJava.util.Scanner;/*** @program: Gradletestusesubmodule *@author: Yafei Li * @create: 2018-06-28 19:45 * Write your own Dijkstra algorithm to solve the graph theory and its Application 1.4 section A to B shortest path problem **/ Public classMydijstraalgorithm { Public Static voidMain (string[] args) {mydijstraalgorithm dijstraalgorithm=NewMydijstraalgorithm (); int[] Mindisarr = Dijstraalgorithm.dijstra (0);  for(inti = 0; i < mindisarr.length; i++) {System.out.println (mindisarr[i]); }}//vertx The source point of the input Public int[] Dijstra (intvertx) {        int[] arrweight=getarrofgraph (); int[] Arrvertx = Arrweight[vertx];//Other nodes andDistance to Vertx        Boolean[] Isfound =New Boolean[Arrvertx.length]; ISFOUND[VERTX]=true;         for(inti = 0; i < arrvertx.length; i++) {//Traverse all the points            intmindis=Integer.max_value; intv=Vertx;  for(intj = 0; J < Arrvertx.length; J + +) {                if(!Isfound[j]) {                    if(Mindis >Arrvertx[j]) {Mindis=Arrvertx[j]; V=J; }}} Isfound[v]=true;  for(intj = 0; J < Arrvertx.length; J + +) {                if(!Isfound[j]) {                    if(Mindis + arrweight[v][j] < Arrvertx[j]) {//distance from Vertx to V plus distance from V to JArrvertx[j]=mindis +Arrweight[v][j]; }                }            }        }        returnArrvertx; }     Public int[] Getarrofgraph () {Class clazz= This. GetClass (); InputStream ins= Clazz.getresourceasstream ("/data2.txt"); Scanner Scanner=NewScanner (INS); int[] Intarr =New int[8] [8]; intRow=0;  while(Scanner.hasnextline ()) {String line=Scanner.nextline (); String[] Strarr= Line.split ("");  for(inti = 0; i < strarr.length; i++) {Intarr[row][i]=Integer.parseint (Strarr[i]); } row++; }        returnintarr; }}

Below is the data, put under the resource

The following data indicates that the row number, the column number represents the node, and the node is 0-7

Where 8 represents a distance of 8 for the No. 0 node to the 4th node

999 means that there are no neighbors between the two nodes, indicating their distance infinity

8 999 1 9992 0 1 999 6 999 999 999999 1 0 9 4 3 999 999999 999 9 0 999 6 999 28 6 4 999 0 2 7 2999 999 3 6 2 0 999 41 999 999 999 7 999 0 9999 999 999 2 2 4 9 0

Dijstra algorithm to find the shortest distance from the source point to each vertex

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.