引入:
Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。Dijkstra算法能得出最短路径的最优解,但由于它遍历计算的节点很多,所以效率低。
代码:
package Dijkstra; public class Node { private char id; private boolean isInTree; public Node(char id) { this.setId(id); setInTree(false); } public char getId() { return id; } public void setId(char id) { this.id = id; } public boolean isInTree() { return isInTree; } public void setInTree(boolean isInTree) { this.isInTree = isInTree; } }
package Dijkstra; public class DistPare { private int parentNode; private int distance; public DistPare(int parentNode, int distance) { this.setParentNode(parentNode); this.distance = distance; } public int getParentNode() { return parentNode; } public void setParentNode(int parentNode) { this.parentNode = parentNode; } public int getDistance() { return distance; } public void setDistance(int distance) { this.distance = distance; } }
package Dijkstra; public class Graph { private final int MAXNUM = 20; private final int MAXINT = 999999; private int nodeNum; //number of the nodes in Graph private int treeNum; //number of the nodes in tree private int adjMatrix[][]; //distance between two nodes private Node nodeList[]; //all nodes private DistPare sPath[]; //shortest path between parent node and i private int currentNode; private int currentDist; public Graph(){ adjMatrix = new int[MAXNUM][MAXNUM]; nodeList = new Node[MAXNUM]; sPath = new DistPare[MAXNUM]; nodeNum = 0; treeNum = 0; for(int i=0; i<MAXNUM; i++) for(int j=0; j<MAXNUM; j++) adjMatrix[i][j] = MAXINT; } public void addVertex(char id) { nodeList[nodeNum++] = new Node(id); } //directed graph public void addEdge(int start, int end, int weight) { adjMatrix[start][end] = weight; } public void dijkstra() { int sourceNode = 0; nodeList[sourceNode].setInTree(true); treeNum++; for(int i = 0; i < nodeNum; i++) { int weight = adjMatrix[sourceNode][i]; sPath[i] = new DistPare(sourceNode, weight); } while(treeNum < nodeNum) { int minNode = getMin(); int minDist = sPath[minNode].getDistance(); if(minDist == MAXINT) { System.out.println("The node can't be reached!"); } else { currentNode = minNode; currentDist = minDist; } nodeList[currentNode].setInTree(true); treeNum++; adjustPath(); } displayPath(); } private void adjustPath() { // TODO Auto-generated method stub int num = 1; while(num < nodeNum) { if(nodeList[num].isInTree()) { num ++; continue; } int currentToFringe = adjMatrix[currentNode][num]; int startToFringe = currentDist + currentToFringe; int sPathDist = sPath[num].getDistance(); if(startToFringe<sPathDist) { sPath[num].setParentNode(currentNode); sPath[num].setDistance(startToFringe);; } num ++; } } private void displayPath() { // TODO Auto-generated method stub for(int i=0; i<nodeNum; i++) { System.out.print(nodeList[i].getId() + "="); if(sPath[i].getDistance() == MAXINT) System.out.print("infinity"); else System.out.print(sPath[i].getDistance()); char parent = nodeList[sPath[i].getParentNode()].getId(); System.out.print("(" + parent + ") "); } System.out.println(" "); } /** * get the minimum DistPare * @return */ private int getMin() { int minDist = MAXINT; int indexMin = 0; for(int j=0; j<nodeNum; j++) { if(!nodeList[j].isInTree() && sPath[j].getDistance()<minDist) { minDist = sPath[j].getDistance(); indexMin = j; } } return indexMin; } }
package Dijkstra; public class DijkstraTest { public static void main(String[] args) { Graph theGraph = new Graph(); theGraph.addVertex('A');//0 theGraph.addVertex('B');//1 theGraph.addVertex('C');//2 theGraph.addVertex('D');//3 theGraph.addVertex('E');//4 theGraph.addEdge(0, 1, 50);//AB 50 theGraph.addEdge(0, 3, 80);//AD 80 theGraph.addEdge(1, 2, 60);//BC 60 theGraph.addEdge(1, 3, 90);//BD 90 theGraph.addEdge(2, 4, 40);//CE 40 theGraph.addEdge(3, 2, 20);//DC 20 theGraph.addEdge(3, 4, 70);//DE 70 theGraph.addEdge(4, 1, 50);//EB 50 System.out.println("Dijkstra: "); theGraph.dijkstra(); } }