Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
迪克斯特拉';基于Java的优先级队列s算法_Java_Priority Queue_Dijkstra - Fatal编程技术网

迪克斯特拉';基于Java的优先级队列s算法

迪克斯特拉';基于Java的优先级队列s算法,java,priority-queue,dijkstra,Java,Priority Queue,Dijkstra,我需要在Java中使用优先级队列实现Dijkstra算法。以下是我目前的代码: public class Node { long idNum; String label; HashSet<Edge> outEdges; HashSet<Edge> inEdges; int indegree; int outdegree; int inNum, outNum;

我需要在Java中使用优先级队列实现Dijkstra算法。以下是我目前的代码:

public class Node {
        long idNum;
        String label;
        HashSet<Edge> outEdges;
        HashSet<Edge> inEdges;
        int indegree;
        int outdegree; 

        int inNum, outNum;
        HashMap<Node, Edge> incoming, outgoing;

        Node(String label, Long idNum) {
            this.label = label;
            this.idNum = idNum;

            inNum =0;
            outNum=0; 
            incoming = new HashMap<Node, Edge>();
            outgoing = new HashMap<Node, Edge>();

        }
        Node(String Label){
            this.label=label;
        }

        public void addOutgoing(Node n, Edge e){
            if(n==null) return;
            outgoing.put(n,e);
            outNum++;
        }
        public void addIncoming(Node n, Edge e){
            if(n==null) return;
            incoming.put(n, e);
            inNum++;
        }
        public void delIn(Node n){
            incoming.remove(n);
            inNum--; 
        }
        public void delOut(Node n){
            outgoing.remove(n);
            outNum--;
        }

        public int getinNum(){
            return this.inNum; 
        }
        public boolean containsEdge(Edge e){
            if(incoming.containsValue(e) || outgoing.containsValue(e)){
                return true;
            }
            return false;
        }

        String getLabel(){
            return this.label;
        }


    }

    public class Edge {

        long idNum, weight;
        String sLabel, dLabel, eLabel;
        Node sNode, dNode;
        Node from;
        Node to;
        int distance;

        public Edge(long idNum, String sLabel, String dLabel, String eLabel) {
            this.idNum = idNum;
            // this.weight=weight;
            this.sLabel = sLabel;
            this.dLabel = dLabel;
            this.eLabel = eLabel;
        }

        public Edge(Node from, Node to) {
            this.from = from;
            this.to = to;
        }

        long getidNum() {
            return this.idNum;
        }

        public int getDistance() {
            return this.distance;
        }

    }


public class DiGraph implements DiGraph_Interface {
    // private Map<Node, Edge> digraph = new HashMap<Node, Edge>();
    private Map<String, Long> nodes = new HashMap<String, Long>();
    private Set<Node> nodes1 = new HashSet<Node>();
    private Set<Edge> edges = new HashSet<Edge>();
    private Map<Node, Node> edges1 = new HashMap<Node, Node>();
    private Set<Long> edge_ids = new HashSet<Long>();

    public long numEdges = 0;
    public long numNodes = 0;

    public DiGraph() { // default constructor
        // explicitly include this
        // we need to have the default constructor
        // if you then write others, this one will still be there

    }

    @Override
    public boolean addNode(long idNum, String label) {
        Node node = new Node(label, idNum);
        if(nodes.containsKey(label) || idNum <0 || label==null || nodes.containsValue(idNum)){
            return false;
        }
        nodes.put(label, idNum);
        nodes1.add(node);
        numNodes++;
        return true;

    }

    @Override
    public boolean addEdge(long idNum, String sLabel, String dLabel, long weight, String eLabel) {
        Edge e = new Edge(idNum, sLabel, dLabel, eLabel);
        Node n1 = new Node(sLabel, idNum);
        Node n2 = new Node(dLabel, idNum);
        if(edge_ids.contains(idNum)){
            return false;
        }
        for(Node n: nodes1){
            if(n.containsEdge(e)){
                return false;}
        }
        for(Edge edge: edges){
            if(edge.dLabel == dLabel && edge.sLabel == sLabel){return false;}
        }

        boolean check1=false;
        boolean check2=false;
        for(Node n: nodes1){
            if(n.label.equals(sLabel)){
                e.sNode=n; 
                check1=true;
            }
            if(n.label.equals(dLabel)){
                e.dNode=n;
                check2=true;
            }
        }
        if(!check1 || !check2){return false;}

        e.sNode.addOutgoing(e.dNode, e);
        e.dNode.addIncoming(e.sNode,e);

        n1.addOutgoing(n2, e);
        n2.addIncoming(n1, e);
        edge_ids.add(idNum);
        edges.add(e);
        numEdges++;
        return true; 

    }

    @Override
    public boolean delNode(String label) {
        Node node = new Node(label);
        if (!nodes.containsKey(label)) {
            return false;
        }
        if (nodes.containsKey(label) || nodes1.contains(node)) {
            nodes.remove(label, nodes.get(label));
            nodes1.remove(node);
            numNodes--;
            return true;
        }
        Set<Edge> remainingEdges = new HashSet<Edge>();
        for(Edge edge : edges){
            if(!node.containsEdge(edge)){
                remainingEdges.add(edge);
            }
        }   
        edges =  remainingEdges;
        numNodes--;
        return true;
    }

    @Override
    public boolean delEdge(String sLabel, String dLabel) {
        if(!nodes.containsKey(dLabel)|| !nodes.containsKey(sLabel)){
            return false;
        }
        for(Edge edge: edges){
            if(edge.dLabel == dLabel && edge.sLabel == sLabel){
                edge.sNode.delOut(edge.dNode);
                edge.dNode.delIn(edge.sNode);
                long idNum = edge.getidNum();
                numEdges--;
                edges.remove(edge);
                edge_ids.remove(idNum);
                return true;
            }
        }
        return false; 
    }


    @Override
    public long numNodes() {
        return this.numNodes;
    }

    @Override
    public long numEdges() {
        return this.numEdges;
    }

    @Override
    public String[] topoSort() {


        ArrayList<Node> nodeArray = new ArrayList<Node>();
        Stack<Node> nodeStack = new Stack<Node>();
        for(Node n: nodes1){
            nodeArray.add(n);
        }
        String[] topoSort = new String[(int) numNodes]; 
        int counter=0;

        int i=0;
        //for(int i=0; i< numNodes; i++){
            for(Node n: nodes1){

                if(n.inNum==0){
                    nodeStack.push(n);
                }
                if(nodeStack.isEmpty()){
                    return null;
                }
                while(!nodeStack.isEmpty()){
                    nodeStack.pop();
                    nodeArray.remove(n);
                if(n.incoming==null){
                    topoSort[i]=n.getLabel();
                    counter++;
                    i++;
                }
                }
            //}
        }
        if(counter != numNodes){
            return null;
        }
        return topoSort;
    }

    @Override
    public ShortestPathInfo[] shortestPath(String label) {
        Node startNode = new Node(label);

        return null;
    }
}
公共类节点{
长idNum;
字符串标签;
散列集路由;
HashSet-inEdges;
内因度;
内倾度;
内内,外内;
HashMap传入、传出;
节点(字符串标签,长idNum){
this.label=标签;
this.idNum=idNum;
inNum=0;
outNum=0;
传入=新HashMap();
传出=新HashMap();
}
节点(字符串标签){
这个标签=标签;
}
公共void addouting(节点n,边e){
如果(n==null)返回;
输出(n,e);
outNum++;
}
公共void addIncoming(节点n,边e){
如果(n==null)返回;
输入。输入(n,e);
inNum++;
}
公共void delIn(节点n){
传入。移除(n);
铟--;
}
公共void delOut(节点n){
移除(n);
outNum--;
}
公共int GETINUM(){
把这个还给我;
}
公共莎草(边缘e){
if(传入.containsValue(e)| |传出.containsValue(e)){
返回true;
}
返回false;
}
字符串getLabel(){
返回此.label;
}
}
公共阶级边缘{
长idNum,重量;
字符串sLabel、dLabel、eLabel;
节点sNode,dNode;
节点来自;
节点到节点;
整数距离;
公共边缘(长idNum、字符串sLabel、字符串dLabel、字符串eLabel){
this.idNum=idNum;
//这个。重量=重量;
this.sLabel=sLabel;
this.dLabel=dLabel;
this.eLabel=eLabel;
}
公共边缘(节点从、节点到){
this.from=from;
这个;
}
long getidNum(){
返回this.idNum;
}
公共int getDistance(){
返回此值。距离;
}
}
公共类有向图实现有向图接口{
//私有映射有向图=新HashMap();
私有映射节点=新HashMap();
私有集nodes1=新HashSet();
私有集边=新HashSet();
private-Map-edges1=新的HashMap();
私有集边缘_id=new HashSet();
公共长numEdges=0;
公共长节点=0;
公共有向图(){//默认构造函数
//明确包括这一点
//我们需要有默认的构造函数
//如果你再写其他的,这个仍然会存在
}
@凌驾
公共布尔addNode(长idNum,字符串标签){
节点节点=新节点(标签,idNum);

if(nodes.containsKey(label)| | idNum可以使用java.util.TreeSet作为优先级队列。它包含用于将元素放入队列的add()方法和用于获取具有最低值的元素的pollFirst()方法


为了进行比较,您可以创建要放入队列的类对象(很可能,它不仅仅是节点,而是包含对节点的引用和恢复路由所需的附加信息的附加类)实现接口Comparable或创建一个Comparator并将其作为参数传递给TreeSet构造函数。在这两种情况下,您都需要实现方法compareTo(),该方法将根据算法的要求按节点的距离进行比较。

节点开始
类:

public class Node {
    // add a parent attribute to the class
    // this will be used in your shortestPath method
    // i have explained it below
    private Node parent;
}
public class Edge {
    // why do you have four of these? You only need two
    private Node sNode, dNode;
    private Node from;
    private Node to;
}
边缘
类:

public class Node {
    // add a parent attribute to the class
    // this will be used in your shortestPath method
    // i have explained it below
    private Node parent;
}
public class Edge {
    // why do you have four of these? You only need two
    private Node sNode, dNode;
    private Node from;
    private Node to;
}
我觉得你的有向图类太复杂了。你可以稍微简化一下:

public class DiGraph implements DiGraph_Interface {

    private LinkedList<Node>[] adjList;
    private HashSet<Edge> edges;

    // implement the interface methods as you have done
}