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

相关推荐
好家伙VCC2 分钟前
**发散创新:基于Solidity的通证经济模型设计与智能合约实现**在区块链技术日益成熟的今天,**通证经济(Token Econo
java·python·区块链·智能合约
jjjava2.04 分钟前
计算机体系与进程管理全解析
java·开发语言
程序员爱德华5 分钟前
LeetCode刷题
算法·leetcode
memcpy06 分钟前
LeetCode 1202. 交换字符串中的元素【无向图连通分量】中等
算法·leetcode·职场和发展
AI人工智能+电脑小能手6 分钟前
【大白话说Java面试题】【Java基础篇】第5题:HashMap的底层原理是什么
java·开发语言·数据结构·后端·面试·hash-index·hash
旷世奇才李先生6 分钟前
Java微服务实战:Spring Cloud Alibaba架构优化(从单体到分布式高可用)
java·微服务·架构
小成202303202656 分钟前
数据结构(整理常见结构总结到树层级)
java·c语言·数据结构·c++·链表
谢谢 啊sir7 分钟前
L1-120 智慧文本编辑器 - java
java·开发语言
fengfuyao9858 分钟前
基于遗传算法的分布式电源选址定容优化(考虑环境因素)
算法·matlab·平面
睡觉就不困鸭9 分钟前
第10天 删除有序数组中的重复项
数据结构·算法