711971. Maximum Connected group

Hard

Maximum Connected Group

Summary

The problem is about finding the maximum connected group in a graph. Given an undirected graph and its adjacency list representation, we need to find the maximum connected subgraph (group) where all nodes are reachable from each other. This problem involves concepts like graphs, connectivity, and traversal algorithms.

Detailed Explanation

To solve this problem, we can use Depth-First Search (DFS) or Breadth-First Search (BFS) algorithm with some modifications to track the connected groups. Here's a step-by-step approach:

  1. Initialize an empty list max_group to store the maximum connected group.
  2. Iterate over each node in the graph using DFS/BFS.
  3. For each node, perform a separate traversal to find all reachable nodes (connected subgraph).
  4. Calculate the size of the current connected subgraph by counting the number of visited nodes.
  5. If the size of the current connected subgraph is greater than the size of max_group, update max_group with the new group.
  6. Continue iterating over each node and updating max_group until all nodes have been processed.

Here's an ASCII art diagram illustrating the concept:

  A -- B -- C
 /      /\
|  D  |
/______\

In this example, the maximum connected group is {A, B, C, D}, which has a size of 4. The algorithm would identify this group and update max_group accordingly.

Time complexity: O(V + E), where V is the number of vertices (nodes) and E is the number of edges. Space complexity: O(V), as we need to store information about each node during traversal.

Optimized Solutions

Java

View on GeeksforGeeks
import java.util.*;

class Node {
    int id;
    List<Node> neighbors = new ArrayList<>();
}

public class Main {
    public static void main(String[] args) {
        // Initialize the graph
        Node A = new Node(1);
        Node B = new Node(2);
        Node C = new Node(3);
        Node D = new Node(4);

        A.neighbors.add(B);
        B.neighbors.add(A);
        B.neighbors.add(C);
        C.neighbors.add(B);
        C.neighbors.add(D);
        D.neighbors.add(C);

        // Find the maximum connected group
        int maxSize = 0;
        List<Node> maxGroup = null;

        for (Node node : new ArrayList<>(Arrays.asList(A, B, C, D))) {
            Set<Node> visited = new HashSet<>();
            findMaxConnectedGroup(node, visited);
            if (visited.size() > maxSize) {
                maxSize = visited.size();
                maxGroup = new ArrayList<>(visited);
            }
        }

        System.out.println("Maximum connected group: " + maxGroup);
    }

    private static void findMaxConnectedGroup(Node node, Set<Node> visited) {
        visited.add(node);

        for (Node neighbor : node.neighbors) {
            if (!visited.contains(neighbor)) {
                findMaxConnectedGroup(neighbor, visited);
            }
        }
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.