简体   繁体   English

图遍历到达目标顶点

[英]Graph traversal to reach destination vertex

I've solved the following question that asked to find all the paths that go from 0th node to n-1 node.我已经解决了以下问题,该问题要求找到从第 0 个节点到第 n-1 个节点的所有路径。 The code works fine however I'm confused about the time complexity of the solution.代码工作正常,但我对解决方案的时间复杂度感到困惑。 I calculated the time complexity of the following code to be O(V+E) where V is the vertices & E is the edges between vertices.我计算出以下代码的时间复杂度为 O(V+E),其中 V 是顶点,E 是顶点之间的边。 And since in DFS we're going through V vertices & each Vertex can have E edges, I think the TC is O(V+E)因为在 DFS 中我们要经过 V 个顶点,每个顶点可以有 E 个边,我认为 TC 是 O(V+E)

However according to leetcode solution section, the time complexity for this algorithm is O(2^N-1 x N).然而,根据leetcode解决方案部分,该算法的时间复杂度为O(2^N-1 x N)。 I don't really understand how leetcode came up with this TC.我真的不明白 leetcode 是如何想出这个 TC 的。 I don't think the leetcode's time complexity is the correct time complexity for the following code.我不认为 leetcode 的时间复杂度是以下代码的正确时间复杂度。

Can someone please help me figure out the time complexity?有人可以帮我算出时间复杂度吗?

Leetcode question link: https://leetcode.com/problems/all-paths-from-source-to-target/ Leetcode 问题链接: https ://leetcode.com/problems/all-paths-from-source-to-target/

Question Description:问题描述:

Given a directed acyclic graph of N nodes.给定 N 个节点的有向无环图。 Find all possible paths from node 0 to node N-1, and return them in any order.找出从节点 0 到节点 N-1 的所有可能路径,并以任意顺序返回它们。 The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1. graph[i] is a list of all nodes j for which the edge (i, j) exists.该图给出如下:节点是 0, 1, ..., graph.length - 1. graph[i] 是所有节点 j 的列表,其中边 (i, j) 存在。

Example image:示例图像:

在此处输入图片说明

public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        path.add(0);
        
        dfs(graph, result, path, 0);
        
        return result;
    }

    private void dfs(int[][] graph, List<List<Integer>> result, List<Integer> path, int node) {
        if(node == graph.length-1) {
            result.add(new ArrayList<>(path));
            return;
        }
        
        int[] edges = graph[node];
        
        for(int edge : edges) {
            
            path.add(edge);
            
            dfs(graph, result, path, edge);
            
            path.remove(path.size()-1);
        }
    }

Imagine the worst case that we have node-1 to node-N, and node-i linked to node-j if i < j.想象一下最坏的情况,如果 i < j,我们有 node-1 到 node-N,并且 node-i 链接到 node-j。 There are 2^(N-2) paths and (N+2)*2^(N-3) nodes in all paths.所有路径中有 2^(N-2) 个路径和 (N+2)*2^(N-3) 个节点。 We can roughly say O(2^N).我们可以粗略地说 O(2^N)。 To further prove this, these posts may help : Java Iterative with Stack and Queue, and statistics comparing Iterative vs Recursive speeds and what's the time complexity / space complexity for this solution?为了进一步证明这一点,这些帖子可能会有所帮助: Java 迭代与堆栈和队列,以及比较迭代与递归速度的统计数据以及此解决方案的时间复杂度/空间复杂度是多少?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM