dijkstra算法

根据实际情况,overload一下构造方法即可。

package com.util;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * dijkstra算法,求最短路径
 */
public class DijkstraUtil {

    private RoutePlanner planner = new RoutePlanner();
    private Graph g = new Graph();

    public DijkstraUtil() {
        List<Node> list = new ArrayList<Node>();
        Node aNode = new Node("A");
        list.add(aNode);
        // A -> B
        Edge aEdge1 = new Edge();
        aEdge1.setStartNodeID(aNode.getId());
        aEdge1.setEndNodeID("B");
        aEdge1.setWeight(10);
        aNode.getEdgeList().add(aEdge1);
        // A -> C
        Edge aEdge2 = new Edge();
        aEdge2.setStartNodeID(aNode.getId());
        aEdge2.setEndNodeID("C");
        aEdge2.setWeight(20);
        aNode.getEdgeList().add(aEdge2);
        // A -> E
        Edge aEdge3 = new Edge();
        aEdge3.setStartNodeID(aNode.getId());
        aEdge3.setEndNodeID("E");
        aEdge3.setWeight(30);
        aNode.getEdgeList().add(aEdge3);

        // ***************** B Node *******************
        Node bNode = new Node("B");
        list.add(bNode);
        // B -> C
        Edge bEdge1 = new Edge();
        bEdge1.setStartNodeID(bNode.getId());
        bEdge1.setEndNodeID("C");
        bEdge1.setWeight(5);
        bNode.getEdgeList().add(bEdge1);
        // B -> E
        Edge bEdge2 = new Edge();
        bEdge2.setStartNodeID(bNode.getId());
        bEdge2.setEndNodeID("E");
        bEdge2.setWeight(10);
        bNode.getEdgeList().add(bEdge2);

        // ***************** C Node *******************
        Node cNode = new Node("C");
        list.add(cNode);
        // C -> D
        Edge cEdge1 = new Edge();
        cEdge1.setStartNodeID(cNode.getId());
        cEdge1.setEndNodeID("D");
        cEdge1.setWeight(30);
        cNode.getEdgeList().add(cEdge1);

        // ***************** D Node *******************
        Node dNode = new Node("D");
        list.add(dNode);

        // ***************** E Node *******************
        Node eNode = new Node("E");
        list.add(eNode);
        // E -> D
        Edge eEdge1 = new Edge();
        eEdge1.setStartNodeID(eNode.getId());
        eEdge1.setEndNodeID("D");
        eEdge1.setWeight(20);
        eNode.getEdgeList().add(eEdge1);

        g.setM_nodeList(list);
    }

    public RoutePlanResult plan(String originID, String targetID) throws Exception {
        return planner.plan(g.getM_nodeList(), originID, targetID);
    }

    public PlanCourse plan(String originID) throws Exception {
        return planner.plan(g.getM_nodeList(), originID);
    }

    public class Edge {
        private String startNodeID;
        private String endNodeID;
        private double weight;

        public String getStartNodeID() {
            return startNodeID;
        }

        public void setStartNodeID(String startNodeID) {
            this.startNodeID = startNodeID;
        }

        public String getEndNodeID() {
            return endNodeID;
        }

        public void setEndNodeID(String endNodeID) {
            this.endNodeID = endNodeID;
        }

        public double getWeight() {
            return weight;
        }

        public void setWeight(double weight) {
            this.weight = weight;
        }

    }

    public class Node {
        private String id;
        private List<Edge> edgeList;

        public Node(String id) {
            this.id = id;
            this.edgeList = new ArrayList<Edge>();
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public List<Edge> getEdgeList() {
            return edgeList;
        }

        public void setEdgeList(List<Edge> edgeList) {
            this.edgeList = edgeList;
        }

    }

    public class Graph {
        private List<Node> m_nodeList = null;

        public List<Node> getM_nodeList() {
            return m_nodeList;
        }

        public void setM_nodeList(List<Node> m_nodeList) {
            this.m_nodeList = m_nodeList;
        }
    }

    class PassedPath {
        private String curNodeID;
        private boolean beProcessed;
        private double weight;
        private List<String> passedIDList;

        public PassedPath(String ID) {
            this.curNodeID = ID;
            this.weight = Double.MAX_VALUE;
            this.passedIDList = new ArrayList<String>();
            this.beProcessed = false;
        }

        public String getCurNodeID() {
            return curNodeID;
        }

        public void setCurNodeID(String curNodeID) {
            this.curNodeID = curNodeID;
        }

        public boolean isBeProcessed() {
            return beProcessed;
        }

        public void setBeProcessed(boolean beProcessed) {
            this.beProcessed = beProcessed;
        }

        public double getWeight() {
            return weight;
        }

        public void setWeight(double weight) {
            this.weight = weight;
        }

        public List<String> getPassedIDList() {
            return passedIDList;
        }

        public void setPassedIDList(List<String> passedIDList) {
            this.passedIDList = passedIDList;
        }

    }

    class PlanCourse {
        private Hashtable<String, PassedPath> htPassedPath;

        public PlanCourse(List<Node> nodeList, String originID) throws Exception {
            this.htPassedPath = new Hashtable<String, PassedPath>();
            Node originNode = null;
            for (Node node : nodeList) {
                if (originID.equals(node.getId())) {
                    originNode = node;
                } else {
                    PassedPath pPath = new PassedPath(node.getId());
                    this.htPassedPath.put(node.getId(), pPath);
                }
            }
            if (originNode == null) {
                throw new Exception("The origin node is not exist !");
            }
            this.InitializeWeight(originNode);
        }

        private void InitializeWeight(Node originNode) {
            if ((originNode.getEdgeList() == null) || (originNode.getEdgeList().size() == 0)) {
                return;
            }

            for (Edge edge : originNode.getEdgeList()) {
                PassedPath pPath = getPassedPath(edge.getEndNodeID());
                if (pPath == null) {
                    continue;
                }

                pPath.getPassedIDList().add(originNode.getId());
                pPath.setWeight(edge.getWeight());
            }
        }

        public PassedPath getPassedPath(String nodeID) {
            return this.htPassedPath.get(nodeID);
        }
    }

    class RoutePlanner {
        public RoutePlanResult plan(List<Node> nodeList, String originID, String destID) throws Exception {
            PlanCourse planCourse = plan(nodeList, originID);
            return this.getResult(planCourse, destID);
        }

        public PlanCourse plan(List<Node> nodeList, String originID) throws Exception {
            PlanCourse planCourse = new PlanCourse(nodeList, originID);
            Node curNode = this.getMinWeightRudeNode(planCourse, nodeList, originID);
            while (curNode != null) {
                PassedPath curPath = planCourse.getPassedPath(curNode.getId());
                for (Edge edge : curNode.getEdgeList()) {
                    PassedPath targetPath = planCourse.getPassedPath(edge.getEndNodeID());
                    double tempWeight = curPath.getWeight() + edge.getWeight();

                    if (tempWeight < targetPath.getWeight()) {
                        targetPath.setWeight(tempWeight);
                        targetPath.getPassedIDList().clear();

                        for (int i = 0; i < curPath.getPassedIDList().size(); i++) {
                            targetPath.getPassedIDList().add(curPath.getPassedIDList().get(i));
                        }

                        targetPath.getPassedIDList().add(curNode.getId());
                    }
                }
                planCourse.getPassedPath(curNode.getId()).setBeProcessed(true);
                curNode = this.getMinWeightRudeNode(planCourse, nodeList, originID);
            }
            return planCourse;
        }

        private RoutePlanResult getResult(PlanCourse planCourse, String destID) {
            PassedPath pPath = planCourse.getPassedPath(destID);

            if (pPath.getWeight() == Integer.MAX_VALUE) {
                RoutePlanResult result1 = new RoutePlanResult(null, Integer.MAX_VALUE);
                return result1;
            }

            String[] passedNodeIDs = new String[pPath.getPassedIDList().size()];
            for (int i = 0; i < passedNodeIDs.length; i++) {
                passedNodeIDs[i] = pPath.getPassedIDList().get(i);
            }
            RoutePlanResult result = new RoutePlanResult(passedNodeIDs, pPath.getWeight());

            return result;
        }

        private Node getMinWeightRudeNode(PlanCourse planCourse, List<Node> nodeList, String originID) {
            double weight = Double.MAX_VALUE;
            Node destNode = null;
            for (int i = 0; i < nodeList.size(); i++) {
                Node node = (Node) nodeList.get(i);
                if (originID.equals(node.getId())) {
                    continue;
                }

                PassedPath pPath = planCourse.getPassedPath(node.getId());
                if (pPath.isBeProcessed()) {
                    continue;
                }

                if (pPath.getWeight() < weight) {
                    weight = pPath.getWeight();
                    destNode = node;
                }
            }
            return destNode;
        }
    }

    public class RoutePlanResult {
        public RoutePlanResult(String[] passedNodes, double value) {
            m_resultNodes = passedNodes;
            m_value = value;
        }

        private String[] m_resultNodes;

        private double m_value;

        public String[] getM_resultNodes() {
            return m_resultNodes;
        }

        public void setM_resultNodes(String[] m_resultNodes) {
            this.m_resultNodes = m_resultNodes;
        }

        public double getM_value() {
            return m_value;
        }

        public void setM_value(double m_value) {
            this.m_value = m_value;
        }

        public String getStringResult() {
            String result;
            StringBuffer sb = new StringBuffer("[");
            for (int i = 0; i < m_resultNodes.length; i++)
                sb.append(m_resultNodes[i] + ",");
            if (sb.length() > 1)
                result = sb.substring(0, sb.length() - 1) + "]";
            else
                result = sb.append("]").toString();
            return result;
        }
    }
}

 

调用方法如下:

import com.util.DijkstraUtil;
import com.util.DijkstraUtil.RoutePlanResult;

/** 最短路径 **/
public class Test {

    public static void main(String[] args) throws Exception {
        DijkstraUtil du = new DijkstraUtil();
        RoutePlanResult result = du.plan("A", "D");
        System.out.println(result.getM_value());
        System.out.println(result.getStringResult());

    }
}

 

posted on 2017-02-07 15:19  狂奔的呙牛  阅读(127)  评论(0编辑  收藏  举报

导航