SlideShare a Scribd company logo
Class No.30  Data Structures http://ecomputernotes.com
Running Time Analysis Union  is clearly a constant time operation. Running time of  find ( i ) is proportional to the height of the tree containing node  i . This can be proportional to n in the worst case (but not always) Goal: Modify  union  to ensure that heights stay small http://ecomputernotes.com
Union  by Size Maintain sizes (number of nodes) of all trees, and during  union. Make smaller tree the subtree of the larger one. Implementation: for each root node  i , instead of setting  parent[i]  to  -1 , set it to  -k  if tree rooted at  i  has  k  nodes. This also called  union-by-weight . http://ecomputernotes.com
Union  by Size union(i,j): root1 = find(i); root2 = find(j); if (root1 != root2) if (parent[root1] <= parent[root2])  { //  first tree has more nodes parent[root1] += parent[root2]; parent[root2] = root1;  } else { // second tree has more nodes parent[root2] += parent[root1]; parent[root1] = root2;  } http://ecomputernotes.com
Union  by Size Eight elements, initially in different sets. http://ecomputernotes.com 1 2 3 4 5 6 7 8 -1 -1 -1 -1 -1 -1 -1 -1 1 2 3 4 5 6 7 8
Union  by Size Union(4,6) http://ecomputernotes.com 1 2 3 4 5 6 7 8 -1 -1 -1 -2 -1 4 -1 -1 1 2 3 4 5 6 7 8
Union  by Size Union(2,3) http://ecomputernotes.com 1 2 3 4 5 6 7 8 -1 -2 2 -2 -1 4 -1 -1 1 2 3 4 5 6 7 8
Union  by Size Union(1,4) http://ecomputernotes.com 1 2 3 4 5 6 7 8 4 -2 2 -3 -1 4 -1 -1 1 2 3 4 5 6 7 8
Union  by Size Union(2,4) http://ecomputernotes.com 1 2 3 4 5 6 7 8 4 4 2 -5 -1 4 -1 -1 1 2 3 4 5 6 7 8
Union  by Size Union(5,4) http://ecomputernotes.com 1 2 3 4 5 6 7 8 4 4 2 -6 4 4 -1 -1 1 2 3 4 5 6 7 8
Analysis of  Union  by Size If unions are done by weight (size), the depth of any element is never greater than log 2 n . http://ecomputernotes.com
Analysis of  Union  by Size Intuitive Proof:   Initially, every element is at depth zero.  When its depth increases as a result of a union operation (it’s in the smaller tree), it is placed in a tree that becomes at least twice as large as before (union of two equal size trees). How often can each union be done? -- log 2 n  times, because after at most log 2 n  unions, the tree will contain all  n  elements. http://ecomputernotes.com
Union  by Height Alternative to union-by-size strategy: maintain heights,  During union, make a tree with smaller height a subtree of the other. Details are left as an exercise. http://ecomputernotes.com
Sprucing up  Find So far we have tried to optimize  union . Can we optimize  find ? Yes, using  path compression  (or compaction). http://ecomputernotes.com
Sprucing up  Find During  find ( i ), as we traverse the path from  i  to root, update parent entries for all these nodes to the root. This reduces the heights of all these nodes. Pay now, and reap benefits later! Subsequent  find  may do less work http://ecomputernotes.com
Sprucing up  Find Updated code for  find find (i)  { if (parent[i] < 0) return i; else  return parent[i] = find(parent[i]); } http://ecomputernotes.com
Path Compression Find(1) 12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
Path Compression Find(1) 12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
Path Compression Find(1) 12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
Path Compression Find(1) 12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
Path Compression Find(1) 12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
Path Compression Find ( a ) http://ecomputernotes.com a b f c d e
Path Compression Find ( a ) http://ecomputernotes.com a b f c d e
Timing with Optimization Theorem: A sequence of  m union  and  find  operations,  n  of which are  find  operations, can be performed on a disjoint-set forest with union by rank (weight or height) and path compression in worst case time proportional to ( m    ( n ))   ( n )  is the inverse Ackermann’s function which grows extremely slowly. For all practical puposes,   ( n )    4. Union-find is essentially proportional to  m  for a sequence of  m  operations, linear in  m . http://ecomputernotes.com
Ad

More Related Content

What's hot (7)

computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
ecomputernotes
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
Tareq Hasan
 
Heap sort
Heap sortHeap sort
Heap sort
Mohd Arif
 
Working of Merge Sort Code
Working of Merge Sort CodeWorking of Merge Sort Code
Working of Merge Sort Code
Muhammad Abdullah
 
Stacks
StacksStacks
Stacks
amitphadikar2012
 
Heaps
HeapsHeaps
Heaps
Hafiz Atif Amin
 

Viewers also liked (13)

Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
ecomputernotes
 
computer notes - Deleting a node
computer notes - Deleting a nodecomputer notes - Deleting a node
computer notes - Deleting a node
ecomputernotes
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
ecomputernotes
 
09.1 types of computer operation
09.1   types of computer operation09.1   types of computer operation
09.1 types of computer operation
Khan Yousafzai
 
Unattended Computer Operations in the Banking Industry
Unattended Computer Operations in the Banking IndustryUnattended Computer Operations in the Banking Industry
Unattended Computer Operations in the Banking Industry
HelpSystems
 
Computer operations 7
Computer operations 7Computer operations 7
Computer operations 7
Naheelah Irving
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
ecomputernotes
 
Ch 9 types of computer operations
Ch 9 types of computer operationsCh 9 types of computer operations
Ch 9 types of computer operations
Khan Yousafzai
 
KMP Pattern Matching algorithm
KMP Pattern Matching algorithmKMP Pattern Matching algorithm
KMP Pattern Matching algorithm
Kamal Nayan
 
Gace Basic Computer Operation And Troubleshooting
Gace Basic Computer Operation And TroubleshootingGace Basic Computer Operation And Troubleshooting
Gace Basic Computer Operation And Troubleshooting
Nisa Peek
 
Basic Computer Operations
Basic Computer OperationsBasic Computer Operations
Basic Computer Operations
Karen
 
Computers in banking
Computers in bankingComputers in banking
Computers in banking
VerronBriscoe
 
Role of computers in research
Role of computers in researchRole of computers in research
Role of computers in research
Saravana Kumar
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
ecomputernotes
 
computer notes - Deleting a node
computer notes - Deleting a nodecomputer notes - Deleting a node
computer notes - Deleting a node
ecomputernotes
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
ecomputernotes
 
09.1 types of computer operation
09.1   types of computer operation09.1   types of computer operation
09.1 types of computer operation
Khan Yousafzai
 
Unattended Computer Operations in the Banking Industry
Unattended Computer Operations in the Banking IndustryUnattended Computer Operations in the Banking Industry
Unattended Computer Operations in the Banking Industry
HelpSystems
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
ecomputernotes
 
Ch 9 types of computer operations
Ch 9 types of computer operationsCh 9 types of computer operations
Ch 9 types of computer operations
Khan Yousafzai
 
KMP Pattern Matching algorithm
KMP Pattern Matching algorithmKMP Pattern Matching algorithm
KMP Pattern Matching algorithm
Kamal Nayan
 
Gace Basic Computer Operation And Troubleshooting
Gace Basic Computer Operation And TroubleshootingGace Basic Computer Operation And Troubleshooting
Gace Basic Computer Operation And Troubleshooting
Nisa Peek
 
Basic Computer Operations
Basic Computer OperationsBasic Computer Operations
Basic Computer Operations
Karen
 
Computers in banking
Computers in bankingComputers in banking
Computers in banking
VerronBriscoe
 
Role of computers in research
Role of computers in researchRole of computers in research
Role of computers in research
Saravana Kumar
 
Ad

Similar to Computer notes - Analysis of Union (20)

computer notes - Data Structures - 29
computer notes - Data Structures - 29computer notes - Data Structures - 29
computer notes - Data Structures - 29
ecomputernotes
 
Disjoint set
Disjoint setDisjoint set
Disjoint set
DeepikaT13
 
Alignments
AlignmentsAlignments
Alignments
James McInerney
 
Computer notes - Maze Generator
Computer notes - Maze GeneratorComputer notes - Maze Generator
Computer notes - Maze Generator
ecomputernotes
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
ecomputernotes
 
Stochastic optimization from mirror descent to recent algorithms
Stochastic optimization from mirror descent to recent algorithmsStochastic optimization from mirror descent to recent algorithms
Stochastic optimization from mirror descent to recent algorithms
Seonho Park
 
Computer notes data structures - 9
Computer notes   data structures - 9Computer notes   data structures - 9
Computer notes data structures - 9
ecomputernotes
 
Introduction to Some Tree based Learning Method
Introduction to Some Tree based Learning MethodIntroduction to Some Tree based Learning Method
Introduction to Some Tree based Learning Method
Honglin Yu
 
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
Andres Mendez-Vazquez
 
FiDoop: Parallel Mining of Frequent Itemsets Using MapReduce
FiDoop: Parallel Mining of Frequent Itemsets Using MapReduceFiDoop: Parallel Mining of Frequent Itemsets Using MapReduce
FiDoop: Parallel Mining of Frequent Itemsets Using MapReduce
IJCSIS Research Publications
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
Amrinder Arora
 
Computer notes - The const Keyword
Computer notes - The const KeywordComputer notes - The const Keyword
Computer notes - The const Keyword
ecomputernotes
 
DA FDAFDSasd
DA FDAFDSasdDA FDAFDSasd
DA FDAFDSasd
WinnerLogin1
 
Artificial Neural Network
Artificial Neural NetworkArtificial Neural Network
Artificial Neural Network
Dessy Amirudin
 
CLIM: Transition Workshop - Sea Ice, Unstable Subspace, Model Error: Three To...
CLIM: Transition Workshop - Sea Ice, Unstable Subspace, Model Error: Three To...CLIM: Transition Workshop - Sea Ice, Unstable Subspace, Model Error: Three To...
CLIM: Transition Workshop - Sea Ice, Unstable Subspace, Model Error: Three To...
The Statistical and Applied Mathematical Sciences Institute
 
5a.Greedy Technique_prims_Krus_Dijkstra.ppt
5a.Greedy Technique_prims_Krus_Dijkstra.ppt5a.Greedy Technique_prims_Krus_Dijkstra.ppt
5a.Greedy Technique_prims_Krus_Dijkstra.ppt
Suma Raj
 
Phylogenetics Analysis in R
Phylogenetics Analysis in RPhylogenetics Analysis in R
Phylogenetics Analysis in R
Klaus Schliep
 
13_Unsupervised Learning.pdf
13_Unsupervised Learning.pdf13_Unsupervised Learning.pdf
13_Unsupervised Learning.pdf
EmanAsem4
 
Path compression
Path compressionPath compression
Path compression
DEEPIKA T
 
learning about union find algorithm lectures
learning about union find algorithm lectureslearning about union find algorithm lectures
learning about union find algorithm lectures
NamPhmPhng9
 
computer notes - Data Structures - 29
computer notes - Data Structures - 29computer notes - Data Structures - 29
computer notes - Data Structures - 29
ecomputernotes
 
Computer notes - Maze Generator
Computer notes - Maze GeneratorComputer notes - Maze Generator
Computer notes - Maze Generator
ecomputernotes
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
ecomputernotes
 
Stochastic optimization from mirror descent to recent algorithms
Stochastic optimization from mirror descent to recent algorithmsStochastic optimization from mirror descent to recent algorithms
Stochastic optimization from mirror descent to recent algorithms
Seonho Park
 
Computer notes data structures - 9
Computer notes   data structures - 9Computer notes   data structures - 9
Computer notes data structures - 9
ecomputernotes
 
Introduction to Some Tree based Learning Method
Introduction to Some Tree based Learning MethodIntroduction to Some Tree based Learning Method
Introduction to Some Tree based Learning Method
Honglin Yu
 
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
Andres Mendez-Vazquez
 
FiDoop: Parallel Mining of Frequent Itemsets Using MapReduce
FiDoop: Parallel Mining of Frequent Itemsets Using MapReduceFiDoop: Parallel Mining of Frequent Itemsets Using MapReduce
FiDoop: Parallel Mining of Frequent Itemsets Using MapReduce
IJCSIS Research Publications
 
Computer notes - The const Keyword
Computer notes - The const KeywordComputer notes - The const Keyword
Computer notes - The const Keyword
ecomputernotes
 
Artificial Neural Network
Artificial Neural NetworkArtificial Neural Network
Artificial Neural Network
Dessy Amirudin
 
5a.Greedy Technique_prims_Krus_Dijkstra.ppt
5a.Greedy Technique_prims_Krus_Dijkstra.ppt5a.Greedy Technique_prims_Krus_Dijkstra.ppt
5a.Greedy Technique_prims_Krus_Dijkstra.ppt
Suma Raj
 
Phylogenetics Analysis in R
Phylogenetics Analysis in RPhylogenetics Analysis in R
Phylogenetics Analysis in R
Klaus Schliep
 
13_Unsupervised Learning.pdf
13_Unsupervised Learning.pdf13_Unsupervised Learning.pdf
13_Unsupervised Learning.pdf
EmanAsem4
 
Path compression
Path compressionPath compression
Path compression
DEEPIKA T
 
learning about union find algorithm lectures
learning about union find algorithm lectureslearning about union find algorithm lectures
learning about union find algorithm lectures
NamPhmPhng9
 
Ad

More from ecomputernotes (20)

computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
ecomputernotes
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
ecomputernotes
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15
ecomputernotes
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
ecomputernotes
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
ecomputernotes
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
ecomputernotes
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
ecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
ecomputernotes
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
ecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
ecomputernotes
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
ecomputernotes
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
ecomputernotes
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
ecomputernotes
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
ecomputernotes
 
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY ClauseComputer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY Clause
ecomputernotes
 
Computer notes - Manipulating Data
Computer notes - Manipulating DataComputer notes - Manipulating Data
Computer notes - Manipulating Data
ecomputernotes
 
Computer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT StatementsComputer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT Statements
ecomputernotes
 
computer notes - Data Structures - 14
computer notes - Data Structures - 14computer notes - Data Structures - 14
computer notes - Data Structures - 14
ecomputernotes
 
computer notes - Data Structures - 10
computer notes - Data Structures - 10computer notes - Data Structures - 10
computer notes - Data Structures - 10
ecomputernotes
 
Computer notes - Controlling User Access
Computer notes - Controlling User AccessComputer notes - Controlling User Access
Computer notes - Controlling User Access
ecomputernotes
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
ecomputernotes
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
ecomputernotes
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15
ecomputernotes
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
ecomputernotes
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
ecomputernotes
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
ecomputernotes
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
ecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
ecomputernotes
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
ecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
ecomputernotes
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
ecomputernotes
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
ecomputernotes
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
ecomputernotes
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
ecomputernotes
 
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY ClauseComputer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY Clause
ecomputernotes
 
Computer notes - Manipulating Data
Computer notes - Manipulating DataComputer notes - Manipulating Data
Computer notes - Manipulating Data
ecomputernotes
 
Computer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT StatementsComputer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT Statements
ecomputernotes
 
computer notes - Data Structures - 14
computer notes - Data Structures - 14computer notes - Data Structures - 14
computer notes - Data Structures - 14
ecomputernotes
 
computer notes - Data Structures - 10
computer notes - Data Structures - 10computer notes - Data Structures - 10
computer notes - Data Structures - 10
ecomputernotes
 
Computer notes - Controlling User Access
Computer notes - Controlling User AccessComputer notes - Controlling User Access
Computer notes - Controlling User Access
ecomputernotes
 

Recently uploaded (20)

YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 

Computer notes - Analysis of Union

  • 1. Class No.30 Data Structures http://ecomputernotes.com
  • 2. Running Time Analysis Union is clearly a constant time operation. Running time of find ( i ) is proportional to the height of the tree containing node i . This can be proportional to n in the worst case (but not always) Goal: Modify union to ensure that heights stay small http://ecomputernotes.com
  • 3. Union by Size Maintain sizes (number of nodes) of all trees, and during union. Make smaller tree the subtree of the larger one. Implementation: for each root node i , instead of setting parent[i] to -1 , set it to -k if tree rooted at i has k nodes. This also called union-by-weight . http://ecomputernotes.com
  • 4. Union by Size union(i,j): root1 = find(i); root2 = find(j); if (root1 != root2) if (parent[root1] <= parent[root2]) { // first tree has more nodes parent[root1] += parent[root2]; parent[root2] = root1; } else { // second tree has more nodes parent[root2] += parent[root1]; parent[root1] = root2; } http://ecomputernotes.com
  • 5. Union by Size Eight elements, initially in different sets. http://ecomputernotes.com 1 2 3 4 5 6 7 8 -1 -1 -1 -1 -1 -1 -1 -1 1 2 3 4 5 6 7 8
  • 6. Union by Size Union(4,6) http://ecomputernotes.com 1 2 3 4 5 6 7 8 -1 -1 -1 -2 -1 4 -1 -1 1 2 3 4 5 6 7 8
  • 7. Union by Size Union(2,3) http://ecomputernotes.com 1 2 3 4 5 6 7 8 -1 -2 2 -2 -1 4 -1 -1 1 2 3 4 5 6 7 8
  • 8. Union by Size Union(1,4) http://ecomputernotes.com 1 2 3 4 5 6 7 8 4 -2 2 -3 -1 4 -1 -1 1 2 3 4 5 6 7 8
  • 9. Union by Size Union(2,4) http://ecomputernotes.com 1 2 3 4 5 6 7 8 4 4 2 -5 -1 4 -1 -1 1 2 3 4 5 6 7 8
  • 10. Union by Size Union(5,4) http://ecomputernotes.com 1 2 3 4 5 6 7 8 4 4 2 -6 4 4 -1 -1 1 2 3 4 5 6 7 8
  • 11. Analysis of Union by Size If unions are done by weight (size), the depth of any element is never greater than log 2 n . http://ecomputernotes.com
  • 12. Analysis of Union by Size Intuitive Proof: Initially, every element is at depth zero. When its depth increases as a result of a union operation (it’s in the smaller tree), it is placed in a tree that becomes at least twice as large as before (union of two equal size trees). How often can each union be done? -- log 2 n times, because after at most log 2 n unions, the tree will contain all n elements. http://ecomputernotes.com
  • 13. Union by Height Alternative to union-by-size strategy: maintain heights, During union, make a tree with smaller height a subtree of the other. Details are left as an exercise. http://ecomputernotes.com
  • 14. Sprucing up Find So far we have tried to optimize union . Can we optimize find ? Yes, using path compression (or compaction). http://ecomputernotes.com
  • 15. Sprucing up Find During find ( i ), as we traverse the path from i to root, update parent entries for all these nodes to the root. This reduces the heights of all these nodes. Pay now, and reap benefits later! Subsequent find may do less work http://ecomputernotes.com
  • 16. Sprucing up Find Updated code for find find (i) { if (parent[i] < 0) return i; else return parent[i] = find(parent[i]); } http://ecomputernotes.com
  • 17. Path Compression Find(1) 12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
  • 18. Path Compression Find(1) 12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
  • 19. Path Compression Find(1) 12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
  • 20. Path Compression Find(1) 12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
  • 21. Path Compression Find(1) 12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
  • 22. Path Compression Find ( a ) http://ecomputernotes.com a b f c d e
  • 23. Path Compression Find ( a ) http://ecomputernotes.com a b f c d e
  • 24. Timing with Optimization Theorem: A sequence of m union and find operations, n of which are find operations, can be performed on a disjoint-set forest with union by rank (weight or height) and path compression in worst case time proportional to ( m  ( n ))   ( n )  is the inverse Ackermann’s function which grows extremely slowly. For all practical puposes,  ( n )  4. Union-find is essentially proportional to m for a sequence of m operations, linear in m . http://ecomputernotes.com

Editor's Notes

  • #3: End of lecture 35, start of lecture 36
  • #25: End of lecture 36