Java implementation of Dijkstra algorithm application

Source: Internet
Author: User

The margin is for you:

A few days ago in the blog Park, a small partner posted Huawei 2010 10K (Salary) Employees 3-level promotion questions.

The main problem is the algorithm implementation.

in the with a large number of seniors staying in Huawei, as a small partner who loves algorithms , also want to try a little bit of skill.

Problem Address: http://www.cnblogs.com/preacher/p/4126261.html

Let's review the basics before solving the above problems: Dijkstra Shortest Path algorithm

The above questions are put on the next issue of the essay

Simple analysis of the algorithm:

The Dijkstra algorithm is also known as the shortest path of single source, and the so-called single source is the shortest path problem from a vertex to all reachable vertices in a graph.
set g= (V,e) is a forward graph, V for vertices, and E for edges. Each of its sides (I,J) belongs to E, there is a non-negative right w (i,j), in G to specify a node V0, requires the v0 to G from each of the VJ (VJ belongs to V) of the shortest path to find (or point out that does not exist).
Dijstra algorithm is to use greedy strategy, starting from the source point, constantly through the connected points to find the shortest distance to other points
The basic idea is:
sets a collection of vertices s, and continuously expands the collection, a vertex belonging to the set S when and only if the path from the source point to that point has been calculated. Start with only the source point in S, and adjust the shortest path length of the non-S midpoint, find the current shortest path point, add it to the set S, until the end point in S.
Basic steps:
1. Divide all the nodes into two groups:
The first group includes the nodes that have determined the shortest path;
second group: includes nodes that have not yet determined the shortest path.
2, beginning, the first group contains only the starting point, the second group contains the remaining points;
3, with greedy strategy, according to the shortest path length increment order to add the second group of nodes to the first group, until all the nodes V0 can be included in the first group. In this process, the shortest path is constantly updated, and the shortest path length from V0 to the first set of nodes is always dist less than the path length from V0 to the second set of any nodes.
4, each node corresponds to a distance value, the first set of nodes corresponding to the distance is v0 to this node's shortest path length, the second set of nodes corresponding to the distance value is v0 from the first set of nodes to this node's shortest path length.
5, until all the vertices are scanned (all nodes v0 can be included in the first group), find all the shortest paths v0 to other points.

Animated Demo: http://www.jcc.jx.cn/kejiandb/Dijkstra.swf

Code implementation:

1 ImportJava.util.Scanner;2 3 4  Public classDijkstraal {5     6     7      Public Static voidMain (String argv[]) {8     9     intI=r;intJ=0;TenScanner br=NewScanner (system.in);  OneSYSTEM.OUT.PRINTLN ("Please input the stations (int):");  Aj=Integer.parseint (Br.nextline ());  -SYSTEM.OUT.PRINTLN ("Please input the roads: (int):");  -k=Integer.parseint (Br.nextline ()); theSYSTEM.OUT.PRINTLN ("Please input the Rodas like ' 1 2 3 ': from 1 station to 2 station length 3.");  -String SourceData =br.nextline (); -String Data[]=sourcedata.split (""); - Br.close ();  +     ints[][]; -s=New int[j][]; +      for(inti=0;i<j;i++){ As[i]=New int[j]; at     } -      for(inti=0;i<j;i++){         -          for(intp=0;p<j;p++){ -s[i][p]=10000; -             if(i==p) -S[i][p]=0; in         }         -     }     to      for(intI=0;i<3*k;i=i+3){ +         intm=Integer.parseint (Data[i]); -         intN=integer.parseint (data[i+1]); theS[m-1][n-1]=integer.parseint (data[i+2]); The implementation of the non-direction diagram *S[n-1][m-1]=integer.parseint (data[i+2]); $     }    Panax Notoginseng     /*for (int i=0;i<j;i++) { - for (int p=0;p<j;p++) { the System.out.print (s[i][p]+ ""); + if (s[i][p]!=10000) A System.out.print (""); the         }     + System.out.println (); -     }*/ $  $Distr (S, 0, j); -    - }     the      -      Public Static voidDistr (int[] Data,intJintN) {Wuyi          the          int[] Shortl,chang; String[] Route; -chang=New int[n]; Record the number of station to be passed WuShortl=New int[n]; Record the length of the shortest path -Route=NewString[n]; Route stations that records the shortest path About           for(inti=0;i<n;i++){ $Route[i]= ""; -shortl[i]=Data[j][i]; -              if(shortl[i]>0&&shortl[i]!=10000) -{route[i]=route[i]+ "" +i; AChang[i]=1; }      +              Else theChang[i]=0; -               $          } the          the           for(inti=0;i<n;i++){ the               the             //System.out.println ("The Xunhuan Time I:" +i);  -               for(intt=0;t<n;t++){ in                   the                 //System.out.println ("The Xunhuan time t:" +t);  the                 //System.out.println ("The Xunhuan Time Shortl[t]:" +shortl[t]);  About                  if(shortl[t]!=10000){ the                     //System.out.println ("The Xunhuan Time Shortl[t]:" +shortl[t]);  the                       for(intm=0;m<n;m++){ the                         //System.out.println ("The Xunhuan time M and Data[t][m]:" +m+ "" +data[t][m "); +                          if(! (data[t][m]==10000)){ -                             //System.out.println ("The Xunhuan time M and Data[t][m]:" +data[t][m]+shortl[t]+shortl[m]); the                                  if((Data[t][m]+shortl[t]) <Shortl[m]) {Bayishortl[m]=data[t][m]+Shortl[t]; theChang[m]=chang[t]+1; theroute[m]=route[t]+ "" +m; -                                     //System.out.println ("Change:" +shortl[t]);  -                                       Break; the                                  }                                  the                          }                      the                  } the                                -              }          the         } the  the              94     } the             for(intu=0;u<n;u++){ theSystem.out.println ("The shortest route from 0 address to" +u+ "Address:" +Shortl[u]); theSystem.out.println ("The Stations:" +Chang[u]);98System.out.println ("The Routes:" +j+Route[u]); About             } - }    101}

Implementation Demo:

Java implementation of Dijkstra algorithm application

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.