SlideShare a Scribd company logo
Advanced Algorithms #1
Union/Find on Disjoint-Set Data Structures
www.youtube.com/watch?v=vDotBqwa0AE
Andrea Angella
Who I am?
• Co-Founder of DotNetToscana
• Software Engineer in Red Gate Software (UK)
• Microsoft C# Specialist
• Passion for algorithms
Mail: angella.andrea@gmail.com
Blog: andrea-angella.blogspot.co.uk
Agenda
• Introduction to the series
• Practical Problem: Image Coloring
• The Connectivity Problem
• 5 different implementations
• Image Coloring solution
Why learning algorithms?
• To solve problems
• To solve complex problems
• To solve problems on big data sets
• To become a better developer
• To find a job in top software companies
• To challenge yourself and the community
• Lifelong investment
It is fun!
Why this series?
• Practical (real problems and solutions)
• Pragmatic (no mathematical proofs)
• Algorithms are written from scratch in C#
Advanced Algorithms #1 - Union/Find on Disjoint-set Data Structures.
Credits
• Robert Sedgewick and Kevin Wayne
• Algorithms 4 Edition
http://algs4.cs.princeton.edu/code/
• Coursera:
https://www.coursera.org/course/algs4partI
https://www.coursera.org/course/algs4partII
Problem: Image Coloring
Example
The Connectivity Problem
Example
0 1 2
3 4
N = 5
Connect (0, 1)
Connect (1, 3)
Connect (2, 4)
AreConnected (0, 3) = TRUE
AreConnected (1, 2) = FALSE
CODE
Connected Components
1) Quick Find
0
0
1
1
2
2
2
3
1
4
1
5
2
6
2
7
id[] 0
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
id[]
• Assign to each node a number (the id of the connected component)
• Find: check if p and q have the same id
• Union: change all entries whose id equals id[p] to id[q]
CODE
2) Quick Union
Assign to each node a parent (organize nodes in a forest of trees).
Find
check if p and q have the same root
Union
set the parent of p’s root to the q’s root
0
0
1
1
9
2
4
3
9
4
6
5
6
6
7
7
parent[] 8
8
9
9
0
0
1
1
9
2
4
3
9
4
6
5
6
6
7
7
parent[] 8
8
6
9
CODE
Why Quick Union is too slow?
The average distance to root is too big!
3) Weighted Quick Union
• Avoid tall trees!
• Keep track of the size of each tree.
• Balance by linking root of smaller tree to the root of larger tree.
CODE
4) Quick Union Path Compression
After computing the root of p, set the id of each examined node to point to that root
CODE
5) Weighted Quick Union Path
Compression
Weighted Quick
Union
Quick Union
Path Compression+
Memory improvements
• Keep track of the height of each tree instead of the size
• Height increase only when two trees of the same height are connected
• Only one byte needed to store height (always lower than 32)
Save 3N bytes!
CODE
Image Coloring Solution
CODE
Performance Analysis
Algorithm Find Union
Quick Find N N2
Quick Union N2 N2
Weighted Quick Union N Log N N Log N
Quick Union Path Compression N Log N N Log N
Weighted Quick Union Path Compression N Log* N N Log* N
Linear Union/Find? N N
N Log* N
1 0
2 1
4 2
16 3
65536 4
265536 5
[Fredman-Saks] No linear-time algorithm exists. (1989)
In practice Weighted QU Path Compression is linear!
Don’t miss the next webcasts
• Graph Search (DFS/BFS)
• Suffix Array and Suffix Trees
• Kd-Trees
• Minimax
• Convex Hull
• Max Flow
• Radix Sort
• Combinatorial
• Dynamic Programming
• …
Thank you
https://github.com/angellaa/AdvancedAlgorithms

More Related Content

What's hot (19)

PPTX
K-Means Algorithm Implementation In python
Afzal Ahmad
 
PPT
11. Arrays
Nilesh Dalvi
 
PDF
Data structures Basics
DurgaDeviCbit
 
PDF
L7
lksoo
 
PPTX
Rahat & juhith
Rj Juhith
 
PDF
Data structures (introduction)
Arvind Devaraj
 
PPT
arrays
Enakshi Chanda
 
PPT
Algorithm
sultanarun
 
PDF
The Allen AI Science Challenge
Pavel Kalaidin
 
PPTX
How to implement complex policies on existing network infrastructure
AJAY KHARAT
 
PPT
Data structure lecture 2
Kumar
 
DOCX
MODULE 5-Searching and-sorting
nikshaikh786
 
PDF
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
PPTX
Kmeans
Nikita Goyal
 
PDF
Array linear data_structure_2 (1)
eShikshak
 
PPTX
Big o notation
hamza mushtaq
 
PPTX
Bca ii dfs u-1 introduction to data structure
Rai University
 
PDF
DATA STRUCTURES USING C -ENGGDIGEST
Swapnil Mishra
 
PPT
Compose Method
guestcb0002
 
K-Means Algorithm Implementation In python
Afzal Ahmad
 
11. Arrays
Nilesh Dalvi
 
Data structures Basics
DurgaDeviCbit
 
L7
lksoo
 
Rahat & juhith
Rj Juhith
 
Data structures (introduction)
Arvind Devaraj
 
Algorithm
sultanarun
 
The Allen AI Science Challenge
Pavel Kalaidin
 
How to implement complex policies on existing network infrastructure
AJAY KHARAT
 
Data structure lecture 2
Kumar
 
MODULE 5-Searching and-sorting
nikshaikh786
 
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
Kmeans
Nikita Goyal
 
Array linear data_structure_2 (1)
eShikshak
 
Big o notation
hamza mushtaq
 
Bca ii dfs u-1 introduction to data structure
Rai University
 
DATA STRUCTURES USING C -ENGGDIGEST
Swapnil Mishra
 
Compose Method
guestcb0002
 

Viewers also liked (17)

PDF
07. disjoint set
Onkar Nath Sharma
 
PPT
Disjoint sets
Core Condor
 
PDF
Algorithms, Union Find
Nikita Shpilevoy
 
PDF
Time complexity of union find
Wei (Terence) Li
 
PDF
17 Disjoint Set Representation
Andres Mendez-Vazquez
 
PPT
chapter24.ppt
Tareq Hasan
 
PDF
18 Basic Graph Algorithms
Andres Mendez-Vazquez
 
PPT
lecture 21
sajinsc
 
PPTX
Fibonacci Heaps
Naseeba P P
 
PPTX
Huffman tree
Al-amin Hossain
 
PDF
Fibonacci Heap
Anshuman Biswal
 
PPTX
Set data structure
Tech_MX
 
PPT
3.9 external sorting
Krish_ver2
 
PDF
Graph theory
Lifeparticle
 
PPT
Set concepts
Malti Aswal
 
PDF
Graph theory
Kumar
 
PPTX
Dijkstra
jagdeeparora86
 
07. disjoint set
Onkar Nath Sharma
 
Disjoint sets
Core Condor
 
Algorithms, Union Find
Nikita Shpilevoy
 
Time complexity of union find
Wei (Terence) Li
 
17 Disjoint Set Representation
Andres Mendez-Vazquez
 
chapter24.ppt
Tareq Hasan
 
18 Basic Graph Algorithms
Andres Mendez-Vazquez
 
lecture 21
sajinsc
 
Fibonacci Heaps
Naseeba P P
 
Huffman tree
Al-amin Hossain
 
Fibonacci Heap
Anshuman Biswal
 
Set data structure
Tech_MX
 
3.9 external sorting
Krish_ver2
 
Graph theory
Lifeparticle
 
Set concepts
Malti Aswal
 
Graph theory
Kumar
 
Dijkstra
jagdeeparora86
 
Ad

Similar to Advanced Algorithms #1 - Union/Find on Disjoint-set Data Structures. (20)

PDF
_b65e7611894ba175de27bd14793f894a_15UnionFind.pdf
sulimanalwageh
 
PPT
DAA (Unit-2) (ii).ppt design analysis of algorithms
ssuser99ca78
 
PDF
learning about union find algorithm lectures
NamPhmPhng9
 
DOCX
Algorithm
nivlayalat
 
PPTX
Find operation and smart union algorithm
PriyaKarnan3
 
PDF
Minimum Spanning Trees Artificial Intelligence
jiraf23341
 
PPTX
Set Operations - Union Find and Bloom Filters
Amrinder Arora
 
PPTX
Lecture 6 disjoint set
Abirami A
 
PDF
smtlecture.7
Roberto Bruttomesso
 
PPT
ee220s02lec10.ppt.........................
AnilKumar914552
 
PPT
Uniformed tree searching
Ayaelshiwi
 
PPTX
Disjoint set
DeepikaT13
 
PPT
Advanced data structures and implementation
Umang Gupta
 
PPT
Data Structures 5
Dr.Umadevi V
 
PPTX
DISJOINT SETS.pptx
JyoReddy9
 
PDF
Scribed lec8
Praveen Kumar
 
PPTX
04 Problem Solving in AI (1)-artificial intelligence.pptx
HiraSardar1
 
PPTX
Implementation of union
rajshreemuthiah
 
PPTX
Path compression
DEEPIKA T
 
PDF
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Amrinder Arora
 
_b65e7611894ba175de27bd14793f894a_15UnionFind.pdf
sulimanalwageh
 
DAA (Unit-2) (ii).ppt design analysis of algorithms
ssuser99ca78
 
learning about union find algorithm lectures
NamPhmPhng9
 
Algorithm
nivlayalat
 
Find operation and smart union algorithm
PriyaKarnan3
 
Minimum Spanning Trees Artificial Intelligence
jiraf23341
 
Set Operations - Union Find and Bloom Filters
Amrinder Arora
 
Lecture 6 disjoint set
Abirami A
 
smtlecture.7
Roberto Bruttomesso
 
ee220s02lec10.ppt.........................
AnilKumar914552
 
Uniformed tree searching
Ayaelshiwi
 
Disjoint set
DeepikaT13
 
Advanced data structures and implementation
Umang Gupta
 
Data Structures 5
Dr.Umadevi V
 
DISJOINT SETS.pptx
JyoReddy9
 
Scribed lec8
Praveen Kumar
 
04 Problem Solving in AI (1)-artificial intelligence.pptx
HiraSardar1
 
Implementation of union
rajshreemuthiah
 
Path compression
DEEPIKA T
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Amrinder Arora
 
Ad

More from Andrea Angella (6)

PDF
C# 7.X and beyond
Andrea Angella
 
PPTX
C# 7
Andrea Angella
 
PPTX
Performance testing with NBench
Andrea Angella
 
PPTX
Windows 10 overview
Andrea Angella
 
PPTX
WP7 Game Lab
Andrea Angella
 
PPTX
C# 3.0 e LINQ
Andrea Angella
 
C# 7.X and beyond
Andrea Angella
 
Performance testing with NBench
Andrea Angella
 
Windows 10 overview
Andrea Angella
 
WP7 Game Lab
Andrea Angella
 
C# 3.0 e LINQ
Andrea Angella
 

Recently uploaded (20)

PPTX
Prompt Like a Pro. Leveraging Salesforce Data to Power AI Workflows.pptx
Dele Amefo
 
PPTX
Smart Doctor Appointment Booking option in odoo.pptx
AxisTechnolabs
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
Simplify React app login with asgardeo-sdk
vaibhav289687
 
PDF
intro_to_cpp_namespace_robotics_corner.pdf
MohamedSaied877003
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Why is partnering with a SaaS development company crucial for enterprise succ...
Nextbrain Technologies
 
PDF
Latest Capcut Pro 5.9.0 Crack Version For PC {Fully 2025
utfefguu
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
BB FlashBack Pro 5.61.0.4843 With Crack Free Download
cracked shares
 
PDF
Everything you need to know about pricing & licensing Microsoft 365 Copilot f...
Q-Advise
 
PPTX
iaas vs paas vs saas :choosing your cloud strategy
CloudlayaTechnology
 
PPTX
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
PPTX
prodad heroglyph crack 2.0.214.2 Full Free Download
cracked shares
 
PDF
Ready Layer One: Intro to the Model Context Protocol
mmckenna1
 
PPTX
UI5con_2025_Accessibility_Ever_Evolving_
gerganakremenska1
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
Prompt Like a Pro. Leveraging Salesforce Data to Power AI Workflows.pptx
Dele Amefo
 
Smart Doctor Appointment Booking option in odoo.pptx
AxisTechnolabs
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Simplify React app login with asgardeo-sdk
vaibhav289687
 
intro_to_cpp_namespace_robotics_corner.pdf
MohamedSaied877003
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Why is partnering with a SaaS development company crucial for enterprise succ...
Nextbrain Technologies
 
Latest Capcut Pro 5.9.0 Crack Version For PC {Fully 2025
utfefguu
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
BB FlashBack Pro 5.61.0.4843 With Crack Free Download
cracked shares
 
Everything you need to know about pricing & licensing Microsoft 365 Copilot f...
Q-Advise
 
iaas vs paas vs saas :choosing your cloud strategy
CloudlayaTechnology
 
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
prodad heroglyph crack 2.0.214.2 Full Free Download
cracked shares
 
Ready Layer One: Intro to the Model Context Protocol
mmckenna1
 
UI5con_2025_Accessibility_Ever_Evolving_
gerganakremenska1
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 

Advanced Algorithms #1 - Union/Find on Disjoint-set Data Structures.

  • 1. Advanced Algorithms #1 Union/Find on Disjoint-Set Data Structures www.youtube.com/watch?v=vDotBqwa0AE Andrea Angella
  • 2. Who I am? • Co-Founder of DotNetToscana • Software Engineer in Red Gate Software (UK) • Microsoft C# Specialist • Passion for algorithms Mail: [email protected] Blog: andrea-angella.blogspot.co.uk
  • 3. Agenda • Introduction to the series • Practical Problem: Image Coloring • The Connectivity Problem • 5 different implementations • Image Coloring solution
  • 4. Why learning algorithms? • To solve problems • To solve complex problems • To solve problems on big data sets • To become a better developer • To find a job in top software companies • To challenge yourself and the community • Lifelong investment It is fun!
  • 5. Why this series? • Practical (real problems and solutions) • Pragmatic (no mathematical proofs) • Algorithms are written from scratch in C#
  • 7. Credits • Robert Sedgewick and Kevin Wayne • Algorithms 4 Edition http://algs4.cs.princeton.edu/code/ • Coursera: https://www.coursera.org/course/algs4partI https://www.coursera.org/course/algs4partII
  • 11. Example 0 1 2 3 4 N = 5 Connect (0, 1) Connect (1, 3) Connect (2, 4) AreConnected (0, 3) = TRUE AreConnected (1, 2) = FALSE
  • 12. CODE
  • 14. 1) Quick Find 0 0 1 1 2 2 2 3 1 4 1 5 2 6 2 7 id[] 0 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 id[] • Assign to each node a number (the id of the connected component) • Find: check if p and q have the same id • Union: change all entries whose id equals id[p] to id[q]
  • 15. CODE
  • 16. 2) Quick Union Assign to each node a parent (organize nodes in a forest of trees). Find check if p and q have the same root Union set the parent of p’s root to the q’s root 0 0 1 1 9 2 4 3 9 4 6 5 6 6 7 7 parent[] 8 8 9 9 0 0 1 1 9 2 4 3 9 4 6 5 6 6 7 7 parent[] 8 8 6 9
  • 17. CODE
  • 18. Why Quick Union is too slow? The average distance to root is too big!
  • 19. 3) Weighted Quick Union • Avoid tall trees! • Keep track of the size of each tree. • Balance by linking root of smaller tree to the root of larger tree.
  • 20. CODE
  • 21. 4) Quick Union Path Compression After computing the root of p, set the id of each examined node to point to that root
  • 22. CODE
  • 23. 5) Weighted Quick Union Path Compression Weighted Quick Union Quick Union Path Compression+
  • 24. Memory improvements • Keep track of the height of each tree instead of the size • Height increase only when two trees of the same height are connected • Only one byte needed to store height (always lower than 32) Save 3N bytes!
  • 25. CODE
  • 27. CODE
  • 28. Performance Analysis Algorithm Find Union Quick Find N N2 Quick Union N2 N2 Weighted Quick Union N Log N N Log N Quick Union Path Compression N Log N N Log N Weighted Quick Union Path Compression N Log* N N Log* N Linear Union/Find? N N N Log* N 1 0 2 1 4 2 16 3 65536 4 265536 5 [Fredman-Saks] No linear-time algorithm exists. (1989) In practice Weighted QU Path Compression is linear!
  • 29. Don’t miss the next webcasts • Graph Search (DFS/BFS) • Suffix Array and Suffix Trees • Kd-Trees • Minimax • Convex Hull • Max Flow • Radix Sort • Combinatorial • Dynamic Programming • …