计算 Graph 中邻居总和最多为 K 的节点数
给定一个图根和一个值K,任务是找出图中邻居总和小于K的节点数。
例子:
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
方法:给定的问题可以通过在图上使用深度优先搜索来解决。这个想法是使用递归并访问每个节点以检查其邻居节点的总和是否小于K。可以按照以下步骤解决问题:
- 使用递归在图上应用深度优先搜索并使用哈希集来存储图的访问节点
- 在每个节点迭代该节点的邻居并添加它们的总和
- 如果总和大于K ,则增加计数
- 返回计数作为结果
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
class Node
{
public:
vector neighbors;
int val;
Node(int v)
{
val = v;
neighbors = {};
}
};
// Depth first search function to visit
// every node of the graph and check if
// sum of neighbor nodes is less than K
void dfs(
Node *root,
set &visited,
vector &count, int K)
{
// If the current node is
// already visited then return
if (visited.find(root) != visited.end())
return;
// Mark the current node as visited
visited.insert(root);
// Initialize a variable sum to
// calculate sum of neighbors
int sum = 0;
// Iterate through all neighbors
for (Node *n : root->neighbors)
{
sum += n->val;
}
// If sum is less than K then
// increment count by 1
if (sum < K)
count[0] = count[0] + 1;
for (Node *n : root->neighbors)
{
// Visit the neighbor nodes
dfs(n, visited, count, K);
}
}
// Function to find the number of nodes
// in the graph with sum less than K
int nodeSumLessThanK(
Node *root, int K)
{
// Initialize the variable count
// to count the answer
vector count(1, 0);
// Initialize a HashSet to
// store the visited nodes
set visited;
// Apply DFS on the graph
dfs(root, visited, count, K);
// Return the answer stored in count
return count[0];
}
// Driver code
int main()
{
// Initialize the graph
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;
// Call the function
// and print the result
cout << (nodeSumLessThanK(root, K));
return 0;
}
// This code is contributed by Potta Lokesh
Java
// Java implementation for the above approach
import java.io.*;
import java.util.*;
class GFG {
static class Node {
List neighbors;
int val;
public Node(int val)
{
this.val = val;
neighbors = new ArrayList<>();
}
}
// Function to find the number of nodes
// in the graph with sum less than K
public static int nodeSumLessThanK(
Node root, int K)
{
// Initialize the variable count
// to count the answer
int[] count = new int[1];
// Initialize a HashSet to
// store the visited nodes
Set visited = new HashSet<>();
// Apply DFS on the graph
dfs(root, visited, count, K);
// Return the answer stored in count
return count[0];
}
// Depth first search function to visit
// every node of the graph and check if
// sum of neighbor nodes is less than K
public static void dfs(
Node root,
Set visited,
int[] count, int K)
{
// If the current node is
// already visited then return
if (visited.contains(root))
return;
// Mark the current node as visited
visited.add(root);
// Initialize a variable sum to
// calculate sum of neighbors
int sum = 0;
// Iterate through all neighbors
for (Node n : root.neighbors) {
sum += n.val;
}
// If sum is less than K then
// increment count by 1
if (sum < K)
count[0]++;
for (Node n : root.neighbors) {
// Visit the neighbor nodes
dfs(n, visited, count, K);
}
}
// Driver code
public static void main(String[] args)
{
// Initialize the graph
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;
// Call the function
// and print the result
System.out.println(
nodeSumLessThanK(root, K));
}
}
C#
// C# implementation for the above approach
using System;
using System.Collections.Generic;
public class GFG {
class Node {
public List neighbors;
public int val;
public Node(int val)
{
this.val = val;
neighbors = new List();
}
}
// Function to find the number of nodes
// in the graph with sum less than K
static int nodeSumLessThanK(
Node root, int K)
{
// Initialize the variable count
// to count the answer
int[] count = new int[1];
// Initialize a HashSet to
// store the visited nodes
HashSet visited = new HashSet();
// Apply DFS on the graph
dfs(root, visited, count, K);
// Return the answer stored in count
return count[0];
}
// Depth first search function to visit
// every node of the graph and check if
// sum of neighbor nodes is less than K
static void dfs(
Node root,
HashSet visited,
int[] count, int K)
{
// If the current node is
// already visited then return
if (visited.Contains(root))
return;
// Mark the current node as visited
visited.Add(root);
// Initialize a variable sum to
// calculate sum of neighbors
int sum = 0;
// Iterate through all neighbors
foreach (Node n in root.neighbors) {
sum += n.val;
}
// If sum is less than K then
// increment count by 1
if (sum < K)
count[0]++;
foreach (Node n in root.neighbors) {
// Visit the neighbor nodes
dfs(n, visited, count, K);
}
}
// Driver code
public static void Main(String[] args)
{
// Initialize the graph
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;
// Call the function
// and print the result
Console.WriteLine(
nodeSumLessThanK(root, K));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
3
时间复杂度: O(V + E),其中 V 是顶点数,E 是图中的边数
辅助空间: O(V)