Path of exact cost in edge labelled DAG?

Given edge labelled directed acyclic graph with edge labels $w_i \in \mathbb{N}$ the cost of a path is the sum of the labels.

The problem is:

Find a path from $s$ to $t$ with cost $a$.

I suppose this is NP-hard.

Some questions:

1. Would putting a bound $\max w_i < C$ make the problem easier?
2. What are the best algorithms for this?
3. Is the problem NP-complete?

To each edge associate second label weight $r_i \in \mathbb{N}$. The weight of a path is the sum of $r_i$.

Assume (possibly small) bound $C$ of the costs $w_i$ and solve:

$cost = a$ and $weight < K$.

Would it be still polynomial in $n,m,C$ or NP-hard instances exist (one can assume positive labels if this helps)?

Yes, it's NP-complete, by a reduction from subset sum. It's easiest to see using a multigraph: make a directed path, double each edge, and make one of the two doubled edges have cost zero, then cost $a$ can be achieved if and only if it is the sum of some subset of the weights of the nonzero edges. If you don't like multigraphs, subdivide the edges.

If you assume that $\max w_i<C$ then there's a dynamic programming algorithm with time $O(mnC)$: just process the nodes in a topological ordering, and for each node calculate the set of path lengths by which it can be reached from $s$.

• Thank you. I suppose something close to $O(mnC)$ can be achieved by replacing $w_i$ by a chain of $w_i$ unlabelled edges and then search for path of length $a$? Mar 11, 2011 at 8:25
• Each set is a bitmask of nC bits and you have to propagate these bitmasks across m edges, hence the O(mnC) time bound. Mar 11, 2011 at 8:40
• Since you are looking for a path with cost exactly $a$, a bitmask of $a$ bits suffices. Hence $O(nma)$ time holds also without a $\max w_i<C$ bound. Mar 11, 2011 at 9:12
• @jerr18 @Radu Yes, it's $O(n+ma)$, and it's on non-negative weights. Mar 11, 2011 at 10:06
• One additional remark is that one can implement an $O(n+ma)$ algorithm using dynamic programming even if the graph is not a DAG. Mar 12, 2011 at 5:42

Another way to prove that it is NP-complete consist of noticing that if each label is 1 and $a = \#V$ where $V$ is the set of vertices, we obtain the hamiltonian path problem. It shows too that even if you give an upper bound C, it remains NP-complete.

EDIT: My answer is wrong because in DAG, hamiltonian path problem has a polynomial algorithm.

• The directed Hamiltonian path problem for DAGs is polynomial-time, not NP-complete. Mar 11, 2011 at 15:46
• @David Oups, I hadn't read enough carefully the question. Should I delete my answer to prevent people from being misled if they don't read comments ? Mar 11, 2011 at 23:14
• Currently we have no policy about deleting wrong answers, so you may do anything you want. Edit the answer, as you did, is another solution. Mar 12, 2011 at 16:44