点对象
public class Vertex implements Comparable<Vertex>{//必须实现此接口才能入栈
private String name;//点的名字
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPath() {
return path;
}
public void setPath(int path) {
this.path = path;
}
public boolean isMarked() {
return isMarked;
}
public void setMarked(boolean isMarked) {
this.isMarked = isMarked;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
private int path;//到该点的最短距离
private boolean isMarked;//是否被标记
private String last;//到该点的上一个点
public Vertex(String name){
this.name=name;
this.path=Integer.MAX_VALUE;
isMarked=false;
this.last=null;
}
@Override
public int compareTo(Vertex o) {
return o.path > path?-1:1;
}
}
算法核心
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
public class Grap {
private List<Vertex> vertexs;//所有的点
private Queue<Vertex> queue;//没有访问过的点
private int[][] edgs;//邻接矩阵
public Grap(List<Vertex> v,int[][] edgs) {
this.vertexs=v;
this.edgs=edgs;
initQueue();
}
//初始化队列
public void initQueue(){
queue=new PriorityQueue<>();
for(Vertex v:vertexs){
queue.add(v);
}
}
//djkstra算法
public void dijkstra(){
while(!queue.isEmpty()){//当所有的都为true时结束
Vertex v=queue.element();
v.setMarked(true);
//得到当前节点的邻接点
List<Vertex> neighbors=getNeighbors(v);
//更新邻接点
updateNeighbors(v, neighbors);
//该节点出队列
queue.poll();
}
}
//得到一个点的邻接点
public List<Vertex> getNeighbors(Vertex v){
List<Vertex> neighbors=new ArrayList<Vertex>();
int position=vertexs.indexOf(v);
for(Vertex neighbor:vertexs){
int i=vertexs.indexOf(neighbor);
if(i==position){
continue;
}
if(edgs[position][i]!=Integer.MAX_VALUE&&neighbor.isMarked()){
neighbors.add(neighbor);
}
}
return neighbors;
}
//更新邻接点的值
public void updateNeighbors(Vertex v,List<Vertex> neighbors){
int position=vertexs.indexOf(v);
for(Vertex neighbor:neighbors){
int i=vertexs.indexOf(neighbor);
int path=v.getPath()+edgs[position][i];
if(path<neighbor.getPath()){
vertexs.get(i).setPath(path);
vertexs.get(i).setLast(v.getName());
}
}
}
//输出
public void printPath(Vertex v) {
if(v.getLast()!=null){
Vertex last;
for(Vertex temp:vertexs){
if(temp.getName()==v.getLast()){
last=temp;
printPath(last);
System.out.print(" to ");
}
}
}
System.out.print(v.getName());
}
}
测试
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Vertex> vertexs = new ArrayList<Vertex>();
Vertex a = new Vertex("v1");
a.setPath(0);
Vertex b = new Vertex("v2");
Vertex c = new Vertex("v3");
Vertex d = new Vertex("v4");
Vertex e = new Vertex("v5");
Vertex f = new Vertex("v6");
Vertex g =new Vertex("v7");
vertexs.add(a);
vertexs.add(b);
vertexs.add(c);
vertexs.add(d);
vertexs.add(e);
vertexs.add(f);
vertexs.add(g);
int[][] edges=new int[7][7];
for(int i=0;i<7;i++){
for(int j=0;j<7;j++){
edges[i][j]=Integer.MAX_VALUE;
}
}
edges[0][1]=2;
edges[0][3]=1;
edges[1][3]=3;
edges[1][4]=10;
edges[2][0]=4;
edges[2][5]=5;
edges[3][2]=2;
edges[3][4]=2;
edges[3][5]=8;
edges[3][6]=4;
edges[4][6]=6;
edges[6][5]=1;
Grap graph = new Grap(vertexs, edges);
graph.dijkstra();
System.out.print("v1 to ");
graph.printPath(g);
}
}