Maximum sum of a Node with any number of neighbours in given Graph
Last Updated :
06 Mar, 2023
Given an array vals[] of size N, where each element represents the value of each node (0 ? index ? N-1) and an array Edges[] representing the connections between the nodes. The task is to find a node that is having the maximum sum of itself with any number of adjacent nodes.
Examples:
Input: vals[] = {1, 2, 3, 4, 10, -10, -20}, edges[] = {[0, 1], [1, 2], [1, 3], [3, 4], [3, 5], [3, 6]}
Example 1
Output: 16
Explanation: The subgraph with the maximum sum is centered at node number 3 and includes its neighbors 1 and 4. It can be shown it is not possible to get a subgraph with a sum greater than 16.
Input: vals[] = [-5], edges[] = []
Output: -5
Approach: The problem can be solved based on the following idea:
This problem can be solved using Greedy Algorithm, and the concept of Graph. Just calculate the sum of vertices for each node having value greater than 0. And calculate the node having maximum value.
Follow the steps for the implementation of the above approach:
- Create a NodeSum list and add values of the node’s neighbours that are having a positive value.
- Initialise ans as minimum possible Integer.
- For every node update ans as the maximum of the current ans value and the sum of positive neighbor nodes of the current node and value of the current node.
- In the end, we will get the desired answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxMasterGraphSum(vector< int >& val,
vector<vector< int > >& edges)
{
vector< int > NodeSum(val.size());
for ( int i = 0; i < edges.size(); i++) {
if (val[edges[i][0]] > 0) {
NodeSum[edges[i][1]] += val[edges[i][0]];
}
if (val[edges[i][1]] > 0) {
NodeSum[edges[i][0]] += val[edges[i][1]];
}
}
int ans = INT_MIN;
for ( int i = 0; i < NodeSum.size(); i++) {
ans = max(ans, NodeSum[i] + val[i]);
}
return ans;
}
int main()
{
vector< int > vals = { 1, 2, 3, 4, 10, -10, -20 };
vector<vector< int > > edges
= { { 0, 1 }, { 1, 2 }, { 1, 3 }, { 3, 4 }, { 3, 5 }, { 3, 6 } };
cout << maxMasterGraphSum(vals, edges) << endl;
return 0;
}
|
Python3
import sys
def maxMasterGraphSum(val, edges):
NodeSum = [ 0 ] * len (val)
for i in range ( len (edges)):
if val[edges[i][ 0 ]] > 0 :
NodeSum[edges[i][ 1 ]] + = val[edges[i][ 0 ]]
if val[edges[i][ 1 ]] > 0 :
NodeSum[edges[i][ 0 ]] + = val[edges[i][ 1 ]]
ans = - sys.maxsize
for i in range ( len (NodeSum)):
ans = max (ans, NodeSum[i] + val[i])
return ans
if __name__ = = '__main__' :
vals = [ 1 , 2 , 3 , 4 , 10 , - 10 , - 20 ]
edges = [[ 0 , 1 ], [ 1 , 2 ], [ 1 , 3 ], [ 3 , 4 ], [ 3 , 5 ], [ 3 , 6 ]]
print (maxMasterGraphSum(vals, edges))
|
Javascript
function maxMasterGraphSum( val, edges)
{
let NodeSum= new Array(val.length).fill(0);
for (let i = 0; i < edges.length; i++) {
if (val[edges[i][0]] > 0) {
NodeSum[edges[i][1]] += val[edges[i][0]];
}
if (val[edges[i][1]] > 0) {
NodeSum[edges[i][0]] += val[edges[i][1]];
}
}
let ans = Number.MIN_SAFE_INTEGER;
for (let i = 0; i < NodeSum.length; i++) {
ans = Math.max(ans, NodeSum[i] + val[i]);
}
return ans;
}
let vals = [ 1, 2, 3, 4, 10, -10, -20 ];
let edges = [ [ 0, 1 ], [ 1, 2 ], [ 1, 3 ], [ 3, 4 ], [ 3, 5 ], [ 3, 6 ] ];
console.log(maxMasterGraphSum(vals, edges));
|
Java
import java.util.*;
public class GFG {
static int maxMasterGraphSum(List<Integer> val, List<List<Integer>> edges) {
int [] nodeSum = new int [val.size()];
for (List<Integer> edge : edges) {
if (val.get(edge.get( 0 )) > 0 ) {
nodeSum[edge.get( 1 )] += val.get(edge.get( 0 ));
}
if (val.get(edge.get( 1 )) > 0 ) {
nodeSum[edge.get( 0 )] += val.get(edge.get( 1 ));
}
}
int ans = Integer.MIN_VALUE;
for ( int i = 0 ; i < nodeSum.length; i++) {
ans = Math.max(ans, nodeSum[i] + val.get(i));
}
return ans;
}
public static void main(String[] args) {
List<Integer> vals = Arrays.asList( 1 , 2 , 3 , 4 , 10 , - 10 , - 20 );
List<List<Integer>> edges = Arrays.asList(
Arrays.asList( 0 , 1 ),
Arrays.asList( 1 , 2 ),
Arrays.asList( 1 , 3 ),
Arrays.asList( 3 , 4 ),
Arrays.asList( 3 , 5 ),
Arrays.asList( 3 , 6 )
);
System.out.println(maxMasterGraphSum(vals, edges));
}
}
|
C#
using System;
using System.Collections.Generic;
public class MaxMasterGraphSum {
public static int Calculate(List< int > val, List<List< int >> edges) {
List< int > nodeSum = new List< int >();
for ( int i = 0; i < val.Count; i++) {
nodeSum.Add(0);
}
for ( int i = 0; i < edges.Count; i++) {
if (val[edges[i][0]] > 0) {
nodeSum[edges[i][1]] += val[edges[i][0]];
}
if (val[edges[i][1]] > 0) {
nodeSum[edges[i][0]] += val[edges[i][1]];
}
}
int ans = int .MinValue;
for ( int i = 0; i < nodeSum.Count; i++) {
ans = Math.Max(ans, nodeSum[i] + val[i]);
}
return ans;
}
public static void Main() {
List< int > vals = new List< int >{ 1, 2, 3, 4, 10, -10, -20 };
List<List< int >> edges = new List<List< int >> {
new List< int >{ 0, 1 },
new List< int >{ 1, 2 },
new List< int >{ 1, 3 },
new List< int >{ 3, 4 },
new List< int >{ 3, 5 },
new List< int >{ 3, 6 }
};
Console.WriteLine(Calculate(vals, edges));
}
}
|
Time Complexity: O(V+E) where V is the number of vertices and E is the number of edges
Auxiliary Space: O(V)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...