Dijkstra算法如何使其定向工作

问题描述

我正在使用下面的方法来实现查找最短路径的算法。

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DijkstraAlgorithm {

        private final List<Node> nodes;
        private final List<Edge> edges;
        private Set<Node> settlednodes;
        private Set<Node> unSettlednodes;
        private Map<Node,Node> predecessors;
        private Map<Node,Integer> distance;

        public DijkstraAlgorithm(Graph graph) {
            // create a copy of the array so that we can operate on this array
            this.nodes = new ArrayList<Node>(graph.getNodelIst());
            this.edges = new ArrayList<Edge>(graph.getEdgeList());
        }

        public void execute(Node source) {
            settlednodes = new HashSet<Node>();
            unSettlednodes = new HashSet<Node>();
            distance = new HashMap<Node,Integer>();
            predecessors = new HashMap<Node,Node>();
            distance.put(source,0);
            unSettlednodes.add(source);
            while (unSettlednodes.size() > 0) {
                Node node = getMinimum(unSettlednodes);
                settlednodes.add(node);
                unSettlednodes.remove(node);
                findMinimaldistances(node);
            }
        }

        private void findMinimaldistances(Node node) {
            List<Node> adjacentNodes = getNeighbors(node);
            for (Node target : adjacentNodes) {
                if (getShortestdistance(target) > getShortestdistance(node)
                        + getdistance(node,target)) {
                    distance.put(target,getShortestdistance(node)
                            + getdistance(node,target));
                    predecessors.put(target,node);
                    unSettlednodes.add(target);
                }
            }

        }

        private int getdistance(Node node,Node target) {
            for (Edge edge : edges) {
                if (edge.getSourceNode().equals(node)
                        && edge.getEndNode().equals(target)) {
                    return edge.getWeight();
                }
            }
            throw new RuntimeException("Should not happen");
        }

        private List<Node> getNeighbors(Node node) {
            List<Node> neighbors = new ArrayList<Node>();
            for (Edge edge : edges) {
                if (edge.getSourceNode().equals(node)
                        && !isSettled(edge.getEndNode())) {
                    neighbors.add(edge.getEndNode());
                }
            }
            return neighbors;
        }

        private Node getMinimum(Set<Node> vertexes) {
            Node minimum = null;
            for (Node vertex : vertexes) {
                if (minimum == null) {
                    minimum = vertex;
                } else {
                    if (getShortestdistance(vertex) < getShortestdistance(minimum)) {
                        minimum = vertex;
                    }
                }
            }
            return minimum;
        }

        private boolean isSettled(Node vertex) {
            return settlednodes.contains(vertex);
        }

        private int getShortestdistance(Node destination) {
            Integer d = distance.get(destination);
            if (d == null) {
                return Integer.MAX_VALUE;
            } else {
                return d;
            }
        }

    /*
     * This method returns the path from the source to the selected target and
     * NULL if no path exists
     */
    public LinkedList<Node> getPath(Node target) {
        LinkedList<Node> path = new LinkedList<Node>();
        Node step = target;
        // check if a path exists
        if (predecessors.get(step) == null) {
            return null;
        }
        path.add(step);
        while (predecessors.get(step) != null) {
            step = predecessors.get(step);
            path.add(step);
        }
        // Put it into the correct order
        Collections.reverse(path);
        return path;
    }


    }

这很好用,但是我现在想向我的边缘添加一个方向并运行相同的有向方法,以返回有向PathList。例如,我将双向传递11,将->传递01,将

任何人都可以帮忙吗?

解决方法

我认为最简单的方法是保持定向边缘不变,如果连接是双向的,则创建两个边缘。

改写 NodeAID,NodeBID,01赋予edges.add(new Edge(NodeAID,NodeBID))

NodeAID,NodeBID,10赋予edges.add(new Edge(NodeBID,NodeAID))

和NodeAID,NodeBID,11给出

edges.add(new Edge(NodeAID,NodeBID));    
edges.add(new Edge(NodeBID,NodeAID));

您可以创建同时处理单向和双向的Edge接口,但我认为这会使边缘开始在不同方向上具有不同的信念的情况变得更加复杂。