Intro To Algorithms 2
Intro To Algorithms 2
Reading:
Cormen, Leiserson, and Rivest,
Introduction to Algorithms
Chapters 1, 2, 3.1., 3.2.
Analysis of Algorithms
input size.
– Different measures of input size depending on the problem
– Examples:
Problem Input Size
Sorting List of numbers to sort # of numbers to sort
Multiplication Numbers to multiply # of bits
Graph Set of nodes and edges # of nodes + # of edges
“Running time” of an algorithm: number of primitive “steps”
executed
– Think of each “step” as a constant number of instructions, each of
which takes constant time.
Example: Insertion-Sort (CLR, p. 8)
cost times
1. for j 2 to length[A] n
2. do key A[j] n-1
3.
4. i j-1 n-1
5. while i 0 and A[i] key
6. do A[i+1] A[i]
7. i i-1
8. A[i+1] key n-1
+ *
+ 0
+ 1
'
')
' / ,
' / ,
'2
"!
(
# $
# %&
#
%#
%#
%# ,
%#
.-,
.-,
.-,
&
&
&
In the best case, when the array is already sorted, the running time is
3
a linear function of .
4
!"
5
6%
In the worst case, when the array is opposite of sorted order, the
3
4
& 6%
!"
5
#
%
We are interested in the rate of growth of the runtime as the input size
7
<;:9
8
=
We will normally look for algorithms with the lowest order of growth
7
anytime algorithm.
Non-experimental method:
?
@
2. Analyze runtime for each particular input and calculate the
average over all the inputs
Experimental method:
?
recurrence equation.
E
time to combine solutions of size
GF
B
ED
E
H
J K
B
I
the original problem.
OND VUD
P Q YZ[
E \]^
TSR
M LI
F XWF K
G<F
L
ED
C
ED
ED
P_
\
H
XWF
1
1
1
1
pv
i oq
: Initialization
up t
9
w
and
pv pv
mx zv
h i oj o lt
p pu o nt
fgc
ce st ij y iq y { {
on l
cd xk v x k ztv pk v pk v
3. create arrays
x
to
to
ba` <ml rm st w p p
xk k z i oj i oq t kx
ik j ik q st kz
do
do
Merge
w
4. for
6. for
10.
11.
1.
2.
5.
7.
8.
9.
11-711 Algorithms for NLP
(cont.): Actual Merge
10
times
n
n
g~
~
~ to
then
else
b}|
do if
12. for
Merge
13.
14.
15.
16.
17.
Merge Sort (A,p,r)
times
1. if p r
2. then q (p+r)/2
3. Merge-Sort(A,p,q)
¡
¡ ¡
4. Merge-Sort(A,q+1,r)
5. Merge(A,p,q,r)
¢
¤
the subarrays.
<ª© ª ©
¥
¦
¬O§ ¨§
¨§ ¨§
« «
© ©
¥
subproblems of size .
® ¯
The resulting recurrence equation is:
¥
ª ¨ ¨µ
O¬§ §
¬ ¬
±² ±²
« ³°
© ® X´© ¯
ª©
°
¨§
¨§
«
©
By solving the recurrence equation we get ,
ª©
¥
¶¨§
¨§
·¸
¨¯
«
©
much better than for Insertion-Sort.
¯ ©
¨§
«
simplest one uses a recurrence tree, where each node in the tree
indicates what the cost of solving it is. Then inspecting the tree can
give us the solution.
13 11-711 Algorithms for NLP
In this case, we get , because we divide the problem in half in
¹
º»
¾ ½¼
each recursive step.
of functions.
We usually don’t look at exact running times because they are too
¿
Instead, we look at inputs of large enough size to make only the order
¿
ÄÂÃ
ÄÃ
Ç
ÈÃ
ÅÅ
Formally:
Á
ËÌ
ËÍ
Ä
Ç
Î
ÈÃ
ÂÄÃÉ
ÅÊ
such that for
Ë ÌÈ
ÄÃ
ÄÂÃ
Ë ÍÈ
ÄÃ
Ï
Ð
ÐÅ
ÐÅ
Å
all
Ä
ÑÄ
ÎÒ
means a constant function or a constant
Á
OÓÃ
Ç
Å
ØÖ×
Ø×
Ø×
ØÖ×
Û
Ü×
Ü
ÙÙ
Ù
faster
Formally:
Õ
ß
Ø
Û
à
Ü×
ÖØ×Ý
ÙÞ
such that for all
ØÖ× Û Ü×
ß Ü ÙÙ
Ø×
Ø
á
â ÚÙ
â Ù Ø×
ãØ
àä
Ù
Note:
ÚÙ
Ú ÙÙ
Õ
ØÖ×
Ø×
ØÖ×
å
æ
Ü×
ð
íëì
íì
íì
íëì
ñì
ñ
îî
î
much slower
Formally:
ê
ô
í
õ
ñì
ëíìò
îó
such that for all
ôñ
íì
íëì
í
ö
÷
øí
õù
î÷
î
Theorem:
ê
íì î
ñ íñìì
î
iff î î and
ïî
ïî
ð
íëì
íì
í
íëì
íì
ú
û
ñì
ëì
ñì
îî
îî
18 11-711 Algorithms for NLP
-notation (“Little-o” notation)
ü
Upper bound that is not asymptotically tight
ý
þÿ
þÿ
ÿ
ÿ
ÿ
Formally:
ý
þÿ
ÿ
ÿ
a constant such that
þÿ
ÿ
for all
We will normally use “Big-O” notation since we won’t want to make
ý
Note: if and only if
Formally:
!
!
$
#
"
for all
%
"
&
,
(
– Example: )
1 0
1 0
1 0
1 0
/.
/.
/.
5.
/.
/.
5.
/.
)
430
)
-
2.
-
0
0
0
The , , and relations are reflexive.
'
*
(
– Example:
1 0
/ .
/ .
)
-
-
.
0
0
Note the analogy to the comparison of two real numbers.
'
e.g.
10
8
7 0
/.
/ .
9
<;:
)
-
2.
e.g.
1 0
8
7 0
/.
/ .
9
;=
-
2.
?
@
B B F
B B B
nor
B
A @
A @
A @
22
? ? B
@ @ ? B
@
D F
I A@
?
@
C B C B
C B
E
A @
A @
are neither
A @
Transpose symmetry:
G G
Lack of Trichotomy:
G
iff
iff
iff
B B B B B
B B B B B B
Transitivity: all
B
A@ A@ A @
A @
A @
A @
? ? ?
Reflexivity:
@ @ @ G@ G@ B
Symmetry:
G@
D E F D E H A@
?
Some
CB CB CB C B
C B
C B
A@ A@ A@ A@ A@ A@
? ? ? ? ? ?
> > > > >