Given a tree with N nodes, the task is to find the sum of lengths of all the paths. Path length for two nodes in the tree is the number of edges on the path and for two adjacent nodes in the tree, the length of the path is 1.
Examples:
Input:
0
/ \
1 2
/ \
3 4
Output: 18
Paths of length 1 = (0, 1), (0, 2), (1, 3), (1, 4) = 4
Paths of length 2 = (0, 3), (0, 4), (1, 2), (3, 4) = 4
Paths of length 3 = (3, 2), (4, 2) = 2
The sum of lengths of all paths =
(4 * 1) + (4 * 2) + (2 * 3) = 18
Input:
0
/
1
/
2
Output: 4
Naive approach: Check all possible paths and then add them to compute the final result. The complexity of this approach will be O(n2).
Efficient approach: It can be noted that each edge in a tree is a bridge. Hence that edge is going to be present in every path possible between the two subtrees that the edge connects.
For example, the edge (1 – 0) is present in every path possible between {1, 3, 4} and {0, 2}, (1 – 0) is used for 6 times that is size of the subtree {1, 3, 4} multiplied by the size of the subtree {0, 2}. So for each edge, compute how many times that edge is going to be considered for the paths going over it. DFS can be used to store the size of the subtree and the contribution of all edges can be computed with another dfs. The complexity of this approach will be O(n).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int sz = 1e5; // Number of vertices int n; // Adjacency list representation // of the tree vector<int> tree[sz]; // Array that stores the subtree size int subtree_size[sz]; // Array to mark all the // vertices which are visited int vis[sz]; // Utility function to create an // edge between two vertices void addEdge(int a, int b) { // Add a to b's list tree[a].push_back(b); // Add b to a's list tree[b].push_back(a); } // Function to calculate the subtree size int dfs(int node) { // Mark visited vis[node] = 1; subtree_size[node] = 1; // For every adjacent node for (auto child : tree[node]) { // If not already visited if (!vis[child]) { // Recursive call for the child subtree_size[node] += dfs(child); } } return subtree_size[node]; } // Function to calculate the // contribution of each edge void contribution(int node, int& ans) { // Mark current node as visited vis[node] = true; // For every adjacent node for (int child : tree[node]) { // If it is not already visisted if (!vis[child]) { ans += (subtree_size[child] * (n - subtree_size[child])); contribution(child, ans); } } } // Function to return the required sum int getSum() { // First pass of the dfs memset(vis, 0, sizeof(vis)); dfs(0); // Second pass int ans = 0; memset(vis, 0, sizeof(vis)); contribution(0, ans); return ans; } // Driver code int main() { n = 5; addEdge(0, 1); addEdge(0, 2); addEdge(1, 3); addEdge(1, 4); cout << getSum(); return 0; } |
Python3
# Python3 implementation of the approach sz = 10**5 # Number of vertices n = 5an = 0 # Adjacency list representation # of the tree tree = [[] for i in range(sz)] # Array that stores the subtree size subtree_size = [0] * sz # Array to mark all the # vertices which are visited vis = [0] * sz # Utility function to create an # edge between two vertices def addEdge(a, b): # Add a to b's list tree[a].append(b) # Add b to a's list tree[b].append(a) # Function to calculate the subtree size def dfs(node): leaf = True # Mark visited vis[node] = 1 # For every adjacent node for child in tree[node]: # If not already visited if (vis[child] == 0): leaf = False dfs(child) # Recursive call for the child subtree_size[node] += subtree_size[child] if leaf: subtree_size[node] = 1 # Function to calculate the # contribution of each edge def contribution(node,ans): global an # Mark current node as visited vis[node] = 1 # For every adjacent node for child in tree[node]: # If it is not already visisted if (vis[child] == 0): an += (subtree_size[child] * (n - subtree_size[child])) contribution(child, ans) # Function to return the required sum def getSum(): # First pass of the dfs for i in range(sz): vis[i] = 0 dfs(0) # Second pass ans = 0 for i in range(sz): vis[i] = 0 contribution(0, ans) return an # Driver code n = 5 addEdge(0, 1) addEdge(0, 2) addEdge(1, 3) addEdge(1, 4) print(getSum()) # This code is contributed by Mohit Kumar |
18
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.
Recommended Posts:
- Root to leaf paths having equal lengths in a Binary Tree
- Count number of times each Edge appears in all possible paths of a given Tree
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Count all k-sum paths in a Binary Tree
- Remove nodes from Binary Tree such that sum of all remaining root-to-leaf paths is atleast K
- Print all k-sum paths in a binary tree
- Print all the paths from root, with a specified sum in Binary tree
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Given a binary tree, print all root-to-leaf paths
- Count all possible paths from top left to bottom right of a mXn matrix
- Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal
- Count unique paths with given sum in an N-ary Tree
- Maximum subsequence sum obtained by concatenating disjoint subarrays whose lengths are prime
- Sum of cost of all paths to reach a given cell in a Matrix
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Count of paths in given Binary Tree with odd bitwise AND for Q queries
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Queries to find the count of shortest paths in a Tree that contains a given edge
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 : mohit kumar 29

