狄克斯特拉算法

要计算非加权图中的最短路径, 可使用广度优先搜索。 要计算
加权图中的最短路径,可使用狄克斯特拉算法。

图片.png

在无向图中,每条边都是一个环。狄克斯特拉算法只适用于有向无环图


图片.png
图片.png

以下是具体的算法实现

/**
 * 广度优先算法适用于无权边计算最短路径
 * 狄克斯特拉算法适用于有权边计算最短路径
 * 仅当权重为正时才适用
 * 如果权重为负,应使用贝尔曼-福德算法
 * 
 */
package com.algorithm.kavin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Dijkstra {
    
 // 用于记录处理过得node
    private static List<String> processed = new ArrayList<>();
 // 创建graph所在map
    private static Map<String, Map<String, Integer>> graph = new HashMap<>();
    

    public static String findSmallerNode(Map<String, Integer> cost) {
        int smallerCost = Integer.MAX_VALUE;
        String smallerCostNode = null;
        for (String key : cost.keySet()) {
            int tempCost = cost.get(key);
            if (tempCost < smallerCost && !processed.contains(key)) {
                smallerCost = tempCost;
                smallerCostNode = key;
            }
        }
        return smallerCostNode;
    }

    public static void main(String[] args) {

        graph.put("start", new HashMap<>());
        graph.get("start").put("A", 6);
        graph.get("start").put("B", 2);
        
        graph.put("A", new HashMap<>());
        graph.get("A").put("fin", 1);

        graph.put("B", new HashMap<>());
        graph.get("B").put("A",3);
        graph.get("B").put("fin", 1);
        
        graph.put("fin", new HashMap<>());

        // 创建cost消耗所在map
        Map<String, Integer> costs = new HashMap<>();
        costs.put("A", 6);
        costs.put("B", 2);
        costs.put("fin", Integer.MAX_VALUE);

        // 创建父节点
        Map<String, String> parent = new HashMap<>();
        parent.put("A", "start");
        parent.put("B", "start");
        parent.put("fin", null);

        

        // 开始处理问题
        String smallestNode = findSmallerNode(costs);
        while (smallestNode != null) {
            Integer cost = costs.get(smallestNode);
            Map<String, Integer> neighbour = graph.get(smallestNode);
            for (String i : neighbour.keySet()) {
                Integer newCost = cost + neighbour.get(i);
                if (costs.get(i) > newCost) {
                    costs.put(i, newCost);
                    parent.put(i, smallestNode);
                }
            }
            processed.add(smallestNode);
            smallestNode = findSmallerNode(costs);
        }
        System.out.println(graph);
    }
}


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

推荐阅读更多精彩内容

  • 欢迎访问我的博客:http://wangnan.tech 第七章 狄克斯特拉算法 前一章使用了广度优先搜索,它找出...
    GhostStories阅读 3,856评论 0 2
  • 这篇文章是《图解算法》一书的摘抄总结。原书标题是《Grokking Algorithms》,grok是中文“意会”...
    SeanCheney阅读 8,268评论 0 14
  • 1 序 2016年6月25日夜,帝都,天下着大雨,拖着行李箱和同学在校门口照了最后一张合照,搬离寝室打车去了提前租...
    RichardJieChen阅读 10,608评论 0 12
  • 图是一种比线性表和树更复杂的数据结构,在图中,结点之间的关系是任意的,任意两个数据元素之间都可能相关。图是一种多对...
    Alent阅读 6,867评论 1 22
  • 本文内容:1、什么是算法?2、算法分析基础?3、文集列表 建议数据结构和算法分开来学,这里只有算法,没有什么是数据...
    半纸渊阅读 4,123评论 0 5