Dijkstra算法

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值