Dijkstra算法总结

1.如何建立图2. BFS3. DFS
摘要由CSDN通过智能技术生成

1.如何建立图

 

Graph 一般是adjecent list,

class DirectedGraphNode {

    int label;

    List<DirectedGraphNode> neighbors;

    ...

}

也可以使用 HashMap 和 HashSet 搭配的方式来存储邻接表

hashmap<Integer, List<Integer>>() 或者HashMap<Integer, HashSet<Integer>>() 来表示;

有时候根据题目需要,图的表示也有HashMap<String, PriorityQueue<String>>() 

Dijkstra 算法:

Time Complexity: O(VLogV + ElogV).

Dijkstra写法统一格式,pq加入的时候,不需要加visited,所有的visited在while循环刚poll出来判断,一定不要在下一层pq offer的时候判断,因为这样会漏掉最优解;让pq加进去后,排序后,poll出来的就是最优解;这样是通用的写法,不容易出错!

Maze II 核心思想就是,到达同一个点的时候,step可能不一样,那么我们必须要有一个数据结构去sort一下,每次取最小的step,然后继续往下走,那么这里就是需要PQ,这题考的是Dijkstra (dai ka stra) Algorithm,步骤跟BFS有点不一样的地方是,收集下一层的时候,不能visite = false 就直接赋值visite = true, 那样会漏掉到同样一个点的最小距离;(update its priority if it was already in the pq);

class Solution {
    private class Node {
        public int x;
        public int y;
        public int step;
        public Node (int x, int y, int step) {
            this.x = x;
            this.y = y;
            this.step = step;
        }
    }
    
    public int shortestDistance(int[][] maze, int[] start, int[] destination) {
        if(maze == null || maze.length == 0 || maze[0].length == 0) {
            return -1;
        }
        int m = maze.length;
        int n = maze[0].length;
        boolean[][] visited = new boolean[m][n];
        PriorityQueue<Node> pq = new PriorityQueue<Node>((a, b) -> (a.step - b.step));
        pq.offer(new Node(start[0], start[1], 0));
        
        int[][] dirs = {
  {0,1}, {0,-1}, {-1,0}, {1,0}};
        while(!pq.isEmpty()) {
            Node node = pq.poll();
            if(node.x == destination[0] && node.y == destination[1]) {
                return node.step;
            }
            if(visited[node.x][node.y]) {
                continue;
            }
            visited[node.x][node.y] = true;
            
            for(int[] dir: dirs) {
                int nx = node.x;
                int ny = node.y;
                int step = node.step;
                while(isvalid(nx + dir[0], ny + dir[1], maze)) {
                    nx += dir[0];
                    ny += dir[1];
                    step++;
                }
                if(!visited[nx][ny]) {
                    pq.offer(new Node(nx, ny, step));
                }
            }
        }
        return -1;
    }
    
    private boolean isvalid(int x, int y, int[][] maze) {
        int m = maze.length;
        int n = maze[0].length;
        return (0 <= x && x < m && 0 <= y && y < n && maze[x][y] != 1);
    }
}

Cheapest Flights Within K Stops 核心思想就是,每次走最小的cost的路径,那么到达des的时候,就是最小的cost,那么我们需要做的是把边cost的信息,融入到node里面去,那么node进行排序,也就是cost进行排序;graph怎么建立,graph: HashMap<Integer, HashMap<Integer, Integer>>  存 from , <neighbor, cost>将边的信息(cost信息),整合到node里面去,也就是到达这个city需要的cost是多少,然后剩下的步骤是多少,cost是累加的,剩下的步骤是减少的。pq: 存Node  Node<cost, city, step> sort by cost; 搜索:if( step >

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值