dijkstra

参考链接:

image.png

小顶堆实现,查找unsettled中距离源点最小的节点时间复杂度较低,但是小顶堆里,一个节点可能同时存在不止一个。

import java.util.*;

public class Dijkstra {

    public static void main(String[] args) {
        Node nodeA = new Node("A");
        Node nodeB = new Node("B");
        Node nodeC = new Node("C");
        Node nodeD = new Node("D");
        Node nodeE = new Node("E");
        Node nodeF = new Node("F");
        nodeA.distToSource = 0;
        nodeA.adjacent.put(nodeB,10);
        nodeA.adjacent.put(nodeC,15);
        nodeB.adjacent.put(nodeD,12);
        nodeB.adjacent.put(nodeF,15);
        nodeC.adjacent.put(nodeE,10);
        nodeD.adjacent.put(nodeE,2);
        nodeD.adjacent.put(nodeF,1);
        nodeF.adjacent.put(nodeE,5);
        List<Node> nodes = new ArrayList<>(Arrays.asList(nodeA,nodeB,nodeC,nodeD,nodeE,nodeF));
        Dijkstra obj = new Dijkstra();
        int minDist = obj.dijkstra(nodes, nodeA, nodeE);

    }

    public int dijkstra(List<Node> nodes, Node source, Node destination){
        Set<Node> settled = new HashSet<>();
        PriorityQueue<Node> unSettled = new PriorityQueue<>((node1, node2) -> node1.distToSource - node2.distToSource);
        unSettled.add(source);
        while(unSettled.size()>0){
            Node node = unSettled.poll();
            if(settled.contains(node)){// 因为小顶堆里,一个节点可能同时存在不止一个
                continue;
            }
            node.shortestPath = new ArrayList<>(node.shortestPath);
            node.shortestPath.add(node.name);
            settled.add(node);
            if(node == destination){
                return destination.distToSource;
            }
            for (Map.Entry<Node, Integer> entry : node.adjacent.entrySet()) {
                Node adjacent = entry.getKey();
                int edgeDist = entry.getValue();
                if(!settled.contains(adjacent)){
                    if(node.distToSource+edgeDist < adjacent.distToSource){
                        adjacent.distToSource = node.distToSource+edgeDist;
                        adjacent.shortestPath = new ArrayList<>(node.shortestPath);
                    }
                    unSettled.offer(adjacent);
                }
            }
        }
        return destination.distToSource;
    }

    static class Node{
        String name = null;
        int distToSource = Integer.MAX_VALUE;
        Map<Node, Integer> adjacent = new HashMap<>();
        List<String> shortestPath = new LinkedList<>();

        public Node(String name){
            this.name = name;
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Dijkstra算法是给定一个起点也就是原点,然后该原点通过与它临近的顶点不断向外扩张,最终得到该原点到其它所有顶...
    lambdacalculus阅读 9,759评论 1 3
  • 与大家分享若干Dijkstra的观点。希望对初入门者有所启发。(注:原文出自网络,已经无法溯源原始链接或页面。) ...
    Bintou老师阅读 4,474评论 1 3
  • 1. 两个list的nodes,searched and unsearched 2. start from a s...
    宇翔_0e77阅读 1,293评论 0 0
  • 适用问题 使用于求单个源的最短路径(单源到单目的或多目的均可),要求边的权重非负。 算法 时间复杂度:访问条边,优...
    devilisdevil阅读 1,514评论 0 0
  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    余生动听阅读 13,589评论 0 11