All Data Structures
Disjoint set union
Core Implementation
Basic DSU with Path Compression and Union by Rank
Implementation Variations
1. DSU with Path Splitting
2. DSU with Path Halving
3. Weighted Quick Union
Time Complexities
Operation | Simple DSU | Path Compression | Path Splitting | Weighted QU |
---|---|---|---|---|
Make Set | O(1) | O(1) | O(1) | O(1) |
Find | O(N) | O(α(N))* | O(log N) | O(log N) |
Union | O(N) | O(α(N))* | O(log N) | O(log N) |
Connected | O(N) | O(α(N))* | O(log N) | O(log N) |
* α(N) is the inverse Ackermann function, which is effectively constant
Space Complexities
Implementation | Space Complexity | Additional Notes |
---|---|---|
Basic DSU | O(N) | Parent array only |
Ranked DSU | O(N) | Extra array for ranks |
Weighted QU | O(N) | Extra array for sizes |
Path Compression | O(N) | No extra space beyond parent array |
Common Data Structure Patterns
-
Connected Components
- Graph connectivity
- Component counting
- Cycle detection
-
Dynamic Connectivity
- Online connectivity queries
- Real-time updates
- Network partitioning
-
Minimum Spanning Tree
- Kruskal’s algorithm
- Network design
- Clustering
-
Component Properties
- Size tracking
- Component merging
- Set operations
-
Graph Algorithms
- Cycle detection
- Connected components
- Path compression
-
Network Flow
- Network connectivity
- Cut problems
- Flow partitioning
Edge Cases to Consider
- Single Element Set
- Self-Union
- Invalid Elements
Optimization Techniques
- Path Compression with Recursion Elimination
- Rank Balancing
Memory Management
- Compact Representation
- Dynamic Allocation
Performance Considerations
- Batch Operations
- Size-based Optimization
Common Pitfalls
- Missing Path Compression
- Incorrect Rank Update
- Not Maintaining Component Count