【算法与数据结构】797、LeetCode所有可能的路径

所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解

一、题目

在这里插入图片描述
在这里插入图片描述

二、解法

  思路分析:有向无环图(Directed acyclic graph, DAG)是图论中的一个概念,它指的是一个无回路的有向图。问题是要找到 0 0 0节点到 n − 1 n-1 n1节点的所有路径,对于所有路径的问题,我们可以用深度优先搜索来做(广度优先搜索也可以)。深度优先搜索DFS一共有三步:

    1. 确定递归参数。一共有两个参数,分别是有向无环图数组和当前节点。
    1. 终止条件。题目要求到 n − 1 n-1 n1节点,因此 x = = n − 1 x == n - 1 x==n1时即可终止。
    1. 单层递归逻辑。我们首先定义两个数组result和path,分别用来表示所有可能路径和 0 0 0节点到 n − 1 n-1 n1节点的路径。for循环遍历当前节点 x x x的连接节点,将其连接节点添加到path中,接着递归,再回溯即可。

  程序如下

class Solution {
private:
    vector<vector<int>> result;     // 所有可能的路径
    vector<int> path;               // 0到n-1的路径
    void dfs(vector<vector<int>>& graph, int x) {   // 1、递归输入参数
        // 2、终止条件
        if (x == graph.size() - 1) {
            result.push_back(path);
            return;
        }
        // 3、单层递归逻辑
        for (int i = 0; i < graph[x].size(); i++) {
            path.push_back(graph[x][i]);    // 遍历的节点加入到路径中来
            dfs(graph, graph[x][i]);        // 递归
            path.pop_back();                // 回溯操作,撤销加入到path中的节点
        }
    }
public:
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        path.push_back(0);  // 从0节点开始出发
        dfs(graph, 0);      // 深度优先遍历
        return result;
    }  
};

复杂度分析:

  • 时间复杂度: O ( n × 2 n ) O(n \times 2 ^ n) O(n×2n),其中 n n n为节点数量。最坏的情况是每一个节点都可以去往编号比它大的节点,每个节点有经过或者不经过两种情况,共n个节点,此时路径数为 O ( 2 n ) O(2^n) O(2n),每条路径长度为 O ( n ) O(n) O(n)。因此总的时间复杂度为 O ( n × 2 n ) O(n \times 2 ^ n) O(n×2n)
  • 空间复杂度: O ( n ) O(n) O(n),主要为栈的开销。

三、完整代码

# include <iostream>
# include <vector>
using namespace std;

class Solution {
private:
    vector<vector<int>> result;     // 所有可能的路径
    vector<int> path;               // 0到n-1的路径
    void dfs(vector<vector<int>>& graph, int x) {   // 1、递归输入参数
        // 2、终止条件
        if (x == graph.size() - 1) {
            result.push_back(path);
            return;
        }
        // 3、单层递归逻辑
        for (int i = 0; i < graph[x].size(); i++) {
            path.push_back(graph[x][i]);    // 遍历的节点加入到路径中来
            dfs(graph, graph[x][i]);        // 递归
            path.pop_back();                // 回溯操作,撤销加入到path中的节点
        }
    }
public:
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        path.push_back(0);  // 从0节点开始出发
        dfs(graph, 0);      // 深度优先遍历
        return result;
    }  
};

int main() {
    Solution s1;
    vector<vector<int>> graph = { {1,2},{3},{3},{} };
    vector<vector<int>> result = s1.allPathsSourceTarget(graph);
    for (vector<vector<int>>::iterator it = result.begin(); it != result.end(); it++) {
        for (vector<int>::iterator jt = (*it).begin(); jt != (*it).end(); jt++) {
            cout << *jt << " ";
        }
        cout << endl;
    }
    system("pause");
    return 0;
}

end

  • 29
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚安66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值