Counting approximately-shortest paths in directed acyclic graphs

Counting approximately-shortest paths in directed acyclic graphs


Given a directed acyclic graph with positive edge-weights, two vertices and , and a threshold-weight , we present a fully-polynomial time approximation-scheme for the problem of counting the - paths of length at most . We extend the algorithm for the case of two (or more) instances of the same problem. That is, given two graphs that have the same vertices and edges and differ only in edge-weights, and given two threshold-weights and , we show how to approximately count the - paths that have length at most in the first graph and length not much larger than in the second graph. We believe that our algorithms should find application in counting approximate solutions of related optimization problems, where finding an (optimum) solution can be reduced to the computation of a shortest path in a purpose-built auxiliary graph.

1 Introduction

Systematic generation and enumeration of combinatorial objects (such as graphs, set systems, and many more) has been a topic of extensive study in the field of combinatorial algorithms for decades [10]. Counting of combinatorial objects has been investigated at least as thoroughly, even leading to their own computational complexity class #P, defined in Valiant’s seminal paper [14]. A counting problem usually asks for the number of solutions to a given combinatorial problem, such as the number of perfect matchings in a bipartite graph. In combinatorial optimization, the number of optimum solutions can sometimes be computed by a modification of an algorithm for finding a single optimum solution. For instance, for shortest - paths in graphs with positive edge weights, Dijkstra’s algorithm easily admits such a modification. The problem we discuss in this paper has a more general flavor: We aim at counting the number of approximate solutions, in the sense of solutions whose objective value is within a given threshold from optimum. For shortest - paths, it is not obvious how to count the number of paths within, say, 10% from optimum. A related problem of enumerating feasible solutions makes a step in this direction: If we can enumerate solutions in order of decreasing quality, starting from an optimum solution, we have a way to count approximate solutions. Even though for some problems there are known enumeration algorithms that return the next feasible solution in the sequence of solutions within only polynomial extra time (called “polynomial delay”), this approach will usually not be satisfactory in our setting. The reason is that the number of approximate solutions can be exponential, and counting by enumerating then takes exponential time, while our interest is only in the count itself.

In this paper we propose a way to count approximate solutions for the shortest - path problem in directed acyclic graphs (DAGs) in polynomial time, but the count that we get is only approximate, even though we come as close to the exact count as we wish (technically, we propose an FPTAS). We also show that exact counting for our problem is #P-hard, thus (together with the FPTAS) fully settling its complexity. We achieve our result by a modification of a conceptually interesting dynamic program for all feasible solutions for the knapsack problem [13]. Our motivation for studying our counting problem comes from a new approach [2] to cope with uncertainty in optimization problems. There, we not only need to count the number of approximate solutions for a given problem instance, but we also need to count the number of solutions that are approximate (within a given approximation ratio) for two problem instances at the same time. For the case of shortest - paths, this means that we are given two input graphs that are structurally identical, but are allowed to differ in their edge weights. We now want to count the number of - paths that are within, say, 10% from optimum in both input graphs at the same time. For this problem we propose both a pseudo-polynomial algorithm and an algorithm that calculates an approximate solution for a potentially slightly different threshold in fully polynomial time. Our hope is that our study paves the way for approximately counting approximate solutions for other optimization problems, such as minimum spanning trees.

The rest of the paper is organized as follows. We outline possible implications of our result in Section 1.1. We show in Section 1.2 that our problem is #P-complete. We present the algorithms in Section 2, and conclude the paper in Section 3.

1.1 Dynamic Programming as Shortest-Path Computation in DAGs

The concept of computing a shortest - path in a directed acyclic graph has a large number of applications in many areas of algorithmics. This is partly due to the fact that dynamic programming algorithms in which the inductive step consists of searching for a maximum or a minimum among some functions of previously-computed values can be viewed as the problem of looking for the shortest or longest path in a directed acyclic graph.1

In many problems that admit a dynamic programming solution we are interested not only in the single optimum, but also in other approximately optimal solutions. For instance, if we single out the context of analysis of biological data, de novo peptide sequencing [4, 11], sequence alignment [12], or Viterbi decoding of HMMs [3, 5] all use dynamic programming to find a shortest path in some implicit graph. Due to the nature of the data in these applications, producing a single solution is often insufficient and enumerating all solutions close to the optimum is necessary. Our contribution, therefore, provides a faster solution than explicit enumeration for the problems where counting of approximate solutions is required [12]. Counting and sampling from close-to-optimum solutions is the key-element of the recent optimization method with uncertain input data of Buhmann et al. [2]. Our work thus makes a step towards practical algorithms in this context.

1.2 Counting Approximate Solutions is #P-Complete

The problem of counting the number of all self-avoiding - walks in a directed (or undirected) graph is known to be #P-complete [15]. The proof makes use of graphs containing cycles, thus it cannot be used to show the hardness of the problem of counting approximate shortest paths on a directed acyclic graph. In fact, we can easily count all - paths in a directed acyclic graph in time proportional to the number of edges, if we traverse the graph vertices sorted in topological order and add up the number of paths arriving to each vertex from its predecessors. The difficulty thus lies in the addition of edge-weights and the requirement to count - paths of length at most . In the following, we show that this problem is #P-complete, by a reduction from the -complete partition problem. Given a set of positive integers , the partition problem asks for a partition of into sets and such that the sums of numbers in both sets are equal.

Given an instance of the partition problem, we construct a graph with vertices as follows. We consider the elements of in an arbitrary order . Then, for every , the graph will contain two parallel edges between vertices and with lengths and , respectively. Then every path from to corresponds to one partition of to subsets and . If, between two consecutive vertices and , the edge with length is chosen, will belong to the set . If the chosen edge has length , the element will belong to the set . The length of the - path then corresponds to the difference between the sums of elements in and in and the number of paths of length is then equal to the number of optimal solutions of the partition problem.

If we had an algorithm that can count the number of - paths of length at most and the number of - paths of length at most , the difference between these two numbers is the number of paths of length exactly and thus the number of solutions to the partition problem.

Since the partition problem is reducible from the #P-complete knapsack problem [6] and its own reduction as well as ours is parsimonious [9], the problem of counting all - paths of length at most is #P-complete. Note that the existence of parallel edges is not necessary for the reduction; we could bisect each parallel edge creating an auxiliary vertex to form a graph of the same functionality but without parallel edges. Also, observe that the use of negative edge-weights is not necessary; we can add to every edge-weight a very large number (say, the maximum number in ), and then ask whether there exists a path of length . Thus, we have shown the following.

Theorem 1.1

Let be a directed acyclic graph with integer edge-weights, and be an integer. The problem of counting all - paths of length at most is #P-complete, even if all edge-weights are non-negative.

2 Approximation Algorithms

In this section we present an FPTAS for our counting problem. That is, we present an algorithm that when given a directed acyclic graph on vertices, two dedicated vertices and , a weight-threshold , and a constant , computes a -approximation of the total number of - paths of length at most , and which runs in time polynomial in both and .

Let us note why the most immediate attempt to solve the problem directly does not work. We could try to calculate the number of paths from to each vertex that are shorter than all possible thresholds . We can do this incrementally by calculating the paths for vertices sorted in topological order and for each new vertex combining the paths that arrived from previously computed vertices. We can then pick some polynomially large subset of the thresholds and round all distances down to the nearest one in the subset. While we would end up with an algorithm of polynomial run-time, it would not constitute a FPTAS, since we would exactly count the number of paths that are no longer than some length which does not differ much from our desired maximum length , instead of approximately counting the number of solutions that are shorter than the exact length .

We first show a recurrence that can be used to exactly count the number of - paths of length at most . Evaluating the recurrence takes exponential time, but we will later show how to group partial solutions together in such way that we trade accuracy for the number of recursive calls. We adapt the approach of Štefankovič et al. [13], which they used to approximate the number of all feasible solutions to the knapsack problem.

Let be a directed acyclic graph with vertices. We will label the vertices in such order that there is no path from to unless , i.e., defines a topological ordering. We suppose that and , otherwise the graph can be pruned by discarding all vertices that appear before and after in the topological order, since no path from to ever visits these.

Now, for a given , instead of asking for the number of - paths that have length at most , we indirectly ask: for a given value , what is the smallest threshold such that there are at least paths from to of length at most ? Let denote the minimum length such that there are at least paths from to of length at most . To find the number of - paths of length at most using this function , we simply search for the largest such that , and return it as the output. In particular, if the length of the shortest - path is (which can be computed in polynomial time), we can find, for any , the number of -approximate - paths by setting .

For a concrete vertex with in-degree , let us denote its neighbors that precede it in the topological order by and let us denote the corresponding incoming edge lengths by . For simplicity, we usually drop the index when it is clear from the context and just write , and . Now, can be expressed by the following recurrence

Intuitively, the paths starting at and arriving at must split in some way among incoming edges. The values define such split. We look for a set of that minimizes the maximum allowed path length needed such that the incoming paths can be distributed according to , . Note that while the values of do not have to be integer, is equal to . Moreover, when evaluating the recursion, it is enough to search for values such that each of the values is an integer.

Calculating using the given recurrence will not result in a polynomial time algorithm since we might need to consider an exponential number of values for , namely on a DAG with a maximal number of edges.2 To overcome this, we will consider only a polynomial number of possible values for , and always round down to the closest previously considered one in the recursive evaluation. If we are looking for an algorithm that counts with precision, the ratio between two successive considered values of must be at most .

For this purpose, we introduce a new function . In order to achieve precision of , we will only consider values of for minimum path numbers in the form of for all positive integers such that , where . The values of for other numbers of paths will be undefined. The function is defined by the recurrence


To give a meaning to the expression when , we define it to be equal to , which is consistent with its limit when goes to . We now show that the rounding does not make the values of too different from the values of .

Lemma 1

Let and . Then


We first prove the first inequality, proceeding by induction on . The base case holds since for any . Suppose now that the first inequality of (2) holds for every , . Then, for every ,

Thus, since every predecessor of is earlier in the vertex ordering, we can use the obtained inequality to get the claimed bound

The other inequality follows by a simpler induction on . The base case holds since for all . Assume now that the second part of (2) holds for all . Then

We can now use the recursive definition to obtain the claimed inequality :

We can now use to obtain a -approximation for the counting problem. Basically, for any , we show that for the largest integer such that , the value will be no more than away from the optimum.

Lemma 2

Given , let be such that and be such that . Then .


Using Lemma 1 twice, we get . As is at most , and is largest such that , and is monotonous in its second parameter, it must be that . Similarly, is larger than , so by monotonicity . Thus both and must lie between and and their ratio can be at most and at least . ∎

We now show that computing the values of can be done in time polynomial in and . This then, together with Lemma 2, gives an FPTAS for the counting problem.

Theorem 2.1

For any , any edge-weighted directed acyclic graph , and any vertices , , there is an FPTAS that counts the number of all - paths in of length at most in time .


Recall that a directed acyclic graph on vertices has at most - paths. The values of in therefore span at most , and the values of in span at most , where

Thus, we evaluate function for at most different parameter pairs.

To show that the evaluation of can be done in polynomial time, we need to show that we can efficiently find that minimize Expression (1). Fortunately, is monotonous with increasing , we can thus apply a greedy approach. Given , we will evaluate for all possible values of in one run. Instead of looking for the tuple such that we will consider an integer tuple that minimizes restricted by . We start with all equal to and always increase by one the that minimizes . Whenever the sum of all gets larger than some value , we store the current maximum of as the value . We terminate once reaches . It can be shown that such approach calculates the same values of as searching through ratios . As we can increase each at most times, we make at most steps, each of which involves choosing a minimum from values and replacing it with a new value. The latter can be done in time , for instance by keeping the values in a heap. The sum of the ’s for all considered vertices is equal to the number of edges . The update of , calculation of from , and comparison with the maximum number of paths can all be done in time if we choose in the form in order to be able to implement multiplication by by a sequence of bit-shifts and a single addition. The resulting bit-time complexity is thus . ∎

We note that processing the dynamic programming table for all path numbers in one go would to improve the time complexity of the original Knapsack FPTAS [13] by a factor of .

2.1 Counting solutions of given lengths in multiple instances

In this section we consider the problem of counting solutions that are approximately-optimum for two given instances at the same time. The two instances differ in edge lengths, but share the same topology, effectively forming a bi-criteria instance. Formally, given two directed acyclic graphs and , differing only in edge-weights, given two vertices and , and given two threshold values and , we are interested in the number of the - paths that have at the same time length at most in and length at most in .

To solve this algorithmic problem, we cannot directly apply the approach for the single-instance case (by defining to be a pair of path lengths, one for each of the two instances), as we now have two lengths per edge and it is unclear how to suitably define a maximum over pairs in Equation (2). In fact, we can show that we cannot construct a FPTAS for the two instance scenario, or indeed any approximation algorithm.

Theorem 2.2

Let and be two directed acyclic graphs with the same sets of vertices and edges, but possibly different edge-weights, let and be two vertices in them, let and be two length thresholds. The existence of an algorithm that in time polynomial in number of vertices computes any finite approximation of the number of paths from to that are shorter than if measured in the graph and shorter than if measured in the graph , implies that .


We show this by reducing the decision version of the knapsack problem to the aforementioned problem. Let us have a knapsack instance with items with weights and prices . Given a total weight limit and a price limit we want to know if we can select a set of items such that the total weight is at most and the total price is at least . The corresponding DAG will have vertices , with two edges between all successive vertices and that will correspond to the action of taking or not taking the -st element into the knapsack. The first edge between and will have length in the graph and length in the graph , the second edge will have length in the graph and in the graph . We can now ask for the number of paths from to that are shorter than in the graph and shorter than in the graph . If we had an algorithm that gives us a number that differs from this number by any real and finite multiplicative ratio , we could determine whether the original knapsack problem had at least one solution since the ratio between and is not a real number. ∎

This proof is perhaps surprising due to the fact that Gopalan et al. [7] showed a FPTAS that counts the number of solutions of multi-criteria knapsack instances. This shows that while knapsack is a special version of our problem, it is in fact less complex due to the common assumption that the item values are non-negative.

While we cannot obtain a -approximation of the number of - paths that have length at most in the first instance, and at the same time length at most in the second instance, we will adopt the techniques for FPTAS in a single instance, and show a polynomial-time algorithm that provides heuristics for good estimates of - paths that have length at most in the first instance, and at the same time length at most in the second instance. We will only consider the case where is positive.

To do so, we define a function similar in spirit to that uses a maximum path-length in the form of a “budget” as a parameter of . Formally, is the smallest length such that there are at least - paths, each of length at most with respect to the edge lengths in the first instance, and of length at most with respect to the edge length in the second instance. Similarly to , we can express recursively using the following notation. Let be a vertex of in-degree , and let be the neighbors of preceding it in the topological order. The edge-length of the incoming edge , , is in the first instance, and in the second instance. Then, satisfies the following recursion:

If we wanted to use to directly use to solve our counting problem, the function would have to be evaluated not only for an exponential number of path counts , but also for possibly exponential number of values of . To end up with polynomial runtime, we thus need to consider only a polynomial number of values for both parameters of . For this purpose, we will introduce a function that does this by considering only path lengths in the form of , where , and path numbers in the form of , where , for positive and . Function is defined by the following recurrence:

Similarly to the case of one instance only, one can show that approximates well, this time in two variables.

Lemma 3

Let , , and . Then


We proceed as in the proof of Lemma 1. Note that the function is monotone non-decreasing in , but monotone non-increasing in . Proceeding by induction on , the base case holds since for any and . We suppose that Equation (3) holds for all . Then, for every ,

Thus, since every predecessor of has index smaller than ,

The proof of the inequality is similar. Assuming that (3) holds for every , we obtain

Plugging it into the definition of , we obtain

Using Lemma 3, we can show that provides enough information to compute an approximation of . However, we cannot get a approximation to the optimal value as in Lemma 2, because we need to round the value of to a power of in order for it to be legal parameter of and we further round it during the evaluation of . We will therefore relate the result of to the results of we would have gotten if we considered the value of when rounded up towards the nearest number that can be represented as for integer and the value . Due to the choice of , the ratio of these two values is .

Lemma 4

Let be such that , be such that , and be largest such that . Then , , and .


The statement that follows from the definition of and : decreasing the limit on the path length in the first instance from to cannot increase the number of possible paths. By applying Lemma 3 twice, we get




From the definition of and (5) we can conclude . This implies that , due to our choice of . Similarly, from the definition of and (4) we get and thus . ∎

Lemma 4 shows that the computed number of - paths cannot be larger than by more than a factor of , nor can it be smaller than by a factor larger than . Furthemore, with the aforementioned choice of as , the difference between the rounded up value of which is and the rounded down value which is is . We can now state the overall running time of the approach. Compared to the function we need to evaluate for values of , in addition to the values of and . Otherwise the arguments are identical to the proof of Theorem 2.1. Note that is by definition in , but we list it explicitly since it can be much smaller in practice.

Lemma 5

Given path-lengths and for two given instances and of a graph with edges and vertices, there is an algorithm that finds satisfying in time .

Putting together Lemma 4 and Lemma 5 we can state the overall result:

Theorem 2.3

For any , , any edge-weighted directed acyclic graphs on the same topology and , and any two of their vertices , , there exists a length satisfying and an FPTAS for counting the number of paths from to no longer than when evaluated on the graph and no longer than when evaluated on the graph in the time .

It is easy to see that we can extend the approach to count paths that approximate instances at the same time by adding “budgets” for the desired maximal lengths of paths in instances . The time complexity would again increase, for every additional instance with threshold by .

Pseudo-polynomial algorithm for two instances.

If the discrepancy between and as defined in Lemma 4 is too large and all edges have integer lengths, we can consider all possible lengths in the first instance, instead of rounding to values in the form of .

The function will be extended with the budget representing the exact maximum length of a path in the first instance.

We will state the lemma and theorem about accuracy and runtime without proofs, since these are similar to the proofs of Lemma 2 and Theorem 2.1. Notice that the algorithm evaluating is pseudo-polynomial.

Lemma 6

Given , let be such that and be such that . Then .

Theorem 2.4

Given two graphs with integer weights, and any , there is an algorithm that computes a -approximation for the number of - paths that have length at most in the first instance, and length at most in the second instance, and runs in time , where denotes the number of edges in the graph.

3 Concluding Remarks

We have shown that there is an efficient algorithm to approximate the number of approximately shortest paths in a directed acyclic graph. This problem is implicitly or explicitly present as an algorithmic tool in algorithmic solutions to a large number of different computational problems, not limited to the evaluation of solutions achieved by dynamic programming which we noted in Section 1.1.

Our result allows us, for instance, to approximately count only the small (or large) terms of a polynomial , , represented as a product of polynomially many polynomial factors , where each has polynomially many terms, and where every . This is especially interesting if the full expansion of has exponentially many terms. This may be a powerful tool, if extended to the case of both negative and positive , enabling the counting of approximate solutions for problems with known generating polynomials of solutions by weight. For instance, counting of large graph matchings [8] or short spanning trees [1] can be done via generating polynomials (which, in general, have exponentially many terms). This direction is our primary future work.

We have also showed that our algorithm can be extended, given threshold weights , and polynomially many graphs , to count - paths that have, at the same time, length at most in and at most in , . In the case when , this algorithm is necessary for application of the aforementioned robust optimization method [2] to the various mentioned optimization problems.

Acknowledgements. We thank Octavian Ganea and anonymous reviewers for their suggestions and comments. The work has been partially supported by the Swiss National Science Foundation under grant no. 200021_138117/1, and by the EU FP7/2007-2013, under the grant agreement no. 288094 (project eCOMPASS).


  1. Note that due to the lack of cycles, the problems of looking for shortest and longest paths on DAGs are computationally identical.
  2. To see this, observe that in a topologically sorted graph , any subset of gives a unique candidate for an - path.


  1. Broder, A.Z., Mayr, E.W.: Counting minimum weight spanning trees. J. Algorithms 24, 171–176 (July 1997)
  2. Buhmann, J.M., Mihalák, M., Šrámek, R., Widmayer, P.: Robust optimization in the presence of uncertainty. In: Proc. 4th Conference on Innovations in Theoretical Computer Sciencei (ITCS). pp. 505–514. ACM, New York, NY, USA (2013)
  3. Burge, C., Karlin, S.: Prediction of complete gene structures in human genomic DNA. Journal of molecular biology 268(1), 78–94 (1997)
  4. Chen, T., Kao, M.Y., Tepel, M., Rush, J., Church, G.M.: A dynamic programming approach to de novo peptide sequencing via tandem mass spectrometry. Journal of Computational Biology 8(3), 325–337 (2001)
  5. Durbin, R., Eddy, S.R., Krogh, A., Mitchison, G.: Biological sequence analysis: probabilistic models of proteins and nucleic acids. Cambridge university press (1998)
  6. Dyer, M., Frieze, A., Kannan, R., Kapoor, A., Perkovic, L., Vazirani, U.: A mildly exponential time algorithm for approximating the number of solutions to a multidimensional knapsack problem. Combinatorics, Probability and Computing 2(3), 271–284 (1993)
  7. Gopalan, P., Klivans, A., Meka, R., Štefankovič, D., Vempala, S., Vigoda, E.: An FPTAS for # knapsack and related counting problems. In: Proc. 52nd Annual IEEE Symposium on Foundations of Computer Science (FOCS). pp. 817–826 (2011)
  8. Jerrum, M.: Two-dimensional monomer-dimer systems are computationally intractable. Journal of Statistical Physics 48(1-2), 121–134 (1987)
  9. Karp, R.M.: Reducibility among combinatorial problems. Springer (1972)
  10. Kreher, D.L., Stinson, D.R.: Combinatorial Algorithms: Generation, Enumeration, and Search (1998)
  11. Lu, B., Chen, T.: A suboptimal algorithm for de novo peptide sequencing via tandem mass spectrometry. Journal of Computational Biology 10(1), 1–12 (2003)
  12. Naor, D., Brutlag, D.: On suboptimal alignments of biological sequences. In: Proc. 4th Annual Symposium on Combinatorial Pattern Matching (CPM). pp. 179–196. Springer (1993)
  13. Štefankovič, D., Vempala, S., Vigoda, E.: A deterministic polynomial-time approximation scheme for counting knapsack solutions. SIAM Journal on Computing 41(2), 356–366 (2012)
  14. Valiant, L.G.: The complexity of computing the permanent. Theoretical computer science 8(2), 189–201 (1979)
  15. Valiant, L.G.: The complexity of enumeration and reliability problems. SIAM J. Comput. 8(3), 410–421 (1979)
Comments 0
Request Comment
You are adding the first comment!
How to quickly get a good reply:
  • Give credit where it’s due by listing out the positive aspects of a paper before getting into which changes should be made.
  • Be specific in your critique, and provide supporting evidence with appropriate references to substantiate general statements.
  • Your comment should inspire ideas to flow and help the author improves the paper.

The better we are at sharing our knowledge with each other, the faster we move forward.
The feedback must be of minimum 40 characters and the title a minimum of 5 characters
Add comment
Loading ...
This is a comment super asjknd jkasnjk adsnkj
The feedback must be of minumum 40 characters
The feedback must be of minumum 40 characters

You are asking your first question!
How to quickly get a good answer:
  • Keep your question short and to the point
  • Check for grammar or spelling errors.
  • Phrase it like a question
Test description