An example of Dijkstra algorithm based on Java implementation _java

Source: Internet
Author: User

This paper introduces the Dijkstra algorithm based on Java, and believes that it is helpful for readers to study data structure domain algorithm.

Dijkstra proposes an algorithm to generate the shortest path to each vertex by increasing the order of path lengths between each vertex and the source Point v. That is to find out the shortest length of a shortest path, and then refer to it to find a short length of the shortest path, and so on, and so on, until from the source Point V to the other vertices of the shortest path all to find out.
The code implementation looks like the following:

Package Com.algorithm.impl;  public class Dijkstra {private static int M = 10000;//blocked public static void main (string[] args) {int[][] weight1 =  
    {//adjacency matrix {0,3,2000,7,m}, {3,0,4,2,m}, {m,4,0,5,4}, {7,2,5,0,6}, {m,m,4,6,0} 

    }; 
        Int[][] weight2 = {0,10,m,30,100}, {m,0,50,m,m}, {m,m,0,m,10}, {m,m,20,0,60},
    
    {m,m,m,m,0}}; 
    int start=0; 
     
    int[] ShortPath = Dijkstra (Weight2,start); 
 for (int i = 0;i < shortpath.length;i++) System.out.println (the shortest distance from "+start+" to "+i+" is: "+shortpath[i]");  public static int[] Dijkstra (int[][] weight, int start) {//accept a weighted matrix of a direction graph, and a start number start (from 0 number, vertex exists array)//return a int[]      Array representing the shortest path length from start to it int n = weight.length;  Vertex number int[] ShortPath = new Int[n];  Save start to the shortest path to other points string[] path = new String[n]; 
 The string that saves the start to the shortest path at the other points is represented for (int i=0;i<n;i++) path[i]=new string (start+ "-->" +i); Int[] Visited = new Int[n];
 Mark whether the shortest path of the current vertex has been obtained, 1 indicates that it has been evaluated/initialized, the first vertex has been shortpath[start] = 0;
 
 Visited[start] = 1;        for (int count = 1; count < n; count++) {//to join n-1 vertex int k =-1;
  Select an unmarked vertex int dmin = Integer.max_value that is closest to the beginning vertex start;
   for (int i = 0; i < n; i++) {if (visited[i] = = 0 && weight[start][i] < DMin) {dmin = Weight[start][i];
  K = i;
  }///mark the newly selected vertex as the shortest path, and the shortest path to start is dmin shortpath[k] = DMin;
  
  Visited[k] = 1; With K as the middle point, fix the distance from start to inaccessible points for (int i = 0; i < n; i++) {if (Visited[i] = 0 && weight[start][k] + weight[k]
   [I] < weight[start][i]) {weight[start][i] = Weight[start][k] + weight[k][i]; 
  Path[i] = Path[k] + "-->" + i;
 The shortest path for (int i = 0; i < n; i++) {System.out.println (from "+start+" to "+i+" is: "+path[i]); 
 } System.out.println ("=====================================");
 return shortpath;
 }
}

The results of this program are:

The shortest path from 0 to 0 is: 0-->0 the
Shortest path from 0 to 1 is: 0-->1
from 0 to 2 the shortest path is: 0-->3-->2
from 0 to 3 the shortest path is: 0-->3 The
shortest path from 0 to 4 is: 0-->3-->2-->4
===================================== the
shortest distance from 0 to 0 is: 0
The shortest distance from 0 to 1 is: the shortest distance from 0 to 2 is: the shortest distance from 0 to 3 is the shortest distance
from 0 to 4:60
Related Article

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.