2

We all know how to find the shortest path between two vertices, but what if I just want to know the answer to this question - is there a path, (any path), between vertex A and B of length larger than some X?

Should start from the shortest path, and then merge adjacent nodes if its length is less than X? (or extend the search "circle" gradually)

I don't want to first find all possible paths and then filter out those shorter than X, I want to stop the moment I find any path above a certain length between the two vertices, (and stop if I haven't found one after some MAX iterations)

(I assume I first do a shortest paths to find if there is at all any path between the nodes to avoid searching if there isn't)

It somehow feels it should be simpler that what I'm making it, but I can't seem to find a text-book algorithm for it (e.g. BFS / DFS / Dijsktra / Bellman Ford are not really helping here, right?)

I'm sure it's simple, but I need some push in the right direction.

(cross posted from https://softwareengineering.stackexchange.com/questions/283038/algorithm-to-find-whether-there-is-a-path-any-path-above-length-x-between-two based on suggestions in the comments)

Eran Medan
  • 431
  • 1
  • 4
  • 12

1 Answers1

2

If you allow duplicate vertices on your path, you can make a network from your graph by giving each edge weight $-1$. Then, use Bellman Ford to find the shortest path and see if it has length $-X$ or less.

If you don't allow repeats, then the problem is NP hard, since finding the longest simple path between two vertices tells you whether there is a Hamiltonian path or not.

Joey Eremondi
  • 30,277
  • 5
  • 67
  • 122