You are on page 1of 26



    
:  1 
   
 


   :  
Introduction to Algorithms

      


 ! " #$% &'(
& ) &
 #( ! %( *
+  &, ( 
$% *
&$($(  % $, + - &. *, + , #( / &'(
 01
: & % 2, 3

 $, 4"% &'(
& )   / 
. $ 1 ,  1' !
6  $ 
 &'(
 ! .1
   +
&
  *% *' &'(
 0 &(
 &
  &7  .2
& 8% ! - &,

& ) ) *
 %$ &$- *% *)  "
9 &$($(%

*)   ;  &


%
 8(
   &
  4 %(% <) /  :
(

. &'(
 01  &   & "
& %  &   &
  %
/ &
 = >? @%% !%% Program 3
  9 $, &
  &7  .3
= >? & 
&'(
 &
  A % *, + , 1   *) <)
. #( $" % &


   11
 B,"   B
 *
&,

1 :(Programming Language)   
. #( $, 0 % *
3
  &% C
(% % & 
 DD

 @  / & 


&
 &=$ #%
 3
  ! 3
  1 : (Compiler)  
(Machine &BF &B=$ ) (Assembly Language) 4B
% &B=$ EB
3
 
. Language)

*B B

B  !  &( &
  &
$ "%9 : (Algorithm)  
H B% B &B $ 
  % *  #  2
$, !G,) *
1  
  (

  BI  &B(   A   / *


'
 ,  J, -  . 
 < I

1

    
:  1 
   
 
B B A B
 @B%  I   @% !1) *
 K %  L$  &  =
. &  %O &=$  #% " -  ( &$"
   #( )
&$B($(%
  &B %
 ( 
$% ) )   &,

' &
 &
$ " 
!BG
/ LB B &BI / B8% B   & 
&'(
  <P% %
: & % 
 Q"% 
 
. ) )  &
    % % O 
 , .1
. &
 $ - $,  R  .2
B 7  (clear) &BI *B% *) # &
   *
+  .3
(unambiguous) &I
7
  01 ;% *)  /   *
@%
,  %% *) # &
  .4
. &'(
   <P &  &"  &  &" *

Algorithm Specification     21

0B1  
&'(
&
  Q8  ) 
  ( &%) A % Q  +, L1
L %B9% 
    #$() &  *
.  S 9 *
A$% % *) *
Q8 
:Q8  01 !1) *
 . + %
 & %
) &B   &=$ &%
 &=$ ! %(  9 $, 
  &% .1
. &I   *% 
, &"  01 ! %( I . & $6
&   
( &( $
% <) &   
 ! %( 
  &% .2
01 ! %( I /(Flowcharts) & "% 
 1 9)  $, A %

. + -  & ( &
  *% 
, &" 
B1 <  (Pseudo Code) A8 
% &= ! %( 
  &% .3
& $D &=$  C++ 
&
 &= *
3

:&
  &'(
  % & $
, CI % 

*B
a[1 : n]  *
&
8-   *) T  : (Selection sort)   : 
. " ,% " % % &
8" , #%   / n≥1 H  Type 
;

2

    
:  1 
   
 

: % A8 
% ! %( " ,% " % % a[1 : n] &
8" , # % % !%

for (i=1; i<=n;i++) {


examine a[i] to a[n] and suppose the smallest element is at a[j];
interchange a[i] and a[j];
}
: * % 8  * %

 R%( / &
 &=$ Q(
%  %
( a[j] * ) (Minimum) =    .1
:$ 
 a[i] B a[j] =  &
.2
Type t = a[i]; a[i] = a[j]; a[j]=t;

a[i] &B "


!% L  / a[i] 1 minimum *) T % %  & 8  &


 . "  minimum 0 % =) " ,  
$  a[i+1], a[i+2], … BB
.  minimum B a[n] * "  )
: SelectionSort 3
  #%V &"( GO
 ! %(

1 void SelectionSort (Type a[], int n)


2 // Sort the array a[1 : n] into non-decreasing order
3 {
4 for(int i=1; i<=n; i++){
5 int j=i;
6 for(int k=i+1; k<=n; k++)
7 if (a[k] < a[j]) j=k;
8 Type t = a[i]; a[i] = a[j]; a[j] = t;
9 }
10 }

n *B
&B
&B,

C  9 #% % SelectionSort(a,n) &
 :  
:* HBBB  a[1:n] &BBB%
 BBB * BBB % &BBB % / BBB, (n>0)
a [1 ] ≤ a [ 2 ] ≤ ... ≤ a [ n ].

3

    
:  1 
   
 

&  *% / 8  5 *


 $  % $ i=q O
/ i <) ) *
GO :
!
*? q *
) i *% 
, GO L  a [ q ] ≤ a [ r ], q < r ≤ n . *)
*) B / (i.e., i=n) B( LB$% B  B % $ ! *
 . =%% D a[1: q]
a[1] ≤ a[ 2] ≤ ... ≤ a[ n ].
n-1  =% *) *
4  (  for &"$ $,  *) &" 01 *
GO
. &
  & I <) Q *

Algorithms Analysis
  "# 31

:
  ( $, / &
  $, ! *

% 
+, %
W @$
, #7  
% &
  1 .1
W &

  % " C  9 
% &
  1 .2
W  
 A   &
  ! %( A  A Q %  1 .3
W & "
 & 8   % &
 $ & 
 2 D 1 .4
W +2 "$ -  1 .5

. + B  !G$  
 &% !" 
, &  (() 9 &


 01 %%
01 Q "%   / A1 01   !% A  &(  !" D /  1 
. 3
  &% O *
L  

0B1 . &
   + 9
&-O,  
  $, !$ > ) 
%
. 
  * % $%
  % *
$, 
%% 


/ & %
$%  *
&
  %% ! O *
 @ "  :$  %  •
. &
$%   % ! O *
  
$% , #(  % *
X"
.  %  
  3
  * % &
O +  &
 1 :&'$ # •

4

    
:  1 
   
 
    / * "( * "
 X - , A" D 
   $% *
A *) 

 &
  %( % &F *% 

" : @) <) / & 
&'(
 
 & "

) *
B &
  *
I) A &
  *? / &
 %(
 &
 &= *% 


"  !   

% & ((  $


 , H  *
$
 1 A &
  * " :   *
! )
. " &'(
   !"%

  $  %  ( )# 41


:* % (() * %'(
$,  % <  &
  % *
#(
&BD  "% &  &% ) *
&'(
  
(! )  ))   % •
.  1
:" *
 &  1  * : (Polynomials)    8(
 .1
. @
) , ) 
&D  * : (m,n) &( *
(Matrices) 
 8(
 .2
m.n ) m+n ) max(m,n) : 
&
 )
(Vertices) " , &D  * : (Graphs)   8(
 .3
. 
,

) (Edges) ;OI , )
 B
 B , &D  * :(Sorting) # % % 8(
 .4
.  % %
&BD  * : (Syntax Analysis) <,"  $% 8(
 .5
. &
$ 

4B
&
   % *
#(% H  /  $
 *
;) +, ) ;  %  •
B
%  / B $
 0B1  % *
$,   1 
% .  $
 01 ,
. &$ ) 1 %  $
 !
) & (  $


5

    
:  1 
   
 

:" *
& "
  $
, , $,  % !% &
8- *
I , *, H &
  .1
.&
8" ,  @, H <  *  (& 01  &$  1  )
*  & "
  $
, , $,  % !% &
8-   # % % &
  .2
. 4-
 *   %  $
, ,  
4B
  # BI B $
, , $,  % !% * %
# I &
  .3
.,

. ;  *
 $
 , #( 
  "% #( & ((  $
 ;)  % 

:   +   , ( )# +- & 


#.
int, struct, : B
& O,6    " $%   $
 , #(% •
/+  #include, class, etc.,
> B ) 3
 B <) ,%(% D %  S  % + ,   $
 , #(% •
. + + 

B, ;B

B1 $ 1, *? 
$% *
& %%
  $
  , •
X &B
$%$ & ((  $
 , P(X) *  : O
.&
$%    $

P(X1;X2)= P(X1)+P(X2) :*?
: $,)    $
 , *? (if then else) &  9 & $
  , •

P(if A then B else C) ≤ P(A)+max(P(B), P(C))

<B B%
 1 i H  ∑ P(i)
i
:1  $
 , *? "$  , •

. &"$  i !- 
 &"$%
 & ((  $
 , P(i)  &"$ !%
/ &B  ! %(% D % (2 6  ) & 8  3
  Q$% 
 
) •
B $
 B, 1 8  3
  2,%(D &"
 & ((  $
 , *
. 8  3
  1  +
 & ((

6

    
:  1 
   
 
 &"    # / <, 8  3
   % &
O  $
 , #( •
4B
&B 8 6 < 2,%(D   $
 *, % !% H  .& , &

$
   &
  "O
 k<n H  T(k) &D T(n) *   n &
-
& B(() &B $
 *   * , # I & $
,  %   / #
C  
:*?
T(0)= 0
T(n)=T(n-1)+1 for n>=1
T(n)=n 1 &  &-O 01  *) 


: " *
: * %  " " , n 4
 !"% &
  
  "% ) : 
<  % 9 1
. <, 9 2

:<  % 9 & 8 6 1 :#

1 float Sum(float a[], int n)


2 {
3 float s=0.0;
4 for (int i=1; i<=n; i++)
5 s+=a[i];
6 return s;
7 }

return + ,  S  % & $


, *) % L  / n <(  
 ! "D)  
 +
  $
 , / 3 (  + S  % + , % / & (()  $
,
5 (  4
  ( & $
, n % @) 
 n+1 <( 4  (  for &"$
:  . 6 (  ; %(D &
$% + % L 

TSum(n)=1+n+1+n+1=2n+3

7

    
:  1 
   
 
:<, 9 & 8 6 2
1 float RSum(float a[], int n)
2 {
3 if(n==0)
4 return (0.0);
5 else
6 return (RSum(a,n-1)+a[n]);
7 }

B) ; %B(D & $


, & "
 & $
, 
1 *% (() *% $
, % n==0 
, GO
2,%(D & (() & $
, T RSum ( n −1 )  8 n>0 *  
) . ( 4  3 * (
B ; %B(D  4B
 : *% (() *% $
,  L &I / RSum(a,n-1) &
: RSum &$  % *
* L  . X( (

2 if n = 0
TRSum ( n ) = 
 2 + TRSum ( n − 1) if n > 0

&"( &  &-O  T % &"  ! %(Y% .& , &-O &"( &-O  9
:9
TRSum ( n ) = 2 + TRSum ( n − 1)
= 2 + 2 + TRSum ( n − 2)
= 2( 2) + T RSum ( n − 2)
.......... .......... .........
= n ( 2) + TRSum ( 0)
= 2n + 2 n≥0

4B
 &B
  
  "% ) (m,n) &( X *
* %
a, b *% : 
W * %

: & % & 8 6 & * %


4
 &
 *,  :#

8

    
:  1 
   
 
void add(Type a[][size], Type b[][size], Type c[][size], int m, int n)
{
for (int i=1; i<=m; i++)
for (int j=1; j<=n; j++)
c[i][j] = a[i][j] + b[i][j];
}

 $
 , / * %$ %
* %"$ $, 3
  <% 
 . m.n 1 &'(
  
!
1 &   &"$  & ((  $
 ,  m+1 : 1  &"$  & ((
%  3
  *
H  (  (  4
 & $
, mn  @) 
 m(n+1)
:&-O  
  "%
T(mn)=m+1+m(n+1)+mn=2mn+2m+1

4B% n #( 7 C  , <) +2 " !"% &


  
  "% ) : 
: (Fibonacci) 9%  & %%
*
fn Q
 

:& % & 8 6 &


  *,  :#
1 void Fibonacci(int n)
2 // Compute the nth Fibonacci number
3 {
4 if (n <=1)
5 cout <<n<<endl;
6 else {
7 int fnm2 = 0, fnm1 =1, fn;
8 for (int i = 2; i <=n; i++) {
9 fn = fnm1 + fnm2;
10 fnm2=fnm1; fnm1 = fn;
11 }
12 cout << fn << endl;
13 }
14 }

n > 1 &    / n = 1 ) n=0 


,  *%   / n 1  
 !

, .  <( *
  ( 5  4 * ( *
"O *? n=0 ) n=1 
,
( / n <( *
 8 ( / 2 <( *
 7 (  8, n > 1 *

9

    
:  1 
   
 
<( *
 12 (   / 2n-2 <( *
 10 ( / n-1 <( *
 9
: 1 3
   % *
% .

T(n)=1+1+2+n+n-1+2n-2+1 = 4n+2

&B
  B
  "% #(    . " , n *
I%% &%
a[1:n] *% : 
. (Bubble Sort) ," 

: % 9 ,"  &


 % % & 8 6 % :#

void Bubble Sort (int a[], int n)


{
int i, j;
for(i=n-1; i>=1; i--)
for (j=1; j<=i; j++)
if (a[j+1]<a[j]{
int t=a[j]; a[j]=a[j+1]; a[j+1]=t;
}
}

n-1 *
BI%% B * %$ %
* %"$ $, 3
  <%  n 1 &'(
  :
4-
 % & $
, i  & "
& $
, i <  n-i 
  . &    &"$  

: B
BBB 
n −1 i n −1
T (n ) = ∑∑
i =1 j =1
2 = ∑
i =1
2 i = n ( n − 1) = n 2 − n

# %)#* +- – # *)* +-  +/) +   , 51


:* 
Q =%%  
 01 . % %  
 
&
  % *
Q$%
*
I , *, $($(% H &
  / >%
  =%  !  =%
. 1%
 (&
8" !) &
8" , , *
 =% *) *
/ &
8-

10

    
:  1 
   
 

  "%  TA(d) BB  . n !  &'(
  
 Dn BB

. d  
) *
A &
 $
: "
 A &
 $  *()  
  "% 
( •
MinA(n)=Min{TA(d) ; d∈Dn}
: "
 A &
 $  )()  
  "% 
( •
MaxA(n)=Max{TA(d) ; d∈Dn}
: "
 A &
 $ ( 
  "% 
( •
Average A ( n ) = ∑ P ( d )T
d∈D n
A (d )

 
 4
 *% 
,  / d 1 &
  +
*% *) 
% P(d) H 
1
Average A ( n ) = ∑ TA (d )
| D n | d ∈D n
:&"( &-O C% 
%D & (%

. n !   
 , |Dn| H 

n <B% &B
8- *
BI , *, $($(% H$ &
O  "
 , ) : 
W " ,

# AverageA(n) #( MinA(n)=1  MaxA(n)=n : *) CI *


:#
: x   L &
8"  D
%D T & 

. L &
8"  "
x  * *) 
% q *  •
. 
%D & (%
$ 4I
 *? &
8"  x   @) T % •
G %  n  % !8"  &,

 Dn,i BB
( 1 ≤ i ≤ n ) *

D % !8"  &,



 Dn,o BB
(  / i !- 4-
  +
) x  
. x   G

P ( D n ,o ) = 1 − q
q :&"(  I  #(
P ( D n ,i ) =
n

11

    
:  1 
   
 
T(Dn,o)=n , T(Dn,i)=i :*) 3%%( &
   $% *

: ! *

n
q qn (1 + n )
Average A (n ) =
n

i =1
i + (1 − q ) n =
n 2
+ (1 − q ) n =

q (1 + n )
+ (1 − q ) n
2
n (1 + n )
Average A (n) = : *? L *
I 
x * "$( !$  ?
2
3 (1 + n )
Average A (n) = : *? 1/2 1 *
I x  
% * 
4


  , 61

, 4% 1   % &, ( &(  *
/  
 ! 4% &
  "%  % 
&B
B) *B
&
  &   %  &(  01  % .  
 ! 
*
* %
 *  &$81 -   *) D T  *
 / ! +   

. 
  "% H 
 &B
  &B  & 
 / 
  "% 4%  ( # "% % D !G

&
 R%%) A  *) !
 7 *
+  n *% 
, O
 . * %
 & "

4%  &  I   
1  !G
 *
 . & $
, n+5  !) n  & 

4B
TA(n) = n2 B
 1B "% % A &
  & "
    O
 .  "%
&B
  *
I) A &
  *? TB(n)=4n 
 1 "% % B &
 
. n>4 ) *
B

  & "
> %  / 4%  &%

( 
   &"(  "% P%
. 
  "% 4%  &%
X() $,
/ +   
!  
  & "
 $
, $, &  & 
2I 
:& %F ! 1
  Q % *
D L

12

    
:  1 
   
 
[O, Ω, Θ, o, w] ( ,
# 71
B 7 &  , &,

*
& g *% : [big -oh] O(g) 1    
/ f B *
&,

1 O(g) 8, . &
 &" " , &,

 &(
/&B
 &B" " B, &,

 &( 7 &  , &,

*
I )
/ n0 #B( 7 C  ,   / c 

% #
" "    *
@) H 
n ≥ n0  ) *
f ( n) ≤ cg ( n) *

100n+6=O(n) . n ≥ 2  ) *
3n + 2 ≤ 4n *D 3n+2 = (is)O(n) : 
*D 10n 2 + 4n + 2 = O( n 2 ) . n ≥ 6 BB BB) *BB
100n + 6 ≤ 101n *D
10n 2 + 4n + 2 ≠ O( n) . n ≥ 5  ) *
10n 2 + 4n + 2 ≤ 11n 2

:
#.
f ∈ g ( n) E% f(n) = O(g(n) &%
B % *B
*) B B9 O(n) / B &
   % *
*)  9 O(1)
 9 O(n3) /   % &
   % *
*)  9 O(n2)/  &
 
. () &
   % *
*)  9 O(2n)  /  % &
   % *
*)

+ B  ! B- ) *
) ; () *% O(log2 n)  % *

&'(
 &
  ) 
B9 . O(n) B % *
 )  &'(
 X &
 *
( n BB A 9
. O(n2) *
I) * O(n log2 n)  % *
:*)  @9

:# % % $, $ 
 1   % &
) &( %
O (1), O (log 2 n ), O ( n ), O ( n log 2 n ), O ( n 2 ), O ( n 3 ), O ( 2 n )

f, g * %   %( ) T %  &'(


 X *O% * %
 A, B *%
A, * %
  & "
4 %( 8, TA(n)=O(f(n)) ; TB(n)=O(g(n)) : H 
f, g * % & "
 B

13

    
:  1 
   
 

*B
B B  f ( n ) = a m n m
+ .... + a 1 n + a 0 *  :  
m
f ∈ O ( n ) 8, m & 

:
!

m
f (n) ≤ ∑|a
i=0
i | ni
m
≤ n m ∑ | a i | n i− m
i=0
m
≤ n m ∑ | a i |, for n ≥ 1
i=0

f ∈ O ( n m ) *? % 

[big-Omega] Ω( g ) 1    


&B" " B, &B,

B &( 7 &  , &,

*
& g * 
&  , &,

*
I ) / f  *
&,

1 Ω( g ) .8, &



% #
" "   *) H  /&
 &" " , &,

 &( 7
n ≥ n 0  ) *
f ( n) ≥ cg ( n) * / n0 #( 7 C  ,  /c

. n ≥ 1 <) BBBB) *BBBB


3n + 2 ≥ 3n * 3n + 2 = Ω(n) : 2222
. n ≥ 1 <) BBBB) *BBBB
100 n + 6 ≥ 100 n * 100 n + 6 = Ω ( n )
2 2
. n ≥ 1 <) B B
10 n + 4 n + 2 ≥ n * 10n + 4n + 2 = Ω(n )
B) *B
2 2

. n ≥ 1 <) ) *
6* 2n + n2 ≥ 2n * 6* 2n + n2 = Ω(2n )

B
B8, a m > 0  f ( n ) = a m n + .... + a 1 n + a 0 *B
B  :2
m
2  
f ∈ Ω (n m )

14

    
:  1 
   
 

B 7 & B , &,



*
& g *% : [Theta] Θ( g ) 1    
/ Θ ( g ) = O ( g ) ∩ Ω ( g ) B8, . &
 & " " , &,

 &(
AB % *B
) . Ω ( g )  O ( g ) *B
  
%% % 4% &,

/ *) <)
#( 7 C  , c1, c2 *
* " " *%   "  f ∈ Θ (g )
n ≥ n0  ) *
c1 g ( n ) ≤ f ( n ) ≤ c 2 g ( n ) Q"% H  n0

) *
3n + 2 ≤ 4n  n ≥ 2 <) ) *
3n + 2 ≥ 3n * 3n + 2 = Θ(n) : 
2 2
. 10n + 4n + 2 = Θ(n ) . n0=2  c1=3, c2=4 *?B
BB B
BB%  n ≥ 2 <)
. 6* 2 + n = Θ(2 )
n 2 n

BB8, am > 0  f ( n ) = a m n m + .... + a 1 n + a 0 *BB  :22  


f ∈ Θ (n m )

 B"  f(n) = o(g(n)) &B *% : [Little “oh”] o( g ) 1    
f (n )
lim = 0
n →∞ g (n)
*
3n + 2
LB
BBBBB  lim = 0 * 3n + 2 = o(n2 ) : 2
22222
n → ∞ n 2

. 6*2
n
+n2 ≠ o(2n ) n 2 n

  6*2 +n = o(3 ) *) 
 . 3n + 2 = o(n log2 n)

"  f(n) = w(g(n)) & *% : [Little omega] w(g ) 1    
. lim g ( n ) = 0 . * 
n →∞ f (n)

15

    
:  1 
   
 

( ,
# 3 81
B, &,

 &( 7 &  , &,

*
 HO f, g, h * 
: & % S  Q"% # "%   8, &
 & " "
: Transitivity <% &  1

• f ∈ Θ(g ) and g ∈ Θ(h) then f ∈ Θ ( h )


• f ∈ O ( g ) and g ∈ O ( h ) then f ∈ O ( h )
• f ∈ Ω ( g ) and g ∈ Ω ( h ) then f ∈ Ω ( h )
• f ∈ o ( g ) and g ∈ o ( h ) then f ∈ o ( h )
• f ∈ w ( g ) and g ∈ w ( h ) then f ∈ w (h )

Reflexivity & (D &   2


• f ∈ Θ( f )

f ∈ O( f )
• f ∈ Ω( f )

: Symmetry & G% &  3

f ∈ Θ ( g ) if and only if g ∈ Θ ( f )

: Transpose Symmetry "


 & G% &  4

• f ∈ O (g ) if and only if g ∈ Ω ( f )
• f ∈ o(g ) if and only if g ∈ w( f )

16

    
:  1 
   
 

#.
)()  1 % *
*  "  Θ ( g ( n )) &
   % *
* -

. Ω ( g ( n ))  *()  1 % *


 O ( g ( n )) 

&  &,

1
o ( g ( n )) I w ( g ( n )) &,

 -
: 9  *  & "
  a, b * " " * , *  & "
 *  @9%  -

f (n) = O( g(n)) ≈ a≤b


f (n) = Ω( g (n)) ≈ a≥b
f (n) = Θ( g(n)) ≈ a=b
f (n) = o( g(n)) ≈ a<b
f (n) = w( g(n)) ≈ a >b
: % 9 * =%
  $, # "%   A %  
% 
-
H  c, n0, m0 &
   "  f(n, m)=O(g(n,m)) *
9  . n ≥ n 0 , m ≥ m 0 ) *
f (n, m) ≤ cg(n, m) &-O Q"%%
. Ω( g (n, m)) , Θ( g (n , m)) * %,

 A % *
@9

7  % A & I  D


  # "%   ! %( 
-
&
 9% : 
  ( $, . D
 *
& ((
2 2
H  f ∈ Θ(n) & % 2n + 3n +1 = 2n + Θ(n)
& 01  / n BB &
- <) ) *
2n 2 + 3n +1 = 2n2 + f (n)
 # "%   G% D T  . f(n) = 3n+1 *
%  2n 2 + Θ(n) = Θ(n 2 ) : 
&
$ *
 A   (  A 
H  g ∈ Θ(n ) & % f ∈ Θ (n ) & <) ) *
@)  9%
2

. n BB &
- <) ) *
2n + f (n) = g(n)
2

17

    
:  1 
   
 

& 67 ).)   4  5 91


# "%   ! %(  ,(% % + 9 (O(  & I  4% T 

Floors &Ceilings ,)  8 , 5


@
 x <( ) =) C  , ) 1 x ;- / x " " , <) ) *

BB @
 x <( ) ) C  , =)  x A"( 
) . x BB

. x

,)  8 , 5 3


x −1 < x ≤ x ≤ x < x +1 : * x " " , <) ) *
1
n n
 2  +  2  = n :* n #
C  , <) ) *
2

: * n C  , <) *


 / *  7 a, b *   * , <) ) *
3
n  n 
a  n   a   n 
  =     =  
 b   ab   b   ab
   
    

Exponentials  )9 5 3


: & % "%
   n  m / a ≠ 0 & " " ,) & O ) *


a0 = 1
a1 = a
1
a −1 =
a
(a m )n = a mn

18

    
:  1 
   
 

a ma n = a m + n
) *
: *   <) 
 
*
; () * #
 (F 4% 
 

b
b n
.n = o(a n ) *) <) lim n
= 0   b  a>1 * " " * % 
n → ∞ a

x " " , <) ) *


8, . !% 7$ 4% X() e = 2.71828….  %,  
: 

x 2 x 2 ∞
x i
e x
= 1 + x +
2!
+
2!
+ ...... = ∑i=1 i!
x
C% . e ≥ 1 + x : & %
 Q"%% / x " " , <) ) *
%
  * | x | ≤ 1 *  
) x = 0 * " 
, +(
&"( & %

: % # "%
1 + x ≤ ex ≤ 1 + x + x2
e x = 1 + x + Θ ( x 2 ) : * & O  x (% 
,

Logarithms
  :" 3

:   n  a > 0, b > 0, c > 0 & " " , ) *




a = blogb a
log c ( ab ) = log c a + log c b
log c ( a n ) = n log c a
log c a
log b a =
log c b
1
log b ( ) = − log b a
a

19

    
:  1 
   
 

1
log b a =
log a b

a log b n
= n log b a

: 9 |x| < 1 


, ln(1+x) 4% 9
 
x2 x3 x4 x5
ln( 1 + x ) = x − + − + − .......
2 3 4 5

Factorials +"  5 


: 9 n! A  / #( 7 "  ", n *  
1 if n = 0
n! = 
 n .( n − 1 )! if n > 0
n! = 1.2.3……n *) <)
:% 9 Stirling # "% #( n! % -
n n 1
n!= 2π n ( ) (1 + Θ ( ))
e n

Fibonacci Numbers +7  - 1*

: &  &-O 9%  ,) A %


0 if i = 0

fi = 1 if i = 1
f + f if i > 1
 i −1 i −2
&$($( %  / @ * "( *  ;

*
3% 9%  ,) *
,  *) <)
0 , 1 , 1, 2, 3, 5, 8, 13, 21 , 34 , 55 , ….. : 9%  ,) A % & %

: & % &=  ! %( 9%  ,) A % *) *


@) 


20

    
:  1 
   
 
φ − φˆ i
i
fi = , i = 0 , 1 , 2 , 3 , ....... where
5
1+ 5 1− 5
φ = = 1 . 61803 ....., φˆ = = − 0 . 61803 .....
2 2

21

    
:  1 
   
 

  %   101

: & % & 


 4"
$ 
  "% ) :1%  
1 for (int i=1; i<=n; i++) 1 int i=1;
2 for (int j=1; j<=i; j++) 2 while (i<=n){
3 for (int k=1; k<=j ; k++) 3 x++; i++;
4 x++ ; 4 }
(a) (b)

: & % $ 


  "% ) :2%  
void D(int x[], int n)
{
int i = 1;
do{
x[i] += 2; i+=2; }
} while (i<= n )
i = 1;
while (i <=(n/2) {
x[i] += x[i+1]; i++;
}
}

void Transpose (int a[][] , int n)


{
for (int i =1; i<=n ; i++)
for (int j = i+1; j<=n; j++){
int t = a[i][j] ; a[i][j] = a[j][i]; a[j][i]=t;
}
}

void mystery (int n)

22

    
:  1 
   
 
{
int i, j, k;
int r = 0;
for(i=1; i<=n-1; i++)
for (j = i+1; j<=n; j++)
for (k=1; k<=j; k++)
r = r+1;
}

void pesky (int n)


{
int i, j, k;
int r = 0;
for(i=1; i<=n; i++)
for (j = 1; j<= i; j++)
for (k=j; k<=i+j; k++)
r = r+1;
}
void pestiferous (int n)
{
int i, j, k,l;
int r = 0;
for(i=1; i<=n; i++)
for (j = 1; j<= i; j++)
for (k=j; k<=j+i; k++)
for (l=1; k<= i+j-k; l++)
r = r+1;
}
void conundrum (int n)
{
int i, j, k;
int r = 0;
for(i=1; i<=n; i++)
for (j = i+1; j<= n; j++)
for (k=i+j-1; k<= n; k++)
r = r+1;
}

23

    
:  1 
   
 
: &  & % D
 *) * [ :3%  
1. 17 = Θ (1 )
2. 5 n 2 − 6 n = Θ ( n 2 )
3. n! = O ( n 2 )
4. 2 n 2 2 n + n log 2 n = Θ ( n 2 2 n )
n
5. ∑i= 0
i k = Θ ( n k +1 )

6. 33 n 3 + 4 n 2 = Ω ( n 3 )

: &  7 & % D


 *) * [ :4%  
2
(a) 10 n + 9 = O ( n )
(b) n 2 log 2 n = Θ(n 2 )
n2
(c) = Θ (n 2 )
log 2 n

: (growth rate) 1


 
#( & % 4% #% :5%  
n 1
n, n , log2 n, log2 log2 n, log2 2 n, , n log2 2 n, ( ) n ,
log2 n 3
3
( ) n , 17.
2
: $ 
 ) : 6 %  
kf(n) = O(f(n)) *? "
", k *  1
f(n)+g(n) = O(h(n)) *? g(n) = O(h(n))  f(n) = O(h(n)) *  2
: *? T2(n) = O(g(n))  T1(n) = O(f(n)) *  3
T1(n)+T2(n) = O(max((f(n),g(n))) L  T1(n)T2(n) = O(f(n)g(n))

24

    
:  1 
   
 
8, g ( n) = 1 + c + c + ...... + c  "
" " " , c *  * [  :7 %  
2 n

: *
L  / c<1 *  g ( n) = Θ(1)
* "I )  / c=1 *  g ( n) = Θ( n)
. c>1 *  g ( n) = Θ(c n )

: H  B(x)  A(x)    *  :8%  

n n
A[ x ] = ∑
i= 0
ai x i , B[ x] = ∑bx
j=0
i
i

P[ x ] = A[ x ] * B [ x ] 2 #( !"% & 8  & #% 1 : #$



. & 0 
  "% ) 2

. 1 % *
#(  * %
* %
# I !"% & 8  & #% :9 %  

#% . "
"  ", n *   ( " " " ) " ) ", a T  : 10 %  
. 1 % *
#(  an +" #( & 8  &

1 , n &%
 *
&
&
1 (magic square) < ( 4
 :11 %  
<(   ( <) , ;

* H  n2  1 *  : %% &  ,)
 ( 8  1 - $, &-  ;

<(    
, <) , ;


  n=5 ) *
/ 
  ( $, . <  1 - , ;

"I ) <(
: % < ( 4


25

    
:  1 
   
 

15 8 1 24 17
16 14 7 5 23
22 20 13 6 4
3 21 19 12 10
9 2 25 18 11

. 1 % *
)   
n×n *
*
< ( 4
29? !"% & 8  & #%

  *
) #
C  , 1 (prime number)   :12 %  
"  ", *  
 % % & 8  & #% . " @(    @
(-
.1 % *
)  D !) " ) 
"

26

You might also like