# Is the enumeration of all paths between source and sink in a DAG NP-hard?

Given a directed acyclic graph (DAG), is there a known polynomial time algorithm for enumerating all paths between a source and a sink node?

I can understand that there may be exponential number of paths between the source and the sink, so a polytime algorithm for enumerating them is difficult.

In that case what is the proof that enumerating all paths between source and sink in a DAG is NP-hard?

• An NP-complete problem must be a decision problem. Enumerating all the paths in a directed graph is not a decision problem. – Kyle Jones Jan 8 '18 at 22:00
• Agreed, but we can easily formulate this problem as deciding if there are k paths between source and sink to make it a decision problem. – neo4k Jan 8 '18 at 22:20
• We can't answer your question until you do just that, turn the function problem into a particular decision problem. – Kyle Jones Jan 9 '18 at 0:56
• Done! Thanks for your comment. – neo4k Jan 9 '18 at 1:25

## 1 Answer

The number of paths in a DAG may be exponential, but it can be represented in polynomial space.

In fact, it is easy to calculate: Just run through all of the vertices in topologically-sorted order and for each vertex compute how many paths end there. This is $1$ (for the single-vertex path) plus the sum of the number of paths that end at each of its predecessors. So counting them is in P.

If there are many different paths, you cannot hope to enumerate them all in polynomial time. You can enumerate them in linear space (just enumerate all subsets of vertices and check whether each of them form a path). Or you can number the paths sequentially and convert an arbitrary sequence number to a path in something like $O(n\log n)$ time.

• Thanks! I understand this and I should rather change my question. Is the problem of enumerating all paths between source and sink NP-hard, and if so, what is the proof? – neo4k Jan 9 '18 at 3:13
• @Ain: "NP-hard" applies only to decision problems. You can't even ask that unless you change the question to be about some decision problem. – hmakholm left over Monica Jan 9 '18 at 3:25
• That is not true, I believe. Read definition here en.wikipedia.org/wiki/NP-hardness – neo4k Jan 9 '18 at 3:26
• @Ain: See the first three words in the "Definition" section of the article you link to. – hmakholm left over Monica Jan 9 '18 at 3:27
• And you can read the last sentence. "Another definition is to require that there is a polynomial-time reduction from an NP-complete problem G to H. As any problem L in NP reduces in polynomial time to G, L reduces in turn to H in polynomial time so this new definition implies the previous one. Awkwardly, it does not restrict the class NP-hard to decision problems, for instance it also includes search problems, or optimization problems." – neo4k Jan 9 '18 at 3:29