2

In an acyclic graph, I am trying to find out whether or not a path of length L exists between two given nodes. My questions is, what is the best and the simplest Algorithm to use in this case.

Note that the graph has a maximum of 50 nodes and 100 edges.

I have tried to find all the paths using DFS and then to check if that path exists between the two nodes but I got the answer "Time Limit Exceeded" from the online judge.

I also used the Uniform Cost Search Algorithm but I also a got a negative response.

I need a more efficient way for solving such problem. Thank you.

Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83

4 Answers4

5

I don't know if it will be faster then a DFS approach - but it will give a feasible solution:

Represent the graph as a matrix A, and calculate A^L - a path of length L between i and j exists if and only if A[i][j] != 0

Also, regarding DFS solution: You do not need to find all paths in the DFS - you should limit yourself to paths of length <= L, and by this trim some searches, once the length have exceeded the needed length. You could also escape the search once a path of length L is reaching the target.

Another possible optimization could be bi-directional search.

  • Find all vertices which have path of length L/2 from the source to them.
  • Next, find all vertices which have paths of length L/2 from them to the target (DFS on the reverse graph)
  • Then, check if there is a vertex that is common to both sets, if there is - you got a path of length L from the source to the target.
amit
  • 175,853
  • 27
  • 231
  • 333
2

Since the graph is acyclic you can order vertices topologicaly. Let's name starting vertex A and finish vertex B.

Now the core algorithm starts: For each vertex count all possible distances from A to this vertex. At start there is one path from A to A with length zero. Then take vertices in topological order. When you pick vertex x: Look at each predecessor and update possible distances here.

This should run in O(N^3) time.

usamec
  • 2,156
  • 3
  • 20
  • 27
0

You can use a modified Dijkstra algorithm where instead of saving for every vertex the minimum distance to the origin, you save all the possible distances less or equal to the one desired.

salva
  • 9,943
  • 4
  • 29
  • 57
-2

I believe that you can use the following algorithm to find the longest path in a tree. This assumes that your graph is connected, if it is not you would need to rerun this on each connected component:

  1. Pick an arbitrary node, A.
  2. Do a BFS (or DFS) from A to find the node in the tree farthest from A, call this node B.
  3. Do a BFS (or DFS) from B to find the node in the tree farthest from B, call this node C.
  4. The path from B to C is the longest path in the tree (possibly tied for longest).

Obviously if this path is longer than L then you can shorten it to find a path of length L.

Running Wild
  • 2,951
  • 18
  • 15
  • According to your algorithm, if you can go from B to another node, then why did we stop at B in step 2? aren't we suppose to keep going to the farthest one? What's your definition of "a node being the farthest" ? What's the name of the algorithm you are suggesting here? – Traveling Salesman Jun 20 '12 at 18:59
  • We stop at B because we have to, it's a leaf node and there is no where else to go. Then we do a different search starting from B, we'll reach A, but then we'll go further and get to C. A node X is furthest from node Y if there is no node Z such that the distance from X to Z is greater than X to Y. – Running Wild Jun 20 '12 at 19:09
  • I don't know if this algorithm has a name - I came up with it back when I was in school. I had to prove it at the time, but I remember the proof being a bit tedious so I didn't want to try to redo it here. – Running Wild Jun 20 '12 at 19:11
  • 1
    A don't think the question is about trees. – usamec Jun 20 '12 at 19:12
  • @usamec - Well it's about acyclic graphs, and acyclic graphs are trees. – Running Wild Jun 20 '12 at 20:15
  • @RunningWild - directed graph can be acyclic and not be a tree. – usamec Jun 20 '12 at 20:33
  • @usamec - He didn't say it was directed. – Running Wild Jun 20 '12 at 21:01