Redundant Connection
Find the first extra link in a graph
DSA, Medium, Graphs
Which structure or algorithm, and what does it cost.
A network is built by adding the links in links one at a time, in list order. Each link [a, b] joins point a and point b. A link is extra if you could already walk from a to b over the links before it. Write extra_link(links). It returns the first extra link, as [a, b] exactly as it appears in the list. If no link is extra, return an empty list.
Examples
Constraints
- 0 <= number of links <= 60,000.
- Every point number is between 1 and 100,000, and a != b in every link.
- A link may appear more than once.
Your answer must contain
- Why walking over earlier links for each link is too slow; the method; code; time and memory cost; a hand trace of example 1; a repeated link.
Target Aim for nearly O(m) time and O(n) memory, where m is the number of links and n is the largest point number.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Each link joins two points. When does a new link add no new connection at all?
Hint 2 the approach
Keep track of which points are already joined. What would tell you that a new link is useless?
Hint 3 the key step
Give each point a parent, and name a group by its root. Find both roots. If they match, that link is the answer. Otherwise attach one root under the other.
Walkthrough
Full walkthrough brute force to best
Brute force
For each link in order, search from a over all the earlier links to see if you can reach b. One search can read all earlier links, so m links cost O(m^2) time.
Best method
Keep the points in groups of points that are already joined. Give each point a parent, starting with itself, so a group is named by the point at the top, its root. To find a point's group, follow parents up to the root, and shorten the path as you climb. Take the links in order. Find the roots of both points. If the roots are the same, the two points are already joined, so this link is the first extra one: return it. If they differ, merge the groups by attaching the smaller group's root under the bigger one's root, and keep going. If you reach the end, return an empty list. With shortened paths and merging by size, each find takes almost constant time.
Hand trace
Example 1: link [3,1]: the roots are 3 and 1, so merge, and 3 is now the root of both. Link [1,4]: the roots are 3 and 4, so merge under 3. Link [4,2]: the roots are 3 and 2, so merge under 3. Link [2,5]: the roots are 3 and 5, so merge under 3. Link [5,1]: both roots are 3, so the points are already joined. Return [5,1]. The link [6,2] is never read.
Code
def extra_link(links):
parent = list(range(max((max(link) for link in links), default=0) + 1))
size = [1] * len(parent)
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]] # shorten the path as we climb
x = parent[x]
return x
for a, b in links:
ra, rb = find(a), find(b)
if ra == rb:
return [a, b] # already in one group
if size[ra] < size[rb]:
ra, rb = rb, ra
parent[rb] = ra # merge the smaller group into the bigger
size[ra] += size[rb]
return []Time and memory
Nearly O(m) time and O(n) memory, where m is the number of links and n is the largest point number. Each link needs two finds, and with shortened paths and merging by size a find is almost constant time. The parent and size lists hold n entries.
Common mistakes
- Checking only whether the two points are linked directly. Points can be joined through other points, so you need the group of each point.
- Returning a link from the circle that is not the one that closes it. Take the links in order and stop at the first one whose points are already in one group.
- Not merging the groups when a link joins two different groups. Later links would then miss the new connection.