码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA实现DIJKSTRA算法

时间:2017-05-07 19:53:48      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:rgs   list   util   test   begin   set   algo   tab   end   

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算法

标签:rgs   list   util   test   begin   set   algo   tab   end   

原文地址:http://www.cnblogs.com/donghua/p/6821633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!