Decrease-and-Conquer Algorithms: An Overview - Prof. B. Karki, Study notes of Computer Science

An overview of decrease-and-conquer algorithms, a problem-solving technique used in computer science. The technique involves reducing the size of a problem instance to a smaller instance and exploiting the relationship between their solutions. Three variations of decrease-and-conquer algorithms are discussed: decrease by a constant, decrease by a constant factor, and variable size decrease. Examples of algorithms using these techniques include binary search, insertion sort, and binary search tree search.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-lgb
koofers-user-lgb 🇺🇸

10 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
B.B. Karki, LSU
0.1
CSC 3102
Decrease-and-Conquer
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Decrease-and-Conquer Algorithms: An Overview - Prof. B. Karki and more Study notes Computer Science in PDF only on Docsity!

Decrease-and-Conquer

Basics

 Decrease-and-conquer algorithm works as follows:  Establish the relationship between a solution to a given instance of a problem and a solution to a smaller instance of the same problem.  Exploit this relationship either top down (recursively) or bottom up (without a recursion).

 Three variations:  Decrease by a constant  The size of an instance is reduced by the same constant (usually one) at each iteration of the algorithm.  Decrease by a constant factor  The size of a problem instance is reduced by the same constant factor (usually two) on each iteration of the algorithm.  Variable size decrease  A size reduction pattern varies from one iteration to another.

Decrease-by-Half

 Decrease (by half)-and-conquer technique is common.  Decrease the problem size by a factor of 2.

 Exponentiation problem of computing an.  Exploit relation an^ = ( a n /2)2) for even n  Recursive approach:

 Requires O(log n ) operations.

 Note that the divide-and-conquer actually solves two instances of the problem of size n /2.

Problem of size n

subproblem of size n/

solution to the subproblem

solution to the original problem

f ( n ) =

( an^ /^2 )^2 ( a ( n −^1 )/^2 )^2. a a

 

for even n > 1 for odd n > 1 for n = 1

Variable-Size-Decrease: BST Search

 Binary search tree (BST) search is an excellent example of a variable- size decrease algorithm.

 Searching for an element of a given value v in BST starts by comparing v with the tree’s root, K ( r )  If they match, a desired element is found.  If they do not match, continue search in the left subtree of the root if v < K ( r ) or in the right subtree if v > K ( r )

 On each iteration of the algorithm, the problem of searching in a BST is reduced to searching in a smaller BST.  Worst-case efficiency: Θ( n ) Average-case efficiency: Θ(log n ).

Iterative Approach

 Starting with A [1] and ending with A [ n - 1], A [ i ] is

inserted in its appropriate place among the first i

elements of the array that have already been sorted.

Algorithm InsertionSort ( A[0.. n - 1] ) for i1 to n -1 do vA[ i] ji - 1 while j ≥ 0 and A[ j] > v do A[ j + 1]A[ j] jj - 1 A[ j + 1]v

Efficiency

 Basic operation is key comparison A [ j ] > v.

 The worst case occurs when the input is already an array of strictly decreasing values:

 The best-case occurs when the input is already an array of strictly increasing values:

 The average-case occurs for randomly ordered arrays

Cavg ( n ) =

i + 1

j

j = 1

i + 1 ∑ ≈^

n^2

∈ Θ( n^2 )

i = 1

n − 1 € ∑

Cbest ( n ) = 1 = n − 1 ∈ Θ( n )

i = 1

n − 1 ∑

Cworst ( n ) = 1

j = 0

i − 1 ∑ =^

( n − 1 ) n
∈ Θ( n^2 )

i = 1

n − 1 ∑

B.B. Karki, LSU

CSC 3102

Permutations: Johnson-Trotter Algorithm

„

Johnson-Trotter Algorithm generates

n

! without explicitly generating

permutations for smaller values of

n

.

„

Associate a direction with each component

k

in a permutation

)

Direction is represented by an arrow above the component. )

The component

k

is said to be

mobile

, if the arrow points to a smaller

value adjacent to it.



For

the components 3 and 4 are mobile while 2 and 1 are

not.

rs 3 2

r 4 s 1

(^31) 2

(^13) 2

(^12) 3

(^21) 3

(^23) 1

(^32) 1

rs s

sr s

ss r

ss s

ss s

ss s

Algorithm

JohnsonTrotter

( n

initialize

the first permutation

with

while

there exists a mobile integer

k

do

find

the largest mobile integer

k

swap

k

and the adjacent integer its arrow points to

reverse

the direction of all integers that are larger than

k Example,

n

= 3

s 1 s 2...........

s n

Fake-Coin Problem

 Among n identically looking coins, one is fake (assume that it is lighter). The problem is how to detect the fake coin.

 Dividing n coins into two piles of  n /2  coins each, leaving one extra coin apart if n is odd  Put the two piles on the scale. If the piles weigh the same, the coin put aside must be fake; otherwise we can continue with the lighter pile containing the fake coin W ( n ) = W (n /2) + 1 for n > 1, and W (1) = 0 ( the worst case ) For n = 2 k, W ( n ) = W (2 k ) = k = log 2 n

 Dividing n coins into three piles  Divide n coins into three piles of  n /3 ,  n /3  and n - 2n /2   Put first two piles on the scale. The lighter one of two should contain the fake coin. If both piles weigh equal, then the third pile should contain the fake coin.  After weighing two of the piles, we are left to solve a single problem of the one- third the original size. So it is a decrease by a factor of 3.  The recurrence relation for the number of weighing W ( n ) in the worst case: W ( n ) = W (n /3) + 1 for n > 1, and W (1) = 0 For n = 3 k, W ( n ) = W (3 k ) = k = log 3 n

Computing a Median

 A median is an element that is greater than one half of the

list’s elements and smaller than the other half.

 Selection problem: find the k th^ smallest element in a list of n numbers. k = n /2 , the median k = 1 , the smallest number k = n , the largest element

 Algorithm based on partitioning of an array’s elements into

two subsets (with p as a pivot)

 Set 1: Elements that are less than or equal to some value p  Set 2: Elements that are greater than or equal to p

Median by Partition

 Let s be the partition’s split position

 If s = k , the pivot p obviously solves the problem.  If s > k , the k th^ smallest element in the entire list is the k th smallest element in the left part of the partitioned array.  If s < k , proceed by searching for the ( k-s )th^ smallest element in its right part, which can be solved by the same approach (iteratively).

 Thus the array size reduces in an unpredictable fashion

from one iteration to another.

 Some size reduction less than half and some larger.

 See the example {4, 1, 10, 9, 7, 12, 8, 2, 15} given in

page 186 of the textbook.