DIJKSTRA(G,w,s)
1 INITIALIZE-SINGLE-SOURCE(G,s)
2 S = emptyset
3 Q = G.V
4 while Q != emptyset
5 u = EXTRACT-MIN(Q)
6 S = S.add(u)
7 for each vertex v belong to G.Adj[u]
8 RELAX(u, v, w)
java代码如下:
@SuppressWarnings("rawtypes")
public static void dijkstra(String graphContent, int startID) throws Exception{
List<Vertex> vertexs = Route.getVertexs(graphContent);
Bellman_Ford.initialize_single_source(vertexs, startID);
Route.getEdges(graphContent);
LinkedList[] lists = Route.getLinkedList(graphContent);
List<Vertex> S = new LinkedList<Vertex>();
Min_Queue Q = new Min_Queue(vertexs);
while (!Q.isEmpty()){
Vertex u = Q.Extract_Min();
S.add(u);
for (int i = 1; i < lists[u.getID()].size(); i++){
Vertex v = (Vertex)(lists[u.getID()].get(i));
relax(Q, Edge.getEdge(u.getID(), v.getID()));
}
}
}
private static void relax(Min_Queue Q, Edge edge) throws Exception {
// TODO Auto-generated method stub
Vertex u = edge.getStart(),
v = edge.getDest();
if (v.getD() > u.getD() + edge.getWeight()){
if (Q.contains(v)){
Q.Decrease_Key(v, u.getD() + edge.getWeight());
}else{
v.setD(u.getD() + edge.getWeight());
}
v.setParent(u);
}
}
public static void print_Path(String graphContent, int startID, int destID) throws Exception{
dijkstra(graphContent, startID);
Vertex v = Vertex.getVertex(destID);
LinkedList<Vertex> vertexs = new LinkedList<Vertex>();
vertexs.addFirst(v);
while (v.getParent() != null && !v.getParent().equal(startID)){
vertexs.addFirst(v.getParent());
v = v.getParent();
}
if (v.getParent() == null){
throw new Exception("The path doesn't exist: " + v);
}
vertexs.addFirst(Vertex.getVertex(startID));
for (Vertex vertex : vertexs){
System.out.print(vertex + "(" + vertex.getD() + ") ");
}
System.out.println();
}
其中用到的最小优先队列:
/**
* 最小优先队列
* @author obguy
*
*/
public class Min_Queue {
List<Vertex> vertexs = new LinkedList<Vertex>();
public Min_Queue(List<Vertex> vertexs) throws Exception{
this.vertexs.addAll(vertexs);
Build_Min_Heapify(this.vertexs);
}
private void Build_Min_Heapify(List<Vertex> vertexs) throws Exception {
// TODO Auto-generated method stub
for (int i = vertexs.size()/2; i >= 0; i--){
Min_Heapify(vertexs, i);
}
}
private void Min_Heapify(List<Vertex> vertexs, int i) throws Exception {
// TODO Auto-generated method stub
int l = 2 * i + 1;
int r = 2 * i + 2;
int smallest;
int n = vertexs.size();
if (l < n && vertexs.get(l).getD() < vertexs.get(i).getD()){
smallest = l;
}else {
smallest = i;
}
if (r < n && vertexs.get(r).getD() < vertexs.get(smallest).getD()){
smallest = r;
}
if (smallest != i){
Vertex u = vertexs.get(i);
vertexs.set(i, vertexs.get(smallest));
vertexs.set(smallest, u);
Min_Heapify(vertexs, smallest);
}
}
public Vertex Extract_Min() throws Exception{
if (vertexs.isEmpty()){
throw new Exception("heap underflow");
}
Vertex min = vertexs.get(0);
vertexs.set(0, vertexs.get(vertexs.size() - 1));
vertexs.remove(vertexs.size() - 1);
Min_Heapify(vertexs, 0);
return min;
}
public void Decrease_Key(Vertex v, int d) throws Exception{
int i = vertexs.indexOf(v);
if (d > v.getD()){
throw new Exception("new key is bigger than current key: " + v);
}
v.setD(d);
while (i > 0 && vertexs.get(Parent(i)).getD() > v.getD()){
Vertex x = vertexs.get(i);
vertexs.set(i, vertexs.get(Parent(i)));
vertexs.set(Parent(i), x);
i = Parent(i);
}
}
private int Parent(int i) {
// TODO Auto-generated method stub
return (i-1)/2;
}
public boolean contains(Vertex v){
return vertexs.contains(v);
}
public boolean isEmpty(){
return vertexs.isEmpty();
}
}