Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Weighted Clusters

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Attach the smaller tree beneath the larger: The Weighted Clusters.

Brute force may pass the demo but fail when the world fills with players.

How you win

  1. 1Recognize when Union by size matches the clues
  2. 2Keep this true after every move: size[root] equals the number of nodes in that root's component
  3. 3Reach the result within O((n + e) alpha(n))

Rules and pressure

  • Target cost: O((n + e) alpha(n))
  • State rule: size[root] equals the number of nodes in that root's component
Lesson 1 of 3

Live algorithm trace

Union by size

Complete execution
1 of 6
0-1
root
2-3
1-2
4-5

Initialize six singleton weighted components.

1parent = range(n); size = ones
2find both roots
3swap so rootA is larger
4parent[rootB] = rootA
5size[rootA] += size[rootB]
6collect sizes at roots
7sort descending
parent = [0,1,2,3,4,5]sizes = all 1
Truth to preserve / Cost target
Truth to preserve

size[root] equals the number of nodes in that root's component

Cost target

O((n + e) alpha(n))

Track each root's component size. When two roots differ, attach the smaller component beneath the larger root, update only the surviving root's size, and keep trees shallow.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead