dijkstra

作者: senzx | 来源:发表于2021-04-23 07:37 被阅读0次

参考链接:

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

    Dijkstra Conception Dijkstra is a single source shortest ...

  • 最短路径模板

    堆优化的Dijkstra 普通Dijkstra SPFA

  • 图算法(二)最短路

    本文将介绍三种常见的最短路算法:Dijkstra,Floyd,SPFA Dijkstra Dijkstra是有向图...

  • 图的最短路径

    Dijkstra算法&Floyd算法 一、Dijkstra算法 Dijkstra算法思想: 只计算v0出发到其他顶...

  • 深入解析Dijkstra's Algorithm ——

    什么是Dijkstra算法? Dijkstra算法是用来寻找最短路径最著名的算法之一。具体来说,Dijkstra算...

  • 算法模板(三)最短路问题

    Dijkstra SPFA Floyd

  • DFS BFS

    BFS DFS Dijkstra

  • ——Dijkstra

    dijkstra由于是贪心的,每次都找一个距源点最近的点(dmin),然后将该距离定为这个点到源点的最短路径。但如...

  • Dijkstra

  • Dijkstra

    1.问题 单源最短路径 2 最短路径数组就是当前各个节点到A的距离前驱数组就是相应距离的前一个节点 此时在V-S中...

网友评论

      本文标题:dijkstra

      本文链接:https://www.haomeiwen.com/subject/uicfrltx.html