Cable families: Proof strip
Stuck or curious how this works? Visualize Binary Heap Operations ↗
Inputs
links is a square table of 0s and 1s. Points are numbered from 0. A 1 at links[a][b] joins points a and b in both directions. A 0 means no link.
Your tasks
- Create a list
groupSizes. This graph has two-waylinksbetween points. Points belong to the same group if you can travel between them, even through other points. Use union-find: start with one group per point, then join groups when a link connects them. For each point, count all points in its group, including itself. Record these counts in point order. A link from a point to itself does not make a larger group. A graph with no points gives an emptygroupSizes. - Complete
cableFamiliesProofStripso it returnsgroupSizes. Return it, do not print it.
Example
If links is [[0, 1, 0, 1, 0, 0], [1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 1], [0, 0, 0, 0, 1, 1], [0, 0, 0, 1, 1, 1]], then groupSizes is [5, 5, 1, 5, 5, 5] and cableFamiliesProofStrip returns [5, 5, 1, 5, 5, 5].
Rules
- If
groupSizesis empty, return [].
Test data
TEST DATA 1: links is [[0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 1], [0, 0, 0, 1, 1, 0], [0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0]].
TEST DATA 2: links is [[0, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 1, 1, 0], [0, 0, 0, 0, 1]].
Examples
Input: links = [[0,0,0,0,0,0],[0,0,0,1,0,1],[0,0,0,1,1,0],[0,1,1,1,0,0],[0,0,1,0,0,1],[0,1,0,0,1,0]] Output: [1,5,5,5,5,5]
groupSizes is [1, 5, 5, 5, 5, 5]. cableFamiliesProofStrip returns that whole list, so return [1, 5, 5, 5, 5, 5].
Input: links = [[0,1,0,0,0],[1,1,0,0,0],[0,0,0,1,0],[0,0,1,1,0],[0,0,0,0,1]] Output: [2,2,2,2,1]
groupSizes is [2, 2, 2, 2, 1]. cableFamiliesProofStrip returns that whole list, so return [2, 2, 2, 2, 1].
Input: links = [] Output: []
groupSizes is []. cableFamiliesProofStrip returns that whole list, so return [].
Input limits
- 0 <= input size <= 16.
- Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- Input numbers have no decimal part. They range from -1,000,000 to 1,000,000. Follow any extra input rules in task 1.
- The graph has at most 16 points. Its table has the same number of rows and columns. Link entries are 0 or 1; cost entries are 0 through 13.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(V^2 alpha(V)); extra memory, including the helper list, O(n). n is the input length; V is the number of graph points. These labels describe how work and memory grow with the input.
Practice focus
Build groupSizes first, then return exactly what the tasks ask for. Follow the order, keep repeated values when the rule says so, and check the empty case.