Java代码实现
package algorithms;
import util.AlgoGraph;
import java.util.Stack;
/**
* Created with IntelliJ IDEA.
* Created by The_Sam on 2017/5/8 22:08
*/
public class Dijkstra extends ShortestAlgorithms {
private Stack Dealing ;
private boolean Dealed[];
public static void main(String[] args) {
ShortestAlgorithms dk = new Dijkstra();
System.out.println(dk.analyse(AlgoGraph.defaultMap4()));
}
boolean analyse(AlgoGraph algoGraph) {
ini(algoGraph);
int edgenum = algoGraph.getEdgenum();
while (!Dealing.empty()) {
int i=Dealing.pop();
Dealed[i]=true;
for (int j = 0; j < edgenum; j++) {//这里以枚举边的方式找以i为u的边,或许还有更好的做法,比如邻接表的形式
int u = algoGraph.edge.get(j).u;
if (i != u) continue;
int v = algoGraph.edge.get(j).v;
int cost = algoGraph.edge.get(j).cost;
if (dis[v] > dis[u] + cost) {
dis[v] = dis[u] + cost;
pre[v] = u;
if(!Dealed[v])Dealing.push(v);
}
}
}
return true;
}
void ini(AlgoGraph algoGraph) {
int nodenum = algoGraph.getNodenum();
//初始化,将dis[s]设为0,其他的都设为无限大
dis = new int[nodenum];//dis[i] 为 i点到s的最短距离
pre = new int[nodenum];//pre[i] 为i搭配s的最短距离的上一个节点
Dealed =new boolean[nodenum];
Dealing=new Stack();
int s = algoGraph.getOriginal();//原点
Dealing.push(s);
for (int i = 0; i < nodenum; i++) {
dis[i] = i == s ? 0 : MAXN;
}
}
}
AlgoGraph.java 表示图的代码附上
package util;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
/**
* Created by The_Sam on 2017/5/8.
* 图是从0到nodenum-1表示相应节点
*/
public class AlgoGraph {
@Getter
@Setter
private int original;
@Getter
@Setter
private int nodenum;
private int indegrees[];
// private int indegrees[];//indegree[i]表示i节点的入度
public ArrayList edge;
public int getEdgenum() {
return edge.size();
}
public AlgoGraph(int original, int nodenum, ArrayList edge) {
this.original = original;
this.nodenum = nodenum;
this.edge = edge;
}
/**
* @Description :返回函数各个节点的入度
* @Author : The_Sam
* @Datetime : 2017/5/8 23:04
* @Params :
* @Return : int[]
*/
public int[] getIndegrees() {
indegrees=new int[nodenum];
for (AlgoEdge e :
edge) {
indegrees[e.v]++;
}
return indegrees;
}
/**
* @Description: 返回一个图,为算法导论图24-4,带负权无负权回路有向环图
* @Author: The_Sam
* @Datetime: 2017/5/8 21:22
* @Params: []
* @Return: AlgoGraph
*/
static public AlgoGraph defaultMap1() {
ArrayList edge = new ArrayList();
edge.add(new AlgoEdge(0, 1, 6));
edge.add(new AlgoEdge(0, 2, 7));
edge.add(new AlgoEdge(1, 2, 8));
edge.add(new AlgoEdge(1, 3, 5));
edge.add(new AlgoEdge(1, 4, -4));
edge.add(new AlgoEdge(2, 3, -3));
edge.add(new AlgoEdge(2, 4, 9));
edge.add(new AlgoEdge(3, 1, -2));
edge.add(new AlgoEdge(4, 0, 2));
edge.add(new AlgoEdge(4, 3, 7));
int original = 0;
int nodenum = 5;
return new AlgoGraph(original, nodenum, edge);
}
/**
* @Description: 返回一个图,为算法导论图24-4改变版,有负权回路有向环图
* @Author: The_Sam
* @Datetime: 2017/5/8 21:22
* @Params: []
* @Return: AlgoGraph
*/
static public AlgoGraph defaultMap2() {
ArrayList edge = new ArrayList();
edge.add(new AlgoEdge(0, 1, 6));
edge.add(new AlgoEdge(0, 2, 7));
edge.add(new AlgoEdge(1, 2, 8));
edge.add(new AlgoEdge(1, 3, 5));
edge.add(new AlgoEdge(1, 4, -4));
edge.add(new AlgoEdge(2, 3, -3));
edge.add(new AlgoEdge(2, 4, 9));
edge.add(new AlgoEdge(3, 1, -2));
edge.add(new AlgoEdge(4, 0, 2));
edge.add(new AlgoEdge(4, 3, 5));//此处改变 edge.add(new AlgoEdge(4, 3, 7));
int original = 0;
int nodenum = 5;
return new AlgoGraph(original, nodenum, edge);
}
/**
* @Description: 返回一个图,为算法导论图24-4改变版,有向无环图
* @Author: The_Sam
* @Datetime: 2017/5/8 21:22
* @Params: []
* @Return: AlgoGraph
*/
static public AlgoGraph defaultMap3() {
ArrayList edge = new ArrayList();
edge.add(new AlgoEdge(0, 1, 6));
edge.add(new AlgoEdge(0, 2, 7));
edge.add(new AlgoEdge(1, 2, 8));
edge.add(new AlgoEdge(1, 3, 5));
edge.add(new AlgoEdge(1, 4, -4));
edge.add(new AlgoEdge(2, 3, -3));
edge.add(new AlgoEdge(2, 4, 9));
//edge.add(new AlgoEdge(3, 1, -2));
//edge.add(new AlgoEdge(4, 0, 2));
//edge.add(new AlgoEdge(4, 3, 5));//此处改变 edge.add(new AlgoEdge(4, 3, 7));
int original = 0;
int nodenum = 5;
return new AlgoGraph(original, nodenum, edge);
}
/**
* @Description: 返回一个图,为算法导论图24-4改变版,无负权有向环图
* @Author: The_Sam
* @Datetime: 2017/5/8 21:22
* @Params: []
* @Return: AlgoGraph
*/
static public AlgoGraph defaultMap4() {
ArrayList edge = new ArrayList();
edge.add(new AlgoEdge(0, 1, 6));
edge.add(new AlgoEdge(0, 2, 7));
edge.add(new AlgoEdge(1, 2, 8));
edge.add(new AlgoEdge(1, 3, 5));
edge.add(new AlgoEdge(1, 4, 4));
edge.add(new AlgoEdge(2, 3, 3));
edge.add(new AlgoEdge(2, 4, 9));
//edge.add(new AlgoEdge(3, 1, -2));
//edge.add(new AlgoEdge(4, 0, 2));
//edge.add(new AlgoEdge(4, 3, 5));//此处改变 edge.add(new AlgoEdge(4, 3, 7));
int original = 0;
int nodenum = 5;
return new AlgoGraph(original, nodenum, edge);
}
}
AlgoEdge.java表示边的代码附上
package util;
/**
* Created by The_Sam on 2017/5/8.
*/
public class AlgoEdge {
public int u,v;
public int cost;
public AlgoEdge(int u, int v, int cost) {
this.u = u;
this.v = v;
this.cost = cost;
}
}