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#
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

K-Means Algorithm Implementation In python
K-Means Algorithm Implementation In pythonK-Means Algorithm Implementation In python
K-Means Algorithm Implementation In python
Afzal Ahmad
 
11. Arrays
11. Arrays11. Arrays
11. Arrays
Nilesh Dalvi
 
Data structures Basics
Data structures BasicsData structures Basics
Data structures Basics
DurgaDeviCbit
 
L7
L7L7
L7
lksoo
 
Rahat & juhith
Rahat & juhithRahat & juhith
Rahat & juhith
Rj Juhith
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)Arvind Devaraj
 
arrays
arraysarrays
The Allen AI Science Challenge
The Allen AI Science ChallengeThe Allen AI Science Challenge
The Allen AI Science Challenge
Pavel 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-sorting
nikshaikh786
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
Mohamed 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
 
Big o notation
Big o notationBig o notation
Big o notation
hamza mushtaq
 
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
Rai University
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
Swapnil Mishra
 
Compose Method
Compose MethodCompose Method
Compose Method
guestcb0002
 

What's hot (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
 

Viewers also liked

07. disjoint set
07. disjoint set07. disjoint set
07. disjoint set
Onkar Nath Sharma
 
Algorithms, Union Find
Algorithms, Union FindAlgorithms, Union Find
Algorithms, Union Find
Nikita Shpilevoy
 
Time complexity of union find
Time complexity of union findTime complexity of union find
Time complexity of union find
Wei (Terence) Li
 
17 Disjoint Set Representation
17 Disjoint Set Representation17 Disjoint Set Representation
17 Disjoint Set Representation
Andres Mendez-Vazquez
 
18 Basic Graph Algorithms
18 Basic Graph Algorithms18 Basic Graph Algorithms
18 Basic Graph Algorithms
Andres Mendez-Vazquez
 
lecture 21
lecture 21lecture 21
lecture 21sajinsc
 
Fibonacci Heaps
Fibonacci Heaps Fibonacci Heaps
Fibonacci Heaps
Naseeba P P
 
Huffman tree
Huffman tree Huffman tree
Huffman tree
Al-amin Hossain
 
Set data structure
Set data structure Set data structure
Set data structure Tech_MX
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sorting
Krish_ver2
 
Set concepts
Set conceptsSet concepts
Set concepts
Malti Aswal
 
Graph theory
Graph theoryGraph theory
Graph theoryKumar
 

Viewers also liked (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 to 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 Clustering
FEG
 
BSSML17 - Deepnets
BSSML17 - DeepnetsBSSML17 - Deepnets
BSSML17 - Deepnets
BigML, Inc
 
Web based interactive big data visualization
Web based interactive big data visualizationWeb based interactive big data visualization
Web based interactive big data visualization
Wenli Zhang
 
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
 
Entity embeddings for categorical data
Entity embeddings for categorical dataEntity embeddings for categorical data
Entity embeddings for categorical data
Paul 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 Cases
Jo-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 printable
Design 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 Fix
Stefan 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 Cross
Andrew Flatters
 
A data analyst view of Bigdata
A data analyst view of Bigdata A data analyst view of Bigdata
A data analyst view of Bigdata
Venkata Reddy Konasani
 
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 Troubleshooting
Denise Jacobs
 
ch7-Create an IoT Solution
ch7-Create an IoT Solutionch7-Create an IoT Solution
ch7-Create an IoT Solution
ssuser06ea42
 
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 automation
Alexey Tokar
 
Decision tree upload
Decision tree uploadDecision tree upload
Decision tree uploadnithum
 

Similar to 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
 

More from Andrea Angella

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

More from 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
 

Recently uploaded

BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Yara Milbes
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 

Recently uploaded (20)

BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 

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