Graph Connectivity Analysis: You're given an undirected graph with 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 a and b directly 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
Graph Connectivity AnalysisABCDdeg=1deg=2deg=1deg=3Two-Pointer Analysis (Query = 3)Sorted degrees: [1, 1, 2, 3]1LEFT3RIGHTStep 1: 1 + 3 = 4 > 3 โœ“Count all pairs with right pointer: 3 pairsSubtract direct edges where neededFinal CountValid Pairs:(A,D), (B,C)(B,D), (C,D)Total: 4
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

n
2n
โš  Quadratic Growth
Space Complexity
O(nยฒ)

O(n) for degrees array and O(nยฒ) for storing direct edge counts between pairs

n
2n
โš  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
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
73.9K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen