Number of Connected Components
Count the groups of linked machines
DSA, Medium, Graphs
Which structure or algorithm, and what does it cost.
There are n machines, numbered 0 to n - 1. Each link [a, b] joins machine a and machine b in both directions. A group is a set of machines where each one can reach every other one over the links. Write count_groups(n, links). It returns the number of groups. A machine with no links is a group of its own.
Examples
Constraints
- 1 <=
n<= 70,000. - 0 <= number of links <= 150,000.
- Every machine number is between 0 and n - 1, and a != b in every link. A link may appear more than once.
Your answer must contain
- Why each link lowers the count by one or not at all; the method; code; time and memory cost; a hand trace of example 1; machines with no links.
Target Aim for nearly O(n + m) time and O(n) memory, where m is the number of links.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Imagine there are no links at all. Every machine is its own group. What happens to the number of groups each time you add one link?
Hint 2 the approach
A link either joins two different groups, which removes one group, or joins two machines already in one group, which changes nothing. How can you tell which?
Hint 3 the key step
Name each group by a root machine, with a parent for each machine. Find both roots. If they differ, merge the groups and lower the count by one.
Walkthrough
Full walkthrough brute force to best
Brute force
Build the list of neighbours for each machine. Then, for every machine, search to collect all the machines it can reach, and count the machine only if it is the smallest number in its set. That is n searches, and each reads up to n + m entries, so this costs O(n * (n + m)) time.
Best method
Start with n groups, one for each machine, where every machine is its own parent and so its own root. Take the links one by one. Find the root of each machine by following parents up, and shorten the path as you climb. If the two roots differ, the machines are in different groups: attach the smaller group's root under the bigger one's root and lower the group count by one. If the roots match, the link adds nothing. After the last link, the group count is the answer. Every merge turns two groups into one, so it removes exactly one group, and a link inside a group removes none.
Hand trace
Example 1: the count starts at 7. Link [0,3]: the roots 0 and 3 differ, so merge, and the count is 6. Link [3,5]: the roots are 0 and 5, so merge, and the count is 5. Link [1,4]: the roots 1 and 4 differ, so merge, and the count is 4. Link [5,0]: both roots are 0, so nothing changes. The answer is 4.
Code
def count_groups(n, links):
parent = list(range(n))
size = [1] * n
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]] # shorten the path as we climb
x = parent[x]
return x
groups = n # with no links, every machine is a group
for a, b in links:
ra, rb = find(a), find(b)
if ra != rb:
if size[ra] < size[rb]:
ra, rb = rb, ra
parent[rb] = ra # merge the smaller group into the bigger
size[ra] += size[rb]
groups -= 1
return groupsTime and memory
Nearly O(n + m) time and O(n) memory, where m is the number of links. Setting up costs O(n). Each link costs two finds, which take almost constant time because paths are shortened and groups merge by size.
Common mistakes
- Forgetting machines with no links. Each one is its own group, so the count must start at n.
- Lowering the count for every link. A link between two machines that are already in one group must not lower it.
- Counting the links or the size of the largest group instead of the number of groups.