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

代码随想录算法训练营四十三天|图论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.xdnf.cn/news/18094.html

相关文章:

  • Ubuntu 25.04 安装并使用 MySQL 8.4.5 的步骤
  • MySQL完整重置密码流程(针对 macOS)
  • AI应用安全 - Prompt注入攻击
  • 深入解析Java代理模式:灵活控制对象访问的核心技术
  • 配置国内加速源后仍然无法拉取镜像
  • STC8单片机驱动I2C屏幕:实现时间、日期与温湿度显示
  • Rust 中 i32 与 *i32 的深度解析
  • 解决zabbix图片中文乱码
  • 46.Sentinel规则持久化
  • 8位量化简介(40)
  • 铨林接纸机学习记录1
  • ramdisk内存虚拟盘(一)——前世今生
  • 按键序列常用示例
  • Mini MAX AI应用矩阵测评报告——基于旗下多款产品的综合体验与行业价值分析
  • 六大主流负载均衡算法
  • 分享一个基于Hadoop的二手房销售签约数据分析与可视化系统,基于Python可视化的二手房销售数据分析平台
  • Oracle按照特定列值排序和C#统计特定列值的所有行
  • p5.js 3D盒子的基础用法
  • 【解决笔记】MyBatis-Plus 中无 selectList 方法
  • RK3588消费级8K VR一体机 是否有坑?
  • ABP vNext+ WebRTC DataChannel 低延迟传感推送
  • 《JMeter核心技术、性能测试与性能分析》 教学大纲及标准
  • JavaScript性能优化30招
  • Nacos-5--Nacos2.x版本的通信原理
  • C#---StopWatch类
  • 【开源大模型和闭源大模型分别有哪些?两者的对比?部署私有化模型的必要性有哪些?】
  • 五、ZooKeeper、Kafka、Hadoop、HBase、Spark、Flink集群化软件的部署
  • @Autowired @Resource IDE警告 和 依赖注入
  • 代码随想录刷题Day33
  • C#控制台输入(Read()、ReadKey()和ReadLine())