6

We are given an Adjacency list of the form

U -> (U,V,C) -> (U,V,C) ...  
U2 -> ...  
U3 -> ...  
.  
.  
etc

(U,V,C) means there's an edge from U to V with cost C.

The given Adjacency list is for a single connected tree with N nodes thus containing N-1 edges.

A set of nodes F=F1,F2,F3...Fk are given.

Now the question is what is the best way to find the longest path amongst the nodes in F? Is it possible to do it in O(N)?

Is DFS from each node in F the only option?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
SrinathV
  • 61
  • 1
  • 5

2 Answers2

1

I understood your question as asking to find a pair of nodes from the set F so that the unique path between those two nodes is as long as it can be. The path is unique because your graph is a tree.

The problem can be solved trivially by doing DFS from every node in F as you mention, for an O(n k) solution where n is the size of the graph and k is the size of the set F.

However, you can solve it potentially faster by a divide and conquer approach. Pick any node R from the graph, and use a single DFS to tabulate distances Dist(R, a) to every other node a a and at the same time partition the nodes to subtrees S1,...,Sm where m is the number of edges from R; that is, these are the m trees hanging at the root R. Now, for any f and g that belong to different subtrees it holds that the path between them has Dist(R, f) + Dist(R, g) edges, so it is possible to search for the longest such path in O(k^2) time. In addition, you have then to recurse to the subproblems S1,...,Sm to cover the case where the longest path is inside one of those trees. The overall complexity can be lower than O(n k) but the math is left as an exercise to the reader.

Antti Huima
  • 25,136
  • 3
  • 52
  • 71
  • Two diagrams would make this explanation so much more clear. It's beautiful, but only words makes it hard to digest. It took me a good 5 minutes to understand. – Jared Goguen Apr 21 '16 at 05:56
  • Also, the claim that the given algorithm is less than O(nk) should not be left to the reader. Assuming that the worse case is 2 f-nodes per subtree, the initial comparisons are (k^2-k)/4. The subtree analyses are then 2m(n/m), leading to (k^2-k)/4+2n comparisons, but this assumes that 2 f-node subtrees are the worst case. It seems like log(k) f-nodes would actually be the worst case. Could you elaborate on your assertion that this is O(nk)? – Jared Goguen Apr 21 '16 at 06:22
-2

If I understood your question correctly, you are trying to find the longest cost path in a spanning tree.

You can find the path in just 2 complete traversal i.e., O(2N) ~ O(N) for large value of N.

you should do below step.

  1. Pick any node in the spanning tree.
  2. Run any algo (DFS or BFS) from the node and find the longest cost path from this node.

This will not be your longest cost path as you started by randomly picking a node.

  1. Run BFS or DFS one more time from the last node of longest cost path found at step 2.
  2. This time the longest cost path you get, will be the Longest cost path in spanning tree.

You do not have to run DFS from each node.

Maljam
  • 6,244
  • 3
  • 17
  • 30
Nyk
  • 7
  • 3
  • This will always correct in case of undirected spanning tree as when you traverse by randomly picking a node, you are not sure if the start point will lead you the longest path. When you run the algo for first time, you traverse the longest path leaving one scenario left which is the actual longest path does not involve the node which you picked at first. So when you are running the algo again it will surely give you the longest path. In both cases if the longest path involve the node which you picked first or even it does not. Try to dry run it on a use case, you will understand for sure. – Nyk Apr 21 '16 at 06:02
  • This definitely makes sense if every node belongs to F, but every node does not belong to F. It could be that this is still a good methodology, but you might need to address that. (Or maybe I'm missing something, let me know if so.) – Jared Goguen Apr 21 '16 at 06:02
  • @JaredGoguen Yes, If every node belongs to set F. As in the question, its mentioned that there are N nodes and N-1 edges and there is no loop so I assumed that every node is connected to a single tree. – Nyk Apr 21 '16 at 06:20
  • It is given that the structure is a tree, but not all nodes are of interest (i.e. in F). – Jared Goguen Apr 21 '16 at 06:27