Advertisement

Count Unreachable Pairs of Nodes in an Undirected Graph - LeetCode 2316 Solution

Count Unreachable Pairs of Nodes in an Undirected Graph - Complete Solution Guide

Count Unreachable Pairs of Nodes in an Undirected Graph is LeetCode problem 2316, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an integer n . There is an undirected graph with n nodes, numbered from 0 to n - 1 . You are given a 2D integer array edges where edges[i] = [a i , b i ] denotes that there exists an undirected edge connecting nodes a i and b i . Return the number of pairs of different nodes that are unreachable from each other . Example 1: Input: n = 3, edges = [[0,1],[0,2],[1,2]] Output: 0 Explanation: There are no pairs of nodes that are unreachable from each other. Therefore, we return 0. Examp

Detailed Explanation

The problem asks us to find the number of pairs of nodes in an undirected graph that are unreachable from each other. We are given the number of nodes `n` and a list of edges `edges`. Two nodes are considered unreachable if there is no path connecting them in the graph. The goal is to count all such pairs of nodes.

Solution Approach

The solution uses the Union-Find algorithm to find connected components in the graph. The algorithm iterates through the edges and merges the nodes connected by each edge into the same connected component. After processing all edges, it iterates through all nodes and calculates the size of each connected component. Then it computes the number of unreachable pairs based on the sizes of the connected components.

Step-by-Step Algorithm

  1. Step 1: Initialize a `parent` array where `parent[i]` represents the parent of node `i`. Initially, each node is its own parent (i.e., each node is in its own connected component). Initialize a `size` array where `size[i]` represents the size of the connected component that node `i` belongs to. Initially, each component has a size of 1.
  2. Step 2: Implement the `find(i)` function, which finds the root (representative) of the connected component that node `i` belongs to. Path compression is used to optimize the find operation by updating the parent of each node along the path to point directly to the root.
  3. Step 3: Implement the `union(i, j)` function, which merges the connected components that nodes `i` and `j` belong to. Union by size is used to optimize the union operation by attaching the smaller tree to the larger tree. This helps to keep the trees relatively flat, which reduces the time complexity of the `find` operation.
  4. Step 4: Iterate through the `edges` array. For each edge `(u, v)`, call the `union(u, v)` function to merge the connected components that nodes `u` and `v` belong to.
  5. Step 5: Initialize `unreachable_pairs` to 0 and `nodes_so_far` to 0.
  6. Step 6: Iterate through all nodes from 0 to `n-1`. For each node `i`, check if `parent[i] == i`. If it is, then node `i` is the root of a connected component.
  7. Step 7: If node `i` is a root, calculate `component_size = size[i]`. Add `component_size * nodes_so_far` to `unreachable_pairs`. Then, add `component_size` to `nodes_so_far`.
  8. Step 8: Return `unreachable_pairs`.

Key Insights

  • Insight 1: We can use the Union-Find algorithm to efficiently determine the connected components in the graph.
  • Insight 2: Once we know the size of each connected component, we can calculate the number of unreachable pairs by multiplying the size of each component with the sum of the sizes of all the previous components.
  • Insight 3: Using path compression and union by size in the Union-Find implementation optimizes the algorithm's performance.

Complexity Analysis

Time Complexity: O(n + e)

Space Complexity: O(n)

Topics

This problem involves: Depth-First Search, Breadth-First Search, Union Find, Graph.

Companies

Asked at: Commvault.