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

广搜理论基础

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

相关推荐
计信金边罗2 天前
是否存在路径(FIFOBB算法)
算法·蓝桥杯·图论
闻缺陷则喜何志丹4 天前
【二分图 图论】P9384 [THUPC 2023 决赛] 着色|普及+
c++·算法·图论·二分图·洛谷
鸽子炖汤5 天前
LRC and VIP
c++·算法·图论
JK0x076 天前
代码随想录算法训练营 Day61 图论ⅩⅠ Floyd A※ 最短路径算法
算法·图论
qq_447429416 天前
数据结构与算法:图论——拓扑排序
linux·c语言·学习·图论
zc.ovo6 天前
图论刷题1
算法·深度优先·图论
珂朵莉MM7 天前
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)解题报告 | 珂学家
人工智能·算法·职场和发展·深度优先·图论
蒙奇D索大7 天前
【数据结构】图论核心算法解析:深度优先搜索(DFS)的纵深遍历与生成树实战指南
数据结构·算法·深度优先·图论·图搜索算法
ShiinaMashirol7 天前
代码随想录打卡|Day50 图论(拓扑排序精讲 、dijkstra(朴素版)精讲 )
java·图论
JK0x079 天前
代码随想录算法训练营 Day60 图论Ⅹ Bellmen_ford 系列算法
android·算法·图论