Count Pairs Of Nodes - Problem
Graph Connectivity Analysis: You're given an undirected graph with
For each query, you need to count how many pairs of nodes have a combined "incident count" that exceeds a given threshold. The incident count for a pair of nodes
Key Points:
Goal: Return an array where each element corresponds to the answer for each query - the number of node pairs whose combined connectivity exceeds the query threshold.
n nodes and a list of edges. Your task is to analyze the connectivity patterns by answering multiple queries.For each query, you need to count how many pairs of nodes have a combined "incident count" that exceeds a given threshold. The incident count for a pair of nodes
(a, b) is defined as the total number of edges connected to either node a or node b.Key Points:
- Only count pairs where
a < b(avoid duplicates) - Multiple edges between the same nodes are allowed
- An edge connecting nodes
aandbdirectly should only be counted once in their incident count
Goal: Return an array where each element corresponds to the answer for each query - the number of node pairs whose combined connectivity exceeds the query threshold.
Input & Output
basic_example.py โ Basic Graph
$
Input:
n = 4, edges = [[1,2],[2,4],[1,4]], queries = [2,3]
โบ
Output:
[6, 5]
๐ก Note:
Node degrees: [1:2, 2:2, 3:0, 4:2]. For query=2: pairs (1,3), (1,4), (2,3), (2,4), (3,4) exceed threshold after subtracting direct edges. For query=3: pairs (1,4), (2,3), (2,4), (3,4) exceed threshold.
simple_path.py โ Linear Path
$
Input:
n = 3, edges = [[1,2],[2,3]], queries = [2]
โบ
Output:
[2]
๐ก Note:
Node degrees: [1:1, 2:2, 3:1]. Pairs: (1,2)โ1+2-1=2, (1,3)โ1+1=2, (2,3)โ2+1-1=2. All pairs equal 2, none exceed query=2.
multiple_edges.py โ Multiple Edges
$
Input:
n = 2, edges = [[1,2],[1,2],[1,2]], queries = [1,2,3]
โบ
Output:
[1, 1, 0]
๐ก Note:
Node degrees: [1:3, 2:3]. Pair (1,2) has incident=3+3-3=3. For queries [1,2,3]: pair exceeds 1 and 2, but equals 3.
Visualization
Tap to expand
Understanding the Visualization
1
Count Connections
Calculate how many connections each person has in the network
2
Sort by Influence
Arrange people from least to most connected for efficient searching
3
Two-Pointer Search
Use two pointers to find pairs whose combined influence exceeds threshold
4
Adjust for Direct Links
Subtract direct friendships to avoid double-counting connections
Key Takeaway
๐ฏ Key Insight: By sorting nodes by degree and using two pointers, we can efficiently count pairs without checking every combination. The critical step is properly handling direct edges to avoid double-counting shared connections.
Time & Space Complexity
Time Complexity
O(nยฒ * q + m)
O(m) to process edges, then O(nยฒ) for each of the q queries to check all pairs
โ Quadratic Growth
Space Complexity
O(nยฒ)
O(n) for degrees array and O(nยฒ) for storing direct edge counts between pairs
โ Quadratic Space
Constraints
- 2 โค n โค 2 ร 104
- 0 โค edges.length โค 105
- 1 โค ui, vi โค n
- ui โ vi
- 1 โค queries.length โค 20
- 0 โค queries[j] < n ร (n-1) / 2
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code