代码随想录算法训练营第五十六天 | 图论理论基础、深搜理论基础、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 个高频元素
栈与队列总结

相关推荐
不吃饭的猪7 分钟前
【无标题】
java·开发语言·docker
六月的雨__7 分钟前
二手物品交易小程序的设计
java·sql·学习·小程序
小梁不秃捏21 分钟前
科比老大职业生涯数据预测(基于随机森林模型)
算法·随机森林·机器学习
科技之歌29 分钟前
Leetcode 115 不同的子序列
算法·leetcode·职场和发展
斯择微韵35 分钟前
力扣习题--哈沙德数
算法·leetcode·职场和发展
Neituijunsir37 分钟前
2024.06.27 校招 实习 内推 面经
c++·算法·面试·车载系统·自动驾驶·汽车·求职招聘
Casual_Lei39 分钟前
Hibernate
java·oracle·hibernate
DisonTangor40 分钟前
新纪录将圆周率计算到了小数点后202万亿位 用了28块61.44TB SSD
算法·链表·数学建模
在无清风1 小时前
数据结构(期末)
java·开发语言
计算机周老师1 小时前
java-arraylist 源码分析 1
java·python·算法