SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Andere mochten auch (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
 

Ähnlich wie 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
 
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 TechniquesVictorSzoltysek
 
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
 

Ähnlich wie 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
 

Mehr von Andrea Angella

Mehr von 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
 

Kürzlich hochgeladen

+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
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
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...SelfMade bd
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...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
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%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 kaalfonteinmasabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%+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
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
%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 Hazyviewmasabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 

Kürzlich hochgeladen (20)

+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...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
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...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach 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...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%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
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%+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...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%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
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 

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