dijkstra

參考鏈接:

image.png

小頂堆實(shí)現(xiàn),查找unsettled中距離源點(diǎn)最小的節(jié)點(diǎn)時(shí)間復(fù)雜度較低,但是小頂堆里,一個(gè)節(jié)點(diǎn)可能同時(shí)存在不止一個(gè)。

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)){// 因?yàn)樾№敹牙?,一個(gè)節(jié)點(diǎn)可能同時(shí)存在不止一個(gè)
                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;
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Dijkstra算法是給定一個(gè)起點(diǎn)也就是原點(diǎn),然后該原點(diǎn)通過(guò)與它臨近的頂點(diǎn)不斷向外擴(kuò)張,最終得到該原點(diǎn)到其它所有頂...
    lambdacalculus閱讀 4,423評(píng)論 1 3
  • 與大家分享若干Dijkstra的觀(guān)點(diǎn)。希望對(duì)初入門(mén)者有所啟發(fā)。(注:原文出自網(wǎng)絡(luò),已經(jīng)無(wú)法溯源原始鏈接或頁(yè)面。) ...
    Bintou老師閱讀 1,003評(píng)論 1 3
  • 1. 兩個(gè)list的nodes,searched and unsearched 2. start from a s...
    宇翔_0e77閱讀 226評(píng)論 0 0
  • 適用問(wèn)題 使用于求單個(gè)源的最短路徑(單源到單目的或多目的均可),要求邊的權(quán)重非負(fù)。 算法 時(shí)間復(fù)雜度:訪(fǎng)問(wèn)條邊,優(yōu)...
    devilisdevil閱讀 244評(píng)論 0 0
  • 今天感恩節(jié)哎,感謝一直在我身邊的親朋好友。感恩相遇!感恩不離不棄。 中午開(kāi)了第一次的黨會(huì),身份的轉(zhuǎn)變要...
    余生動(dòng)聽(tīng)閱讀 10,798評(píng)論 0 11

友情鏈接更多精彩內(nèi)容