








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 14
This page cannot be seen from the preview
Don't miss anything!









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)-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
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 ).
Algorithm InsertionSort ( A[0.. n - 1] ) for i ← 1 to n -1 do v ← A[ i] j ← i - 1 while j ≥ 0 and A[ j] > v do A[ j + 1] ← A[ j] j ← j - 1 A[ j + 1] ← v
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
j = 1
i + 1 ∑ ≈^
i = 1
n − 1 € ∑
i = 1
n − 1 ∑
j = 0
i − 1 ∑ =^
i = 1
n − 1 ∑
B.B. Karki, LSU
CSC 3102
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
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 - 2 n /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
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
Set 1: Elements that are less than or equal to some value p Set 2: Elements that are greater than or equal to p
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).
Some size reduction less than half and some larger.