当前位置:   article > 正文

LeetCode第797题: 所有可能的路径

LeetCode第797题: 所有可能的路径

目录

1.问题描述

2.问题分析


1.问题描述

        给你一个有 n 个节点的有向无环图(DAG),请你找出所有从节点 0 到节点 n-1 的路径并输出(不要求按特定顺序)。

        graph[i] 是一个从节点 i 可以访问的所有节点的列表(即从节点 i 到节点 graph[i][j]存在一条有向边)。

        示例1:

输入:graph = [[1,2],[3],[3],[]]

输出:[[0,1,3],[0,2,3]]

解释:有两条路径 0 -> 1 -> 3 和 0 -> 2 -> 3

        示例2:

输入:graph = [[4,3,1],[3,2,4],[3],[4],[]]

输出:[[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]

  • n == graph.length

  • 2 <= n <= 15

  • 0 <= graph[i][j] < n

  • graph[i][j] != i(即不存在自环)

  • graph[i] 中的所有元素互不相同

  • 保证输入为有向无环图(DAG

2.问题分析

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

        这题让在有向无环图中输出从顶点0到顶点n-1的所有路径,可以使用dfs,从顶点0开始搜索,搜索所有路径,因为是无环的,所以搜索的时候不会出现死循环。到顶点n-1的时候就把这条路径上所有的点都保存下来。因为是dfs搜索,往下走的时候选择节点,往回走的时候要记得撤销选择。

JAVA

  1. public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
  2. List<List<Integer>> ans = new ArrayList<>();
  3. ArrayList<Integer> path = new ArrayList<>();
  4. path.add(0);// 把起始节点0加进来
  5. dfs(graph, 0, ans, path);
  6. return ans;
  7. }
  8. private void dfs(int[][] graph, int index, List<List<Integer>> ans, List<Integer> path) {
  9. // 到最后一个节点的时候,说明找了一个一条有效路径
  10. if (index == graph.length - 1) {
  11. ans.add(new ArrayList<>(path));
  12. return;
  13. }
  14. // 当前节点指向哪些节点(可以看做是n叉树的子节点,然后遍历他的子节点)
  15. int[] directs = graph[index];
  16. for (int i = 0; i < directs.length; i++) {
  17. path.add(directs[i]);// 把当前节点加入到路径中
  18. dfs(graph, directs[i], ans, path);// 递归
  19. path.remove(path.size() - 1); // 撤销选择
  20. }
  21. }

C++

  1. public:
  2. vector<vector<int>> allPathsSourceTarget(vector<vector<int>> &graph) {
  3. vector<vector<int>> ans;
  4. vector<int> path;
  5. path.push_back(0);// 把起始节点0加进来
  6. dfs(graph, 0, ans, path);
  7. return ans;
  8. }
  9. void dfs(vector<vector<int>> &graph, int index, vector<vector<int>> &ans, vector<int> &path) {
  10. // 到最后一个节点的时候,说明找了一个一条有效路径
  11. if (index == graph.size() - 1) {
  12. ans.emplace_back(path);
  13. return;
  14. }
  15. // 当前节点指向哪些节点(可以看做是n叉树的子节点,然后遍历他的子节点)
  16. for (int g: graph[index]) {
  17. path.emplace_back(g);// 把当前节点加入到路径中
  18. dfs(graph, g, ans, path);// 递归
  19. path.pop_back(); // 撤销选择
  20. }
  21. }

C

  1. void dfs(int **graph, int graphSize, int *graphColSize, int *returnSize,
  2. int **returnColumnSizes, int **ans, int *path, int v, int count) {
  3. // 到最后一个节点的时候,说明找了一个一条有效路径
  4. if (v == graphSize - 1) {
  5. ans[*returnSize] = malloc(count * sizeof(int));
  6. memcpy(ans[*returnSize], path, count * sizeof(int));
  7. (*returnColumnSizes)[(*returnSize)++] = count;
  8. return;
  9. }
  10. // 当前节点指向哪些节点(可以看做是n叉树的子节点,然后遍历他的子节点)
  11. for (int i = 0; i < graphColSize[v]; ++i) {
  12. path[count++] = graph[v][i];// 把当前节点加入到路径中
  13. dfs(graph, graphSize, graphColSize, returnSize, returnColumnSizes, ans, path, graph[v][i], count);// 递归
  14. count--;// 撤销选择
  15. }
  16. }
  17. int **allPathsSourceTarget(int **graph, int graphSize, int *graphColSize, int *returnSize, int **returnColumnSizes) {
  18. int **ans = malloc(20000 * sizeof(int *));
  19. int *path = malloc(15 * sizeof(int));
  20. int v = 0;
  21. int count = 0;
  22. *returnSize = 0;
  23. *returnColumnSizes = malloc(20000 * sizeof(int));
  24. path[count++] = v;// 把起始节点0加进来
  25. dfs(graph, graphSize, graphColSize, returnSize, returnColumnSizes, ans, path, v, count);
  26. return ans;
  27. }

Python

  1. def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
  2. def dfs(index):
  3. # 到最后一个节点的时候,说明找了一个一条有效路径
  4. if index == len(graph) - 1:
  5. ans.append(path[:])
  6. return
  7. # 当前节点指向哪些节点(可以看做是n叉树的子节点,然后遍历他的子节点)
  8. for direct in graph[index]:
  9. path.append(direct) # 把当前节点加入到路径中
  10. dfs(direct) # 递归
  11. path.pop() # 撤销选择
  12. ans = []
  13. path = [0]
  14. dfs(0)
  15. return ans

复杂度分析

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号