Heap Sorting

Only available on StudyMode
  • Topic: Sorting algorithm, Heapsort, Quicksort
  • Pages : 17 (2119 words )
  • Download(s) : 27
  • Published : January 23, 2013
Open Document
Text Preview
CEng 315 - Week 4b
Dept. of Computer Engineering
Middle East Technical Univ.
Ankara - TURKEY

Anaysis of Algorihms, Fall 2012

CEng 315

1

Recurrences (cont.)

• Inhomogeneous Recurrences: We now consider
recurrences of a slightly more general form:
a0tn + a1tn-1 + … + aktn-k = bnp(n)

(2)

• The left hand side is the same as (1), but on the
right hand side we have bnp(n),
where b is a constant and p(n) is a polynomial in n
of degree d.

Anaysis of Algorihms, Fall 2012

CEng 315

2

Recurrences (cont.)
Example: Solve the following recurrence: tn - 2tn-1 = 3n.
In this case b =3 and p(n) = 1, a polynomial of degree d=0.
A little manipulation allows us to reduce this example to the form (1). To see this, we first multiply the recurrence by 3, obtaining 3tn - 6tn-1 = 3n+1.
If we replace n by n+1 in the original recurrence, we get
tn+1 - 2tn = 3n+1.
Finally, subtracting these two equations, we have
tn+1 - 5tn + 6tn-1 = 0,
which can be solved by the above method.
The characteristic equation is
x2 - 5x + 6 = 0. That is, (x-2)(x-3) = 0.

• Intuitively we can see that the factor (x-2) corresponds to the left-hand side of the original recurrence, whereas the factor (x-3) has appeared as a result of our manipulation to get rid of the right-hand side. Anaysis of Algorihms, Fall 2012

CEng 315

3

Recurrences (cont.)
• Generalizing this approach, to solve (2):
a0tn + a1tn-1 + … + aktn-k = bnp(n) (2)
we can show that it is sufficient to take
the following characteristic equation:
(a0xk+ a1xk-1 +…+ ak )(x-b)d+1 = 0,

where d is the degree of polynomial p(n)
• Once this equation is obtained, proceed as in the
homogeneous case.

Anaysis of Algorihms, Fall 2012

CEng 315

4

Recurrences (cont.)
Example: The number of movements of a ring required in the Towers of Hanoi problem is given by the following recurrence relation: tn = 2tn-1 + 1, n  1, subject to t0 = 0.
The recurrence can be written
tn - 2tn-1 = 1, which is of the form (2) with b = 1, p(n) = 1, a polynomial of degree 0.
The characteristic equation is therefore
(x-2)(x-1) = 0.
The roots of this equation are 1 and 2, so the general solution of the recurrence is
tn = c11n +c22n.
We need two initial conditions. We know that t0 = 0; to find a second initial condition we use the recurrence itself to calculate
t1 = 2t0 +1 = 1.
We finally have c1 +c2 = 0 n = 0
c1 + 2c2 = 1 n = 1
From which we obtain the solution, tn = 2n –1, where c1= -1, c2 = 1. Then we can conclude that tn  (2n).
Anaysis of Algorihms, Fall 2012

CEng 315

5

Recurrences (cont.)

• A further generalization of the same type of the
argument allows us finally to solve recurrences of the
form

a0tn + a1tn-1 + … + aktn-k = b1np1(n) + b2np2(n)+…

(3)

where the bi are distinct constants and pi(n) are
polynomials in n respectively of degree di. It suffices to
write the characteristic equation

(a0xk+ a1xk-1 +…+ ak )(x - b1)d1+1(x - b2)d2+1 … = 0,
Anaysis of Algorihms, Fall 2012

CEng 315

6

Recurrences (cont.)
Example: Solve the following recurrence:
tn = 2tn-1 + n + 2n
n  1, subject to t0 = 0.
tn - 2tn-1 = n + 2n, which is of the form (3) with
b1=1, p1(n) = n, b2 =2, p2(n) = 1.
The characteristic equation is (x-2)(x-1)2(x-2) = 0, Roots:1,1,2,2. The general solution of the recurrence is therefore of the form tn = c11n + c2n1n + c32n + c4n2n
Using the recurrence, we can calculate
t1= 3 (t1= 0+1+21 = 3), t2 = 12, t3 = 35.
c1 +c3 = 0 n = 0,
c1 + c2 + 2c3 + 2c4 = 3 n = 1
c1 + 2c2 + 4c3 + 8c4 = 12 n = 2
c1 + 3c2 + 8c3 + 24c4 = 35 n = 3 ,
arriving at tn = -2 – n + 2n+1 + n2n
Therefore, tn  (n2n).
Anaysis of Algorihms, Fall 2012

CEng 315

7

Recurrences (cont.)
Change of Variable:
We write T(n) for the term of a general recurrence, and tk for the term of a new recurrence obtained by a change of variable.
Example: Solve the recurrence if n is a power of 2 and if
T(n) = 4T(n/2) + n
n > 1.
•...
tracking img