代码随想录-算法训练营-番外(图论01:图论理论基础,所有可到达的路径)

bash 复制代码
day01 图论part01 
今日任务:图论理论基础/所有可到达的路径
代码随想录图论视频部分还没更新
https://programmercarl.com/kamacoder/图论理论基础.html#图的基本概念

day01

所有可达路径

邻接矩阵

复制代码
 import java.util.Scanner;
 import java.util.List;
 import java.util.ArrayList;
 ​
 public class Main{
     static List<List<Integer>> result = new ArrayList<>();
     static List<Integer> path = new ArrayList<>();
     
     public static void main(String[] args){
         Scanner sc = new Scanner(System.in);
         int n = sc.nextInt();
         int m = sc.nextInt();
         int[][] graph = new int[n + 1][n + 1];
         for(int i = 0; i < m; i++){
             graph[sc.nextInt()][sc.nextInt()] = 1;
         }
         path.add(1); //先加一个节点
         dfs(graph, 1, n); 
         if (result.isEmpty()) System.out.println(-1);
         
         for(List<Integer> pa : result){
           for (int i = 0; i < pa.size() - 1; i++) {
                 System.out.print(pa.get(i) + " ");
             }
             System.out.println(pa.get(pa.size() - 1));
         } 
     }
     
     private static void dfs(int[][] graph, int x, int n){//n就是结束节点
         if(x == n){
             result.add(new ArrayList(path));
             return;
         }
         for(int i = 1; i <= n; i++){
             if (graph[x][i] == 1) { 
                 path.add(i); 
                 dfs(graph, i, n); 
                 path.remove(path.size() - 1); 
             }
         }
         return;
     }
 }

邻接表

复制代码
 //感觉graph不用LinkedList而是直接用ArrayList也可以,因为这个场景下不涉及什么增删改而基本都是访问
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Scanner;
 ​
 public class Main {
     static List<List<Integer>> result = new ArrayList<>(); // 收集符合条件的路径
     static List<Integer> path = new ArrayList<>(); // 1节点到终点的路径
 ​
     public static void dfs(List<LinkedList<Integer>> graph, int x, int n) {
         if (x == n) { // 找到符合条件的一条路径
             result.add(new ArrayList<>(path));
             return;
         }
         for (int i : graph.get(x)) { // 找到 x指向的节点
             path.add(i); // 遍历到的节点加入到路径中来
             dfs(graph, i, n); // 进入下一层递归
             path.remove(path.size() - 1); // 回溯,撤销本节点
         }
     }
 ​
     public static void main(String[] args) {
         Scanner scanner = new Scanner(System.in);
         int n = scanner.nextInt();
         int m = scanner.nextInt();
 ​
         // 节点编号从1到n,所以申请 n+1 这么大的数组
         List<LinkedList<Integer>> graph = new ArrayList<>();
         for (int i = 0; i <= n; i++) {
             graph.add(new LinkedList<>());
         }
 ​
         while (m-- > 0) {
             int s = scanner.nextInt();
             int t = scanner.nextInt();
             // 使用邻接表表示 s -> t 是相连的
             graph.get(s).add(t);
         }
 ​
         path.add(1); // 无论什么路径已经是从1节点出发
         dfs(graph, 1, n); // 开始遍历
 ​
         // 输出结果
         if (result.isEmpty()) System.out.println(-1);
         for (List<Integer> pa : result) {
             for (int i = 0; i < pa.size() - 1; i++) {
                 System.out.print(pa.get(i) + " ");
             }
             System.out.println(pa.get(pa.size() - 1));
         }
     }
 }

感谢大佬分享:

代码随想录算法训练营第五十天|Day50 图论_本关任务:创建邻接表存储的无向图,并输出图的邻接表。-CSDN博客

相关推荐
WanderInk1 分钟前
JavaWeb CRUD 与分页系统架构学习教程
java·后端·架构
_Djhhh4 分钟前
权限系统设计方案实践(Spring Security + RBAC 模型)
java·linux·数据库·spring
天上掉下来个程小白23 分钟前
开发环境搭建-06.后端环境搭建-前后端联调-Nginx反向代理和负载均衡概念
java·运维·spring boot·后端·nginx·负载均衡·苍穹外卖
lizz3125 分钟前
机器学习中的线性代数:奇异值分解 SVD
线性代数·算法·机器学习
试着生存26 分钟前
java根据List<Object>中的某个属性排序(数据极少,顺序固定)
java·python·list
_星辰大海乀27 分钟前
LinkedList 双向链表
java·数据结构·链表·list·idea
MSTcheng.30 分钟前
【C语言】动态内存管理
c语言·开发语言·算法
不去幼儿园34 分钟前
【启发式算法】Dijkstra算法详细介绍(Python)
人工智能·python·算法·机器学习·启发式算法·图搜索算法
小韩学长yyds38 分钟前
Java调用第三方HTTP接口:从入门到实战
java·开发语言·http
苏十八40 分钟前
JavaEE Servlet02
java·服务器·网络·java-ee·json