Java Implementation and upgrade of dijela algorithm (dijkstra)

Source: Internet
Author: User
Tags comparable

To find the shortest path in the program, I used the dijela algorithm, found an implementation on the Internet, and then upgraded myself to a lower level, as shown below:

Package reverse;
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;
Import java. util. PriorityQueue;


Public class DijSuccess {
Public static int INFINITY = 99999;
Public static Map <String, Vertex> vertexMap = new HashMap <String, Vertex> ();

// Margin
Static class Edge {
Public Vertex dest;
Public double cost;
Public Edge (Vertex d, double c ){
This. dest = d;
This. cost = c;
}

}

// Static class: Vertex
Static class Vertex implements Comparable <Vertex> {
Public String name;
Public List <Edge> adj;
Public double dist;
Public Vertex prev;
Public int scratch;
Public boolean visited;
Public Vertex (String nm ){
This. name = nm;
Adj = new ArrayList <Edge> ();
Reset ();
}
Public void reset (){
Visited = false;
Dist = DijSuccess. INFINITY;
}
@ Override
Public int compareTo (Vertex o ){
Double c = o. dist;

Return dist <c? -1: dist> c? 1:0;
}

}

// Dijkstra Algorithm Implementation: Find the shortest path from startName point to all other points: select the defined end point
Public static void dijkstra (String startName, String endName ){
PriorityQueue <Vertex> pq = new PriorityQueue <Vertex> (); // This queue is sorted in ascending order of weights, because Vertex implements the Comparable Interface
Vertex start = vertexMap. get (startName );
Start. dist = 0;
For (Vertex v: vertexMap. values ())
Pq. add (v );
Int seenNum = 0;
While (! Pq. isEmpty () & seenNum <vertexMap. size ()){
Vertex v = pq. remove ();
If (v. name. equals (endName) {// exactly the point you are looking
System. out. println (startName + "---->" + v. name + ":" + v. dist );
System. out. println (getPreNames (v ));
Break;

}
If (v. scratch! = 0)
Continue;
V. scratch = 1;
SeenNum ++;

For (Edge e: v. adj ){
Vertex w = e. dest;
Double v_to_w = e. cost;
If (w. dist> v. dist + v_to_w ){
W. dist = v. dist + v_to_w;
W. prev = v;
Pq. remove (w); // leave
Pq. add (w); // inserted in the queue header by priority, first inserted in the queue header, followed

}
}
}
System. out. println ("hello! ");
While (pq. peek ()! = Null ){
System. out. println (pq. poll ());
}
}

/**
* Obtain the path of the shortest path
* Seven
* @ Param v
* @ Return
*/
Public static String getPreNames (Vertex v ){
String routeEndName = v. name;
StringBuilder sb = new StringBuilder ();
While (v. prev! = Null ){
Sb. append (v. prev. name + ",");
V = v. prev;
}
String reverseRoute = routeEndName + "," + sb. toString ();
String [] reverseArray = reverseRoute. split (",");
StringBuilder route = new StringBuilder ();

For (int I = 0; I <reverseArray. length; I ++ ){
Route. append (reverseArray [reverseArray. length-1-i]);
Route. append (",");
}
Return route. substring (0, route. length ()-1 );
}




Public static void main (String [] args ){
Vertex v1 = new Vertex ("v1 ");
Vertex v2 = new Vertex ("v2 ");
Vertex v3 = new Vertex ("v3 ");
Vertex v4 = new Vertex ("v4 ");
Vertex v5 = new Vertex ("v5 ");

List <Edge> e1l = v1.adj;
List <Edge> e2l = v2.adj;
List <Edge> e3l = v3.adj;
List <Edge> e4l = v4.adj;
List <Edge> e5l = v5.adj;


Edge e12 = new Edge (v2, 10 );
Edge e14 = new Edge (v4, 30 );
Edge e15 = new Edge (v5, 100 );
E1l. add (e14 );
E1l. add (e15 );
E1l. add (e12 );

Edge e23 = new Edge (v3, 50 );
E2l. add (e23 );

Edge e35 = new Edge (v5, 10 );
E3l. add (e35 );

Edge e43 = new Edge (v3, 20 );
Edge e45 = new Edge (v5, 60 );
E4l. add (e43 );
E4l. add (e45 );

/*
The above code constructs a Directed Graph
V1 ---- & gt; v5: 100
V1 -----> V4: 30
V1 ------> V2: 10

V2 ------> V3: 50
V3 -------> V5: 10
V4 -------> V3: 20
V4 -------> V5: 60
*/
VertexMap. put ("v1", v1 );
VertexMap. put ("v2", v2 );
VertexMap. put ("v3", v3 );
VertexMap. put ("v4", v4 );
VertexMap. put ("v5", v5 );

Dijkstra ("v1", "v5 ");
}

}

 

 
 

 

 

 

 

 

 

Algorithm Overview:

Use PriorityQueue for data storage.


Start to record all vertices and the default distance.

Then, take out A vertex A, calculate the distance reached by the remaining vertex through vertex A, and select the shortest vertex B. Then, click B.

 


On the Internet, we can see that the implementation is not the end point, but the starting point, and the shortest paths to other points are listed.

// Dijkstra Algorithm Implementation
Public static void dijkstra (String startName ){
PriorityQueue <Vertex> pq = new PriorityQueue <Vertex> (); // This queue is sorted in ascending order of weights, because Vertex implements the Comparable Interface
Vertex start = vertexMap. get (startName );
Start. dist = 0;
For (Vertex v: vertexMap. values ())
Pq. add (v );
Int seenNum = 0;
While (! Pq. isEmpty () & seenNum <vertexMap. size ()){
Vertex v = pq. remove ();
System. out. println ("v0 ---->" + v. name + ":" + v. dist );
If (v. scratch! = 0)
Continue;
V. scratch = 1;
SeenNum ++;
For (Edge e: v. adj ){
Vertex w = e. dest;
Double v_to_w = e. cost;
If (w. dist> v. dist + v_to_w ){
W. dist = v. dist + v_to_w;
W. prev = v;
Pq. remove (w); // leave
Pq. add (w); // inserted in the queue header by priority, first inserted in the queue header, followed

}
}
}
System. out. println ("hello! ");
While (pq. peek ()! = Null ){
System. out. println (pq. poll ());
}
}

 

 

 


 

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.