Algorithm Nightmare: An O(EV+VlogVlogK) Solution for Counting Paths of Length K
2025-08-25
This article tackles a seemingly simple algorithmic problem: finding the number of paths of length K between nodes A and B in a directed, unweighted graph. Starting with basic BFS and dynamic programming, the author delves into more advanced techniques, including matrix exponentiation, linear recurrences, generating functions, annihilating polynomials, and the Berlekamp-Massey algorithm. The result is a stunning O(EV+VlogVlogK) solution, significantly faster than traditional O(EK) or O(V³logK) approaches. The author clearly explains the principles and connections between these algorithms, highlighting the problem's complexity and the elegance of the solution.
Read more
Development
linear recurrences