0% found this document useful (0 votes)
8 views96 pages

Daa Unit2

Uploaded by

jyothigandham004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views96 pages

Daa Unit2

Uploaded by

jyothigandham004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 96

ADITYA ENGINEERING COLLEGE (A)

UNIT-II
Divide-and-Conquer Method

By

Dr.M.Rajababu

Associate Professor&HOD
Dept of Information Technology
Aditya Engineering College(A)
Surampalem.
Aditya Engineering College (A)

Divide-and-Conquer
• The most-well known algorithm design strategy useful for solving many
problems.
• There are three stages in Divide and conquer strategy:
1. DIVIDE: Divide the problem into two or more sub problems.
2. CONQUER: Solve the sub problems recursively or non-recursively.
3. COMBINE: Combine the results of the sub problems to derive a
solution of the given problem.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Divide-and-Conquer

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Divide-and-Conquer
• finding the maximum of a set S of n numbers

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Merge Sort

4 -5
Subdivide the sorting task
H E M G B K A Q F L P D R C J N

H E M G B K A Q F L P D R C J N

Insight Through Computing


Subdivide again

H E M G B K A Q F L P D R C J N

H E M G B K A Q F L P D R C J N

Insight Through Computing


And again

H E M G B K A Q F L P D R C J N

H E M G B K A Q F L P D R C J N

Insight Through Computing


And one last time

H E M G B K A Q F L P D R C J N
Insight Through Computing
Now merge

E H G M B K A Q F L D P C R J N

H E M G B K A Q F L P D R C J N
Insight Through Computing
And again

A B E G H K M Q C D F J L N P R

E G H M A B K Q D F L P C J N R

Insight Through Computing


And one last time
A B C D E F G H J K L M N P Q R

A B E G H K M Q C D F J L N P R

Insight Through Computing


Done!
A B C D E F G H J K L M N P Q R

Insight Through Computing


Aditya Engineering College (A)

Control abstraction for divide and conquer


Algorithm DandC (P)// P-Problem
{
if small (P) then return S(P);
else
{
divide P into k subproblems P1, P2 -, Pk.
Apply DandC algorithm to each subproblem Pi, lik
return (Combine (DandC(P1),DandC(P2),… ……….. ,DandC(Pk));
}
}
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)

Analysis of the Algorithm


• Let us assume that T(n) denotes the time complexity of the problem P and
T(ni) is the Time complexity of the problem Pi, lik. Let g(n) be time
required to compute the solution when the problem is small
Then the time complexity T(n) can be given as follows :
g(n) n is small
T(n) = {
T(n1) + T(n2) +…+T(nk) + f(n), otherwise

Where T(ni) is the time complexity of the problem Pi and n1+n2 + ….+ nk=n,
f(n) be the time required for combining the solutions .
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)

Analysis of the Algorithm


• Time complexity of the divide and conquer algorithms is given by
g(n) n=1

T(n) =  aT(n/b) + f(n) n>1


Where a and b are constants
• Here g(n) is the time required to compute the solution when the problem is
small and f(n) is the time required for combining the solutions .

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Divide-and-Conquer applications
• Binary search
• Finding the maximum and minimum
• Sorting:Merge sort and Quick sort
• Defective chessboard
• Multiplication of long integers
• Binary tree traversals
• Matrix multiplication: Strassen’s algorithm

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Binary Search
Input: A sorted sequence of n elements stored in an array.
Output: The position of x (to be searched).
Step 1: If only one element remains in the array, solve it directly.
Step 2: Compare x with the middle element of the array.
Step 2.1: If x = middle element, then output it and stop.
Step 2.2: If x < middle element, then recursively solve the problem
with x and the left half array.
Step 2.3: If x > middle element, then recursively solve the problem
with x and the right half array.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Recursive Binary Search Algorithm Aditya Engineering College (A)

Algorithm BinSrch(A, i, j, x)
{ // A Array, i least index, j  highest index, x  element for search.
if (i = j) then
{ if (x == A[i]) then return i;
else return 0;
}
else
{
mid  (i+j)/2;
if (x ==A[mid]) then return mid;
else if (x < Amid]) then
return BinSrch(A, i, mid –1, x)
else
return BinSrch (A, mid + 1, j, x)
}
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
}
Aditya Engineering College (A)
Algorithm binarySearch( A, low,high ,int x)
{
while (low <= high)
{
int mid = (low + high) / 2;
if (x == A[mid])
{ return mid;
}
else if (x < A[mid])
{
high = mid - 1;
} else
{ low = mid + 1; }
}
}
return -1;
}
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)

Binary Search algorithm Analysis


• In recursive binary search we have
T(n) = 1 + T(n/2)
T(1) = 1
• T(n/2) = 1 + T(n/4),
and T(n/4) = 1 + T(n/8), etc.
• So T(n) = 1 + T(n/2) = 1 + 1 + T(n/4) = 1 + 1 + 1 + T(n/8)
and the pattern is T(n) = k + T(n/2k)
now when k = log n, then n/(2log n)=n/n = 1,
• T(n) = log n + 1 = O(log n)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Binary Search algorithm Analysis

• Best case time complexity T(n)=O(1)


• Worst case time complexity T(n)=O(log n)
• Average case time complexity T(n)=O(log n)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Finding the Maximum/Minimum


• Given an array of size n, you need to find the maximum and
minimum element present in the array.
• In this approach, Divide the array into two equal parts and recursively
find the maximum and minimum of those parts.
• After this, compare the maximum and minimum of those parts to get
the maximum and minimum of the whole array.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Algorithm MaxMin(int A[], int n)


{
max = A[0] ;
min = A[0] ;
for ( i = 1 to n-1 )
{
if ( A[i] > max )
max = A[i] ;
else if ( A[i] < min )
min = A[i] ;
}

MaxMin requires 2(n-1) element comparisons in the best, average & worst cases.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)
Algorithm MaxMin(a,i,j,max,min)
{
if ( i==j ) { max = min=a[i];} //small p
else if ( i +1== j)//small p
{
if ( a[i] < a[j] )
{
max = a[j] ;
min = a[i] ;}
else {
max = a[i] ;
min = a[j] ;}
}
}
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)

else //if p is not small,divide p into sub problems


{
int mid = i+j/2;
MaxMin(a,i,mid,max,min);
MaxMin(a,mid+1,j,max1,min1)
if ( max < max1 )
max = max1;
if ( min> min1 )
min = min1;
}
}
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)

Time complexity
T(n)= 2T(n/2)+2 n>2
= 1 n=2
T(n) = 2T(n/2)+2
= 2(2T(n/4)+2)+2
= 4T(n/4)+4+2 T(n)=O(n)
:
=2k -1T(n/2k-1)+2k-1+…+4+2
=2k-1+2k-1+…+4+2+1
=2k-1+2k -2=n/2+n-2=3n/2-2
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Merge sort
• Merge sort is a sorting algorithm that uses the divide and conquer
strategy
• Divide the unsorted array into two sub lists at the middle.
• Further divide these sub lists into sub lists at their middles until each
sub list contain only one element.
• Merge these sub lists two at a time till we get the final sorted list using
the following steps:
1. Compare the sub-list’s first elements.
2. Remove the smallest element and put it into the result array.
3. Continue the process until all elements have been put into the
result array.
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)

Merge sort
• Merge sort consists of three steps:
• 1.Partition array into two sub lists s1 and s2 with n/2 elements each.
• 2.Recursively sort s1 and s2.
• 3.Merge s1 and s2 into a single sorted list.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

23 98 14 45

14 23 45 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6 67

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98
98 23 45 14 6 67 33 42

6 14 23 33 42 45 67 98
Thursday, December 5, 2024 M.Raja Babu
Aditya Engineering College (A)

Merge sort algorithm


Algorithm MergeSort (low, high)
{
if (low < high) then//if there are more than one element
{
mid :=(low + high)/2 //Divide the list into two parts
MergeSort (low, mid) // Apply mergesort to the left list
MergeSort (mid+1, high)// Apply mergesort to the Right list
Merge (low, mid, high)// Merge two lists into one list
}
}

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Algorithm Merge (low, mid, high)
{
h := low, i := low, j := mid + 1
while ((h  mid) and (j  high)) do
{
if (a[h]  a[j]) then
{ b [i] := a[h] ;h :=h + 1 }
else { b[i] :=a[j], j := j +1}
i := i + 1
}
if (h > mid) then //add elements left in the second
list
for k:=j to high do
{ b[i]:= a[k]; i:=i+1
}
else
for k:=h to mid do //add elements left in the first
list
{ b[i]:= a[k];
i:=i+1
}
for k := low to high do // copying back the sorted
list to a[]
Thursday, December 5, 2024 M.Raja Babu
Analysis of Merge sort Aditya Engineering College (A)

Time complexity of Mergesort can be described by the recurrence relation :


1 n=1
T(n) = 2T(n/2) + n n>1

T(n) = 2(2T(n/4) + n/2) + n


= 4 T(n/4) + 2n
= 4(2T(n/8) + n/4) + 2 n
= 8T(n/8) + 3n
………………
= 22k T(n/ 2k2) + k n
= 22k T(1) + k n
= n2+ n logn
T(n) = n + n (log n) = O( n log n)
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)

Analysis of Merge sort


• Time complexity of Merge Sort is O(n*log n) in all the 3 cases (worst,
average and best) as merge sort always divides the array in two
halves and takes linear time to merge two halves.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Stable and unstable sorting


• The stability of a sorting algorithm is concerned with how the
algorithm treats equal (or repeated) elements.
• Stable sorting algorithms preserve the relative order of equal
elements, while unstable sorting algorithms don’t.
• Merge Sort, Insertion Sort, and Bubble Sort are stable and Quick
sort, Heap sort and Selection Sort are unstable.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Stable and sorting

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Quick sort
• Quick sort is another sorting algorithm which uses Divide and Conquer
for its implementation.
• Unlike the Merge Sort, Quick sort doesn't use any extra array in its
sorting process.
• Quick sort first chooses a pivot and then partition the array around this
pivot.
• In the partitioning process, all the elements smaller than the pivot are
put on one side of the pivot and all the elements larger than it on the
other side.
• This partitioning process is repeated on the smaller sub arrays and hence
finally results in a sorted array.
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)

Quick sort

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Quick sort
• We can pick Pivot element in different ways:
1. pick first element as pivot.
2. pick last element as pivot
3. Pick a random element as pivot.
4. Pick median as pivot.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Quick sort
• Quick sort has three steps:
1. Pick an arbitrary element of the array (the pivot).
2. Divide the array into two segments, those that are smaller and those
that are greater, with the pivot in between (partition).
3. Recursively sort the segments to the left and right of the pivot.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Quick sort algorithm


Algorithm Quicksort (a, low, high)
{
if (low < high) then //if there are more than one element
{
j := partition(a, low, high);
// j is the position of the partitioning element
Quicksort (a,low, j – 1);
Quicksort(a,j + 1 , high);
//there is no need for combining solutions
}
}
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
int partition(int a[], low , high) Aditya Engineering College (A)

{
v=a[low];i=low; j=high+1;a[j]=∞;
do
{ do{
i++;
}while(a[i]<v);
do{
j--;
}while(v<a[j]);
if(i<j) {swap(a[i[,a[j]);}
}while(i<j);
a[low]=a[j];a[j]=v;
return(j);}
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)

Sort the following list of elements using Quick sort:

10 16 8 12 15 6 3 9 5

45 26 77 14 68 61 97 39 99 90

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Analysis of Quick sort


• Let T(n) be the time complexity of Quick sort and if the pivot ends up
at position k, then we have
T(n) T(nk)  T(k )  n
T(1)=1
• Partition can be done in n comparisons , where n is the size of the
array.
• To determine best, worst, and average case complexity we need to
determine the values of k that correspond to these cases.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Analysis of Quick sort


• The best case of quicksort occurs when the pivot element divides the
array into two exactly equal parts, in every step. The recurrence
equation is as follows:
T(n) = 2T(n/2) + n
T(1)=1
using the substitution method the equation can be written as follows:
T(n)=2kT(n/2k) + kn
so T(n)=O(nlogn)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Analysis of Quick sort


• The worst case of quicksort occurs when the first element is always the
pivot element .
The recurrence equation is as follows:
T(n) = T(n − 1) + n
using the substitution method the equation can be written as follows:
T(n)= T(n − k) + (n+(n-1)+(n-2 ) ..... + (n-k+1))

so T(n)=O(n2)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Analysis of Quick sort


• The average case time complexity of quick sort is :
T(n)=O(n log n)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Quick sort Vs Merge sort


• The worst case complexity of quick sort is O(n2) as it takes a lot more
comparisons in the worst condition.
• Merge sort has the same worst case and average case complexities, that
is O(n log n).
• Quick sort is faster than merge sort in some cases such as for small data
sets.
• Merge sort requires additional memory space to store the auxiliary
arrays.
• Merge sort is more efficient than quick sort.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Quick sort Vs Merge sort
Aditya Engineering College (A)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Defective chess board problem


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

1x1 2x2 4x4


8x8

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Defective chess board


• A defective chessboard is a chessboard of size nxn that has exactly one defective
square.

1x1 2x2 4x4 8x8


k=0 k=1 k=2 k=3
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)

Defective chess board problem


• In the defective chess board problem ,we are required to tile a defective chess board using triominoes satisfying the
following conditions:
1.triominoes should not overlap.
2.triominoes should not cover the defective square.
3. triominoes must cover all other squares.
• A triomino is an L shaped object that can cover three squares of a chessboard.
• A triomino has four orientations:

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

• Tiling a Defective Chessboard :


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

1x1 2x2 4x4

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Divide and conquer solution Aditya Engineering College (A)

1.if n=1 then this problem is easily solved and the no of triominoes
used in tiling is zero.
2.if n=2 then there are exactly three non defective squares and these
squares are covered by using one triomino in one of the four
orientations.
3.Divide the board into 4 smaller chess boards of size n/2Xn/2.One of
these is a defective chessboard. Make the other three chess boards
defective by placing a triomino at their common corner.
4.Recursively tile these four defective chess boards. The recursion
terminates when the chess board size has been reduced to 2x2.
5.The solutions of the sub-problems of the problem can be combined as
the solution of the problem.
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Divide and conquer solution Aditya Engineering College (A)

1.if n=1 then this problem is easily solved and the no of triominoes
used in tiling is zero.
2.if n=2 then there are exactly three non defective squares and these
squares are covered by using one triomino in one of the four
orientations.
3.Divide the board into 4 smaller chess boards of size n/2Xn/2.One of
these is a defective chessboard. Make the other three chess boards
defective by placing a triomino at their common corner.
4.Recursively tile these four defective chess boards. The recursion
terminates when the chess board size has been reduced to 2x2.
5.The solutions of the sub-problems of the problem can be combined as
the solution of the problem.
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Time complexity Aditya Engineering College (A)

• Time Complexity For Defective Chess Board Problem can be described


by the recurrence equation :
T(n) = 4T(n/2) +1 n>2
=1 n=2
• using master theorem method, we can get the time complexity
T(n)=O(n2)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024

You might also like