代码随想录算法训练营第五十六天 | 图论理论基础、深搜理论基础、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、可以使用邻接矩阵或邻接表来记录点和边的情况。

java 复制代码
// 邻接矩阵
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、确认递归函数及参数:参数中需要传入当前遍历到的节点以及节点个数。

java 复制代码
public static void dfs(int x, int n) {}

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

java 复制代码
if (x == n) {
    res.add(new ArrayList(path));
    return;
}

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

java 复制代码
// 邻接矩阵
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模式

java 复制代码
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. 所有可能的路径

思路

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

代码

java 复制代码
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 个高频元素
栈与队列总结

相关推荐
其实你热情似火6 分钟前
Java基础第21天-正则表达式
java·开发语言·正则表达式
wolf犭良16 分钟前
37、aiomysql实操习题
开发语言·python·算法
rgb2gray24 分钟前
描述城市出行需求模式的复杂网络视角:大规模起点-目的地需求网络的图论分析
网络·图论
xin007hoyo24 分钟前
算法笔记.spfa算法(bellman-ford算法的改进)
数据结构·笔记·算法
向哆哆26 分钟前
Java 加密与解密:从算法到应用的全面解析
java·开发语言·算法
uhakadotcom35 分钟前
刚发布的PyTorch 2.7提供了什么 新特性
算法·面试·github
新生农民1 小时前
30分钟解决8道算法题
java·数据结构·算法
ん贤2 小时前
图论算法体系:并查集、生成树、排序与路径搜索全解析
图论
bbc1212262 小时前
2025/4/23 心得
数据结构·算法
sinat_262292112 小时前
Java面试实战:音视频场景下的微服务架构与缓存技术剖析
java·spring boot·redis·微服务·kafka·分布式系统·面试技巧