Maximum product of a pair of nodes from largest connected component in a Graph

Given an undirected weighted graph G consisting of N vertices and M edges, and two arrays Edges[][2] and Weight[] consisting of M edges of the graph and weights of each edge respectively, the task is to find the maximum product of any two vertices of the largest connected component of the graph, formed by connecting all edges with the same weight.
Examples:
Input: N = 4, Edges[][] = {{1, 2}, {1, 2}, {2, 3}, {2, 3}, {2, 4}}, Weight[] = {1, 2, 1, 3, 3}
Output: 12
Explanation:
- Components of edges of weight 1, 1 ? 2 ? 3. The maximum product of any two vertices of this component is 6.
- Components of edges of weight 2, 1 ? 2. The maximum product of any two vertices of this component is 2.
- Components of edges of weight 3, 4 ? 2 ? 3. The maximum product of any two vertices of this component is 12.
Therefore, the maximum product among all the connected components of size 3 (which is maximum) is 12.
Input: N = 5, Edges[][] = {{1, 5}, {2, 5}, {3, 5}, {4, 5}, {1, 2}, {2, 3}, {3, 4}}, Weight[] = {1, 1, 1, 1, 2, 2, 2}
Output: 20
Approach: The given problem can be solved by performing the DFS traversal on the given graph and maximize the product of the first and second maximum node for all the connected components of the same weight. Follow the steps below to solve the problem:
- Store all the edges corresponding to all the unique weight in a map M.
- Initialize a variable, say res as 0 to store the maximum product of any two nodes of the connected components of the same weights.
- Traverse the map and for each key as weight create a graph by connecting all the edges mapped with the particular weight and perform the following operations:
- Find the value of the maximum(say M1) and the second maximum(say M2) node’s value and the size of all the connected components of the graph by performing the DFS Traversal on the created graph.
- Update the value of res to the maximum of res, M1, and M2 if the size of the currently connected components is at least the largest size connected component found previously.
- After completing the above steps, print the value of res as the maximum product.
Below is the implementation of the above approach:
C++
// C++ program for the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Stores the first and second largest// element in a connected componentint Max, sMax;Â
// Stores the count of nodes// in the connected componentsint cnt = 0;Â
// Function to perform DFS Traversal// on a given graph and find the first// and the second largest elementsvoid dfs(int u, int N, vector<bool>& vis,         vector<vector<int> >& adj){    // Update the maximum value    if (u > Max) {        sMax = Max;        Max = u;    }Â
    // Update the second max value    else if (u > sMax) {        sMax = u;    }Â
    // Increment size of component    cnt++;Â
    // Mark current node visited    vis[u] = true;Â
    // Traverse the adjacent nodes    for (auto to : adj[u]) {Â
        // If to is not already visited        if (!vis[to]) {            dfs(to, N, vis, adj);        }    }Â
    return;}Â
// Function to find the maximum// product of a connected componentint MaximumProduct(    int N, vector<pair<int, int> > Edge,    vector<int> wt){    // Stores the count of edges    int M = wt.size();Â
    // Stores all the edges mapped    // with a particular weight    unordered_map<int,                  vector<pair<int, int> > >        mp;Â
    // Update the map mp    for (int i = 0; i < M; i++)        mp[wt[i]].push_back(Edge[i]);Â
    // Stores the result    int res = 0;Â
    // Traverse the map mp    for (auto i : mp) {Â
        // Stores the adjacency list        vector<vector<int> > adj(N + 1);Â
        // Stores the edges of        // a particular weight        vector<pair<int, int> > v = i.second;Â
        // Traverse the vector v        for (int j = 0; j < v.size(); j++) {Â
            int U = v[j].first;            int V = v[j].second;Â
            // Add an edge            adj[U].push_back(V);            adj[V].push_back(U);        }Â
        // Stores if a vertex        // is visited or not        vector<bool> vis(N + 1, 0);Â
        // Stores the maximum        // size of a component        int cntMax = 0;Â
        // Iterate over the range [1, N]        for (int u = 1; u <= N; u++) {Â
            // Assign Max, sMax, count = 0            Max = sMax = cnt = 0;Â
            // If vertex u is not visited            if (!vis[u]) {Â
                dfs(u, N, vis, adj);Â
                // If cnt is greater                // than cntMax                if (cnt > cntMax) {Â
                    // Update the res                    res = Max * sMax;                    cntMax = cnt;                }Â
                // If already largest                // connected component                else if (cnt == cntMax) {Â
                    // Update res                    res = max(res, Max * sMax);                }            }        }    }Â
    // Return res    return res;}Â
// Driver Codeint main(){    int N = 5;    vector<pair<int, int> > Edges        = { { 1, 2 }, { 2, 5 }, { 3, 5 }, { 4, 5 }, { 1, 2 }, { 2, 3 }, { 3, 4 } };Â
    vector<int> Weight = { 1, 1, 1, 1,                           2, 2, 2 };    cout << MaximumProduct(N, Edges, Weight);Â
    return 0;} |
Java
// Java code to implement the approachimport java.util.*;class GFG {Â
  // Stores the first and second largest  // element in a connected component  static int Max, sMax, cnt;Â
Â
  // Function to perform DFS Traversal  // on a given graph and find the first  // and the second largest elements  static void dfs(int u, int N, List<Boolean> vis,                  List<List<Integer> > adj) {Â
    // Update the maximum value    if (u > Max) {      sMax = Max;      Max = u;    } Â
    // Update the second max value    else if (u > sMax) {      sMax = u;    }Â
    // Increment size of component    cnt++;Â
    // Mark current node visited    vis.set(u, true);Â
    // Traverse the adjacent nodes    for (int i = 0; i < adj.get(u).size(); i++) {      int to = adj.get(u).get(i);Â
      // If to is not already visited      if (!vis.get(to)) {        dfs(to, N, vis, adj);      }    }  }Â
  // Function to find the maximum  // product of a connected component  static int MaximumProduct(int N, List<int[]> Edge, List<Integer> wt) {    int M = wt.size();Â
    // Stores all the edges mapped    // with a particular weight    Map<Integer, List<int[]> > mp = new HashMap<>();    for (int i = 0; i < M; i++) {      if (!mp.containsKey(wt.get(i))) {        mp.put(wt.get(i), new ArrayList<>());      }      mp.get(wt.get(i)).add(Edge.get(i));    }    List<Integer> keys = new ArrayList<>(mp.keySet());    Collections.sort(keys, Collections.reverseOrder());Â
    // Stores the result    int res = 0;Â
    // Traverse the map mp    for (Integer i : keys) {      List<List<Integer> > adj = new ArrayList<>();      for (int j = 0; j <= N; j++)        adj.add(new ArrayList<>());Â
      // Stores the edges of      // a particular weight      List<int[]> v = mp.get(i);Â
      // Traverse the vector v      for (int j = 0; j < v.size(); j++) {        int U = v.get(j)[0];        int V = v.get(j)[1];        adj.get(U).add(V);        adj.get(V).add(U);      }Â
      // Stores the maximum      // size of a component      List<Boolean> vis = new ArrayList<>();      for (int j = 0; j <= N; j++)        vis.add(false);      int cntMax = 0;      for (int u = 1; u <= N; u++) {Â
        // Assign Max, sMax, count = 0        Max = 0;        sMax = 0;        cnt = 0;Â
        // If vertex u is not visited        if (!vis.get(u)) {          dfs(u, N, vis, adj);Â
          // If cnt is greater          // than cntMax          if (cnt > cntMax) {Â
            // Update the res            res = Max * sMax;            cntMax = cnt;          } Â
          // If already largest          // connected component          else if (cnt == cntMax) {Â
            // Update res            res = Math.max(res, Max * sMax);          }        }      }    }    return res;  }Â
Â
  // Driver code  public static void main(String[] args) {    int N = 5;    List<int[]> Edges = new ArrayList<>();    Edges.add(new int[]{1, 2});    Edges.add(new int[]{2, 5});    Edges.add(new int[]{3, 5});    Edges.add(new int[]{4, 5});    Edges.add(new int[]{1, 2});    Edges.add(new int[]{2, 3});    Edges.add(new int[]{3, 4});Â
    List<Integer> Weights = Arrays.asList(1, 1, 1, 1, 2, 2, 2);Â
    System.out.println(MaximumProduct(N, Edges, Weights));  }}Â
// This code is contributed by phasing17 |
Python3
# Function to perform DFS Traversal# on a given graph and find the first# and the second largest elementsdef dfs(u, N, vis, adj):         global Max    global sMax    global cnt         # Update the maximum value    if u > Max:        sMax = Max        Max = u    # Update the second max value    elif u > sMax:        sMax = u    # Increment size of component    cnt+=1Â
    # Mark current node visited    vis[u] = True         # Traverse the adjacent nodes    for to in adj[u]:        # If to is not already visited        if not vis[to]:            dfs(to, N, vis, adj)Â
def maximumProduct(N, Edge, wt):    # Stores the count of edges    M = len(wt)Â
    # Stores all the edges mapped    # with a particular weight    mp = {}Â
    # Update the map mp    for i in range(M):        weight = wt[i]        if weight not in mp:            mp[weight] = [Edge[i]]        else:            mp[weight].append(Edge[i])Â
    # Stores the result    res = 0         keys = list(mp.keys())    keys.sort(reverse=True)    # Traverse the map mp    for key in keys:        weight = mp[key]        # Stores the adjacency list        adj = [ [] for _ in range(N + 1)]Â
        # Stores the edges of        # a particular weight        v = weightÂ
        # Traverse the vector v        for j in range(len(v)):            U, V = v[j]Â
            # Add an edge            adj[U].append(V)            adj[V].append(U)Â
        # Stores if a vertex        # is visited or not        vis = [False for _ in range(N + 1)]Â
        # Stores the maximum        # size of a component        cntMax = 0Â
        # Iterate over the range [1, N]        for u in range(1, N+1):                         global Max            global sMax            global cntÂ
            # Assign Max, sMax, count = 0            Max = 0            sMax = 0            cnt = 0;Â
            # If vertex u is not visited            if not vis[u]:                dfs(u, N, vis, adj);Â
                # If cnt is greater                # than cntMax                if cnt > cntMax:                    # Update the res                    res = Max * sMax;                    cntMax = cnt;                 Â
                # If already largest                # connected component                elif cnt == cntMax:Â
                    # Update res                    res = max(res, Max * sMax);    return resÂ
# driver codeN = 5;Edges = [[1, 2], [2, 5], [3, 5], [4, 5], [1, 2], [2, 3], [3, 4]];Weight = [1, 1, 1, 1, 2, 2, 2];print(maximumProduct(N, Edges, Weight));Â
# This code is contributed by phasing17. |
Javascript
// JavaScript implementation of the approachÂ
function dfs(u, N, vis, adj) {    // Update the maximum value    if (u > Max) {        sMax = Max;        Max = u;    }Â
    // Update the second max value    else if (u > sMax) {        sMax = u;    }Â
    // Increment size of component    cnt++;Â
    // Mark current node visited    vis[u] = true;         // Traverse the adjacent nodes    for (const to of adj[u]) {        // If to is not already visited        if (!vis[to]) {            dfs(to, N, vis, adj);        }    }}Â
function maximumProduct(N, Edge, wt) {         // Stores the count of edges    const M = wt.length;Â
    // Stores all the edges mapped    // with a particular weight    const mp = {};Â
    // Update the map mp    for (let i = 0; i < M; i++) {        const weight = wt[i];        if (!mp.hasOwnProperty(weight)) {            mp[weight] = [Edge[i]];        } else {            mp[weight].push(Edge[i]);        }    }Â
    // Stores the result    let res = 0;         let keys = Object.keys(mp)    keys.sort(function(a, b)    {        return -a + b;    })    // Traverse the map mp    for (const key of keys) {        let weight = mp[key]        // Stores the adjacency list        const adj = new Array(N + 1);        for (var i = 0; i <= N; i++)            adj[i] = []Â
        // Stores the edges of        // a particular weight        const v = weight;Â
        // Traverse the vector v        for (let j = 0; j < v.length; j++) {Â
            const U = v[j][0];            const V = v[j][1];Â
            // Add an edge            let l1 = adj[U]            l1.push(V)            adj[U] = l1                         let l2 = adj[V]            l2.push(U)            adj[V] = l2        }Â
        // Stores if a vertex        // is visited or not        const vis = new Array(N + 1).fill(false)Â
        // Stores the maximum        // size of a component        let cntMax = 0;Â
        // Iterate over the range [1, N]        for (let u = 1; u <= N; u++) {Â
            // Assign Max, sMax, count = 0            Max = 0            sMax = 0            cnt = 0;Â
            // If vertex u is not visited            if (!vis[u]) {                dfs(u, N, vis, adj);Â
                // If cnt is greater                // than cntMax                if (cnt > cntMax) {                    // Update the res                    res = Max * sMax;                    cntMax = cnt;                }Â
                // If already largest                // connected component                else if (cnt == cntMax) {Â
                    // Update res                    res = Math.max(res, Max * sMax);                }            }        }    }    return res}Â
// driver codeconst N = 5;const Edges = [[1, 2], [2, 5], [3, 5], [4, 5], [1, 2], [2, 3], [3, 4]];const Weight = [1, 1, 1, 1, 2, 2, 2];console.log(maximumProduct(N, Edges, Weight)); |
C#
// C# code to implement the approachÂ
using System;using System.Collections.Generic;using System.Linq;Â
class MainClass {    // Stores the first and second largest    // element in a connected component    static int Max, sMax;Â
    // Stores the count of nodes    // in the connected components    static int cnt = 0;Â
    // Function to perform DFS Traversal    // on a given graph and find the first    // and the second largest elements    static void dfs(int u, int N, List<bool> vis,                    List<List<int> > adj)    {        // Update the maximum value        if (u > Max) {            sMax = Max;            Max = u;        }Â
        // Update the second max value        else if (u > sMax) {            sMax = u;        }Â
        // Increment size of component        cnt++;Â
        // Mark current node visited        vis[u] = true;Â
        // Traverse the adjacent nodes        for (int i = 0; i < adj[u].Count; i++) {            int to = adj[u][i];Â
            // If to is not already visited            if (!vis[to]) {                dfs(to, N, vis, adj);            }        }Â
        return;    }Â
    // Function to find the maximum    // product of a connected component    static int MaximumProduct(int N,                              List<Tuple<int, int> > Edge,                              List<int> wt)    {        // Stores the count of edges        int M = wt.Count;Â
        // Stores all the edges mapped        // with a particular weight        Dictionary<int, List<Tuple<int, int> > > mp            = new Dictionary<int,                             List<Tuple<int, int> > >();Â
        // Update the map mp        for (int i = 0; i < M; i++) {            if (!mp.ContainsKey(wt[i])) {                mp.Add(wt[i], new List<Tuple<int, int> >());            }            mp[wt[i]].Add(Edge[i]);        }Â
        var keys = mp.Keys.ToList();        keys.Sort();        keys.Reverse();Â
        // Stores the result        int res = 0;Â
        // Traverse the map mp        foreach(var i in keys)        {Â
            // Stores the adjacency list            List<List<int> > adj = new List<List<int> >();            for (int j = 0; j <= N; j++)                adj.Add(new List<int>());Â
            // Stores the edges of            // a particular weight            List<Tuple<int, int> > v = mp[i];Â
            // Traverse the vector v            for (int j = 0; j < v.Count; j++) {Â
                int U = v[j].Item1;                int V = v[j].Item2;Â
                // Add an edge                adj[U].Add(V);                adj[V].Add(U);            }Â
            // Stores if a vertex            // is visited or not            List<bool> vis = new List<bool>();            for (int j = 0; j <= N; j++)                vis.Add(false);Â
            // Stores the maximum            // size of a component            int cntMax = 0;Â
            // Iterate over the range [1, N]            for (int u = 1; u <= N; u++) {Â
                // Assign Max, sMax, count = 0                Max = 0;                sMax = 0;                cnt = 0;Â
                // If vertex u is not visited                if (!vis[u]) {Â
                    dfs(u, N, vis, adj);Â
                    // If cnt is greater                    // than cntMax                    if (cnt > cntMax) {Â
                        // Update the res                        res = Max * sMax;                        cntMax = cnt;                    }Â
                    // If already largest                    // connected component                    else if (cnt == cntMax) {Â
                        // Update res                        res = Math.Max(res, Max * sMax);                    }                }            }        }Â
        // Return res        return res;    }Â
    // Driver Code    public static void Main(string[] args)    {        int N = 5;        List<Tuple<int, int> > Edges            = new List<Tuple<int, int> >();        Edges.Add(Tuple.Create(1, 2));        Edges.Add(Tuple.Create(2, 5));        Edges.Add(Tuple.Create(3, 5));        Edges.Add(Tuple.Create(4, 5));        Edges.Add(Tuple.Create(1, 2));        Edges.Add(Tuple.Create(2, 3));        Edges.Add(Tuple.Create(3, 4));Â
        List<int> Weight            = new List<int>{ 1, 1, 1, 1, 2, 2, 2 };        Console.WriteLine(MaximumProduct(N, Edges, Weight));    }}Â
// This code is contributed by phasing17 |
20
Â
Time Complexity: O(N2 * log N + M)
Auxiliary Space: O(N2)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




