SlideShare a Scribd company logo
1 of 11
CHAPTER 5 Divide and Conquer
Algorithm 5.1.4 Tiling a Deficient Board with Trominoes This algorithm constructs a tiling by trominoes of a deficient  n × n  board where  n  is a power of 2.
Input Parameters:  n , a power of 2 (the board size); the location  L  of the missing square Output Parameters: None tile ( n , L ) { if ( n  == 2) { // the board is a right tromino  T tile with  T return } divide the board into four  n /2 ×  n /2 subboards place one tromino as in Figure 5.1.4(b) // each of the 1 × 1 squares in this tromino  // is considered as missing let  m 1 , m 2 , m 3 , m 4  be the locations of the missing squares tile( n /2, m 1 ) tile( n /2, m 2 ) tile( n /2, m 3 ) tile( n /2, m 4 ) }
Algorithm 5.2.2 Merge This algorithm receives as input indexes  i ,  m , and  j , and an array  a , where  a [ i ], ... ,  a [ m ] and  a [ m  +1], ... ,  a [ j ] are each sorted in nondecreasing order. These two nondecreasing subarrays are merged into a single nondecreasing array.
Input Parameters:  a , i , m , j Output Parameter:  a merge ( a , i , m , j ) { p  =  i  // index in  a [ i ], ... ,  a [ m ] q  =  m  + 1 // index in  a [ m  + 1], ... ,  a [ j ] r  =  i  // index in a local array  c while ( p  ≤  m  &&  q  ≤  j ) { // copy smaller value to  c if ( a [ p ] ≤  a [ q ]) { c [ r ] =  a [ p ] p  =  p  + 1 } else { c [ r ] =  a [ q ] q  =  q  + 1 } r  =  r  + 1 } ...
... // copy remainder, if any, of first subarray to  c while ( p  ≤  m ) { c [ r ] =  a [ p ] p  =  p  + 1 r  =  r  + 1 } // copy remainder, if any, of second subarray to  c while ( q  ≤  j ) { c [ r ] =  a [ q ] q  =  q  + 1 r  =  r  + 1 } // copy  c  back to  a for  r  =  i  to  j a [ r ] =  c [ r ] }
Algorithm 5.2.3 Mergesort This algorithm sorts the array  a [ i ], ... ,  a [ j ] in nondecreasing order. It uses the merge algorithm (Algorithm 5.2.2). Input Parameters:  a , i , j Output Parameter:  a mergesort ( a , i , j ) { // if only one element, just return if ( i  ==  j ) return // divide a into two nearly equal parts m  = ( i  +  j )/2 // sort each half mergesort ( a , i , m ) mergesort ( a , m  + 1, j ) // merge the two sorted halves merge ( a , i , m , j ) }
Algorithm 5.3.2 Finding the Distance Between a Closest Pair of Points This algorithm finds the distance between a closest pair of points. The input is an array  p [1], ... ,  p [ n ] of  n  = 2 points. If  p  is a point,  p . x   is the  x - coordinate of  p , and  p . y   is the  y -coordinate of  p . The function  merge  is Algorithm 5.2.2 and  mergesort  is Algorithm 5.2.3. The function  merge  uses as the key the  y -coordinate of the point. The function  mergesort  uses as the key either the  x - or  y -coordinate of the point; the comments indicate which. The function  dist ( p , q ) returns the Euclidean distance between points  p  and  q .
Input Parameters:  p Output Parameter: None closest_pair ( p ) { n  =  p . last mergesort ( p ,1, n ) // sort by x-coordinate return  rec_cl_pair ( p ,1, n ) } //  rec_cl_pair  assumes that input is sorted by  x -coordinate. // At termination, the input is sorted by  y -coordinate. rec_cl_pair ( p , i , j ) { if ( j  -  i  < 3) { mergesort ( p , i , j ) // sort by  y -coordinate // find the distance delta between a closest pair delta  =  dist ( p [ i ], p [ i  + 1]) if ( j  -  i  == 1) // two points return delta // three points if ( dist ( p [ i  + 1], p [ i  + 2]) <  delta ) delta  =  dist ( p [ i  + 1], p [ i  + 2])  if ( dist ( p [ i ], p [ i  + 2]) <  delta ) delta  =  dist ( p [ i ], p [ i  + 2])  return  delta } ...
... k  = ( i  +  j )/ 2 l  =  p [ k ]. x deltaL  =  rec_cl_pair ( p , i , k ) deltaR  =  rec_cl_pair ( p , k  + 1, j ) delta  =  min ( deltaL , deltaR ) //  p [ i ], ... ,  p [ k ] is now sorted by  y -coordinate, and //  p [ k  + 1], ... ,  p [ j ] is now sorted by  y -coordinate. merge ( p , i , k , j ) //  p [ i ], ... ,  p [ j ] is now sorted by  y -coordinate. // store points in the vertical strip in  v . t  = 0 for  k  =  i  to  j if ( p [ k ]. x  > l -  delta  &&  p [ k ]. x  < l +  delta ) { t  =  t  + 1 v [ t ] =  p [ k ] } // look for closer pairs in the strip by comparing // each point in the strip to the next 7 points. for  k  = 1 to  t  - 1 for  s  =  k  + 1 to  min ( t , k  + 7) delta  =  min ( delta , dist ( v [ k ], v [ s ])) return  delta }
Algorithm 5.4.1 Matrix Product This algorithm computes the product  C  of the  n × n  matrices  A  and  B  directly from the definition of the matrix product. Input Parameters:  A , B Output Parameter:  C matrix_product ( A , B , C ) { n  =  A . last for  i  = 1 to  n for  j  = 1 to  n  { C [ i ][ j ] = 0 for  k  = 1 to  n C [ i ][ j ] =  C [ i ][ j ] +  A [ i ][ k ] *  B [ k ][ j ] } }

More Related Content

What's hot

design and analysis of algorithm
design and analysis of algorithmdesign and analysis of algorithm
design and analysis of algorithmMuhammad Arish
 
Algorithm Using Divide And Conquer
Algorithm Using Divide And ConquerAlgorithm Using Divide And Conquer
Algorithm Using Divide And ConquerUrviBhalani2
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals reviewElifTech
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02Krish_ver2
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Comparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategiesComparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategiesTalha Shaikh
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsAmrinder Arora
 
08 decrease and conquer spring 15
08 decrease and conquer spring 1508 decrease and conquer spring 15
08 decrease and conquer spring 15Hira Gul
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programingrupali_2bonde
 

What's hot (20)

algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
algorithm unit 1
algorithm unit 1algorithm unit 1
algorithm unit 1
 
design and analysis of algorithm
design and analysis of algorithmdesign and analysis of algorithm
design and analysis of algorithm
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
Algorithm Using Divide And Conquer
Algorithm Using Divide And ConquerAlgorithm Using Divide And Conquer
Algorithm Using Divide And Conquer
 
Daa unit 2
Daa unit 2Daa unit 2
Daa unit 2
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Comparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategiesComparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategies
 
algorithm Unit 5
algorithm Unit 5 algorithm Unit 5
algorithm Unit 5
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
5.1 greedy 03
5.1 greedy 035.1 greedy 03
5.1 greedy 03
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 
08 decrease and conquer spring 15
08 decrease and conquer spring 1508 decrease and conquer spring 15
08 decrease and conquer spring 15
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 

Viewers also liked (16)

Mergesort
MergesortMergesort
Mergesort
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
 
Treatments of Mental Illnesses
Treatments of Mental IllnessesTreatments of Mental Illnesses
Treatments of Mental Illnesses
 
Scan scheduling 50 1
Scan scheduling 50 1Scan scheduling 50 1
Scan scheduling 50 1
 
C look scheduling 51 1
C look scheduling 51 1C look scheduling 51 1
C look scheduling 51 1
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
 
Disk scheduling algorithm.52
Disk scheduling algorithm.52Disk scheduling algorithm.52
Disk scheduling algorithm.52
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
 
Disk scheduling
Disk schedulingDisk scheduling
Disk scheduling
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
polymorphism
polymorphism polymorphism
polymorphism
 
file handling c++
file handling c++file handling c++
file handling c++
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 

Similar to Chap05alg

Tucker tensor analysis of Matern functions in spatial statistics
Tucker tensor analysis of Matern functions in spatial statistics Tucker tensor analysis of Matern functions in spatial statistics
Tucker tensor analysis of Matern functions in spatial statistics Alexander Litvinenko
 
Indefinite Integration One shot Revision
Indefinite Integration One shot Revision Indefinite Integration One shot Revision
Indefinite Integration One shot Revision CHANDHINIJAYARAMAN
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptxSajalFayyaz
 
Chapter 2 Image Processing: Pixel Relation
Chapter 2 Image Processing: Pixel RelationChapter 2 Image Processing: Pixel Relation
Chapter 2 Image Processing: Pixel RelationVarun Ojha
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)stasimus
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024RUHULAMINHAZARIKA
 
chap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithmchap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithmSadiaSharmin40
 

Similar to Chap05alg (20)

Chap08alg
Chap08algChap08alg
Chap08alg
 
Chap08alg
Chap08algChap08alg
Chap08alg
 
Tucker tensor analysis of Matern functions in spatial statistics
Tucker tensor analysis of Matern functions in spatial statistics Tucker tensor analysis of Matern functions in spatial statistics
Tucker tensor analysis of Matern functions in spatial statistics
 
Chap11alg
Chap11algChap11alg
Chap11alg
 
Chap11alg
Chap11algChap11alg
Chap11alg
 
Indefinite Integration One shot Revision
Indefinite Integration One shot Revision Indefinite Integration One shot Revision
Indefinite Integration One shot Revision
 
Chap12alg
Chap12algChap12alg
Chap12alg
 
Chap12alg
Chap12algChap12alg
Chap12alg
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
 
Chap09alg
Chap09algChap09alg
Chap09alg
 
Chap09alg
Chap09algChap09alg
Chap09alg
 
Chapter 2 Image Processing: Pixel Relation
Chapter 2 Image Processing: Pixel RelationChapter 2 Image Processing: Pixel Relation
Chapter 2 Image Processing: Pixel Relation
 
Chap06alg
Chap06algChap06alg
Chap06alg
 
Chap06alg
Chap06algChap06alg
Chap06alg
 
Nbvtalkatbzaonencryptionpuzzles
NbvtalkatbzaonencryptionpuzzlesNbvtalkatbzaonencryptionpuzzles
Nbvtalkatbzaonencryptionpuzzles
 
Nbvtalkatbzaonencryptionpuzzles
NbvtalkatbzaonencryptionpuzzlesNbvtalkatbzaonencryptionpuzzles
Nbvtalkatbzaonencryptionpuzzles
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
E33018021
E33018021E33018021
E33018021
 
chap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithmchap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithm
 

More from Munkhchimeg (20)

Protsesor
ProtsesorProtsesor
Protsesor
 
Lecture916
Lecture916Lecture916
Lecture916
 
Lecture915
Lecture915Lecture915
Lecture915
 
Lecture914
Lecture914Lecture914
Lecture914
 
Lecture913
Lecture913Lecture913
Lecture913
 
Lecture911
Lecture911Lecture911
Lecture911
 
Lecture912
Lecture912Lecture912
Lecture912
 
Lecture910
Lecture910Lecture910
Lecture910
 
Lecture5
Lecture5Lecture5
Lecture5
 
Lecture9
Lecture9Lecture9
Lecture9
 
Lecture8
Lecture8Lecture8
Lecture8
 
Lecture7
Lecture7Lecture7
Lecture7
 
Lecture6
Lecture6Lecture6
Lecture6
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture3
Lecture3Lecture3
Lecture3
 
Ded Algorithm
Ded AlgorithmDed Algorithm
Ded Algorithm
 
Ded Algorithm1
Ded Algorithm1Ded Algorithm1
Ded Algorithm1
 
Tobch Lecture
Tobch LectureTobch Lecture
Tobch Lecture
 
Lecture914
Lecture914Lecture914
Lecture914
 
Tobch Lecture
Tobch LectureTobch Lecture
Tobch Lecture
 

Recently uploaded

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

Chap05alg

  • 1. CHAPTER 5 Divide and Conquer
  • 2. Algorithm 5.1.4 Tiling a Deficient Board with Trominoes This algorithm constructs a tiling by trominoes of a deficient n × n board where n is a power of 2.
  • 3. Input Parameters: n , a power of 2 (the board size); the location L of the missing square Output Parameters: None tile ( n , L ) { if ( n == 2) { // the board is a right tromino T tile with T return } divide the board into four n /2 × n /2 subboards place one tromino as in Figure 5.1.4(b) // each of the 1 × 1 squares in this tromino // is considered as missing let m 1 , m 2 , m 3 , m 4 be the locations of the missing squares tile( n /2, m 1 ) tile( n /2, m 2 ) tile( n /2, m 3 ) tile( n /2, m 4 ) }
  • 4. Algorithm 5.2.2 Merge This algorithm receives as input indexes i , m , and j , and an array a , where a [ i ], ... , a [ m ] and a [ m +1], ... , a [ j ] are each sorted in nondecreasing order. These two nondecreasing subarrays are merged into a single nondecreasing array.
  • 5. Input Parameters: a , i , m , j Output Parameter: a merge ( a , i , m , j ) { p = i // index in a [ i ], ... , a [ m ] q = m + 1 // index in a [ m + 1], ... , a [ j ] r = i // index in a local array c while ( p ≤ m && q ≤ j ) { // copy smaller value to c if ( a [ p ] ≤ a [ q ]) { c [ r ] = a [ p ] p = p + 1 } else { c [ r ] = a [ q ] q = q + 1 } r = r + 1 } ...
  • 6. ... // copy remainder, if any, of first subarray to c while ( p ≤ m ) { c [ r ] = a [ p ] p = p + 1 r = r + 1 } // copy remainder, if any, of second subarray to c while ( q ≤ j ) { c [ r ] = a [ q ] q = q + 1 r = r + 1 } // copy c back to a for r = i to j a [ r ] = c [ r ] }
  • 7. Algorithm 5.2.3 Mergesort This algorithm sorts the array a [ i ], ... , a [ j ] in nondecreasing order. It uses the merge algorithm (Algorithm 5.2.2). Input Parameters: a , i , j Output Parameter: a mergesort ( a , i , j ) { // if only one element, just return if ( i == j ) return // divide a into two nearly equal parts m = ( i + j )/2 // sort each half mergesort ( a , i , m ) mergesort ( a , m + 1, j ) // merge the two sorted halves merge ( a , i , m , j ) }
  • 8. Algorithm 5.3.2 Finding the Distance Between a Closest Pair of Points This algorithm finds the distance between a closest pair of points. The input is an array p [1], ... , p [ n ] of n = 2 points. If p is a point, p . x is the x - coordinate of p , and p . y is the y -coordinate of p . The function merge is Algorithm 5.2.2 and mergesort is Algorithm 5.2.3. The function merge uses as the key the y -coordinate of the point. The function mergesort uses as the key either the x - or y -coordinate of the point; the comments indicate which. The function dist ( p , q ) returns the Euclidean distance between points p and q .
  • 9. Input Parameters: p Output Parameter: None closest_pair ( p ) { n = p . last mergesort ( p ,1, n ) // sort by x-coordinate return rec_cl_pair ( p ,1, n ) } // rec_cl_pair assumes that input is sorted by x -coordinate. // At termination, the input is sorted by y -coordinate. rec_cl_pair ( p , i , j ) { if ( j - i < 3) { mergesort ( p , i , j ) // sort by y -coordinate // find the distance delta between a closest pair delta = dist ( p [ i ], p [ i + 1]) if ( j - i == 1) // two points return delta // three points if ( dist ( p [ i + 1], p [ i + 2]) < delta ) delta = dist ( p [ i + 1], p [ i + 2]) if ( dist ( p [ i ], p [ i + 2]) < delta ) delta = dist ( p [ i ], p [ i + 2]) return delta } ...
  • 10. ... k = ( i + j )/ 2 l = p [ k ]. x deltaL = rec_cl_pair ( p , i , k ) deltaR = rec_cl_pair ( p , k + 1, j ) delta = min ( deltaL , deltaR ) // p [ i ], ... , p [ k ] is now sorted by y -coordinate, and // p [ k + 1], ... , p [ j ] is now sorted by y -coordinate. merge ( p , i , k , j ) // p [ i ], ... , p [ j ] is now sorted by y -coordinate. // store points in the vertical strip in v . t = 0 for k = i to j if ( p [ k ]. x > l - delta && p [ k ]. x < l + delta ) { t = t + 1 v [ t ] = p [ k ] } // look for closer pairs in the strip by comparing // each point in the strip to the next 7 points. for k = 1 to t - 1 for s = k + 1 to min ( t , k + 7) delta = min ( delta , dist ( v [ k ], v [ s ])) return delta }
  • 11. Algorithm 5.4.1 Matrix Product This algorithm computes the product C of the n × n matrices A and B directly from the definition of the matrix product. Input Parameters: A , B Output Parameter: C matrix_product ( A , B , C ) { n = A . last for i = 1 to n for j = 1 to n { C [ i ][ j ] = 0 for k = 1 to n C [ i ][ j ] = C [ i ][ j ] + A [ i ][ k ] * B [ k ][ j ] } }