美文网首页
图的BFS & DFS & Dijkstra算法

图的BFS & DFS & Dijkstra算法

作者: JaJIng | 来源:发表于2019-04-04 13:56 被阅读0次
图.PNG

python 队列实现的图的BFS,类似于哈夫曼树遍历:


BFS.PNG

栈实现的图的DFS:


DFS.PNG

BFS扩展最短路径:


parent.PNG

Dijkstra(加权最短路径计算):


dijkstra 推导.PNG
dijkstra.PNG 初始化.PNG
Dijkstra.PNG

写了个java版,比起js和python这种基于对象而不是oo语言,有点啰嗦:

import java.util.*;

/**
 * @author jajing
 */
public class Dijkstra {


    private static class Node implements Comparable<Node>{
        String name;
        Integer distance;
        Node(String name,Integer distance){
            this.name = name;
            this.distance = distance;
        }

        @Override
        public int compareTo(Node o) {
            //正序
            return this.distance - o.distance;
        }
    }

    //图的两点间最短路径
    public static void main(String[] args) {
        Map<String,Map<String,Integer>> map = new HashMap<String,Map<String,Integer>>();
        Map mA = new HashMap();mA.put("B",5);mA.put("C",1);
        Map mB = new HashMap();mB.put("A",5);mB.put("C",2);mB.put("D",1);
        Map mC = new HashMap();mC.put("A",1);mC.put("B",2);mC.put("D",4);mC.put("E",8);
        Map mD = new HashMap();mD.put("B",1);mD.put("C",4);mD.put("E",3);mD.put("F",6);
        Map mE = new HashMap();mE.put("C",8);mE.put("D",3);
        Map mF = new HashMap();mF.put("D",6);
        map.put("A",mA);
        map.put("B",mB);
        map.put("C",mC);
        map.put("D",mD);
        map.put("E",mE);
        map.put("F",mF);
        Map<String,Integer>  result =  dijkstra(map,"A");
        for(Map.Entry<String,Integer> e:result.entrySet()){
            System.out.println(e.getKey() + ":" + e.getValue());
            /**
             * A:0
             * B:3
             * C:1
             * D:4
             * E:7
             * F:10
             */
        }

    }

    public static Map<String,Integer> initDistance(Map<String,Map<String,Integer>> map,String start){
        Map<String,Integer> distance = new HashMap<String, Integer>();
        for(String key:map.keySet()){
            distance.put(key,Integer.MAX_VALUE);
        }
        return distance;
    }

    public static Map<String,Integer>  dijkstra(Map<String,Map<String,Integer>> map,String start){
        //最短距离表,初始都最大化
        Map<String,Integer> minDistance = initDistance(map,start);
        Set<String> seen = new HashSet<String>();
        //最短路径的父结点
        Map<String,String> parent = new HashMap<String, String>();

        PriorityQueue<Node> pQueue = new PriorityQueue<Node>();

        pQueue.offer(new Node(start,0));
        parent.put(start,"Null");
        minDistance.put(start,0);

        while(!pQueue.isEmpty()){
            Node current = pQueue.poll();
            //一定要在取出时才放到seen去!!
            seen.add(start);
            String currentName = current.name;
            Integer currentDist = current.distance;
            
            Map<String,Integer> connects = map.get(currentName);
            for(Map.Entry<String,Integer> entry : connects.entrySet()){
                String nextKey = entry.getKey();
                Integer nextValue = entry.getValue();

                if(seen.contains(nextKey)) continue;

                Integer newDistance = currentDist + nextValue;
                if(newDistance < minDistance.get(nextKey)){
                    parent.put(nextKey,currentName);
                    minDistance.put(nextKey,newDistance);
                    pQueue.offer(new Node(nextKey,newDistance));
                }
            }
        }
        return minDistance;

    }
}

这里要感谢@正月点灯笼的课件

相关文章

  • DFS BFS

    BFS DFS Dijkstra

  • 图的BFS & DFS & Dijkstra算法

    python 队列实现的图的BFS,类似于哈夫曼树遍历: 栈实现的图的DFS: BFS扩展最短路径: Dijkst...

  • 图的结构 BFS DFS

    题目:BFS 一个队列,一个set集合 题目:DFS 题目:Kruskal算法 题目:Prim Dijkstra算法

  • BFS

    [TOC] BFS 和 DFS BFS广度有限搜索和DFS深度优先搜索算法是特别常用的两种算法 DFS 算法就是回...

  • 图的桥和割点

    内容概要: 图中的桥 图的DFS遍历树和BFS遍历树及其与寻桥算法的关系 图的割点 DFS和BFS的对比小结 桥(...

  • 图论小结

    图的存储 邻接矩阵 邻接表 图的遍历 DFS(双向DFS) BFS(剪枝) 单源最短路径算法 dij 条件:无负权...

  • 77. 组合/207. 课程表

    77. 组合 相关标签:回溯算法 207. 课程表 相关标签: DFS BFS 图 拓扑排序

  • ❤算法解析---图的广度优先搜索(BFS)和深度优先搜索(DFS

    图的广度优先搜索(BFS)和深度优先搜索(DFS)算法解析 https://blog.csdn.net/weixi...

  • 用Python实现树的BFS与DFS

    一、BFS与DFS简介 在理解动态规划、BFS和DFS一文中,已经集合具体例子,介绍了图的BFS与DFS。但是比较...

  • Java做题要用到的工具

    图的搜索算法:BFS和DFS详解 参考文章:https://www.jianshu.com/p/2226dbe98...

网友评论

      本文标题:图的BFS & DFS & Dijkstra算法

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