Java Implementation Dijkstra algorithm

Source: Internet
Author: User

ImportJava.util.Queue;Importjava.util.LinkedList; Public classdijkstra{ Public Static voidMain (String args[]) {System.out.println ("Dijkstra Algo"); /*construct the adjacent table begin*/Node n0=NewNode (0); Node N1=NewNode (1); Node N2=NewNode (2); Node N3=NewNode (3); Node N4=NewNode (4); Node N5=NewNode (5); Adjnode n0n1=NewAdjnode (7, N1); Adjnode n0n2=NewAdjnode (9, N2); Adjnode N0n5=NewAdjnode (14, N5); Adjnode n1n0=NewAdjnode (7, N0); Adjnode n1n2=NewAdjnode (10, N2); Adjnode n1n3=NewAdjnode (15, N3); Adjnode N2n0=NewAdjnode (9, N0); Adjnode N2n5=NewAdjnode (2, N5); Adjnode n2n3=NewAdjnode (11, N3); Adjnode n3n1=NewAdjnode (15, N1); Adjnode N3N2=NewAdjnode (11, N2); Adjnode N3n4=NewAdjnode (6, N4); Adjnode N4n3=NewAdjnode (6, N3); Adjnode N4n5=NewAdjnode (9, N5); Adjnode N5n4=NewAdjnode (9, N4); Adjnode N5N2=NewAdjnode (2, N2); Adjnode N5n0=NewAdjnode (14, N0); Adjnode[] N0adj={N0N1,N0N2,N0N5}; Adjnode[] N1adj={N1N0,N1N2,N1N3}; Adjnode[] N2adj={N2N0,N2N5,N2N3}; Adjnode[] N3adj={N3N1,N3N2,N3N4}; Adjnode[] N4adj={N4N3,N4N5}; Adjnode[] N5adj={n5n4,n5n2,n5n0};      N0.addadjnodes (N0ADJ);      N1.addadjnodes (N1ADJ);      N2.addadjnodes (N2ADJ);      N3.addadjnodes (N3ADJ);      N4.addadjnodes (N4ADJ);      N5.addadjnodes (N5ADJ); /*construct the adjacent table end*/node[] G={N0,N1,N2,N3,N4,N5}; Queue<Node> queue =NewLinkedlist<node>();      Adjnode[] Curradjnodes; N0.color= 1; N0.VT++; N0.setshortestpathlen (0);      Queue.offer (N0); Node Currnode=Queue.poll ();  while(Currnode! =NULL) {Curradjnodes=currnode.getalladjnodes ();  for(inti=0;i<curradjnodes.length;i++){            if(Curradjnodes[i].adjnode.getshortestpathlen () > (Currnode.getshortestpathlen () +curradjnodes[i].weight)) {Curradjnodes[i].adjnode.setshortestpathlen (Currnode.getshortestpathlen ()+curradjnodes[i].weight);            Curradjnodes[i].adjnode.setprenode (Currnode); }          if(Curradjnodes[i].adjnode.color = = 0) {Curradjnodes[i].adjnode.color= 1; CURRADJNODES[I].ADJNODE.VT++;          Queue.offer (Curradjnodes[i].adjnode); }} Currnode.color= 2; Currnode=Queue.poll (); }         for(inti=0;i<g.length;i++) {System.out.println ("Shortest path of" + i + "node:" + g[i].getshortestpathlen () + "VT:" +G[I].VT); }  }}
 Public classnode{Private intindex = 0,shortestpathlen = 10000; PrivateNode Prenode =NULL; Privateadjnode[] Nodearray;  Public intcolor = 0;  Public intVT = 0;  PublicNode (intIND) {Index=IND; }     Public voidaddadjnodes (adjnode[] nodes) {Nodearray=nodes; }     Publicadjnode[] Getalladjnodes () {returnNodearray; }     Public voidSetprenode (Node N) {Prenode=N; }     Public voidSetshortestpathlen (intLen) {Shortestpathlen=Len; }     Public intGetshortestpathlen () {returnShortestpathlen; }}
 Public class adjnode{  int weight = 0;   NULL ;    Public Adjnode (int  w,node n) {    = n;         = w;  }}

Java Implementation Dijkstra algorithm

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.