Download - Algorithm Analysis 2
-
7/25/2019 Algorithm Analysis 2
1/30
CSCE 3110Data Structures &Algorithm Analysis
Rada Mihalcea
http://www.cs.unt.edu/~rada/CSCE3110
Algorithm Analsis !!
Reading: Weiss, chap. 2
http://www.cs.unt.edu/~rada/CSCE31101http://www.cs.unt.edu/~rada/CSCE31101 -
7/25/2019 Algorithm Analysis 2
2/30
Last ime
Steps in pro"lem sol#ing
Algorithm analsis
Space comple$it
%ime comple$it
&seudo'code
-
7/25/2019 Algorithm Analysis 2
3/30
Algorithm Analysis
(ast time:E$perimental approach ) pro"lems
(ow le#el analsis ) count operations
A"stract e#en *urtherCharacteri+e an algorithm as a *unction o* the,pro"lem si+e-
E.g.!nput data arra pro"lem si+e is length o*arra
!nput data matri$ pro"lem si+e is $ M
-
7/25/2019 Algorithm Analysis 2
4/30
Asymptotic !otation
2oal: to simpli* analsis " getting rid o*unneeded in*ormation lie ,rounding-
14000400151400040006e want to sa in a *ormal wa 3n75 n7
%he ,8ig'9h- otation:
gi#en *unctionsf(n)and gn4 we sa thatf(n)is 9g(n) i* and onl i* there are positi#econstants cand n0such thatf(n) c g(n)*ornn0
-
7/25/2019 Algorithm Analysis 2
5/30
"raphic #llustration
*n 7n;*n is 9n
%he order o* *nis n
g(n) =n
c g(n) =4n
n
(n)= 2n +6
-
7/25/2019 Algorithm Analysis 2
6/30
$ore e%amples
6hat a"out *n >n7 ? !s it 9n?
@ind a c such that >n7 = cn *or an n n0
B0n3
; 70n ; > is 9n3
6ould "e correct to sa is 9n3;n
ot use*ul4 as n3e$ceeds " *ar n4 *or large #alues
6ould "e correct to sa is 9nB
9D4 "ut gn should "e as closed as possi"le to *n
3logn ; log log n 9 ?
Simple Rule: Drop lower order
terms and constant factors
-
7/25/2019 Algorithm Analysis 2
7/30
roperties o' (ig)*h
!* *n is 9gn then a*n is 9gn *or an a.
!* *n is 9gn and hn is 9gn then *n;hn is 9gn;gn
!* *n is 9gn and hn is 9gn then *nhn is 9gngn
!* *n is 9gn and gn is 9hn then *n is 9hn
!* *n is a polnomial o* degree d 4 then *n is 9n dn$ 9an4 *or an *i$ed $ 0 and a 1
An algorithm o* order n to a certain power is "etter than an algorithm o* order a 1 tothe power o* n
log n$is 9log n4 *o$ $ 0 ) how?
log $n is 9n *or $ 0 and 0An algorithm o* order log n to a certain power is "etter than an algorithm o* n raisedto a power .
-
7/25/2019 Algorithm Analysis 2
8/30
Asymptotic analysis )terminology
Special classes o* algorithms:logarithmic: O(log n)
linear: O(n)
quadratic: O(n2
)polynomial: O(nk), k 1
exponential: O(an), n > 1
&olnomial #s. e$ponential ?
(ogarithmic #s. polnomial ?
-
7/25/2019 Algorithm Analysis 2
9/30
Some !um+ers
log n n n log n n2
n3
2n
0 1 0 1 1 21 2 2 4 8 4
2 4 8 16 64 16
3 8 24 64 512 256
4 16 64 256 4096 65536
5 32 160 1024 32768 4294967296
-
7/25/2019 Algorithm Analysis 2
10/30
-elaties/ o' (ig)*h
,Relati#es- o* the 8ig'9h
*n: 8ig 9mega ) asmptotic loer"ound
*n: 8ig %heta) asmptotic tight"ound8ig'9mega ) thin o* it as the in#erse o* 9n
gn is *n i* *n is 9gn
8ig'%heta ) com"ine "oth 8ig'9h and 8ig'9mega
*n isgn i* *n is 9gn and gn is *n
Mae the di**erence:
3n;3 is 9n and is n
3n;3 is 9n7 "ut is not n7
-
7/25/2019 Algorithm Analysis 2
11/30
$ore relaties/
(ittle'oh) *n is ogn i* *or an c0 thereis n0 such that *n = cgn *or n n0.
(ittle'omega
(ittle'theta
7n;3 is on7
7n ; 3 is on ?
-
7/25/2019 Algorithm Analysis 2
12/30
E%ample
Remem"er the algorithm *or computing pre*i$ a#erages
' compute an arra A starting with an arra F
' e#er element AGiH is the a#erage o* all elements FGIH with I = i
Remem"er some pseudo'code J Solution 1
Algorithmpre'i%Aerages14Input4 An n)element array o' num+ers.
Output4 An n )element array A o' num+ers such that A5i6 is the aerageo' elements 506, ... , 5i6.
Let A +e an array o' nnum+ers.
fori0 ton) 1 do
a 0
for7 0 toido
a a 8 5j6
A5i6 a9i8 1
returnarray A
Analyze this
-
7/25/2019 Algorithm Analysis 2
13/30
E%ample cont:;
Algorithmpre'i%Aerages24
#nput4 An n)element array o' num+ers.
*utput4 An n )element array A o' num+ers such that
A5i6 is the aerage o' elements 506, ... , 5i6.Let A +e an array o' nnum+ers.
s0
fori 0 tondo
s s 8 5i6
A5i6 s9i8 1
returnarray A
-
7/25/2019 Algorithm Analysis 2
14/30
(ac< to the original =uestion
6hich solution would ou choose?9n7 #s. 9n
Some math J
properties o* logarithms:log"$ log"$ ; log"
log"$/ log"$ ' log"
log"$a alog"$
log"a log$a/log$"
properties o* e$ponentials:a";c a"a ca"c a"c
a"/ac a"'c
" a loga"
"c a cKloga"
-
7/25/2019 Algorithm Analysis 2
15/30
#mportant Series
Sum o* sLuares:
Sum o* e$ponents:
2eometric series:
Special case when A 7 70; 71; 77; J ; 7 7;1' 1
Nlargefor36
)12)(1( 3
1
2 NNNNi
N
i
++
=
=
-1kandNlargefor|1|
1
1
+
+
=
k
Ni
kN
i
k
1
11
0
=
+
=
A
AA
NN
i
i
=
+==+++=N
i
NNiNNS1
2/)1(21)(
-
7/25/2019 Algorithm Analysis 2
16/30
Analy>ing recursie algorithms
*unction *oo param A4 param 8
statement 1N
statement 7N
i* termination condition returnN
*ooA4 8N
O
-
7/25/2019 Algorithm Analysis 2
17/30
Soling recursie e=uations +yrepeate; su+stitution
T(n) =T(n/2)+ c substitute for T(n/=T(n/4) + c+ c substitute for T(n/4)= T(n/8) + c + c + c
= T(n/23) + 3c in more compact form= = T(n/2k) + kc inductive leap
T(n) = T(n/2logn
) + clogn choose k = logn=T(n/n) + clogn=T(!) + clo"n = b + clo"n = #(lo"n)
-
7/25/2019 Algorithm Analysis 2
18/30
Soling recursie e=uations +ytelescoping
%n %n/7 ; c initial eLuation%n/7 %n/> ; c so this holds
%n/> %n/P ; c and this J
%n/P %n/1 %7 ; c e#entuall J
%7 %1 ; c and this J
%n %1 ; clogn sum eLuations4 canceling theterms appearing on "oth sides
%n Qlogn
-
7/25/2019 Algorithm Analysis 2
19/30
ro+lem
Running time *or *inding a num"er in a sortedarra
G"inar searchH
&seudo'code
Running time analsis
-
7/25/2019 Algorithm Analysis 2
20/30
AD
A% A"stract ata %pes
A logical #iew o* the data o"Iects togetherwith speci*ications o* the operations reLuired
to create and manipulate them.
escri"e an algorithm ) pseudo'code
escri"e a data structure ) A%
-
7/25/2019 Algorithm Analysis 2
21/30
What is a data type?
A set of objets! eah alled an instane of the data ty"e#
$o%e objets are s&ffiiently i%"ortant to be "ro'ided
ith a s"eial na%e#
A set of o"erations# "erations an be realized 'iao"erators! f&ntions! "roed&res! %ethods! and s"eial
synta* (de"ending on the i%"le%enting lang&age)
+ah objet %&st ha'e so%e re"resentation (not
neessarily knon to the &ser of the data ty"e)
+ah o"eration %&st ha'e so%e i%"le%entation (also not
neessarily knon to the &ser of the data ty"e)
-
7/25/2019 Algorithm Analysis 2
22/30
What is a representation?
A s"eifi enoding of an instane
,his enoding .$, be knon to i%"le%entors
of the data ty"e b&t N++ N, be knon to
&sers of the data ty"e
,er%inology "we implement data types using
data structures
-
7/25/2019 Algorithm Analysis 2
23/30
Two varieties of data types
"a&e data ty"esin hih the re"resentation isnot knon to the &ser#
,rans"arent data ty"esin hih the re"resentationis "rofitably knon to the &ser- i#e# the enodingis diretly aessible and/or %odifiable by the&ser#
6hich one ou thin is "etter?6hat are the means pro#ided " C;; *orcreating opaLue data tpes?
-
7/25/2019 Algorithm Analysis 2
24/30
Why are opaque data types etter?
e"resentation an be hanged itho&t affeting&ser
ores the "rogra% designer to onsider the
o"erations %ore aref&lly+na"s&lates the o"erations
Allos less restriti'e designs hih are easier toe*tend and %odify
esign alays done ith the e*"etation that thedata ty"e ill be "laed in a library of ty"esa'ailable to all#
-
7/25/2019 Algorithm Analysis 2
25/30
!ow to design a data type
Step # Specification
ake a list of the o"erations (j&st their na%es)
yo& think yo& ill need# e'ie and refine the
list#
eide on any onstants hih %ay be re&ired#
esribe the "ara%eters of the o"erations in detail#
esribe the se%antis of the o"erations (hatthey do) as "reisely as "ossible#
-
7/25/2019 Algorithm Analysis 2
26/30
!ow to design a data type
Step $# Application
e'elo" a real or i%aginary a""liation to test the
s"eifiation#
issing or ino%"lete o"erations are fo&nd as a
side-effet of trying to &se the s"eifiation#
-
7/25/2019 Algorithm Analysis 2
27/30
!ow to design a data type
Step %# &mplementation
eide on a s&itable re"resentation#
4%"le%ent the o"erations#
,est! deb&g! and re'ise#
-
7/25/2019 Algorithm Analysis 2
28/30
E%ample ) AD #nteger
ame o* A% !nteger
9peration escription C/C;;
Create e*ines an identi*ier with an
unde*ined #alue int id1N
Assign Assigns the #alue o* one integer id1 id7N
identi*ier or #alue to another integer
identi*ier
isELual Returns true i* the #alues associated id1 id7Nwith two integer identi*iers are the
same
-
7/25/2019 Algorithm Analysis 2
29/30
E%ample ? AD #nteger
(ess%han Returns true i* an identi*ier integer is
less than the #alue o* the second id1=id7
integer identi*ier
egati#e Returns the negati#e o* the integer #alue 'id1
Sum Returns the sum o* two integer #alues id1;id7
9peration Signatures
Create: identi*ier !nteger
Assign: !nteger !denti*ier
!sELual: !nteger4!nteger 8oolean(ess%han: !nteger4!nteger 8oolean
egati#e: !nteger !nteger
Sum: !nteger4!nteger !nteger
-
7/25/2019 Algorithm Analysis 2
30/30
$ore e%amples
6ell see more e$amples throughout thecourse
Stac
ueue%ree
And more