3

Let's an algorithm to find all paths between two nodes in a directed, acyclic, non-weighted graph, that may contain more than one edge between the same two vertices. (this DAG is just an example, please I'm not discussing this case specifically, so disregard it's correctness though it's correct, I think).

We have two effecting factors which are:

  1. mc: max number of outgoing edges from a vertex.
  2. ml: length of the max length path measured by number of edges.

Using an iterative fashion to solve the problem, where complexity in the following stands for count of processing operations done.

for the first iteration the complexity = mc

for the second iteration the complexity = mc*mc

for the third iteration the complexity = mc*mc*mc

for the (max length path)th iteration the complexity = mc^ml

Total worst complexity is (mc + mc*mc + ... + mc^ml).

1- can we say it's O(mc^ml)?

2- Is this exponential complexity?, as I know, in exponential complexity, the variable only appear at the exponent, not at the base.

3- Are mc and ml both are variables in my algorithm comlexity?

Jaap
  • 81,064
  • 34
  • 182
  • 193
Median Hilal
  • 1,483
  • 9
  • 17
  • Why would you do that algorithm though? Also, why would you want to find _all_ paths between nodes (vs the best, the cheapest etc)? – Benjamin Gruenbaum May 02 '15 at 11:15
  • 1
    If your graph is acyclic, then ther's only `0` or `1` path between 2 nodes. – Chan Kha Vu May 02 '15 at 11:15
  • Also, note that you can say it's `O(2^2^2^2^mc^ml^2^2^mc*ml^mc^ml)` anyway - O notation is just about an _upper bound_, you don't have to make it tight (although your bound is definitely not tight for a DAG anyway, you can do it in `O(number of nodes)`) and likely even in `NLOGSPACE` using Tarjan's algorithm. – Benjamin Gruenbaum May 02 '15 at 11:16
  • 2
    @FalconUA - this is not true for DIRECTED acyclic graph. – libik May 02 '15 at 11:19
  • Oh, sorry, didn't read the question properly – Chan Kha Vu May 02 '15 at 11:20
  • possible duplicate of [How do I find all paths through a set of given nodes in a DAG?](http://stackoverflow.com/questions/320721/how-do-i-find-all-paths-through-a-set-of-given-nodes-in-a-dag) – Benjamin Gruenbaum May 02 '15 at 11:21
  • I edited the question to clarify more, **"that may contain more than one edge between the same two vertices"** , to carify: Consider a simple (even extreme) of three vertices v1, v2, v3 and six edges e1,...,e6 where the first three edges connect v1 and v2, and the second three connect v2 and v3. Thus, we have 3*3 = 9 paths, e1e4, e1e5, e1e6, e2e4,...,e3e6 . Well, this has a exponential growth that cannot not linear with our graph size that have 3 vertices and six edges. However, please, leave my DAG , and concentrate on complexity – Median Hilal May 02 '15 at 11:23
  • @Benjamin here I'm talking only about complexity, as I explicitly state at the beginning of the example, please disregard my DAG, it's just an example to simplify. My concern is only with time complexity. – Median Hilal May 02 '15 at 11:26
  • I think, it depends on how you want to store the paths. You can *count* all paths between 2 nodes in O(E + V) with DFS, so you can also build a structure, that will recover any path by its index in O(E). But if you want to list the vertexes in each path, than the complexity is exponential, because there can be an exponential number of paths. – Kolmar May 02 '15 at 11:38

1 Answers1

2

There's a faster way to achieve the answer in O(V + E), but seems like your question is about calculating complexity, not about optimizing algorithm.

Yes, seems like it's O(mc^ml)
Yes, they bot can be variables in your algorithm complexity

As about the complexity of your algorithm: let's do some transformation, using the fact that a^b = e^(b*ln(a)):

mc^ml = (e^ln(mc))^ml = e^(ml*ln(mc)) < e^(ml*mc) if ml,mc -> infinity

So, basically, your algorithm complexity upperbound is O(e^(ml*mc)), but we can still shorten it to see, if it's really an exponential complexity. Assume that ml, mc <= N, where N is, let's say, max(ml, mc). So:

e^(ml*mc) <= e^N^2 = e^(e^2*ln(N)) = (e^e)^(2*ln(N)) < (e^e)^(2*N) = [C = e^e] = O(C^N).

So, your algorithm complexity will be O(C^N), where C is a constant, and N is something that growth not faster than linear. So, basically - yes, it is exponetinal complexity.

Chan Kha Vu
  • 9,834
  • 6
  • 32
  • 64
  • Thanks. However, 1- why to look for such a result that the complexity is upper bounded O(e^(ml x mc)) if it's originally upperbounded of O(mc^ml) and O(mc^ml) < O(e^(ml x mc)) ! I just cannot get you point, even though your conclusion is true. 2- Considering one of the two variables a constant, is it fair? what if both variables grow to infinity? 3- for your faster way to achieve the answer in O(mc + ml), please, check my question for this problem here: http://stackoverflow.com/questions/29995965/finding-all-paths-between-a-set-of-vertices-in-a-dag – Median Hilal May 02 '15 at 12:35
  • and keep in mind that my DAG may contain more than one edge between the same two vertices, to carify: Consider a simple (even extreme) of example three vertices v1, v2, v3 and six edges e1,...,e6 where the first three edges connect v1 and v2, and the second three connect v2 and v3. Thus, we have 3*3 = 9 paths, e1e4, e1e5, e1e6, e2e4,...,e3e6 . Well, this has a exponential growth that cannot be linear with our graph size that have 3 vertices and six edges. – Median Hilal May 02 '15 at 12:37