Professional Documents
Culture Documents
Introduction
1.1: What is an Algorithm?
Al-Khowrizm is a set of rules that carry out some calculation, it can be done by hand,
calculators or computers. For example, to multiply two numbers 254 and 123 can be done by
American style, English style or la russe as given in Fig. 1.1 below:
L
R
254
254
254
123
123
123
127
246
246
-------------63
492
738
692
254
31
984
1722
+ 508
+ 508
15
1968
3690
254
762
7
3936
7626
------------------3
7872
15498
31242
31242
1
15744
31242
0
(a) American
(b) English
(c) la russe
Fig. 1.1 Different ways to multiply two numbers
la russe algorithm is given as:
Set =0
Repeat
Divide L by 2
If the remainder is 1 add R to
Multiply R by 2
Until L=0
This a la russe algorithm is more suitable for computer with divide by 2 and multiply by 2 are
shift left and and shift right instructions in machine languages
Another way, to multiply by Divide and Conquer (D&C) method: Assume that both x and y have
2k digits, and they can be divided into two parts. Let x=xLxR and y=yLyR then
x.y=xLyL.102k+(xLyR+xRyL)10k+xRyR
(1.1)
For example x=2345, and y=1372 then xL=23, xR=45, yL=13 and yR=72 and the result of x.y is:
xy= 23x13x104+(23x72=45x13)102+45x72
=3217340
It is easy to verify that xLyR+xRyL = -(xR-xL)(yR-yL)+xRyR+xLyL, therefore to multiply two 2kdigit numbers together needs only three(instead of four) times to multiply two k-digit numbers.
It turns out that multiply two n-digits numbers needs 3lg n n lg 3 multiplications, instead of n 2
multiplications, between digit and digit.
n N n0 N , n N , n n0
Prove that
i
i 1
n(n 1)
2
(1.2)
Induction proof normally has 2 steps: (a) Basic step: Verify the equation for some small n (b)
Induction step: Assume that the property is true upto n-1 then we need to prove it is also true for
n.
Recall back to the problem given in (1.2)
a) Basic Step: Verify (1.2) for n=1: LHS=1 and RHS=
1(1 1)
1 , equation 1.2 is correct for
2
n=1.
b) Induction step: Assume that the formula is true upto n-1 then we need to prove it is also true
for n
by .assumption
n
n 1
(n 1)n
(n 1 2)n n(n 1)
1 i 1 i n 2 n
2
2
1.4.2 Constructive Induction
The fibonnaci sequence is defined as
Fib(0)=0
Fib(1)=1
Fib(n)=Fib(n-1)+Fib(n-2).
a id na 2 (n 1)d
i 0
ar
1 r .
a
n
1 r
i 0
a
if r <1.
1 r
1.5.3 Permutation:
Permutation of n objects is an ordered arrangement of these objects. First object can be any
object (n different ways to be selected), then the second object should be differed from the first
object, it means that there are n-1 ways to select the second object . Therefore there are n!
permutations of n objects
n
n!=n(n-1)(n-2).1= i
(1.3)
i 1
There are 6 ways to arrange three objects a,b,c. They are abc, acb, bac, bca, cab, cba.
1.5.4 Combination
Combination of r objects from n objects is a selection of r objects without regarding to the
n
order of selection. The number of combinations denoted as C rn or and equal to
r
n
n!
n
(1.4)
C r r r!(n r )!
It is easy to see that
n
n 1
n 1
(1.5)
C r C r 1 C r
Equation 1.5 can be proved by two ways
(1) Use (1.4) to balance two sides of (1.5).
(2) Let consider all combination to select r objects from n objects with and without a special
object, let say object 1. In the first case, we need to select another r-1 from n-1 remaining
n 1
n 1
objects ( C r 1 ). In the second case, we need to select r objects from n-1 objects ( C r ).
Moreover we can derive
n
r
n 1
r
n2
r
n 1
n 1
C r 1 C r
n2
n2
n 3
n 3
C r 1 C r
C r 1 C r
r 1
r 1
C C
r 1
Cr
n 1
k r 1
k
r 1
(1.6)