Docsity

Divide and Conquer - Algorithms and Applications in Java - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Divide and Conquer - Algorithms and Applications in Java - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Divide And Conquer

  • Distinguish between small and large instances.
  • Small instances solved differently from large ones.

Small And Large Instance

  • Small instance.

 Sort a list that has n <= 10 elements.  Find the minimum of n <= 2 elements.

  • Large instance.

 Sort a list that has n > 10 elements.  Find the minimum of n > 2 elements.

Solving A Large Instance

  • A large instance is solved as follows:

 Divide the large instance into k >= 2 smaller instances.  Solve the smaller instances somehow.  Combine the results of the smaller instances to obtain the result for the original large instance.

Sort A Large List

  • Sort a list that has n > 10 elements.

 Sort 15 elements by dividing them into 2 smaller lists. One list has 7 elements and the other has 8.  Sort these two lists using the method for small lists.  Merge the two sorted lists into a single sorted list.

Recursion In Divide And Conquer

  • Often the smaller instances that result from the divide step are instances of the original problem (true for our sort and min problems). In this case,  If the new instance is a small instance, it is solved using the method for small instances.  If the new instance is a large instance, it is solved using the divide-and-conquer method recursively.
  • Generally, performance is best when the smaller instances that result from the divide step are of approximately the same size.

Recursive Find Min

  • Find the minimum of 20 elements.

 Divide into two groups of 10 elements each.  Find the minimum element in each group recursively. The recursion terminates when the number of elements is <= 2. At this time the minimum is found using the method for small instances.  Compare the minimums of the two groups to determine the overall minimum.

Our Definition Of A Chessboard

A chessboard is an n x n grid, where n is a power of 2.

1x1 2x2 4x4 8x

A defective chessboard is a chessboard that has one unavailable (defective) position.

1x1 2x2 4x4 8x

Tiling A Defective Chessboard

Place (n 2 - 1)/3 triominoes on an n x n defective chessboard so that all n 2 - 1 nondefective positions are covered.

1x1 2x2 4x4 8x

Tiling A Defective Chessboard

Divide into four smaller chessboards. 4 x 4

One of these is a defective 4 x 4 chessboard.

Tiling A Defective Chessboard

Complexity

  • Let n = 2k.
  • Let t(k) be the time taken to tile a 2 k^ x 2 k defective chessboard.
  • t(0) = d, where d is a constant.
  • t(k) = 4t(k-1) + c, when k > 0. Here c is a constant.
  • Recurrence equation for t().

Min And Max

Find the lightest and heaviest of n elements using a balance that allows you to compare the weight of 2 elements.

Minimize the number of comparisons.

Max Element

  • Find element with max weight from w[0:n-1].

maxElement = 0; for (int i = 1; i < n; i++)

if (w[maxElement] < w[i]) maxElement = i;

  • Number of comparisons of w values is n-1.