Chapter 6 Decrease and Conquer Student
Chapter 6 Decrease and Conquer Student
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: Computing 𝑎𝑛 .
(𝑎𝑛/2 )2 𝑛 ≡2 0
𝑛 ⌊𝑛/2⌋ 2
𝑎 = {(𝑎 ) ×𝑎 𝑛 ≡2 1 ∧ 𝑛 ≥ 1
1 𝑛=0
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
Roof
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 = ;
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);
}
}
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 𝐽(𝑛).
P1 P1
P2
P8
P1 P1
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
Algorithm
gcd(a, b) {
while (b) {
t = b;
b = a % b;
a = t;
}
return a;
}
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 𝑏.
Selection problem
Finding the 𝑘 𝑡ℎ smallest number in an array 𝑆 = {𝑠1 , 𝑠2 , … , 𝑠𝑛 }
Giải thuật
Selection(S[], lower, upper, k) {
h = random(lower, upper);
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;
}
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 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.