Count the number of nodes in a Graph whose sum of neighbors is at most K
Given a graph root, and a value K, the task is to find the number of nodes in the graph whose sum of neighbors is less than K.
Example:
Input: root = 8 K = 14
/ \
2—3 7—3
/ \ \
5 6 0
\ \ / \
1 2 6 9
Output: 10
Explanation: Nodes with sum of neighbors less than 14 are the nodes with value 8, 7, 6, 9, 3(rightmost), 2, 5, 1, 6, 2
Input: root = 2 K = 5
/ \
3 1
/ \
5 6
Output: 3
Approach: The given problem can be solved by using the depth-first search on the graph. The idea is to use recursion and visit every node to check the sum of its neighbor node is less than K. Below steps can be followed to solve the problem:
- Use recursion to apply depth-first search on the graph and use a hashset to store the visited nodes of the graph
- At every node iterate through the neighbors of that node and add their sum
- If the sum is greater than K then increment the count
- Return the count as the result
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Node
{
public :
vector<Node *> neighbors;
int val;
Node( int v)
{
val = v;
neighbors = {};
}
};
void dfs(
Node *root,
set<Node *> &visited,
vector< int > &count, int K)
{
if (visited.find(root) != visited.end())
return ;
visited.insert(root);
int sum = 0;
for (Node *n : root->neighbors)
{
sum += n->val;
}
if (sum < K)
count[0] = count[0] + 1;
for (Node *n : root->neighbors)
{
dfs(n, visited, count, K);
}
}
int nodeSumLessThanK(
Node *root, int K)
{
vector< int > count(1, 0);
set<Node *> visited;
dfs(root, visited, count, K);
return count[0];
}
int main()
{
Node *root = new Node(2);
root->neighbors.push_back( new Node(3));
root->neighbors.push_back( new Node(1));
root->neighbors[0]->neighbors.push_back( new Node(5));
root->neighbors[1]->neighbors.push_back( new Node(6));
int K = 5;
cout << (nodeSumLessThanK(root, K));
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static class Node {
List<Node> neighbors;
int val;
public Node( int val)
{
this .val = val;
neighbors = new ArrayList<>();
}
}
public static int nodeSumLessThanK(
Node root, int K)
{
int [] count = new int [ 1 ];
Set<Node> visited = new HashSet<>();
dfs(root, visited, count, K);
return count[ 0 ];
}
public static void dfs(
Node root,
Set<Node> visited,
int [] count, int K)
{
if (visited.contains(root))
return ;
visited.add(root);
int sum = 0 ;
for (Node n : root.neighbors) {
sum += n.val;
}
if (sum < K)
count[ 0 ]++;
for (Node n : root.neighbors) {
dfs(n, visited, count, K);
}
}
public static void main(String[] args)
{
Node root = new Node( 2 );
root.neighbors.add( new Node( 3 ));
root.neighbors.add( new Node( 1 ));
root.neighbors.get( 0 )
.neighbors.add( new Node( 5 ));
root.neighbors.get( 1 )
.neighbors.add( new Node( 6 ));
int K = 5 ;
System.out.println(
nodeSumLessThanK(root, K));
}
}
|
Python3
class Node:
def __init__( self , v):
self .val = v
self .neighbors = []
def dfs(root, visited, count, K):
if root in visited:
return
visited.add(root)
sum = 0
for n in root.neighbors:
sum + = n.val
if sum < K:
count[ 0 ] + = 1
for n in root.neighbors:
dfs(n, visited, count, K)
def nodeSumLessThanK(root, K):
count = [ 0 ]
visited = set ()
dfs(root, visited, count, K)
return count[ 0 ]
root = Node( 2 )
root.neighbors.append(Node( 3 ))
root.neighbors.append(Node( 1 ))
root.neighbors[ 0 ].neighbors.append(Node( 5 ))
root.neighbors[ 1 ].neighbors.append(Node( 6 ))
K = 5
print (nodeSumLessThanK(root, K))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
class Node {
public List<Node> neighbors;
public int val;
public Node( int val)
{
this .val = val;
neighbors = new List<Node>();
}
}
static int nodeSumLessThanK(
Node root, int K)
{
int [] count = new int [1];
HashSet<Node> visited = new HashSet<Node>();
dfs(root, visited, count, K);
return count[0];
}
static void dfs(
Node root,
HashSet<Node> visited,
int [] count, int K)
{
if (visited.Contains(root))
return ;
visited.Add(root);
int sum = 0;
foreach (Node n in root.neighbors) {
sum += n.val;
}
if (sum < K)
count[0]++;
foreach (Node n in root.neighbors) {
dfs(n, visited, count, K);
}
}
public static void Main(String[] args)
{
Node root = new Node(2);
root.neighbors.Add( new Node(3));
root.neighbors.Add( new Node(1));
root.neighbors[0]
.neighbors.Add( new Node(5));
root.neighbors[1]
.neighbors.Add( new Node(6));
int K = 5;
Console.WriteLine(
nodeSumLessThanK(root, K));
}
}
|
Javascript
<script>
class Node {
constructor(v) {
this .val = v;
this .neighbors = [];
}
};
function dfs(root, visited, count, K) {
if (visited.has(root))
return ;
visited.add(root);
let sum = 0;
for (let n of root.neighbors) {
sum += n.val;
}
if (sum < K)
count[0] = count[0] + 1;
for (let n of root.neighbors) {
dfs(n, visited, count, K);
}
}
function nodeSumLessThanK(root, K) {
let count = new Array(1).fill(0);
let visited = new Set();
dfs(root, visited, count, K);
return count[0];
}
let root = new Node(2);
root.neighbors.push( new Node(3));
root.neighbors.push( new Node(1));
root.neighbors[0].neighbors.push( new Node(5));
root.neighbors[1].neighbors.push( new Node(6));
let K = 5;
document.write(nodeSumLessThanK(root, K));
</script>
|
Time Complexity: O(V + E), where V is the number of vertices and E is the number of edges in the graph
Auxiliary Space: O(V)
Last Updated :
09 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...