JAVA实现DIJKSTRA算法

Posted 兵形东华

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA实现DIJKSTRA算法相关的知识,希望对你有一定的参考价值。

import java.util.Queue;
import java.util.LinkedList;

public class dijkstra{
  public static void main(String args[]){
    System.out.println("dijkstra algo");
    /*construct the adjacent table begin*/
      Node n0 = new Node(0);
      Node n1 = new Node(1);
      Node n2 = new Node(2);
      Node n3 = new Node(3);
      Node n4 = new Node(4);
      Node n5 = new Node(5);
      AdjNode n0n1 = new AdjNode(7,n1);
      AdjNode n0n2 = new AdjNode(9,n2);
      AdjNode n0n5 = new AdjNode(14,n5);
      
      AdjNode n1n0 = new AdjNode(7,n0);
      AdjNode n1n2 = new AdjNode(10,n2);
      AdjNode n1n3 = new AdjNode(15,n3);

      AdjNode n2n0 = new AdjNode(9,n0);
      AdjNode n2n5 = new AdjNode(2,n5);
      AdjNode n2n3 = new AdjNode(11,n3);
      
      AdjNode n3n1 = new AdjNode(15,n1);
      AdjNode n3n2 = new AdjNode(11,n2);
      AdjNode n3n4 = new AdjNode(6,n4);
      
      AdjNode n4n3 = new AdjNode(6,n3);
      AdjNode n4n5 = new AdjNode(9,n5);
      
      AdjNode n5n4 = new AdjNode(9,n4);
      AdjNode n5n2 = new AdjNode(2,n2);
      AdjNode n5n0 = new AdjNode(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 = new LinkedList<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(int i=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(int i=0;i<G.length;i++){
      System.out.println("shortest path of " + i + "node:" + G[i].getShortestPathLen()+" vt:"+G[i].vt);
    }
  }
}
public class Node{
  private int index = 0,shortestPathLen = 10000;
  private Node preNode = null;
  private AdjNode[] nodeArray;
  public int color = 0;
  public int vt = 0;
  public Node(int ind){
    index = ind;    
  }
  
  public void addAdjNodes(AdjNode[] nodes){
    nodeArray = nodes;    
  }
  
  public AdjNode[] getAllAdjNodes(){
    return nodeArray;    
  }
  
  public void setPreNode(Node n){
    preNode = n;    
  }
  
  public void setShortestPathLen(int len){
    shortestPathLen = len;    
  }
  
  public int getShortestPathLen(){
    return shortestPathLen;    
  }
}
public class AdjNode{
  int weight = 0;
  Node adjNode = null;
  public AdjNode(int w,Node n){
    adjNode = n;    
    weight = w;
  }
}

 

以上是关于JAVA实现DIJKSTRA算法的主要内容,如果未能解决你的问题,请参考以下文章

JAVA实现DIJKSTRA算法

Dijkstra算法的Java实现

Dijkstra算法 ---java实现

Dijkstra算法 Java实现

最短路-Dijkstra算法:朴素版&堆优化版(Java详解)

java利用迪克斯特拉(Dijkstra)算法求拓扑关系最短路径