0% found this document useful (0 votes)
38 views7 pages

Chapter 6 Decrease and Conquer Student

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

Chapter 6 Decrease and Conquer Student

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

1

Chapter 6: Decrease-and-Conquer

Introduction

Decrease by a constant
The size of an instance is reduced by the same constant on each iteration of the
algorithm. Typically, this constant is equal to one.

Example: Computing 𝑎𝑛 .
𝑓(𝑛 − 1) × 𝑎 𝑛>1
𝑓(𝑛) = {
𝑎 𝑛=1

Example: Insertion sort

Decrease by a constant factor


The size of an instance is reduced by the same constant factor on each iteration of
the algorithm. In most applications, this constant factor is equal to two.

Example: Binary search

Example: Computing 𝑎𝑛 .
(𝑎𝑛/2 )2 𝑛 ≡2 0
𝑛 ⌊𝑛/2⌋ 2
𝑎 = {(𝑎 ) ×𝑎 𝑛 ≡2 1 ∧ 𝑛 ≥ 1
1 𝑛=0

Example: Russian peasant method of multiplication


𝑛
× 2𝑚 𝑛 ≡2 0
𝑛×𝑚={ 𝑛 2
⌊ ⌋ × 2𝑚 + 𝑚 𝑛 ≡2 1
2
Variable size decrease
The size-reduction pattern varies from one iteration of an algorithm to another.

Example: Euclid’s algorithm for computing the greatest common divisor


𝑈𝑆𝐶𝐿𝑁(𝑎, 𝑏) = 𝑈𝑆𝐶𝐿𝑁(𝑏, 𝑎 % 𝑏)

Example: Finding an element in a binary search tree


2

Decrease by a constant

Topological Sorting

Example: Consider a set of five required courses {𝐶1 , 𝐶2 , 𝐶3 , 𝐶4 , 𝐶5 }. The courses can
be taken in any order as long as the following course prerequisites are met: 𝐶1 and 𝐶2 have
no prerequisites, C3 requires 𝐶1 and 𝐶2 , 𝐶4 requires 𝐶3 , and 𝐶5 requires 𝐶3 and 𝐶4 . A student
can take only one course per term. In which order should the student take the courses?

C1 C4

C3 C2 C1 C3 C4 C5

C2 C5

C1 C4 C4 C4 C4

C3 C3 C3

C2 C5 C2 C5 C5 C5 C5

Example: Building a house

Roof

Foundations Walls Windows Decorating

Plumbing

A possible sequence:
Foundations → Walls → Roof → Windows → Plumbing → Decorating
3

A rough algorithm:
Repeatedly, identify in a remaining digraph a source, which is a vertex with no
incoming edges, and delete it along with all the edges outgoing from it. The order in which
the vertices are deleted yields a solution to the topological sorting problem.
Note: If there are several sources, break the tie arbitrarily. If there are none, stop because
the problem cannot be solved.

Giải thuật:
TopologicalSort(Graph G) {
indegree[1 .. |V|] = {0};
priorQueue Q = ;

for (each vertex u  V)


for (each vertex v that is adjacent from u)
indegree[v] ++;
for (each vertex v  V)
if (indegree[v] == 0)
enQueue(Q, v);

while (!isEmpty(Q)) {
Vertex u = deQueue(Q);
Output(u);
for (each vertex v that is adjacent from u)
if( -- indegree[v] == 0 )
enQueue(Q, v);
}
}

Analysis of the algorithm


If the graph representation is an adjacency list, the running time of the algorithm is
Θ(|𝑉| + |𝐸|).
4

Decrease by a constant factor

Josephus Problem

Let 𝑛 people numbered 1 to 𝑛 stand on a circle. Starting the count with person
number 1, we eliminate every second person until only one survivor is left. The problem
is to determine the survivor’s number 𝐽(𝑛).

Example: 𝐽(8) = 1, 𝐽(7) = 7, 𝐽(6) = 5.

P1 P1
P2
P8  
  P1 P1

P7  Start  P3  2nd round


  P7  1st round  P3 3rd round


P6  P4 
P5 P5

P1 P5

P7  P2 P7 P7
   
st
Start 1 round 2nd round
P6   P3  P3

  
P5 P4 P5

In order to determine 𝐽(𝑛), we consider the cases of even and odd 𝑛’s separately:

− 𝑛 = 2ℎ:
𝐽(2ℎ) = 2𝐽(ℎ) − 1
− 𝑛 = 2ℎ + 1:
𝐽(2ℎ + 1) = 2𝐽(ℎ) + 1

A closed-form solution to the two-case recurrence is as follows:


𝐽(2𝑘 + 𝑖) = 2𝑖 + 1, 𝑖 ∈ [0, 2𝑘 − 1]
5

Variable size decrease

Euclid’s algorithm for computing the 𝑔𝑐𝑑(𝑎, 𝑏), where 𝑎, 𝑏 ∈ ℤ+

Algorithm
gcd(a, b) {
while (b) {
t = b;
b = a % b;
a = t;
}
return a;
}

Analysis of the algorithm

Lame’s theorem: Let 𝑎 and 𝑏 be positive integers with 𝑎 ≥ 𝑏 ≥ 2. Then the number
of divisions used by the Euclidean algorithm to find 𝑔𝑐𝑑(𝑎, 𝑏) is less than or equal to five
times the number of decimal digits in 𝑏.

Finding an element in a binary search tree

Analysis of the algorithm


• The best case: Θ(log 𝑛)
• The worst case: Θ(𝑛)
• The average case: Θ(log 𝑛)
6

Selection problem
Finding the 𝑘 𝑡ℎ smallest number in an array 𝑆 = {𝑠1 , 𝑠2 , … , 𝑠𝑛 }

Giải thuật
Selection(S[], lower, upper, k) {
h = random(lower, upper);

pos = Partition(S, lower, upper, h);

if (pos == k)
return S[pos];
if (pos > k)
Selection(S, lower, pos – 1, k);
if (pos < k)
Selection(S, pos + 1, upper, k);
}
Partition(S, lower, upper, pos) {
if (lower == upper)
return lower;

pivot = S[pos];
S[lower] S[pos];
pos = lower;
for (i = lower + 1; i  upper; i++)
if (pivot > S[i]) {
pos++;
S[i] S[pos];
}
S[lower] S[pos];

return pos;
}

Analysis of the algorithm


• The best case: Θ(𝑛)
• The worst case: O(𝑛2 )
• The average case: Θ(𝑛)
7

Interpolation Search

Value

ar
k < ax

ax = k k
k > ax

al

x–1 x+1
l x r Index

Let 𝑘 be the search key. The algorithm assumes that the array values are in
ascending order. The search key 𝑘 will be compared with the element whose index (called
𝑥) is computed as (the round-off of) the 𝑥-coordinate of the point on the straight line
through the points (𝑙, 𝑎𝑙 ) and (𝑟, 𝑎𝑟 ) whose 𝑦-coordinate is equal to 𝑘 (Figure).

The following formula shows the way to compute 𝑥:


𝑥−𝑙 𝑘 − 𝑎𝑙 (𝑘 − 𝑎𝑙 )(𝑟 − 𝑙)
= ⟹𝑥=𝑙+⌊ ⌋
𝑟 − 𝑙 𝑎𝑟 − 𝑎𝑙 𝑎𝑟 − 𝑎𝑙

After comparing 𝑘 with 𝑎𝑥 , there are three possibilities


• 𝑎𝑥 = 𝑘: The algorithm stops.
• 𝑎𝑥 > 𝑘: The algorithm proceeds by searching in the same manner among the
elements indexed between 𝑙 and (𝑟 =)𝑥 − 1.
• 𝑎𝑥 < 𝑘: The algorithm proceeds by searching in the same manner among the
elements indexed between (𝑙 =)𝑥 + 1 and 𝑟.

Analysis of the algorithm

The analysis of the algorithm’s efficiency shows that interpolation search uses fewer
than log 2 log 2 𝑛 + 1 key comparisons on the average when searching in an array of 𝑛
random keys.

You might also like