SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

K-Means Algorithm Implementation In python
K-Means Algorithm Implementation In pythonK-Means Algorithm Implementation In python
K-Means Algorithm Implementation In pythonAfzal Ahmad
 
Data structures Basics
Data structures BasicsData structures Basics
Data structures BasicsDurgaDeviCbit
 
Rahat & juhith
Rahat & juhithRahat & juhith
Rahat & juhithRj Juhith
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)Arvind Devaraj
 
The Allen AI Science Challenge
The Allen AI Science ChallengeThe Allen AI Science Challenge
The Allen AI Science ChallengePavel Kalaidin
 
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 AJAY KHARAT
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2Kumar
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingnikshaikh786
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsMohamed Loey
 
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
 
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 structureRai University
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTSwapnil Mishra
 

La actualidad más candente (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
 

Destacado (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
 

Similar a 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 developmentWes McKinney
 
UnSupervised Learning Clustering
UnSupervised Learning ClusteringUnSupervised Learning Clustering
UnSupervised Learning ClusteringFEG
 
BSSML17 - Deepnets
BSSML17 - DeepnetsBSSML17 - Deepnets
BSSML17 - DeepnetsBigML, Inc
 
Web based interactive big data visualization
Web based interactive big data visualizationWeb based interactive big data visualization
Web based interactive big data visualizationWenli Zhang
 
Entity embeddings for categorical data
Entity embeddings for categorical dataEntity embeddings for categorical data
Entity embeddings for categorical dataPaul Skeie
 
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"Fwdays
 
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 CasesJo-fai Chow
 
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 printableDesign World
 
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...ORAU
 
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 FixStefan Krawczyk
 
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 CrossAndrew Flatters
 
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]Mohammad Shaker
 
Advanced CSS Troubleshooting
Advanced CSS TroubleshootingAdvanced CSS Troubleshooting
Advanced CSS TroubleshootingDenise Jacobs
 
ch7-Create an IoT Solution
ch7-Create an IoT Solutionch7-Create an IoT Solution
ch7-Create an IoT Solutionssuser06ea42
 
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...Databricks
 
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...Jad DELLEL
 
Bug prediction + sdlc automation
Bug prediction + sdlc automationBug prediction + sdlc automation
Bug prediction + sdlc automationAlexey Tokar
 
Decision tree upload
Decision tree uploadDecision tree upload
Decision tree uploadnithum
 
A modern architecturereview–usingcodereviewtools-ver-3.5
A modern architecturereview–usingcodereviewtools-ver-3.5A modern architecturereview–usingcodereviewtools-ver-3.5
A modern architecturereview–usingcodereviewtools-ver-3.5SSW
 

Similar a 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
 
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
 
A modern architecturereview–usingcodereviewtools-ver-3.5
A modern architecturereview–usingcodereviewtools-ver-3.5A modern architecturereview–usingcodereviewtools-ver-3.5
A modern architecturereview–usingcodereviewtools-ver-3.5
 

Más de Andrea Angella

Más 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
 

Último

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 

Último (20)

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 

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 • …