JAVA實現DIJKSTRA演算法

來源:互聯網
上載者:User

標籤: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演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.