Day50 图论part01

图论理论基础

大家可以在看图论理论基础的时候,很多内容 看不懂,例如也不知道 看完之后 还是不知道 邻接矩阵,邻接表怎么用, 别着急。

理论基础大家先对各个概念有个印象就好,后面在刷题的过程中,每个知识点都会得到巩固。

图论理论基础 | 代码随想录

深搜理论基础

了解一下深搜的原理和过程

深度优先搜索理论基础 | 代码随想录

98. 所有可达路径

代码随想录

方法1:邻接矩阵

java 复制代码
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class Main{
    public static List<Integer> path = new ArrayList<>();
    public static List<List<Integer>> result = 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];
        while(m-- > 0){
            int s = sc.nextInt();
            int t = sc.nextInt();
            graph[s][t] = 1;
        }
        
        path.add(1);
        dfs(graph, 1, n);
        if(result.isEmpty()){
            System.out.println(-1);
        }
        for(List<Integer> p : result){
            for(int i = 0; i < p.size() - 1; i++){
                System.out.print(p.get(i) + " ");
            }
            System.out.println(p.get(p.size() - 1));
        }
    }
    
    public static void dfs(int[][] graph, int x, int n){//这里n是表示节点数量
        if(x == n){//这里的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);
            }
        }
    }
    
}

方法2:邻接链表

java 复制代码
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;

public class Main{
    public static List<Integer> path = new ArrayList<>();
    public static List<List<Integer>> result = new ArrayList<>();
    
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        List<LinkedList<Integer>> graph = new ArrayList<>(n+1);
        for(int i = 0; i <= n; i++){
            graph.add(new LinkedList<>());
        }
        while(m-- > 0){
            int s = sc.nextInt();
            int t = sc.nextInt();
            graph.get(s).add(t);
        }
        
        path.add(1);
        dfs(graph, 1, n);
        if(result.isEmpty()){
            System.out.println(-1);
        }
        for(List<Integer> p : result){
            for(int i = 0; i < p.size() - 1; i++){
                System.out.print(p.get(i) + " ");
            }
            System.out.println(p.get(p.size() - 1));
        }
    }
    
    public static void dfs(List<LinkedList<Integer>> graph, int x, int n){//这里n是表示节点数量
        if(x == n){//这里的n是表示路径的终节点
            result.add(new ArrayList<>(path));
            return;
        }
        
        for(int i : graph.get(x)){
            path.add(i);
            dfs(graph, i, n);
            path.remove(path.size() - 1);
        }
        
    }
    
}

总结:

1.本题练习的是ACM模式,所以必须熟悉图的存储方法和结果的输出方法。图的存储方法分为邻接矩阵和邻接链表。邻接矩阵是从节点的角度来表示图,使用二维数组来表示图结构。例如: grid[2][5] = 6,表示 节点 2 连接 节点5 为有向图,节点2 指向 节点5,边的权值为6。如果想表示无向图,即:grid[2][5] = 6,grid[5][2] = 6,表示节点2 与 节点5 相互连通,权值为6。邻接链表一般是通过数组+链表,数组里面就存放节点,链表里面存放的是节点可以连通的节点,也就是边。比如1-3-5 表示的是节点1 指向 节点3 和 节点5,并不是节点1连着3,3后面连着5,这点要搞清楚。 链表一般是通过这种方法创建的List<LinkedList<Integer>> graph = new ArrayList<>(n+1);然后记得LinkedList<Integer>一定要先new 出来,然后在add元素进去。

2.dfs里面的处理逻辑就比较简单了,我们先找到当前节点连通的节点 path.add(i);然后以该连通的节点递归继续就行查找dfs(graph, i, n);直到找到目标节点,然后回溯 path.remove(path.size() - 1);

3.由于题目中说了图中不存在自环,所以起始节点不会被自动加入到路径里面,需要我们手动加入到路径当中。

广搜理论基础

广度优先搜索理论基础 | 代码随想录

相关推荐
初晴や15 小时前
【C++】图论:基础理论与实际应用深入解析
c++·算法·图论
闻缺陷则喜何志丹20 小时前
【图论 DFS 换根法】3772. 子图的最大得分|2235
c++·算法·深度优先·力扣·图论·换根法
君义_noip2 天前
信息学奥赛一本通 2134:【25CSPS提高组】道路修复 | 洛谷 P14362 [CSP-S 2025] 道路修复
c++·算法·图论·信息学奥赛·csp-s
罗湖老棍子2 天前
信使(msner)(信息学奥赛一本通- P1376)四种做法
算法·图论·dijkstra·spfa·floyd·最短路算法
修炼地3 天前
代码随想录算法训练营第五十三天 | 卡码网97. 小明逛公园(Floyd 算法)、卡码网127. 骑士的攻击(A * 算法)、最短路算法总结、图论总结
c++·算法·图论
罗湖老棍子3 天前
【例4-6】香甜的黄油(信息学奥赛一本通- P1345)
算法·图论·dijkstra·floyd·最短路算法·bellman ford
--JR5 天前
015——图(1.图的相关概念与存储)
数据结构·c++·算法·链表·图论
闻缺陷则喜何志丹5 天前
【二分查找 图论】P10206 [JOI 2024 Final] 建设工程 2|普及+
c++·算法·二分查找·图论·洛谷
surtr17 天前
全源最短路封装模板(APSP,Floyd求最小环,Floyd求最短路,Johnson算法)
c++·算法·数学建模·动态规划·图论
surtr18 天前
【算法自用】一些比较有趣的题目
算法·动态规划·概率论·图论