SlideShare une entreprise Scribd logo
1  sur  30
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#
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

Contenu connexe

Tendances

Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
Arvind Devaraj
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
Kumar
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
eShikshak
 

Tendances (19)

K-Means Algorithm Implementation In python
K-Means Algorithm Implementation In pythonK-Means Algorithm Implementation In python
K-Means Algorithm Implementation In python
 
11. Arrays
11. Arrays11. Arrays
11. Arrays
 
Data structures Basics
Data structures BasicsData structures Basics
Data structures Basics
 
L7
L7L7
L7
 
Rahat & juhith
Rahat & juhithRahat & juhith
Rahat & juhith
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
 
arrays
arraysarrays
arrays
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
The Allen AI Science Challenge
The Allen AI Science ChallengeThe Allen AI Science Challenge
The Allen AI Science Challenge
 
How to implement complex policies on existing network infrastructure
How to implement complex policies on existing network infrastructure How to implement complex policies on existing network infrastructure
How to implement complex policies on existing network infrastructure
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Kmeans
KmeansKmeans
Kmeans
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
 
Big o notation
Big o notationBig o notation
Big o notation
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
 
Compose Method
Compose MethodCompose Method
Compose Method
 

En vedette

lecture 21
lecture 21lecture 21
lecture 21
sajinsc
 
Set data structure
Set data structure Set data structure
Set data structure
Tech_MX
 
Graph theory
Graph theoryGraph theory
Graph theory
Kumar
 

En vedette (17)

07. disjoint set
07. disjoint set07. disjoint set
07. disjoint set
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
Algorithms, Union Find
Algorithms, Union FindAlgorithms, Union Find
Algorithms, Union Find
 
Time complexity of union find
Time complexity of union findTime complexity of union find
Time complexity of union find
 
17 Disjoint Set Representation
17 Disjoint Set Representation17 Disjoint Set Representation
17 Disjoint Set Representation
 
chapter24.ppt
chapter24.pptchapter24.ppt
chapter24.ppt
 
18 Basic Graph Algorithms
18 Basic Graph Algorithms18 Basic Graph Algorithms
18 Basic Graph Algorithms
 
lecture 21
lecture 21lecture 21
lecture 21
 
Fibonacci Heaps
Fibonacci Heaps Fibonacci Heaps
Fibonacci Heaps
 
Huffman tree
Huffman tree Huffman tree
Huffman tree
 
Fibonacci Heap
Fibonacci HeapFibonacci Heap
Fibonacci Heap
 
Set data structure
Set data structure Set data structure
Set data structure
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sorting
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Set concepts
Set conceptsSet concepts
Set concepts
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Dijkstra
DijkstraDijkstra
Dijkstra
 

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

A look inside pandas design and development
A look inside pandas design and developmentA look inside pandas design and development
A look inside pandas design and development
Wes McKinney
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
Data Day Texas 2017: Scaling Data Science at Stitch Fix
Data Day Texas 2017: Scaling Data Science at Stitch FixData Day Texas 2017: Scaling Data Science at Stitch Fix
Data Day Texas 2017: Scaling Data Science at Stitch Fix
Stefan Krawczyk
 
ch7-Create an IoT Solution
ch7-Create an IoT Solutionch7-Create an IoT Solution
ch7-Create an IoT Solution
ssuser06ea42
 
Decision tree upload
Decision tree uploadDecision tree upload
Decision tree upload
nithum
 

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

A look inside pandas design and development
A look inside pandas design and developmentA look inside pandas design and development
A look inside pandas design and development
 
UnSupervised Learning Clustering
UnSupervised Learning ClusteringUnSupervised Learning Clustering
UnSupervised Learning Clustering
 
BSSML17 - Deepnets
BSSML17 - DeepnetsBSSML17 - Deepnets
BSSML17 - Deepnets
 
Web based interactive big data visualization
Web based interactive big data visualizationWeb based interactive big data visualization
Web based interactive big data visualization
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Entity embeddings for categorical data
Entity embeddings for categorical dataEntity embeddings for categorical data
Entity embeddings for categorical data
 
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
 
Introduction to H2O and Model Stacking Use Cases
Introduction to H2O and Model Stacking Use CasesIntroduction to H2O and Model Stacking Use Cases
Introduction to H2O and Model Stacking Use Cases
 
How to ensure your design is 3D printable
How to ensure your design is 3D printableHow to ensure your design is 3D printable
How to ensure your design is 3D printable
 
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...
 
Data Day Texas 2017: Scaling Data Science at Stitch Fix
Data Day Texas 2017: Scaling Data Science at Stitch FixData Day Texas 2017: Scaling Data Science at Stitch Fix
Data Day Texas 2017: Scaling Data Science at Stitch Fix
 
Machine Learning with ML.NET and Azure - Andy Cross
Machine Learning with ML.NET and Azure - Andy CrossMachine Learning with ML.NET and Azure - Andy Cross
Machine Learning with ML.NET and Azure - Andy Cross
 
A data analyst view of Bigdata
A data analyst view of Bigdata A data analyst view of Bigdata
A data analyst view of Bigdata
 
Structured Forests for Fast Edge Detection [Paper Presentation]
Structured Forests for Fast Edge Detection [Paper Presentation]Structured Forests for Fast Edge Detection [Paper Presentation]
Structured Forests for Fast Edge Detection [Paper Presentation]
 
Advanced CSS Troubleshooting
Advanced CSS TroubleshootingAdvanced CSS Troubleshooting
Advanced CSS Troubleshooting
 
ch7-Create an IoT Solution
ch7-Create an IoT Solutionch7-Create an IoT Solution
ch7-Create an IoT Solution
 
Continuous Applications at Scale of 100 Teams with Databricks Delta and Struc...
Continuous Applications at Scale of 100 Teams with Databricks Delta and Struc...Continuous Applications at Scale of 100 Teams with Databricks Delta and Struc...
Continuous Applications at Scale of 100 Teams with Databricks Delta and Struc...
 
Generative Design 101 for Architecture, Engineering & Construction by Jad Del...
Generative Design 101 for Architecture, Engineering & Construction by Jad Del...Generative Design 101 for Architecture, Engineering & Construction by Jad Del...
Generative Design 101 for Architecture, Engineering & Construction by Jad Del...
 
Bug prediction + sdlc automation
Bug prediction + sdlc automationBug prediction + sdlc automation
Bug prediction + sdlc automation
 
Decision tree upload
Decision tree uploadDecision tree upload
Decision tree upload
 

Plus de Andrea Angella (6)

C# 7.X and beyond
C# 7.X and beyondC# 7.X and beyond
C# 7.X and beyond
 
C# 7
C# 7C# 7
C# 7
 
Performance testing with NBench
Performance testing with NBenchPerformance testing with NBench
Performance testing with NBench
 
Windows 10 overview
Windows 10 overviewWindows 10 overview
Windows 10 overview
 
WP7 Game Lab
WP7 Game LabWP7 Game Lab
WP7 Game Lab
 
C# 3.0 e LINQ
C# 3.0 e LINQC# 3.0 e LINQ
C# 3.0 e LINQ
 

Dernier

%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Dernier (20)

8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 

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: angella.andrea@gmail.com 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#
  • 6.
  • 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 • …