Java implementation of Dijkstra algorithm

Source: Internet
Author: User

public class Dijkstra {private static int N = 1000;private static int[][] Graph = {{0, 1, 5, n, N, N, N, N, n}, {1, 0,  3, 7, 5, N, N, N, n},{5, 3, 0, N, 1, 7, n, N, n}, {n, 7, N, 0, 2, N, 3, N, n}, {n, 5, 1, 2, 0, 3, 6, 9, n},{N, N, 7, N, 3, 0, N, 5, n}, {n, N, N, 3, 6, N, 0, 2, 7}, {n, n, N, N, 9, 5, 2, 0, 4},{N, N, N, N, N, N, 7, 4, 0}};p ubli c static void Main (string[] args) {Dijkstra (0, Graph);} /** * Dijkstra Shortest path. * That is, the shortest path to each of the other nodes in the graph "Node vs". * @param vs Start Node * @param graph */public static void Dijkstra (int vs, int[][] Graph) {int NUM = graph[0].length;//Precursor section count Group int[] Prenode = new int[num];//shortest distance array int[] Mindist = new int[num];//Whether the node has found the shortest path boolean[] Find = new Boolean[num];int Vnear = 0;for (int i = 0; i < mindist.length; i++) {prenode[i] = i;mindist[i] = Graph[vs][i];find[i] = false;} Find[vs] = true;for (int v = 1; v < graph.length; v++) {//Each loop evaluates distance vs nearest node Vnear and shortest distance minint min = n;for (int j = 0; J &L T Graph.length; J + +) {if (!find[j] && mindist[J] < min) {min = Mindist[j];vnear = j;}} Find[vnear] = true;//modifies vs to all other nodes based on vnear and distance for (int k = 0; k < graph.length; k++) {if (!find[k] && (min + Graph[vnear][k]) < Mindist[k]) {prenode[k] = vnear;mindist[k] = min + graph[vnear][k];}}} for (int i = 0; i < NUM; i++) {System.out.println ("V" + vs + "... v" + prenode[i] + "->v" + i + ", s=" + mindist[i]); }}

  

Java implementation of Dijkstra algorithm

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.