代码随想录算法训练营第五十六天 | 图论理论基础、深搜理论基础、98. 所有可达路径、797. 所有可能的路径、广搜理论基础、复习

图论理论基础

文档讲解:https://www.programmercarl.com/kamacoder/%E5%9B%BE%E8%AE%BA%E7%90%86…

深搜理论基础

文档讲解:https://www.programmercarl.com/kamacoder/%E5%9B%BE%E8%AE%BA%E6%B7%B1…

98. 所有可达路径

题目链接:https://kamacoder.com/problempage.php?pid=1170
文档讲解:https://www.programmercarl.com/kamacoder/0098.%E6%89%80%E6%9C%89%E5%8F…

思路

1、可以使用邻接矩阵或邻接表来记录点和边的情况。

// 邻接矩阵
graph = new int[n + 1][n + 1];
while (in.hasNextInt()){
    int i = in.nextInt();
    int j = in.nextInt();
    graph[i][j] = 1;
}
// 邻接表
List<List<Integer>> graph = new ArrayList<>(n + 1);
for (int i = 0; i <= n; i++) {
    graph.add(new ArrayList<>());
}
for (int i = 0; i < m; i++) {
    int s = scanner.nextInt();
    int t = scanner.nextInt();
    // 使用邻接表,表示 s -> t 是相连的
    graph.get(s).add(t);
}

2、确认递归函数及参数:参数中需要传入当前遍历到的节点以及节点个数。

public static void dfs(int x, int n) {}

3、确定终止条件:当当前遍历节点等于终点节点时,终止遍历,并将path加进结果列表res中。

if (x == n) {
    res.add(new ArrayList(path));
    return;
}

4、处理循环中的逻辑:目前搜索节点出发的路径,如果对应的邻接矩阵中值为1,说明可达,就继续深度遍历。遍历后回溯path中的节点。

// 邻接矩阵
for (int i = 1; i <= n; i++) {
    if (graph[x][i] == 1) {
        path.add(i);
        dfs(i, n);
        path.remove(path.size() - 1);
    }
}
// 邻接表
for (int i : graph.get(x)) { // 找到 x 指向的节点
    path.add(i);
    dfs(graph, i, n);
    path.remove(path.size() - 1);
}

代码

邻接矩阵+ACM模式

import java.util.*;

class Main{
    static List<List<Integer>> res = new ArrayList<>();
    static List<Integer> path = new ArrayList<>();
    static int[][] graph;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        graph = new int[n + 1][n + 1];
        while (in.hasNextInt()){
            int i = in.nextInt();
            int j = in.nextInt();
            graph[i][j] = 1;
        }
        path.add(1);
        dfs(1, n);
        if (res.size() == 0) System.out.println(-1);
        else {
            for (List<Integer> pa : res) {
                for (int i = 0; i < pa.size() - 1; i++) {
                    System.out.print(pa.get(i) + " ");
                }
                System.out.println(pa.get(pa.size() - 1));
            }
        }
    }
    public static void dfs(int x, int n) { // x为当前遍历的节点
        if (x == n) {
            res.add(new ArrayList(path));
            return;
        }
        
        for (int i = 1; i <= n; i++) {
            if (graph[x][i] == 1) {
                path.add(i);
                dfs(i, n);
                path.remove(path.size() - 1);
            }
        }
    }
}

797. 所有可能的路径

思路

和上一题的思路一样,只是这道题使用的是邻接表+核心代码模式。

代码

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        path.add(0);
        dfs(graph, 0);
        return res;
    }

    public void dfs(int[][] graph, int x) {
        if (x == graph.length - 1) {
            res.add(new ArrayList(path));
            return;
        }
        for (int i = 0; i < graph[x].length; i++) {
            path.add(graph[x][i]);
            dfs(graph, graph[x][i]);
            path.remove(path.size() - 1);
        }
    }
}

广搜理论基础

文档讲解:https://www.programmercarl.com/kamacoder/%E5%9B%BE%E8%AE%BA%E5%B9%BF…

复习栈与队列部分

150. 逆波兰表达式求值
239. 滑动窗口最大值
347.前 K 个高频元素
栈与队列总结

上一篇:零撸纯看广告小游戏app开发源码