当前位置: 首页 > 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.dtcms.com/a/334752.html

相关文章:

  • 同创物流学习记录2·电车
  • 【手撕JAVA多线程】1.从设计初衷去看JAVA的线程操作
  • 【C++】STL 容器—list 底层剖析
  • Java应届生求职八股(5)---并发编程篇
  • JCTools 无锁并发队列基础:ConcurrentCircularArrayQueue
  • 【论文阅读笔记】--Eurosys--HCache
  • 安全审计-firewall防火墙
  • 探索粒子世界:从基础理论到前沿应用与未来展望
  • 基于动捕实现Epuck2的轨迹跟踪
  • 每日算法刷题Day62:8.16:leetcode 堆8道题,用时2h30min
  • 【Java基础面试题】数据类型
  • 【电路笔记 通信】AXI4-Lite协议 论文阅读 简化的高级可扩展接口(AdvancedeXtensibleInterface4Lite)
  • 小白挑战一周上架元服务——元服务开发06
  • 元宇宙教育:打破时空限制的学习革命
  • MQ迁移方案
  • 顶刊分享--MYC ecDNA增强胰腺癌的瘤内异质性及可塑性
  • 测试18种RAG技术,找出最优方案(四)
  • 云蝠智能VoiceAgent:AI赋能售后服务场景的创新实践
  • docker镜像解决的一些问题
  • 搭建ktg-mes
  • 每日面试题22:静态代理和动态代理的区别
  • C语言指针运算题
  • [Python]PTA:实验2-3-2-for 求N分之一序列前N项和
  • HTML 常用属性介绍
  • 教育的终极指向:一场精心准备的“得体退出”
  • InfluxDB 数据迁移工具:跨数据库同步方案(一)
  • 一个.NET开源、轻量级的运行耗时统计库
  • 解决 Windows 下运行 MCP 脚本弹出 WSH 错误窗口的问题 | Windows Script Host
  • vscode配置cpp运行和调试环境(保姆级)
  • 一文入门Gin框架