当前位置: 首页 > news >正文

代码随想录算法训练营四十三天|图论part01

深度优先搜索(dfs)理论基础

dfs就是可一个方向去搜直到尽头再换方向,换方向的过程就涉及到了回溯。

代码框架

因为dfs搜索可一个方向,并需要回溯,所以用递归的方式来实现是最方便的。

先来回顾一下回溯法的代码框架:

void backtracking(参数) {if (终止条件) {存放结果;return;}for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小)) {处理节点;backtracking(路径,选择列表); // 递归回溯,撤销处理结果}
}

据此给出dfs的代码框架:

void dfs(参数) {if (终止条件) {存放结果;return;}for (选择:本节点所连接的其他节点) {处理节点;dfs(图,选择的节点); // 递归回溯,撤销处理结果}
}

所有可达路径

题目链接:98. 所有可达路径 (kamacoder.com)

【题目描述】

给定一个有 n 个节点的有向无环图,节点编号从 1 到 n。请编写一个程序,找出并返回所有从节点 1 到节点 n 的路径。每条路径应以节点编号的列表形式表示。

【输入描述】

第一行包含两个整数 N,M,表示图中拥有 N 个节点,M 条边

后续 M 行,每行包含两个整数 s 和 t,表示图中的 s 节点与 t 节点中有一条路径

【输出描述】

输出所有的可达路径,路径中所有节点的后面跟一个空格,每条路径独占一行,存在多条路径,路径输出的顺序可任意。

如果不存在任何一条路径,则输出 -1。

注意输出的序列中,最后一个节点后面没有空格! 例如正确的答案是 1 3 5,而不是 1 3 5, 5后面没有空格!

输入示例

5 5
1 3
3 5
1 2
2 4
4 5

输出示例

1 3 5
1 2 4 5

提示信息

用例解释:

有五个节点,其中的从 1 到达 5 的路径有两个,分别是 1 -> 3 -> 5 和 1 -> 2 -> 4 -> 5。

因为拥有多条路径,所以输出结果为:

1 3 5
1 2 4 5

1 2 4 5
1 3 5
都算正确。

数据范围:

图中不存在自环
图中不存在平行边
1 <= N <= 100
1 <= M <= 500

使用邻接矩阵存储

使用二维数组来表示图,因为有n个节点,节点编号从1开始,所以我们申请(n+1)*(n+1)大的二维数组;然后构造m个边:

int[][] graph=new int[n+1][n+1];
for(int i=0;i<m;i++){int a=in.nextInt();int b=in.nextInt();graph[a][b]=1;
}

使用邻接矩阵存储dfs的代码:

public static void dfs(List<List<Integer>> graph, int x, int n) { //x表示当前遍历到的节点if (x == n) {res.add(new ArrayList<>(list));return;}for(int i=1;i<=n;i++){if(graph[x][i]==1){list.add(i);dfs(graph,i,n);list.remove(list.size()-1);}}}

打印结果:

for(List<Integer> tmp:res){for(int i=0;i<tmp.size()-1;i++){System.out.print(tmp.get(i)+" ");}System.out.println(tmp.get(tmp.size()-1));
}

整体代码如下:

import java.util.*;public class Main{static List<List<Integer>> res=new ArrayList<List<Integer>>();static List<Integer> list=new ArrayList<>();public static void main(String[] args){Scanner in=new Scanner(System.in);int n=in.nextInt();int m=in.nextInt();int[][] graph=new int[n+1][n+1];for(int i=0;i<m;i++){int a=in.nextInt();int b=in.nextInt();graph[a][b]=1;}list.add(1);dfs(graph,1,n);if(res.isEmpty())System.out.println(-1);for(List<Integer> tmp:res){for(int i=0;i<tmp.size()-1;i++){System.out.print(tmp.get(i)+" ");}System.out.println(tmp.get(tmp.size()-1));}}public static void dfs(int[][] graph,int x,int n){//x表示当前遍历到的节点if(x==n){res.add(new ArrayList<>(list));return;}for(int i=1;i<=n;i++){if(graph[x][i]==1){list.add(i);dfs(graph,i,n);list.remove(list.size()-1);}}}
}

使用邻接表存储

List<List<Integer>> graph = new ArrayList<List<Integer>>(n + 1);for(int i=0;i<=n;i++){graph.add(new ArrayList<>());}for (int i = 0; i < m; i++) {int a = in.nextInt();int b = in.nextInt();graph.get(a).add(b);}

使用领接表存储的dfs的写法:

public static void dfs(List<List<Integer>> graph, int x, int n) { //x表示当前遍历到的节点if (x == n) {res.add(new ArrayList<>(list));return;}for (int i : graph.get(x)) {list.add(i);dfs(graph, i, n);list.remove(list.size() - 1);}}

打印结果:

if (res.isEmpty()) System.out.println(-1);for (List<Integer> tmp : res) {for (int i = 0; i < tmp.size() - 1; i++) {System.out.print(tmp.get(i) + " ");}System.out.println(tmp.get(tmp.size() - 1));}

整体代码如下:

import java.util.*;public class Main {static List<List<Integer>> res = new ArrayList<List<Integer>>();static List<Integer> list = new ArrayList<>();public static void main(String[] args) {Scanner in = new Scanner(System.in);int n = in.nextInt();int m = in.nextInt();//邻接表写法List<List<Integer>> graph = new ArrayList<List<Integer>>(n + 1);for(int i=0;i<=n;i++){graph.add(new ArrayList<>());}for (int i = 0; i < m; i++) {int a = in.nextInt();int b = in.nextInt();graph.get(a).add(b);}list.add(1);dfs(graph, 1, n);if (res.isEmpty()) System.out.println(-1);for (List<Integer> tmp : res) {for (int i = 0; i < tmp.size() - 1; i++) {System.out.print(tmp.get(i) + " ");}System.out.println(tmp.get(tmp.size() - 1));}}public static void dfs(List<List<Integer>> graph, int x, int n) { //x表示当前遍历到的节点if (x == n) {res.add(new ArrayList<>(list));return;}for (int i : graph.get(x)) {list.add(i);dfs(graph, i, n);list.remove(list.size() - 1);}}
}

广度优先搜索(bfs)理论基础

bfs就是先把本节点所连接的所有节点遍历一遍,走到下一个节点的时候,再把连接该节点的所有节点遍历一遍,四面八方的搜索过程。

代码模板

private static final int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};//表示四个方向
// grid 是地图,也就是一个二维数组
// visited标记访问过的节点,不要重复访问
// x,y 表示开始搜索节点的下标public static void bfs(char[][] grid, boolean[][] visited, int x, int y) {// 定义队列,存储坐标对Queue<int[]> queue = new LinkedList<>();// 起始节点加入队列queue.offer(new int[]{x, y});// 只要加入队列,立刻标记为访问过的节点visited[x][y] = true;// 开始遍历队列里的元素while (!queue.isEmpty()) {// 从队列取出元素int[] current = queue.poll();int curx = current[0];int cury = current[1];// 向当前节点的四个方向(上下左右)遍历for (int i = 0; i < 4; i++) {// 获取周边四个方向的坐标int nextx = curx + dir[i][0];int nexty = cury + dir[i][1];// 检查坐标是否越界,如果越界直接跳过if (nextx < 0 || nextx >= grid.length || nexty < 0 || nexty >= grid[0].length) {continue;}// 如果节点没被访问过if (!visited[nextx][nexty]) {// 将该节点加入队列,作为下一轮要遍历的节点queue.offer(new int[]{nextx, nexty});// 只要加入队列立刻标记,避免重复访问visited[nextx][nexty] = true;}}}}
http://www.lryc.cn/news/623011.html

相关文章:

  • 数字化与人工智能的崛起及其社会影响研究报告
  • 基于uni-app+vue3实现的微信小程序地图范围限制与单点标记功能实现指南
  • Altium Designer 22使用笔记(7)---网表导入,叠层设置
  • 【电路笔记 通信】AXI4-Lite协议 论文阅读 简化的高级可扩展接口(AdvancedeXtensibleInterface4Lite)
  • 【计算机网络架构】混合型架构简介
  • 车载诊断架构 --- 怎么解决对已量产ECU增加具体DTC的快照信息?
  • 超越Transformer:大模型架构创新的深度探索
  • 【自动化运维神器Ansible】Ansible逻辑运算符详解:构建复杂条件判断的核心工具
  • 11、软件需求工程
  • 【系统分析师】软件需求工程——第11章学习笔记(下)
  • 架构调整决策
  • 软件需求管理过程详解
  • M-LAG双活网关
  • linux I2C核心、总线与设备驱动
  • 特洛伊木马和后门程序的定义、联系、区别与应用场景
  • UE5多人MOBA+GAS 45、制作冲刺技能
  • 深入详解PCB布局布线技巧-去耦电容的摆放位置
  • 【AndroidStudio修改中文设置】
  • 玉米及淀粉深加工产业展|2026中国(济南)国际玉米及淀粉深加工产业展览会
  • UE5多人MOBA+GAS 46、制作龙卷风技能
  • 机器学习——PCA算法
  • 心路历程-学Linux的开端
  • 【php反序列化介绍与常见触发方法】
  • Linux 多线程:线程回收策略 线程间通信(互斥锁详解)
  • MyBatis 的 SQL 拦截器:原理、实现与实践
  • 【昇腾】单张48G Atlas 300I Duo推理卡MindIE+WebUI方式跑7B大语言模型_20250816
  • Wi-Fi 7 将如何重塑互联工作场所
  • Python脚本开发-统计Rte中未连接的Port
  • Python---异常链(Exception Chaining)
  • 完整设计 之 定稿 之:后现代元宇宙九宫格(重命名)-腾讯元宝答问