Given an undirected connected tree with N nodes (and N-1 edges), we need to find two paths in this tree such that they are non-intersecting and the product of their length is maximum.
In first tree two paths which are non-intersecting and have highest product are, 1-2 and 3-4, so answer is 1*1 = 1 In second tree two paths which are non-intersecting and has highest product are, 1-3-5 and 7-8-6-2 (or 4-8-6-2), so answer is 3*2 = 6
We can solve this problem by depth first search of tree by proceeding as follows, Since tree is connected and paths are non-intersecting, If we take any pair of such paths there must be a third path, connecting these two and If we remove an edge from the third path then tree will be divided into two components — one containing the first path, and the other containing the second path. This observation suggests us the algorithm: iterate over the edges; for each edge remove it, find the length of the path in both connected components and multiply the lengths of these paths. The length of the path in a tree can be found by modified depth first search where we will call for maximum path at each neighbor and we will add two maximum lengths returned, which will be the maximum path length at subtree rooted at current node.
Input is a tree, but there is no specified root in it as we have only collection of edges. The tree is represented as undirected graph. We traverse adjacency list. For every edge, we find maximum length paths on both sides of it (after removing the edge). We keep track of maximum product caused by an edge removal.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of unique paths in tree such that every path has a value greater than K
- Find maximum number of edge disjoint paths between two vertices
- Minimum Product Spanning Tree
- Product of minimum edge weight between all pairs of a Tree
- Level with maximum number of nodes using DFS in a N-ary tree
- Find the root of the sub-tree whose weighted sum XOR with X is maximum
- Maximum sum possible for every node by including it in a segment of N-Ary Tree
- Find the maximum node at a given level in a binary tree
- Maximum edge removal from tree to make even forest
- Minimum valued node having maximum depth in an N-ary Tree
- Spanning Tree With Maximum Degree (Using Kruskal's Algorithm)
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph
- Print all possible paths from the first row to the last row in a 2D array
- Count all possible paths between two vertices
- Shortest paths from all vertices to a destination
- Number of Unicolored Paths between two nodes
- Print all paths from a given source to a destination using BFS
- Print all possible paths in a DAG from vertex whose indegree is 0
Improved By : PranchalKatiyar