Find triplet such that number of nodes connecting these triplets is maximum
Given a Tree with N nodes, the task is to find a triplet of nodes (a, b, c) such that the number of nodes covered in the path connecting these nodes is maximum. (Count a node only once).
Examples:
Input: N = 4
Edge Set:
1 2
1 3
1 4
Output: (2, 3, 4)
(2, 3, 4) as path between (2, 3) covers nodes 2, 1, 3 and path between (3, 4) covers nodes 3, 1, 4. Hence all nodes are covered.
The Red Path in Tree denotes the path between 2 to 3 node which covers node 1, 2, 3. The green path denotes path between (3, 4) which covers node 3, 1, 4.Input: N = 9
Edge Set :
1 2
2 3
3 4
4 5
5 6
2 7
7 8
4 9
Output: (6, 8, 1)
Approach:
- One important point to notice is, two of the points in triplet must be the end of diameter of tree to cover maximum of the points.
- We need to find the longest length branch stick to the diameter.
- Now for 3rd node, apply DFS along with maintaining the depth for each node (DFS in all directions other than on the Diameter Path Selected ) to all the nodes present on the path of Diameter, the node which is at the farthest distance, would be considered as the 3rd node, as it covers the maximum node other than already covered by the Diameter. Diameter of Tree using DFS
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> #define ll long long int #define MAX 100005 using namespace std; vector<int> adjacent[MAX]; bool visited[MAX]; // To store the required nodes int startnode, endnode, thirdnode; int maxi = -1, N; // Parent array to retrace the nodes int parent[MAX]; // Visited array to prevent DFS // in direction on Diameter path bool vis[MAX]; // DFS function to find the startnode void dfs(int u, int count) { visited[u] = true; int temp = 0; for (int i = 0; i < adjacent[u].size(); i++) { if (!visited[adjacent[u][i]]) { temp++; dfs(adjacent[u][i], count + 1); } } if (temp == 0) { if (maxi < count) { maxi = count; startnode = u; } } } // DFS function to find the endnode // of diameter and maintain the parent array void dfs1(int u, int count) { visited[u] = true; int temp = 0; for (int i = 0; i < adjacent[u].size(); i++) { if (!visited[adjacent[u][i]]) { temp++; parent[adjacent[u][i]] = u; dfs1(adjacent[u][i], count + 1); } } if (temp == 0) { if (maxi < count) { maxi = count; endnode = u; } } } // DFS function to find the end node // of the Longest Branch to Diameter void dfs2(int u, int count) { visited[u] = true; int temp = 0; for (int i = 0; i < adjacent[u].size(); i++) { if (!visited[adjacent[u][i]] && !vis[adjacent[u][i]]) { temp++; dfs2(adjacent[u][i], count + 1); } } if (temp == 0) { if (maxi < count) { maxi = count; thirdnode = u; } } } // Function to find the required nodes void findNodes() { // To find start node of diameter dfs(1, 0); for (int i = 0; i <= N; i++) visited[i] = false; maxi = -1; // To find end node of diameter dfs1(startnode, 0); for (int i = 0; i <= N; i++) visited[i] = false; // x is the end node of diameter int x = endnode; vis[startnode] = true; // Mark all the nodes on diameter // using back tracking while (x != startnode) { vis[x] = true; x = parent[x]; } maxi = -1; // Find the end node of longest // branch to diameter for (int i = 1; i <= N; i++) { if (vis[i]) dfs2(i, 0); } } // Driver code int main() { N = 4; adjacent[1].push_back(2); adjacent[2].push_back(1); adjacent[1].push_back(3); adjacent[3].push_back(1); adjacent[1].push_back(4); adjacent[4].push_back(1); findNodes(); cout << "(" << startnode << ", " << endnode << ", " << thirdnode << ")"; return 0; } |
Python3
# Python3 implementation of the approach MAX = 100005adjacent = [[] for i in range(MAX)] visited = [False] * MAX # To store the required nodes startnode = endnode = thirdnode = Nonemaxi, N = -1, None # Parent array to retrace the nodes parent = [None] * MAX # Visited array to prevent DFS # in direction on Diameter path vis = [False] * MAX # DFS function to find the startnode def dfs(u, count): visited[u] = True temp = 0 global startnode, maxi for i in range(0, len(adjacent[u])): if not visited[adjacent[u][i]]: temp += 1 dfs(adjacent[u][i], count + 1) if temp == 0: if maxi < count: maxi = count startnode = u # DFS function to find the endnode of # diameter and maintain the parent array def dfs1(u, count): visited[u] = True temp = 0 global endnode, maxi for i in range(0, len(adjacent[u])): if not visited[adjacent[u][i]]: temp += 1 parent[adjacent[u][i]] = u dfs1(adjacent[u][i], count + 1) if temp == 0: if maxi < count: maxi = count endnode = u # DFS function to find the end node # of the Longest Branch to Diameter def dfs2(u, count): visited[u] = True temp = 0 global thirdnode, maxi for i in range(0, len(adjacent[u])): if (not visited[adjacent[u][i]] and not vis[adjacent[u][i]]): temp += 1 dfs2(adjacent[u][i], count + 1) if temp == 0: if maxi < count: maxi = count thirdnode = u # Function to find the required nodes def findNodes(): # To find start node of diameter dfs(1, 0) global maxi for i in range(0, N+1): visited[i] = False maxi = -1 # To find end node of diameter dfs1(startnode, 0) for i in range(0, N+1): visited[i] = False # x is the end node of diameter x = endnode vis[startnode] = True # Mark all the nodes on diameter # using back tracking while x != startnode: vis[x] = True x = parent[x] maxi = -1 # Find the end node of longest # branch to diameter for i in range(1, N+1): if vis[i]: dfs2(i, 0) # Driver code if __name__ == "__main__": N = 4 adjacent[1].append(2) adjacent[2].append(1) adjacent[1].append(3) adjacent[3].append(1) adjacent[1].append(4) adjacent[4].append(1) findNodes() print("({}, {}, {})".format(startnode, endnode, thirdnode)) # This code is contributed by Rituraj Jain |
(2, 3, 4)
Recommended Posts:
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Sum of all odd nodes in the path connecting two given nodes
- Find maximum among all right nodes in Binary Tree
- Level with maximum number of nodes
- Level with maximum number of nodes using DFS in a N-ary tree
- Maximum number of leaf nodes that can be visited within the given budget
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Print levels with odd number of nodes and even number of nodes
- Find Maximum number possible by doing at-most K swaps
- Sum of the distances from every node to all other nodes is maximum
- Sum of nodes at maximum depth of a Binary Tree
- Maximum sum of nodes in Binary tree such that no two are adjacent
- Sum of nodes at maximum depth of a Binary Tree | Set 2
- Root to leaf path with maximum distinct nodes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rituraj_jain




