Sum of lengths of all paths possible in a given tree
Last Updated :
09 Feb, 2022
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++
#include <bits/stdc++.h>
using namespace std;
const int sz = 1e5;
int n;
vector< int > tree[sz];
int subtree_size[sz];
int vis[sz];
void addEdge( int a, int b)
{
tree[a].push_back(b);
tree[b].push_back(a);
}
int dfs( int node)
{
vis[node] = 1;
subtree_size[node] = 1;
for ( auto child : tree[node]) {
if (!vis[child]) {
subtree_size[node] += dfs(child);
}
}
return subtree_size[node];
}
void contribution( int node, int & ans)
{
vis[node] = true ;
for ( int child : tree[node]) {
if (!vis[child]) {
ans += (subtree_size[child]
* (n - subtree_size[child]));
contribution(child, ans);
}
}
}
int getSum()
{
memset (vis, 0, sizeof (vis));
dfs(0);
int ans = 0;
memset (vis, 0, sizeof (vis));
contribution(0, ans);
return ans;
}
int main()
{
n = 5;
addEdge(0, 1);
addEdge(0, 2);
addEdge(1, 3);
addEdge(1, 4);
cout << getSum();
return 0;
}
|
Java
import java.util.*;
@SuppressWarnings ( "unchecked" )
class GFG{
static int sz = 100005 ;
static int n;
static ArrayList []tree = new ArrayList[sz];
static int []subtree_size = new int [sz];
static int []vis = new int [sz];
static void AddEdge( int a, int b)
{
tree[a].add(b);
tree[b].add(a);
}
static int dfs( int node)
{
vis[node] = 1 ;
subtree_size[node] = 1 ;
for ( int child : (ArrayList<Integer>)tree[node])
{
if (vis[child] == 0 )
{
subtree_size[node] += dfs(child);
}
}
return subtree_size[node];
}
static int contribution( int node, int ans)
{
vis[node] = 1 ;
for ( int child : (ArrayList<Integer>)tree[node])
{
if (vis[child] == 0 )
{
ans += (subtree_size[child] *
(n - subtree_size[child]));
ans = contribution(child, ans);
}
}
return ans;
}
static int getSum()
{
Arrays.fill(vis, 0 );
dfs( 0 );
int ans = 0 ;
Arrays.fill(vis, 0 );
ans = contribution( 0 , ans);
return ans;
}
public static void main(String []args)
{
n = 5 ;
for ( int i = 0 ; i < sz; i++)
{
tree[i] = new ArrayList();
}
AddEdge( 0 , 1 );
AddEdge( 0 , 2 );
AddEdge( 1 , 3 );
AddEdge( 1 , 4 );
System.out.println(getSum());
}
}
|
Python3
sz = 10 * * 5
n = 5
an = 0
tree = [[] for i in range (sz)]
subtree_size = [ 0 ] * sz
vis = [ 0 ] * sz
def addEdge(a, b):
tree[a].append(b)
tree[b].append(a)
def dfs(node):
leaf = True
vis[node] = 1
for child in tree[node]:
if (vis[child] = = 0 ):
leaf = False
dfs(child)
subtree_size[node] + = subtree_size[child]
if leaf:
subtree_size[node] = 1
def contribution(node,ans):
global an
vis[node] = 1
for child in tree[node]:
if (vis[child] = = 0 ):
an + = (subtree_size[child] *
(n - subtree_size[child]))
contribution(child, ans)
def getSum():
for i in range (sz):
vis[i] = 0
dfs( 0 )
ans = 0
for i in range (sz):
vis[i] = 0
contribution( 0 , ans)
return an
n = 5
addEdge( 0 , 1 )
addEdge( 0 , 2 )
addEdge( 1 , 3 )
addEdge( 1 , 4 )
print (getSum())
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static int sz = 100005;
static int n;
static ArrayList []tree = new ArrayList[sz];
static int []subtree_size = new int [sz];
static int []vis = new int [sz];
static void addEdge( int a, int b)
{
tree[a].Add(b);
tree[b].Add(a);
}
static int dfs( int node)
{
vis[node] = 1;
subtree_size[node] = 1;
foreach ( int child in tree[node])
{
if (vis[child] == 0)
{
subtree_size[node] += dfs(child);
}
}
return subtree_size[node];
}
static void contribution( int node, ref int ans)
{
vis[node] = 1;
foreach ( int child in tree[node])
{
if (vis[child] == 0)
{
ans += (subtree_size[child] *
(n - subtree_size[child]));
contribution(child, ref ans);
}
}
}
static int getSum()
{
Array.Fill(vis, 0);
dfs(0);
int ans = 0;
Array.Fill(vis, 0);
contribution(0, ref ans);
return ans;
}
public static void Main()
{
n = 5;
for ( int i = 0; i < sz; i++)
{
tree[i] = new ArrayList();
}
addEdge(0, 1);
addEdge(0, 2);
addEdge(1, 3);
addEdge(1, 4);
Console.Write(getSum());
}
}
|
Javascript
<script>
let sz = 100005;
let n;
let tree = new Array(sz);
let subtree_size = new Array(sz);
let vis = new Array(sz);
function AddEdge(a,b)
{
tree[a].push(b);
tree[b].push(a);
}
function dfs(node)
{
vis[node] = 1;
subtree_size[node] = 1;
for (let child=0;child<tree[node].length;child++)
{
if (vis[tree[node][child]] == 0)
{
subtree_size[node] += dfs(tree[node][child]);
}
}
return subtree_size[node];
}
function contribution(node,ans)
{
vis[node] = 1;
for (let child=0;child<tree[node].length;child++)
{
if (vis[tree[node][child]] == 0)
{
ans += (subtree_size[tree[node][child]] *
(n - subtree_size[tree[node][child]]));
ans = contribution(tree[node][child], ans);
}
}
return ans;
}
function getSum()
{
for (let i=0;i<vis.length;i++)
{
vis[i]=0;
}
dfs(0);
let ans = 0;
for (let i=0;i<vis.length;i++)
{
vis[i]=0;
}
ans = contribution(0, ans);
return ans;
}
n = 5;
for (let i = 0; i < sz; i++)
{
tree[i] = [];
}
AddEdge(0, 1);
AddEdge(0, 2);
AddEdge(1, 3);
AddEdge(1, 4);
document.write(getSum());
</script>
|
Share your thoughts in the comments
Please Login to comment...