Find the root of the sub-tree whose weighted sum is minimum
Last Updated :
12 Jun, 2021
Given a tree, and the weights of all the nodes, the task is to find the root of the sub-tree whose weighted sum is minimum.
Examples:
Input:
Output: 5
Weight of sub-tree for parent 1 = ((-1) + (5) + (-2) + (-1) + (3)) = 4
Weight of sub-tree for parent 2 = ((5) + (-1) + (3)) = 7
Weight of sub-tree for parent 3 = -1
Weight of sub-tree for parent 4 = 3
Weight of sub-tree for parent 5 = -2
Node 5 gives the minimum sub-tree weighted sum.
Approach: Perform dfs on the tree, and for every node calculate the sub-tree weighted sum rooted at the current node then find the minimum sum value for a node.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int ans = 0, mini = INT_MAX;
vector< int > graph[100];
vector< int > weight(100);
void dfs( int node, int parent)
{
for ( int to : graph[node]) {
if (to == parent)
continue ;
dfs(to, node);
weight[node] += weight[to];
}
}
void findMin( int n)
{
for ( int i = 1; i <= n; i++) {
if (mini > weight[i]) {
mini = weight[i];
ans = i;
}
}
}
int main()
{
int n = 5;
weight[1] = -1;
weight[2] = 5;
weight[3] = -1;
weight[4] = 3;
weight[5] = -2;
graph[1].push_back(2);
graph[2].push_back(3);
graph[2].push_back(4);
graph[1].push_back(5);
dfs(1, 1);
findMin(n);
cout << ans;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int ans = 0 , mini = Integer.MAX_VALUE;
@SuppressWarnings ( "unchecked" )
static Vector<Integer>[] graph = new Vector[ 100 ];
static Integer[] weight = new Integer[ 100 ];
static void dfs( int node, int parent)
{
for ( int to : graph[node])
{
if (to == parent)
continue ;
dfs(to, node);
weight[node] += weight[to];
}
}
static void findMin( int n)
{
for ( int i = 1 ; i <= n; i++)
{
if (mini > weight[i])
{
mini = weight[i];
ans = i;
}
}
}
public static void main(String[] args)
{
int n = 5 ;
for ( int i = 0 ; i < 100 ; i++)
graph[i] = new Vector<Integer>();
weight[ 1 ] = - 1 ;
weight[ 2 ] = 5 ;
weight[ 3 ] = - 1 ;
weight[ 4 ] = 3 ;
weight[ 5 ] = - 2 ;
graph[ 1 ].add( 2 );
graph[ 2 ].add( 3 );
graph[ 2 ].add( 4 );
graph[ 1 ].add( 5 );
dfs( 1 , 1 );
findMin(n);
System.out.print(ans);
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int ans = 0, mini = int .MaxValue;
static List< int >[] graph = new List< int >[100];
static int [] weight = new int [100];
static void dfs( int node, int parent)
{
foreach ( int to in graph[node])
{
if (to == parent)
continue ;
dfs(to, node);
weight[node] += weight[to];
}
}
static void findMin( int n)
{
for ( int i = 1; i <= n; i++)
{
if (mini > weight[i])
{
mini = weight[i];
ans = i;
}
}
}
public static void Main(String[] args)
{
int n = 5;
for ( int i = 0; i < 100; i++)
graph[i] = new List< int >();
weight[1] = -1;
weight[2] = 5;
weight[3] = -1;
weight[4] = 3;
weight[5] = -2;
graph[1].Add(2);
graph[2].Add(3);
graph[2].Add(4);
graph[1].Add(5);
dfs(1, 1);
findMin(n);
Console.Write(ans);
}
}
|
Python3
ans = 0
mini = 2 * * 32
graph = [[] for i in range ( 100 )]
weight = [ 0 ] * 100
def dfs(node, parent):
global mini, graph, weight, ans
for to in graph[node]:
if (to = = parent):
continue
dfs(to, node)
weight[node] + = weight[to]
def findMin(n):
global mini, graph, weight, ans
for i in range ( 1 , n + 1 ):
if (mini > weight[i]):
mini = weight[i]
ans = i
n = 5
weight[ 1 ] = - 1
weight[ 2 ] = 5
weight[ 3 ] = - 1
weight[ 4 ] = 3
weight[ 5 ] = - 2
graph[ 1 ].append( 2 )
graph[ 2 ].append( 3 )
graph[ 2 ].append( 4 )
graph[ 1 ].append( 5 )
dfs( 1 , 1 )
findMin(n)
print (ans)
|
Javascript
<script>
let ans = 0;
let mini = Number.MAX_VALUE;
let graph = new Array(100);
let weight = new Array(100);
for (let i = 0; i < 100; i++)
{
graph[i] = [];
weight[i] = 0;
}
function dfs(node, parent)
{
for (let to = 0; to < graph[node].length; to++)
{
if (graph[node][to] == parent)
continue
dfs(graph[node][to], node);
weight[node] += weight[graph[node][to]];
}
}
function findMin(n)
{
for (let i = 1; i <= n; i++)
{
if (mini > weight[i])
{
mini = weight[i];
ans = i;
}
}
}
let n = 5;
weight[1] = -1;
weight[2] = 5;
weight[3] = -1;
weight[4] = 3;
weight[5] = -2;
graph[1].push(2);
graph[2].push(3);
graph[2].push(4);
graph[1].push(5);
dfs(1, 1);
findMin(n);
document.write(ans);
</script>
|
Complexity Analysis:
- Time Complexity : O(N).
In dfs, every node of the tree is processed once and hence the complexity due to the dfs is O(N) if there are total N nodes in the tree. Therefore, the time complexity is O(N).
- Auxiliary Space : O(n).
Recursion stack.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...