SlideShare a Scribd company logo
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 Small Instance 
• A small instance is solved using some 
direct/simple strategy. 
 Sort a list that has n <= 10 elements. 
• Use count, insertion, bubble, or selection sort. 
 Find the minimum of n <= 2 elements. 
• When n = 0, there is no minimum element. 
• When n = 1, the single element is the minimum. 
• When n = 2, compare the two elements and 
determine which is smaller.
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.
Find The Min Of A Large List 
• Find the minimum of 20 elements. 
 Divide into two groups of 10 elements each. 
 Find the minimum element in each group somehow. 
 Compare the minimums of each group to determine 
the overall minimum.
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.
Tiling A Defective Chessboard
Our Definition Of A Chessboard 
A chessboard is an n x n grid, where n is a 
power of 2. 
1x1 2x2 4x4 8x8
A defective chessboard is a chessboard that 
has one unavailable (defective) position. 
1x1 2x2 4x4 8x8
A Triomino 
A triomino is an L shaped object that can 
cover three squares of a chessboard. 
A triomino has four orientations.
Tiling A Defective Chessboard 
Place (n2 - 1)/3 triominoes on an n x n 
defective chessboard so that all n2 - 1 
nondefective positions are covered. 
1x1 2x2 4x4 8x8
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 
Make the other three 4 x 4 chessboards 
defective by placing a triomino at their common 
cRoercnuerrs.ively tile the four defective 4 x 4 
chessboards.
Tiling A Defective Chessboard
Complexity 
• Let n = 2k. 
• Let t(k) be the time taken to tile a 2k x 2k 
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().
Substitution Method 
t(k) = 4t(k-1) + c 
= 4[4t(k-2) + c] + c 
= 42 t(k-2) + 4c + c 
= 42[4t(k-3) + c] + 4c + c 
= 43 t(k-3) + 42c + 4c + c 
= … 
= 4k t(0) + 4k-1c + 4k-2c + ... + 42c + 4c + c 
= 4k d + 4k-1c + 4k-2c + ... + 42c + 4c + c 
= Theta(4k) 
= Theta(number of triominoes placed)
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.
Min And Max 
• Find the max of n elements making n-1 
comparisons. 
• Find the min of the remaining n-1 elements 
making n-2 comparisons. 
• Total number of comparisons is 2n-3.
Divide And Conquer 
• Small instance. 
 n <= 2. 
 Find the min and max element making at most 
one comparison.
Large Instance Min And Max 
 n > 2. 
 Divide the n elements into 2 groups A and B 
with floor(n/2) and ceil(n/2) elements, 
respectively. 
 Find the min and max of each group 
recursively. 
 Overall min is min{min(A), min(B)}. 
 Overall max is max{max(A), max(B)}.
Min And Max Example 
• Find the min and max of {3,5,6,2,4,9,3,1}. 
• Large instance. 
• A = {3,5,6,2} and B = {4,9,3,1}. 
• min(A) = 2, min(B) = 1. 
• max(A) = 6, max(B) = 9. 
• min{min(A),min(B)} = 1. 
• max{max(A), max(B)} = 9.
Dividing Into Smaller Instances 
{8,2,6,3,9,1,7,5,4,2,8} 
{8,2,6,3,9} {1,7,5,4,2,8} 
{8,2} {6,3,9} 
{6} {3,9} 
{1,7,5} {4,2,8} 
{1} {7,5} {4} {2,8}
Solve Small Instances And Combine 
{8,2} 
{3,9} 
{2,9} 
{1,7} 
{2,8} 
{6} {3,9} {1} {2,8} {7,5} {4} {2,8} 
{6,6} {3,9} 
{1,1} {5,7} 
{4,4} {2,8} 
{1,8} 
{1,9}
Time Complexity 
• Let c(n) be the number of comparisons made 
when finding the min and max of n elements. 
• c(0) = c(1) = 0. 
• c(2) = 1. 
• When n > 2, 
c(n) = c(floor(n/2)) + c(ceil(n/2)) + 2 
• To solve the recurrence, assume n is a power of 2 
and use repeated substitution. 
• c(n) = ceil(3n/2) - 2.
Interpretation Of Recursive Version 
• The working of a recursive divide-and-conquer algorithm 
can be described by a tree—recursion tree. 
• The algorithm moves down the recursion tree dividing large 
instances into smaller ones. 
• Leaves represent small instances. 
• The recursive algorithm moves back up the tree combining 
the results from the subtrees. 
• The combining finds the min of the mins computed at 
leaves and the max of the leaf maxs.
Iterative Version 
• Start with n/2 groups with 2 elements each 
and possibly 1 group that has just 1element. 
• Find the min and max in each group. 
• Find the min of the mins. 
• Find the max of the maxs.
Iterative Version Example 
• {2,8,3,6,9,1,7,5,4,2,8 } 
• {2,8}, {3,6}, {9,1}, {7,5}, {4,2}, {8} 
• mins = {2,3,1,5,2,8} 
• maxs = {8,6,9,7,4,8} 
• minOfMins = 1 
• maxOfMaxs = 9
Comparison Count 
• Start with n/2 groups with 2 elements each 
and possibly 1 group that has just 1element. 
 No compares. 
• Find the min and max in each group. 
 floor(n/2) compares. 
• Find the min of the mins. 
 ceil(n/2) - 1 compares. 
• Find the max of the maxs. 
 ceil(n/2) - 1 compares. 
• Total is ceil(3n/2) - 2 compares.
Initialize A Heap 
• n > 1: 
 Initialize left subtree and right subtree recursively. 
 Then do a trickle down operation at the root. 
• t(n) = c, n <= 1. 
• t(n) = 2t(n/2) + d * height, n > 1. 
• c and d are constants. 
• Solve to get t(n) = O(n). 
• Implemented iteratively in Chapter 13.
Initialize A Loser Tree 
• n > 1: 
 Initialize left subtree. 
 Initialize right subtree. 
 Compare winners from left and right subtrees. 
 Loser is saved in root and winner is returned. 
• t(n) = c, n <= 1. 
• t(n) = 2t(n/2) + d, n > 1. 
• c and d are constants. 
• Solve to get t(n) = O(n). 
• Implemented iteratively in Chapter 14.

More Related Content

What's hot

Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
Shakila Mahjabin
 
K means clustering | K Means ++
K means clustering | K Means ++K means clustering | K Means ++
K means clustering | K Means ++
sabbirantor
 
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
Edureka!
 
Converting High Dimensional Problems to Low Dimensional Ones
Converting High Dimensional Problems to Low Dimensional OnesConverting High Dimensional Problems to Low Dimensional Ones
Converting High Dimensional Problems to Low Dimensional OnesStrand Life Sciences Pvt Ltd
 
K-Means manual work
K-Means manual workK-Means manual work
K-Means manual work
Dr.E.N.Sathishkumar
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
Module 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsModule 2 Design Analysis and Algorithms
Module 2 Design Analysis and Algorithms
Cool Guy
 
Cluster analysis using k-means method in R
Cluster analysis using k-means method in RCluster analysis using k-means method in R
Cluster analysis using k-means method in R
Vladimir Bakhrushin
 
勾配法
勾配法勾配法
勾配法
貴之 八木
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Sahil Kumar
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Emmanuel college
 
K-means Clustering Algorithm with Matlab Source code
K-means Clustering Algorithm with Matlab Source codeK-means Clustering Algorithm with Matlab Source code
K-means Clustering Algorithm with Matlab Source code
gokulprasath06
 
Intro to MATLAB and K-mean algorithm
Intro to MATLAB and K-mean algorithmIntro to MATLAB and K-mean algorithm
Intro to MATLAB and K-mean algorithm
khalid Shah
 

What's hot (20)

Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
K means clustering | K Means ++
K means clustering | K Means ++K means clustering | K Means ++
K means clustering | K Means ++
 
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
 
Converting High Dimensional Problems to Low Dimensional Ones
Converting High Dimensional Problems to Low Dimensional OnesConverting High Dimensional Problems to Low Dimensional Ones
Converting High Dimensional Problems to Low Dimensional Ones
 
K means Clustering Algorithm
K means Clustering AlgorithmK means Clustering Algorithm
K means Clustering Algorithm
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Data miningpresentation
Data miningpresentationData miningpresentation
Data miningpresentation
 
K-Means manual work
K-Means manual workK-Means manual work
K-Means manual work
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
 
main
mainmain
main
 
08 clustering
08 clustering08 clustering
08 clustering
 
Module 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsModule 2 Design Analysis and Algorithms
Module 2 Design Analysis and Algorithms
 
Cluster analysis using k-means method in R
Cluster analysis using k-means method in RCluster analysis using k-means method in R
Cluster analysis using k-means method in R
 
Quicksort
QuicksortQuicksort
Quicksort
 
勾配法
勾配法勾配法
勾配法
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
K-means Clustering Algorithm with Matlab Source code
K-means Clustering Algorithm with Matlab Source codeK-means Clustering Algorithm with Matlab Source code
K-means Clustering Algorithm with Matlab Source code
 
Intro to MATLAB and K-mean algorithm
Intro to MATLAB and K-mean algorithmIntro to MATLAB and K-mean algorithm
Intro to MATLAB and K-mean algorithm
 
Hierachical clustering
Hierachical clusteringHierachical clustering
Hierachical clustering
 

Viewers also liked

02. divide and conquer
02. divide and conquer02. divide and conquer
02. divide and conquer
Onkar Nath Sharma
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
Sadaf Ismail
 
Space complexity
Space complexitySpace complexity
Space complexity
Bhanusree Koduru
 
Tigreblanco
TigreblancoTigreblanco
Tigreblanco
pernutote
 
lecture 15
lecture 15lecture 15
lecture 15sajinsc
 
Greedy method1
Greedy method1Greedy method1
Greedy method1
Rajendran
 
Algorithm
AlgorithmAlgorithm
Algorithm
eShikshak
 
Flight MH370 community structure
Flight MH370 community structureFlight MH370 community structure
Flight MH370 community structure
Universiti Technologi Malaysia (UTM)
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
cinterviews
 
Marge Sort
Marge SortMarge Sort
Marge Sort
Ankit92Chitnavis
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Vikas Sharma
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
Melaku Bayih Demessie
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
Danish Aakash
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1Kumar
 
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...
Marko Rodriguez
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
Amrinder Arora
 

Viewers also liked (20)

02. divide and conquer
02. divide and conquer02. divide and conquer
02. divide and conquer
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Space complexity
Space complexitySpace complexity
Space complexity
 
Tigreblanco
TigreblancoTigreblanco
Tigreblanco
 
lecture 15
lecture 15lecture 15
lecture 15
 
Greedy method1
Greedy method1Greedy method1
Greedy method1
 
Merge sort
Merge sortMerge sort
Merge sort
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Flight MH370 community structure
Flight MH370 community structureFlight MH370 community structure
Flight MH370 community structure
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
Marge Sort
Marge SortMarge Sort
Marge Sort
 
Lec3 Algott
Lec3 AlgottLec3 Algott
Lec3 Algott
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
 
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 

Similar to Lec34

Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
nikshaikh786
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
Nv Thejaswini
 
Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1
subhashchandra197
 
Lecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptxLecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptx
Fazlullah28
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
Riazul Islam
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
LegesseSamuel
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Jay Patel
 
lecture 10
lecture 10lecture 10
lecture 10sajinsc
 
Simplifying Radical Expressions Mathemat
Simplifying Radical Expressions MathematSimplifying Radical Expressions Mathemat
Simplifying Radical Expressions Mathemat
JosaiahMaeGonzaga
 
Recursion
RecursionRecursion
Recursion
Syed Zaid Irshad
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
VarchasvaTiwari2
 
Divide And Conquer.pptx
Divide And Conquer.pptxDivide And Conquer.pptx
Divide And Conquer.pptx
SHAILIPATEL19
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika
MamaMa28
 
Quick sort
Quick sortQuick sort
Quick sort
Dhruv Sabalpara
 
Quick Sort
Quick SortQuick Sort
Quick Sort
Soumen Santra
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
22bcs058
 

Similar to Lec34 (20)

Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1
 
Lecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptxLecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptx
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
 
lecture 10
lecture 10lecture 10
lecture 10
 
Simplifying Radical Expressions Mathemat
Simplifying Radical Expressions MathematSimplifying Radical Expressions Mathemat
Simplifying Radical Expressions Mathemat
 
Recursion
RecursionRecursion
Recursion
 
Mit6 006 f11_quiz1
Mit6 006 f11_quiz1Mit6 006 f11_quiz1
Mit6 006 f11_quiz1
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Divide And Conquer.pptx
Divide And Conquer.pptxDivide And Conquer.pptx
Divide And Conquer.pptx
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika
 
Lec23
Lec23Lec23
Lec23
 
Quick sort
Quick sortQuick sort
Quick sort
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Lec24
Lec24Lec24
Lec24
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
 

More from Nikhil Chilwant

The joyless economy
The joyless economyThe joyless economy
The joyless economy
Nikhil Chilwant
 
I 7
I 7I 7
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
Nikhil Chilwant
 

More from Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
I 7
I 7I 7
I 7
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec39
Lec39Lec39
Lec39
 
Lec38
Lec38Lec38
Lec38
 
Lec37
Lec37Lec37
Lec37
 
Lec30
Lec30Lec30
Lec30
 
Lec29
Lec29Lec29
Lec29
 
Lec27
Lec27Lec27
Lec27
 
Lec26
Lec26Lec26
Lec26
 
Lec25
Lec25Lec25
Lec25
 
Lec21
Lec21Lec21
Lec21
 
Lec20
Lec20Lec20
Lec20
 
Lec19
Lec19Lec19
Lec19
 
Lec18
Lec18Lec18
Lec18
 
Lec17
Lec17Lec17
Lec17
 
Lec16
Lec16Lec16
Lec16
 
Lec15
Lec15Lec15
Lec15
 
Lec14
Lec14Lec14
Lec14
 
Lec13
Lec13Lec13
Lec13
 

Lec34

  • 1. Divide And Conquer • Distinguish between small and large instances. • Small instances solved differently from large ones.
  • 2. 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.
  • 3. Solving A Small Instance • A small instance is solved using some direct/simple strategy.  Sort a list that has n <= 10 elements. • Use count, insertion, bubble, or selection sort.  Find the minimum of n <= 2 elements. • When n = 0, there is no minimum element. • When n = 1, the single element is the minimum. • When n = 2, compare the two elements and determine which is smaller.
  • 4. 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.
  • 5. 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.
  • 6. Find The Min Of A Large List • Find the minimum of 20 elements.  Divide into two groups of 10 elements each.  Find the minimum element in each group somehow.  Compare the minimums of each group to determine the overall minimum.
  • 7. 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.
  • 8. 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.
  • 9. Tiling A Defective Chessboard
  • 10. Our Definition Of A Chessboard A chessboard is an n x n grid, where n is a power of 2. 1x1 2x2 4x4 8x8
  • 11. A defective chessboard is a chessboard that has one unavailable (defective) position. 1x1 2x2 4x4 8x8
  • 12. A Triomino A triomino is an L shaped object that can cover three squares of a chessboard. A triomino has four orientations.
  • 13. Tiling A Defective Chessboard Place (n2 - 1)/3 triominoes on an n x n defective chessboard so that all n2 - 1 nondefective positions are covered. 1x1 2x2 4x4 8x8
  • 14. Tiling A Defective Chessboard Divide into four smaller chessboards. 4 x 4 One of these is a defective 4 x 4 chessboard.
  • 15. Tiling A Defective Chessboard Make the other three 4 x 4 chessboards defective by placing a triomino at their common cRoercnuerrs.ively tile the four defective 4 x 4 chessboards.
  • 16. Tiling A Defective Chessboard
  • 17. Complexity • Let n = 2k. • Let t(k) be the time taken to tile a 2k x 2k 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().
  • 18. Substitution Method t(k) = 4t(k-1) + c = 4[4t(k-2) + c] + c = 42 t(k-2) + 4c + c = 42[4t(k-3) + c] + 4c + c = 43 t(k-3) + 42c + 4c + c = … = 4k t(0) + 4k-1c + 4k-2c + ... + 42c + 4c + c = 4k d + 4k-1c + 4k-2c + ... + 42c + 4c + c = Theta(4k) = Theta(number of triominoes placed)
  • 19. 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.
  • 20. 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.
  • 21. Min And Max • Find the max of n elements making n-1 comparisons. • Find the min of the remaining n-1 elements making n-2 comparisons. • Total number of comparisons is 2n-3.
  • 22. Divide And Conquer • Small instance.  n <= 2.  Find the min and max element making at most one comparison.
  • 23. Large Instance Min And Max  n > 2.  Divide the n elements into 2 groups A and B with floor(n/2) and ceil(n/2) elements, respectively.  Find the min and max of each group recursively.  Overall min is min{min(A), min(B)}.  Overall max is max{max(A), max(B)}.
  • 24. Min And Max Example • Find the min and max of {3,5,6,2,4,9,3,1}. • Large instance. • A = {3,5,6,2} and B = {4,9,3,1}. • min(A) = 2, min(B) = 1. • max(A) = 6, max(B) = 9. • min{min(A),min(B)} = 1. • max{max(A), max(B)} = 9.
  • 25. Dividing Into Smaller Instances {8,2,6,3,9,1,7,5,4,2,8} {8,2,6,3,9} {1,7,5,4,2,8} {8,2} {6,3,9} {6} {3,9} {1,7,5} {4,2,8} {1} {7,5} {4} {2,8}
  • 26. Solve Small Instances And Combine {8,2} {3,9} {2,9} {1,7} {2,8} {6} {3,9} {1} {2,8} {7,5} {4} {2,8} {6,6} {3,9} {1,1} {5,7} {4,4} {2,8} {1,8} {1,9}
  • 27. Time Complexity • Let c(n) be the number of comparisons made when finding the min and max of n elements. • c(0) = c(1) = 0. • c(2) = 1. • When n > 2, c(n) = c(floor(n/2)) + c(ceil(n/2)) + 2 • To solve the recurrence, assume n is a power of 2 and use repeated substitution. • c(n) = ceil(3n/2) - 2.
  • 28. Interpretation Of Recursive Version • The working of a recursive divide-and-conquer algorithm can be described by a tree—recursion tree. • The algorithm moves down the recursion tree dividing large instances into smaller ones. • Leaves represent small instances. • The recursive algorithm moves back up the tree combining the results from the subtrees. • The combining finds the min of the mins computed at leaves and the max of the leaf maxs.
  • 29. Iterative Version • Start with n/2 groups with 2 elements each and possibly 1 group that has just 1element. • Find the min and max in each group. • Find the min of the mins. • Find the max of the maxs.
  • 30. Iterative Version Example • {2,8,3,6,9,1,7,5,4,2,8 } • {2,8}, {3,6}, {9,1}, {7,5}, {4,2}, {8} • mins = {2,3,1,5,2,8} • maxs = {8,6,9,7,4,8} • minOfMins = 1 • maxOfMaxs = 9
  • 31. Comparison Count • Start with n/2 groups with 2 elements each and possibly 1 group that has just 1element.  No compares. • Find the min and max in each group.  floor(n/2) compares. • Find the min of the mins.  ceil(n/2) - 1 compares. • Find the max of the maxs.  ceil(n/2) - 1 compares. • Total is ceil(3n/2) - 2 compares.
  • 32. Initialize A Heap • n > 1:  Initialize left subtree and right subtree recursively.  Then do a trickle down operation at the root. • t(n) = c, n <= 1. • t(n) = 2t(n/2) + d * height, n > 1. • c and d are constants. • Solve to get t(n) = O(n). • Implemented iteratively in Chapter 13.
  • 33. Initialize A Loser Tree • n > 1:  Initialize left subtree.  Initialize right subtree.  Compare winners from left and right subtrees.  Loser is saved in root and winner is returned. • t(n) = c, n <= 1. • t(n) = 2t(n/2) + d, n > 1. • c and d are constants. • Solve to get t(n) = O(n). • Implemented iteratively in Chapter 14.

Editor's Notes

  1. We already have used this method in this class. For example, nearly all the binary tree algorithms studied use this technique. To traverse a binary tree tree small instance == NULL; large instanceevery non empty binary tree; traversal NULL do nothing; non NULL  traverse left, traverse right, visit (say).
  2. For the tiling to be possible, (n2 - 1) must be divisible by 3 whenever n is a power of 2. So, the tiling algorithm that follows actually provides a proof the this is so.