Answer Connected Queries with Union-Find
Merge groups with union and answer connected queries with Union-Find.
The problem
Implement disjoint_set_trace(node_count, operations). There are node_count items numbered 0 to node_count - 1, and each one starts in its own group. Each operation is either ["union", a, b], which merges the groups that contain a and b, or ["connected", a, b], which asks whether a and b are in the same group right now. Return a list with one boolean per "connected" operation, in the order they appear. Use union by size and path compression.
Starter code
def disjoint_set_trace(node_count, operations):
passPublic examples
node_count = 5, operations = [["union", 0, 1], ["union", 1, 2], ["connected", 0, 2], ["connected", 0, 4]]gives[True, False]node_count = 3, operations = [["union", 0, 1], ["union", 1, 0], ["connected", 0, 1]]gives[True]
Solving it in the practice book needs an active paid plan.
Open it in the practice bookOne thought at a time.
