Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What might be a good way to calculate length of all paths between two nodes in a directed acyclic graph? I don't need the actual paths, just the length. Is there a combinatorial formula for that?

share|cite|improve this question
up vote 2 down vote accepted

Let $A$ be the adjacency matrix for your graph - so $A_{ij}=1$ if $(i,j)$ is an edge of your graph and $(A_{ij}=0$ otherwise.

Then $(A^k)_{ij}$ is the number of paths from $i$ to $j$ of length $k$.

One nice property is that, since the graph is acyclic, there can be no path of length $\geq n$, where $n$ is the number of nodes in your graph, since such a path would have to have a cycle. This means that $A^n=0$.

You can easily get the number of paths from $i$ to $j$ by taking the $ij$th entry of $I+A+A^2+...+A^{n-1}=(I-A)^{-1}$, so you need only invert $I-A$ to get all the counts.

I'm not seeing how to get the counts of the lengths, or even the list of lengths, other than explicitly computing $A^k$ for each $k$.

share|cite|improve this answer
Thanks. I ll need to do a bit of search. – rivu Jun 26 '12 at 19:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.