SlideShare a Scribd company logo
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

PPT
Matrix Multiplication(An example of concurrent programming)
PPT
Sparse Matrix and Polynomial
DOCX
Matrix multiplicationdesign
PPTX
Travelling Salesman
PPTX
PPTX
Matrix mult class-17
PPTX
Dynamic Programming - Part 1
PDF
Answers withexplanations
Matrix Multiplication(An example of concurrent programming)
Sparse Matrix and Polynomial
Matrix multiplicationdesign
Travelling Salesman
Matrix mult class-17
Dynamic Programming - Part 1
Answers withexplanations

What's hot (20)

PPT
Lecture 2 family of fcts
PDF
MATLAB for Technical Computing
KEY
An Introduction to Functional Programming using Haskell
PDF
Cs101 endsem 2014
PDF
Programming with matlab session 6
PPTX
Differential calculus
DOCX
Array
PDF
Ee693 questionshomework
PDF
Extended network and algorithm finding maximal flows
PPT
Tsp branch and bound
PPTX
Different types of functions
PDF
Gentle Introduction to Functional Programming
PPTX
Lecture two
PDF
Programming with matlab session 1
PDF
Analysis and design of algorithms part 4
PPTX
The dag representation of basic blocks
PPT
PPTX
Computation Assignment Help
PPTX
FDM Numerical solution of Laplace Equation using MATLAB
PPTX
strassen matrix multiplication algorithm
Lecture 2 family of fcts
MATLAB for Technical Computing
An Introduction to Functional Programming using Haskell
Cs101 endsem 2014
Programming with matlab session 6
Differential calculus
Array
Ee693 questionshomework
Extended network and algorithm finding maximal flows
Tsp branch and bound
Different types of functions
Gentle Introduction to Functional Programming
Lecture two
Programming with matlab session 1
Analysis and design of algorithms part 4
The dag representation of basic blocks
Computation Assignment Help
FDM Numerical solution of Laplace Equation using MATLAB
strassen matrix multiplication algorithm
Ad

Viewers also liked (20)

PPTX
joaquina aizpuru
PPTX
From Ink to Pixel and Beyond...
PPTX
Timebanking, ICT, employment and employability in a European context
PPT
電子書籍と図書館 120619Ver.3
PDF
Worldcup2010 gs report
PPT
2010 Training And Educational Offerings For Northern Ohio’S
PPT
Chap04alg
PPT
Tobch lecture
PPT
Lecture911
PDF
Raporti Mbi Shqiperine ... Raport Su Albania
PPT
Chap02alg
DOCX
Lissajous pattern
PPTX
Nings To Knols Upload
PDF
Hybrid worlds fungi updated - crews
DOC
Protsesor
PPT
Lecture911
PPT
Disco Dirt Evaluation
PPT
Program Edukacyjny W Fabryce Emalii Oskara Schindlera
PDF
Barrier of packaging
DOC
Pm104 standard
joaquina aizpuru
From Ink to Pixel and Beyond...
Timebanking, ICT, employment and employability in a European context
電子書籍と図書館 120619Ver.3
Worldcup2010 gs report
2010 Training And Educational Offerings For Northern Ohio’S
Chap04alg
Tobch lecture
Lecture911
Raporti Mbi Shqiperine ... Raport Su Albania
Chap02alg
Lissajous pattern
Nings To Knols Upload
Hybrid worlds fungi updated - crews
Protsesor
Lecture911
Disco Dirt Evaluation
Program Edukacyjny W Fabryce Emalii Oskara Schindlera
Barrier of packaging
Pm104 standard
Ad

Similar to Chap05alg (20)

PPT
Chap08alg
PPT
Chap08alg
PDF
Tucker tensor analysis of Matern functions in spatial statistics
DOC
algorithm Unit 3
PPT
Chap11alg
PPT
Chap11alg
DOC
Unit 3 daa
PDF
Indefinite Integration One shot Revision
PPT
Chap12alg
PPT
Chap12alg
PPTX
Data structure 8.pptx
PPT
Chap09alg
PPT
Chap09alg
PPTX
1. Week 1_ Functions and Evaluate Functions.pptx
PPT
Chapter 2 Image Processing: Pixel Relation
PPT
Chap06alg
PPT
Chap06alg
PPTX
Nbvtalkatbzaonencryptionpuzzles
PPTX
Nbvtalkatbzaonencryptionpuzzles
PPTX
Introduction to Monads in Scala (2)
Chap08alg
Chap08alg
Tucker tensor analysis of Matern functions in spatial statistics
algorithm Unit 3
Chap11alg
Chap11alg
Unit 3 daa
Indefinite Integration One shot Revision
Chap12alg
Chap12alg
Data structure 8.pptx
Chap09alg
Chap09alg
1. Week 1_ Functions and Evaluate Functions.pptx
Chapter 2 Image Processing: Pixel Relation
Chap06alg
Chap06alg
Nbvtalkatbzaonencryptionpuzzles
Nbvtalkatbzaonencryptionpuzzles
Introduction to Monads in Scala (2)

More from Munhchimeg (20)

PPT
Ded algorithm1
PPT
Ded algorithm
PPT
Tobch lecture1
PPT
Recursive
PPT
Lecture916
PPT
Lecture915
PPT
Lecture914
PPT
Lecture913
PPT
Lecture912
PPT
Lecture910
PPT
Lecture9
PPT
Lecture8
PPT
Lecture7
PPT
Lecture6
PPT
Lecture5
PPT
Lecture4
PPT
Lecture3
DOC
Protsesor
DOC
Pm104 2004 2005
PPT
Lecture916
Ded algorithm1
Ded algorithm
Tobch lecture1
Recursive
Lecture916
Lecture915
Lecture914
Lecture913
Lecture912
Lecture910
Lecture9
Lecture8
Lecture7
Lecture6
Lecture5
Lecture4
Lecture3
Protsesor
Pm104 2004 2005
Lecture916

Recently uploaded (20)

PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
STKI Israel Market Study 2025 version august
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Architecture types and enterprise applications.pdf
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
WOOl fibre morphology and structure.pdf for textiles
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
A novel scalable deep ensemble learning framework for big data classification...
Developing a website for English-speaking practice to English as a foreign la...
STKI Israel Market Study 2025 version august
A comparative study of natural language inference in Swahili using monolingua...
Architecture types and enterprise applications.pdf
O2C Customer Invoices to Receipt V15A.pptx
Univ-Connecticut-ChatGPT-Presentaion.pdf
Getting started with AI Agents and Multi-Agent Systems
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Assigned Numbers - 2025 - Bluetooth® Document
NewMind AI Weekly Chronicles – August ’25 Week III
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
WOOl fibre morphology and structure.pdf for textiles
observCloud-Native Containerability and monitoring.pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
1 - Historical Antecedents, Social Consideration.pdf
Programs and apps: productivity, graphics, security and other tools
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf

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 ] } }