A java Implementation of the Dickus algorithm (Dijkstra)

Source: Internet
Author: User
Tags addall

It took nearly one day to finally implement the dikexch algorithm (Dijkstra. Efficiency needs to be improved, and correctness needs to be verified. Now paste the source code.

 

Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import Java. util. hashset; <br/> Import Java. util. list; <br/> Import Java. util. map; <br/> Import Java. util. set; <br/>/** <br/> * The Dickus algorithm (Dijkstra) was developed by edsger wybe Dijkstra, a Dutch computer scientist. <Br/> * The algorithm solves the shortest path from a single source point in a directed graph to another vertex. <Br/> * algorithm reference: <a href = "http://zh.wikipedia.org/w/index.php" mce_href = "http://zh.wikipedia.org/w/index.php"> http://zh.wikipedia.org/w/index.php </a> <br/> * @ author czs <br/> */<br/> public class Dijkstra {<br/> private int [] [] graph; // weighted directed graph <br/> private int start; // The Source Vertex number starts from 0 <br/> private int dimention; <br/> static int INF = integer. max_value/100; <br/> // used to mark whether vertex has been calculated <br/> private se T <integer> vertexset = new hashset <integer> (); <br/> // stores the result. The map key corresponds to the destination number and the value corresponds to the path number list. <Br/> private Map <integer, list <integer> pathlistmap = new hashmap <integer, list <integer> (); <br/>/** <br/> * constructor, the path matrix and start point must be initialized. <br/> * @ Param graph <br/> * @ Param start <br/> */<br/> Public Dijkstra (INT [] [] graph, int start) {<br/> This. graph = graph; <br/> This. start = start; <br/> This. dimention = graph. length; <br/> calculate (); <br/>}< br/>/** <br/> * calculation function <br/> */<br/> private void calculate () {<Br/> // initialization <br/> for (INT end = 0; end <dimention; end ++) {<br/> If (END = start) {continue;} // exclude the path of the start point. <Br/> List <integer> pathlist = new arraylist <integer> (); <br/> pathlist. add (start); // the start point of each path is start. pathlist only records the number and does not record the path weight. <br/> pathlist. add (end); // The second parameter of each path is the end number <br/> pathlistmap. put (end, pathlist); <br/>}< br/> // computing subject <br/> for (INT bridge = 0; bridge <dimention; bridge ++) {<br/> If (Bridge = Start) {continue ;}< br/> If (! Vertexset. contains (BRIDGE) {// ensure that each base point is calculated cyclically only once <br/> for (INT next = 0; next <dimention; next ++) {<br/> If (next = Start | next = Bridge) {continue ;}< br/> If (startto (BRIDGE) + getrawlength (BRIDGE, next) <startto (next) {<br/> List <integer> pathlist = pathlistmap. get (next); <br/> List <integer> bridgepathlist = pathlistmap. get (BRIDGE); <br/> // clear and use the new <br/> pathlist. clear (); <br/> pathlist. addall (bridgepathli St); <br/> pathlist. add (next); <br/>}< br/> vertexset. add (BRIDGE); <br/>}< br/> // check whether the bridge path is updated. <br/> for (INT end = 0; end <dimention; end ++) {<br/> If (END = Start) {continue ;}< br/> List <integer> pathlist = pathlistmap. get (end); <br/> int size = pathlist. size (); <br/> If (size> 2) {<br/> for (INT end2 = 0; end2 <dimention; end2 ++) {<br/> int isend = pathlist. get (size-2); <br/> If (end2 = isend ){ <Br/> pathlist. clear (); <br/> pathlist. addall (pathlistmap. get (end2); <br/> pathlist. add (end ); <br/>}< br/>/** <br/> * obtain the specified path Length <br/> * @ Param start <br/> * @ Param end <br/> * @ return <br/> */<br/> private int startto (int end) {<br/> int pathlen = 0; <br/> List <integer> pathlist = pathlistmap. get (end); <br/> for (INT I = 0; I <pathlist. size ()-1; I ++) {<br/> pathlen + = grap H [pathlist. get (I)] [pathlist. get (I + 1)]; <br/>}< br/> return pathlen; <br/>}< br/>/** <br/> * directly extract adjacent paths in the matrix. <Br/> * @ Param start <br/> * @ Param end <br/> * @ return <br/> */<br/> private int getrawlength (INT start, int end) {<br/> If (END = Start) {<br/> return 0; <br/>}< br/> return graph [start] [end]; <br/>}< br/>/** <br/> * obtain the path length of the specified target. <br/> * @ Param end <br/> * @ return <br /> */<br/> Public int getlength (INT end) {<br/> If (END = Start) {<br/> return 0; <br/>}< br/> return startto (end ); <br/>}< br/>/** <br /> * Print all path numbers in the console <br/> */<br/> Public void printresult () {<br/> system. out. println (pathlistmap ); <br/>}< br/>/** <br/> * All path numbers <br/> * @ return <br/> */<br/> Public Map <integer, list <integer> getpathlistmap () {<br/> return pathlistmap; <br/>}< br/>/** <br/> * test out put <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/>/* <br/> int [] [] graph = {<br/> {INF, 10, INF, 30,100 },< br/> {INF, INF, 50, INF, INF}, <br/> {INF, 10 }, <br/> {INF, INF, 20, INF, 60}, <br/> {INF, INF }}; */<br/> int [] [] graph = {<br/> {INF, INF, 10, INF, 30,100}, <br/> {INF, INF, 5, INF}, <br/> {INF, 50, INF, INF}, <br/> {INF, INF, 10}, <br/> {INF, 20, INF, 60}, <br/> {INF, INF },< br/>}; <br/> Int start = 0; <br/> int end = 0; <br/> int length = graph. length; <br/> for (START = 0; Start <length; Start ++) {<br/> system. out. println (); <br/> Dijkstra = new Dijkstra (graph, start); <br/> Dijkstra. printresult (); <br/> for (END = 0; end <length; end ++) {<br/> If (END = Start) {continue ;} <br/> int Len = Dijkstra. getlength (end); <br/> system. out. println ("length (" + start + "-" + end + ") =" + (L En = inf )? "Infinity": Len); <br/>}< br/>

The current running result is:

{1 = [0, 1], 2 = [0, 2], 3 = [0, 4, 3], 4 = [0, 4], 5 = [0, 4, 3, 5]}
Length (0-1) = infinity
Length (0-2) = 10
Length (0-3) = 50
Length (0-4) = 30
Length (0-5) = 60

{0 = [1, 0], 2 = [1, 2], 3 = [1, 2, 3], 4 = [1, 4], 5 = [1, 2, 3, 5]}
Length (1-0) = infinity
Length (1-2) = 5
Length (1-3) = 55
Length (1-4) = infinity
Length (1-5) = 65

{0 = [2, 0], 1 = [2, 1], 3 = [2, 3], 4 = [2, 4], 5 = [2, 3, 5]}
Length (2-0) = infinity
Length (2-1) = infinity
Length (2-3) = 50
Length (2-4) = infinity
Length (2-5) = 60

{0 = [3, 0], 1 = [3, 1], 2 = [3, 2], 4 = [3, 4], 5 = [3, 5]}
Length (3-0) = infinity
Length (3-1) = infinity
Length (3-2) = infinity
Length (3-4) = infinity
Length (3-5) = 10

{0 = [4, 0], 1 = [4, 1], 2 = [4, 2], 3 = [4, 3], 5 = [4, 3, 5]}
Length (4-0) = infinity
Length (4-1) = infinity
Length (4-2) = infinity
Length (4-3) = 20
Length (4-5) = 30

{0 = [5, 0], 1 = [5, 1], 2 = [5, 2], 3 = [5, 3], 4 = [5, 4]}
Length (5-0) = infinity
Length (5-1) = infinity
Length (5-2) = infinity
Length (5-3) = infinity
Length (5-4) = infinity

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.