Daa Module 1 Text
Daa Module 1 Text
Algonthrns can be contrasted with programs. Algorithms. like blueprints that deals with large-scale software development Pro· ct
· · Je mana
that are used for constructing a house, help solve a given problem issues such as team management, extensive planning _9ernel'lt
. t h d . , cost estin,<11;..
logically. A program is an expression of that idea, consisting of a set an d proiec sc e uhng are the main focus of software . ~'"JI\
engine . ,
of instructions written in any programmi ng language. The develop- 0 n the other hand, algorithm design and analysis as a fi Id eru1g.
ment of algorithms can also be contrasted with software development. take a micro view of program development. Its focus . iet of study
. . is o deve•-
At the industrial level, software development is usually undertaken by effi cIent algonthms for basic tasks that can serve as a bu'ld' 11JP
a team of programmers who develop software, often for third-party for various app Icat1ons. Often project management issues iofing bin..,.
. 1· .
v""
customers, on a commercial basis. Software engineering is a domain development are not relevant to algorithms. SOftware
Finiteness An algorithm should have a finite number of steps and should terminate after executing the finite set of i
0st
tions. Therefore, finiteness is an important characteristic of an algorithm. ruc.
Some of the addit ional characteristics that an algorithm is supposed to possess are the following:
Generality An algorithm should be generic, independent of any programming language or operating systems, and able t
0
handle all ranges of inputs; it should not be written for a specific instance.
An algorithm that is definite and effective is also called a computational procedure. In addition, an algorithm should be
correct and effic ient, that is, should work correctly for all valid inputs and yield correct outputs. An algorithm that executes fast
but gives a wrong result is useless. Thus, effic iency of an algorithm is secondary compared to program correctness. However
if many correct soluti ons are available for a given problem, then one has to select the best solution (or the optimal solution)
based on factors such as speed, memory usage, and ease of implementation.
Table 1.2 Compl(~'l!ily or TSP Thus. cvcry add ilio 11 or a cily can he 11n1cd to increase the rath ex ponent
ial ly.
[ Numbor of cities 'j Number of routes
Tnbk 1.2 shows 1hc nun1hc r or pnss ibk ro11lcs.
Therefore, it ca n be observed th at, l'or N cities, the number of route<;
0 (os there ls no route)
wo uld
,
I ,
Model of Computation
A computation model or computational model is an abstraction
of a real-wo rld comput er.
A model of computation is a theoretical mathematical model and does
not ex ist in phys i-
cal sense. It is thus a virtual machine, and all programs can be execute
d on this virtual
machin e theoretically. What is the need for a computing model? It
is meanin gless to talk
about the speed of an algorithm as it varies from machin e to mac hine.
An algorithm may
run faster in machin e A compared to in machin e B. This kind of analysi
Fig. 1.3 Stages of
s is not correct as
algorithm analys is should be independent of machin es. A comput ing
problem solving
model thus fac ilitates
such machin e-independent analysis.
putability the
t is u_s e~ widely in com
j:r §i ) Alan Turing the Turing machi~e tha dite d with the d _ory allu
contr,bulion 1owa s
rd n Turrng ,s also cre
4) Is well known for his complexity ana lysis. Ala lligence of es,gl'lif'lg 01
(1 9
· 12 195 asure of tes ting the inte a niac n·'~ti
Ala n Tur ing
ored by many os the 'fa
ther of mo den ; the 'Turing Test' as a me
computing Ho Is consid theory a~d artlficia
n towards computablhty
rom puling·. His contributio ed o theore tical rnacliine, ca lled
al. He des ign
Intelligence Is monument
Fu el station
Queue of trucks
sn~
-=~ ~ ~ ~~
~~ ~~~~ ~
Fig. 1.4 Example of a queue
ns he lp specify th•
n sho uld be spe cif ied . Th ese va lid op era tio
tions of the mo del of co
mp uta tio ute time an;
First. all the \ a lid op era vid e a no tio n of the ne ce ssa ry st ep s to co mp
ng mo de ls pro
t. ln add itio n, the co mp uti . . .
inp ut. pro ces s, an d outpu ch m~ IS use d to perform
of a give □ alg ori thm . ine (R AM ) or a Tu rm ? ma
spa ce co mp lex itie s a ran do m acc ess ma ch ter 18. The
mp utation mo del suc h as s are dis cu sse d m Ch ap
Fo r alg ori thm study, a co d in Ch ap ter 3 an d Tu rin g ma ch ine
ori thm s. RA M is dis cu sse de ls (re fer to Bo x 1. 7).
co mp lex iry an aly sis of alg the stu dy of co mp uta bil ity an d co mp uti ng mo
g is vital for
co ntr ibution of Alan Tu rin
m
for so lvi ng a giv en proble
Da ta Organization ati on shi ps are sto red . Al go rit hm s req uir e da ta ori thm s and data
the wa y dat a and its rel ori thm s. T he ref ore , alg
Data str uct ure co nc ern s can ha ve im pa cts on the eff ici en cy of alg
ir organi zat ion
Th e nat ure of dat a and the ect of pro ble m sol vin g. ple of da ta organization
ure s tog eth er oft en co nst itu te an im po rta nt asp life . Fig ure 1.4 sh ow s an ex am
str uct wi th in ou r da i ly
av oid ch ao s. A que ue
eth ing we are fam ilia r sh ow n in the fig ure , to
Data org an iza tio n is som int sho uld ha ve a qu eu e, as
an d a vehicle
lle d a qu eue . A ga s sta tio n wi th on e ser vic ing po ce ssi ng (fi llin g of ga s) is do ne at on e en d
ca n wh ere the pro
(FI FO )) is an org an iza tio the ch oic e of structures.
(o r Fir st- in -Fi rst -O ut in thi s cas e. Of ten a pro ble m dic tat es
Al l ve hic les ha ve sam e
pri ori ty hig he st priori~
is ad ded at the oth er e □ d. ple , ha nd lin g me dic al em erg en cie s, wh ere
of, for ex am
ma y no t be va lid in cas es
Ho we ver , this str uc tur e int of problem solv-
sho uld be giv en to urg en
t cas es.
s the eff ect ive nes s of alg ori thm s. At so me po
a very important issue tha
t inf lue nce 'algorithm +
Thus, data organization is ly. A po pu lar sta tem en t in co mp ute r sci en ce is
the dat a effective
sho uld be g iven to sto rin g al in the pro ble m- sol vin
g pro ces s.
ing. carefu l con sideration str uct ure oft en pro ve s fat
A wr on g selection of data
data structu re = program '.
1.6.3 Designing an Algorithm ry iss ue s of thi s sta ge :
ble m sol vin g. Th e fol low ing are the tw o pri ma
t sta ge of pro
Algo rithm design is the nex ori thm ? ·
n an alg ori thm ? 2. Ho w to ex pre ss an alg
1. Ho w to de sig
str ate gie s dimr
s usi ng an ap pro pri ate de sig n str ate gy. De sig n I it i•
uti on
thm des ign is a wa y o~ dev ~lo pin g al~ ori thm ic sol ho ne dir ect ory If an yo n t.rt
sea rc
h.
mg
fr
om pa ge , ~·
ti Alg ori rch ing a na me in a tel ep · esa s ·
Jus t 1m ag 111e sea .
ram problem to problem. s· k
d1rectory is ind ex ed ' it ma es sen se to op en the bo ok an d us e the
mdex al
ter me d as a b ru Ie fiorc e str ate gy. mc e a tel ep ho ne · d. • stu dy .
S e PI
om
is is a bet ter str at. g me acr oss ma ny de s alg ori thm
top tu locate the name. Th e y. O ne ma ydco . ign pa ra 1gms m the d . 'd sih<
div ide and co d mm ing D' ·d d for ex am ple , IVl e ·.
gm s are am 1c pro gra . 1v1 e an co fth nq ue r,
the im portant design paradi nq ue ran yn b bl . · ven pro ble m. DynaJ 111,•
bl lem s and co mb ine s th e res ults of th -pr o em s to ge t the fin al so Iu t·ion o e g1
pro em into su b-prob . s the pro ble m as a seque nce of d . .
e su . . sub -pr ob lems r,
. vis
1ng
. ualize
Th en it co mb ine s the op tim al sol uti on s of the
prc,grnmm eci sio ns.
/11tmr/11r l inn 111 Alxori1hm1· 9
get an 0p1imal glnhal s1,hll 1l,11 . rv1a11 y design ,·nri:,nl s Sllch as greedy nppn1:H.:h. hackt ruck 111g, anu bra nch nnd bound tec hn iq ues
ha, c hrcn den II \\ nh 111 th is bl,l'J,, Thl' n,k or alg(ll ithm dcsl).!.II is inipmt nnt 111 developing clli cient algorithms. Thu~, one impor-
tant skill rcq11in.', d 1~,1 p1\,hkm ~ll h 111 g. 1~ the sckction ,llld appli ca tion of' suit nblc de~ ign pHradi gms. /\ ~killed algorith m designer
1~ ralkd .m 'nlg11nst' .
Algorithm Specification
A1k r the :i ll_!nri thm i~ dl.'si gncd, ii shnuld be commu ni cnted lo a programmer so that the algo rithm can be coded as a program .
Thi s stag1.' 1s l.'.,1lkd algo,-irhm specifirn 1io 11 . Onl y three possibilities exist for com muni cat in g the idea of algo rithms. One is to
use na1ur,1l languages sud1 ns English to co mmuni cate th e algorithm . This is preferable. However, the natural language has
$o.11lW di ~:ld \ anta g:t'S such as ambigui ty and lack of precision. Hence, an algorithm is ofte n written and communi cated through
psl.'11d0l'l1dc. Pseud oc0dc is a mix of the natura l language and mathematics. Another way is to use a programming language.
Tiie pn,bkm \\ ith programming language notation is that readers often get bogged clown by the programming code details.
Therefore. the pseud ocode approach is preferable. Even though there are no specific rules for algorithm writing, certa in
guidel ines c:in be fo ll owed to make algorithms more understa ndable. Some of these guidelines are presented in Chapter 2.
Example 1.1 A ssum e that there a re two a lgorithm s A a ~d B for a given proble m P. !he time complexity. functi o ~
a lgorithn1s A and Bare. respecti ve ly, 3n and 2". Which algonthm s hould be selected assummg that all other cond 1t1ons refllain
th e same for both algorithm s?
So/11rio11 A ss uming tha t all condition s remain the same for both a lgorithms, the best algori~hm takes less time when the
111 Table 1.3.
inpu1 is changed to a larger va lu e. Res ults obtained employing different values of n are s hown
1 3 2 ,,
5 1-p 32
10 30 1024
2100
100 300
- -
It can be observed tha t a lgorithm A performs better as n is increased; time complexity increases linearly and gives lesser
100
\'a lues fo r larger va lu es of n. The seco nd a lgorithm instead grows exponentially, and 2 is a very large number,
Therefore, algori thm A is bette r than algorithm B.
Example 1.2 Let us assume that, for a problem P, two algorithms are possible- algorithm A and algorithm B. Let us also
2
ass ume that the time comp lex ities of algorithms A and B are, respectively, 3n and 1On instructions. Here, n is the input size of
9
the problem . Let the input size n be 10 instructions. If the computer system executes I 0 instructions per second, how much
5
I Algorithms I
I
I I I I
Classification Classification Classification Classification
based on based on based on area of based on
implementation design specialization tractability
i
fl ~ q11ltjcr l1 vc rnra GllrCC! di tfcr Imm r cr<ion In r cr<,on
11r from 1i1 nc lo li me. I lcncc, ohjN tiw• IIINI V!trf'I' arc
L -
Suhw,-th•;, n,Pasures-
((-> fl , NI S(' nf
,rnptpmenmtion)
Clt,jectWe meesu,., J H:q1111 cd ror pcrlnrnw11cc cva lu::it ,nn . <icncra ll y. lbcr,c
111ca~ urc~ i11cl11dc rn ca 'l urcrncntc. of qomc comrutcr re-
source,; ~uch a'l comput 111g <;peed. mcm()ry rcqutrcmcnt,.
1 disk usage. and network u'lngc. n,cd ,o,;cn r,hJcct1 ve
_j_ -
Dynamic objective
measures
--
j 1
Static objective
measures
meas ures should be qu,111tiri;ib lc. und ~hould ,il,;o have
predicti ve power and inherent 'l im pl 1cily (JbJCCt1ve
measures can furthcr be class ifi cd ns sta llc and dynam ic
(e.g .. time and space) (e.g ., program length)
objecti ve measures.
Fig. 3.1 Measures for algorithm analysis
Static ob_jective measures Static objecti ve measure'!
. . do not depend on input instances. These measures do
rwt change 1TTcspecti ve of how many instances of an algorithm are scaled (or increased). Examples are program length and
pr,1gram wilu me. Though static objecti ve measures are simple, they are not suitable for algorithm analysis.
Dynamic objective measures Dynamic objective measures are immensely popular. These measures are dependent on input
in~tances and hence are more accurate. They include run-time (or execution time), memory space, cache hit ratio, disk usage,
and network usage. Among these objective measures, computing resources such as time and space for measuring run-time is
,·ery popular.
Time complexity is more important than space complexity. This is due to the declining cost of primary and secondary
memory devices. Hence, except in embedded applications that should be implemented in limited space environments, space
complexity is not of a much concern. The implementation of a program involves two time factors- run-time and compile
time. Run-time can also be called the execution time. For algorithm analysis, only run-time is considered, as compile time
is not important. A program may be compiled or recompiled many times, and hence is machine-dependent. Thus, time in
algorithm analysis always refers to the run-time that an algorithm requires to produce expected results for legal inputs. In
addition. the running time is not expressed in units of seconds, minutes, or milliseconds, but in tenns of CPU cycles . As time
comple xity only aims to predict the algorithm behaviour when the inputs are scaled higher, the units are often immaterial
and hence would not be quoted explicitly.
Time efficiency or time complexity is a measure of how much run-time an algorithm requires for execution when the input
size is scaled. Scaling an input means increasing the value of an input. A variable Norn is usually reserved to represent the
input size of an algorithm. Therefore, time complexity is always expressed as a measure of the input size. Input size is the
length of the input of an algorithm. Why should input size serve as a basis for complexity? For example, sorting of a list of
IOnumbers can be easy. However, the same sorting algorithm becomes complex when used for a list of a million numbers.
This shows that a direct relationship exists between input size and complexity. Thus, time complexity analysis aims to predict
the beha viour of an algorithm by determining the relationship between processing time and input size as a function t(n) or T(n ).
Let us discuss the methods of computing time complexity in the subsequent sections.
"ad the accumulato r with the content of location 1000d where Xis stored
.:i~ h\\' l ,.
ontent of
t
the accumulator to Y. th ath i s sore at 1ocation 2000
~:,~ :,'\\) ~dd th e c .
·e -ult at location 3000 that is were S 1s present
~ T~ 3\),1(\ Store the 1 ~
f. ~l T Ha lt
Logarithm cost model In a logarithmic co st model th
The time complexity of an algorithm is estimate? by ?ounting the
pendent on the operand. If a larger integer 1· ' e~.
number of operations. Thus. every step of an algorithm 1s tra.nslated . . s_used, the ~
determined as the logarithm of the size of nlhe er.,
to a set of statements of a RAM. Every step and every operation of a Operand rnu1t·
1Plied t.
RAM is supposed to get executed in a constant time. In this c~nte~, .
factor c (e.g., clogn). This model can easily be simulated .10
one can classify RAMs into two types: unit cost model and logarithmic world computer by scaling down the computer t·im ewttha a;,
. .
cost model. A u111t cost model 1s more robust and used more Wide!COns,,
Unit cost model In a uniform cost model, the cost for performing
because computations based on a logarithm cost model do Y. Tr
an operation is assumed to be one. In addition, the cost is not dependent
much from those based on a unit cost model. Ooic
on the operand.
d in the follow·tngtwo,
With a suitable model such as a RAM, time complexity analysis of an algorithm can be performe
I. Mathematical analysis 2. Empirical analysis
is translat d;
Marhematical analysis (or a priori analysis or theoretical analysis) is performed before the algorithm e'
Thus; mathemat ical analysis is used to estimate time complexity in terms of step count or operation count
program.
The following are the advantages of this mathematical analysis:
operating system, corr;
I. Mathematical analysis is independent of any particular machine, programming language,
·
interprete r, and case tool.
2. It is applied before the algorithm is converted to a program.
An analysis of an algorithm before the actual code is developed is immensely useful, as
it helps in detennining the effici
solution has a limit, then'
and theoretical limitations of the algorithm (refer to Table 3.1 ). If the problem is unsolvable or the
the constraints of the pror
alternate actions can be detennined either in the form of approximate solutions or by relaxing
because the anaip
Another kind of analysis is called empirical analysis. This is also called a posteriori analysis
study is called experi,r,
perfonned after the implementation of an algorithm in the form of a program. This area of
time T(n) is detennilli
a/gorithmics. In empirical analysis, a program is executed with real-time datasets and the running
the algorithm. The adiar
clocking the speed. Then numerous statistical tests are performed to predict the behaviour of
parameters. This helpspt
of empirical analysis is that it can accurately find the values of machine-dependent algorithm
run faster. Differencesti<r
measurements accurately and, if necessary, tune the parameters of the algorithm, so that it can
these two types of analyses are tabulated in Table 3.1.
Sections JJ-J.5. frn;
Mathemat ical analysis is more popular than empirical analysis and is discussed in detail in
rsive) algoriUJJ11l
analysis is discussed in Section 3.7. Let us now discuss mathematical analysis for iterative (non-recu
Basin of AIKnrithm Analysis 47
3
Table -1 Compari son between mathematical and empirical analyses
M1lhematlc1l 1nalyala
Emplrlcal analy1l1
Mathemntic-,al analysis is done before th . . .
oonve11ed to a program. 0
algorithm 1s Empirical analysis is done after the algorithm is converted
to a code.
1 This analysis is independent of th
, machine environment e program language or
' . This analysis Is dependent on the program language or
machine environment.
This analysis is also independent of input instances.
Results of this analysis are dependent on the distribution
of inputs, and all machine dependencies are taken into
account.
F~r lengt~y alg.orithms, the analysis is difficult. Therefore
th,~ technique ,s preferable for small algorithms and is of, Length of the program is immaterial. Any program of any
\ hm1ted applicability. size can be handled. Therefore, this technique is more
I practical.
Sound mathematical knowledge is required as the analysis
I is based on mathematical concepts. Sound mathematical knowledge is not required .
I
T~is analysis i~ independent of software testing as the
pnmary focus is only on algorithms and not on program code. This analysis is more related to software testing.
Integers Integers are typical algorithm inputs. The common practice is to use the number
of bits required to store an input.
Consider the famous problem of primality testing. The primality testing algorithm takes an
input number N and checks whether
the given number is a prime number or not. Since the input of a primality testing algorithm
is a single number, let us assume
that N = 1O. Therefore, the input size is given as logi(N) = 4 (as the binary representation
of l Ois 1010). It can be observed
that the logarithm function is used to make numbers small.
Matrices For many algorithms involving matrices , such as matrix multiplication, addition/
subtraction, matrix inverse, and
determinant, the order of the matrix can be considered for finding the input size.
Arrays For algorithms involving arrays (or sets or tuples) of many elements, the order
of the arr~y can be used as the input
size. For example , let/= (Xi, X2, .. . , X,,), then the size is given as log of n, that is, log
2 n. Here, n 1s the number of elements
present in the array.
Graphs For algorithms involving graphs, either the number of edges or the number of
vertices, or both can be used as the
input size of the graph .
3.3.2 Measuring Running Time
l he nnr 1.1~i.. i), fl) dctc1111 111c the 1111111i11g
,·:rn be l·:111 wd 1)t1f 111, /llWI I S "avs. Soni
1i111c of an algo ri1h111 , whi ch
c orthe 111cthods that arc used
d
r-::::::,-~ l
/ Ys,s
nd meth od of dete rmin ing the run- time is based on operation count. ~sin ns enipl oyei
The seco the idea is to find an ele
ity may be a difficult task. Therefore,
algorithm for estimating time complex rations arementary (or ba
and expr ess time com plex ity in tenn s of the number of times the basic ope perf o~ d
dominant) operator ..,,e . In
. 1 . 1s . the operation . count of an aIgon.t hm.
t rme comp
exrty ing time . The .d
ysis to illustrate the behaviour of the runn
The third usef ul way is to use asym ptoti c anal
com putin g the actual time complexity. This analysis
can be co~~~ of asyni
oxim ation s rathe r than
analysis is to use appr rnect Wi~
step count or basi c operation count. ner.
ation count methods in a detailed man
Let us now di scuss the step count and oper
Step Count · . .
instr ucti ons or step s that are used b perfonTI the given
The idea is to co unt the num ber
The first step is to fi nd steps per exec
of
ution and ireq ~ uency (or number ofy( a give ) f
n algo nthm to
ofs1e~
II Imes o stat eme nts. The number · ·
le sta teme n t d t t Th .
exec ution is O for non-executab s an gene ra Y I for exec utab le
s a eme nts. 1s num ber vane s onl)
· · ct b
a functron is invoked or composite stru ures are processed for .
exampl
. e, re
t (
urn a + + c) can be considered one
_ + h ·
0 n the othe r
hand , fo r instructions such as 'n' , or both are depende
step co unt· ,·sno t I I add-- ~ n'. w ere e~ther van able 'a' or vari able the s·
input instances , the • n a rtr on ' 1f a fu nct IOn ts ·
· mvo ked , then the step cou nt depends on
the function.
Some of the. spec ific ways of meas uring • step cou nts per exec ution are as follows:
. .. . . 1,:
arati ve state men ts with no initializa tions have a statement cou nt of 0. In case 1111t1ahzat10ns are made. ·
I. ~ecl
co unt 1s 1.
n/end/end if/ eo d whI!. e/end for all have a step count of O.
2. Comments and brac kets such as begi
//11\irs of Alwl1'ithm Analysis 49
~- I \!)1'l' S~1011C. h 1l\1.' ' l ~1" J) ,
1
· · • 1.:m111t 111' 1
-1 \:--~1g111111·111 Sl,lll'lH1.' tll ltlll ' I
. l h)tl111,n -•11 . II
h.11 c .1 :-tep c,111111 11 r 1. l, '"" · n.:t11111 ~1n1c 111c111s. nnd other slntc,ncnl !-l such as break/co ntinue/go lo a
4 = B + 1
fnd
C= A+ 2
D= A+ B
It can be observed that the total is calculated by multiplying the number of steps per execution by frequency. Therefore,
T(n) = 3. This means that the algorithm computes a fixed number of computations irrespective of the algorithm input size.
Therefore, the algorithm is a constant time algorithm. What is the implication of this algorithm analysis? Constant-time algo-
rithms are easier to compute, as they are independent of the input size. These algorithms are tractable or solvable. Therefore,
the complex ity of the preceding algorithm segment is simple and, therefore, the algorithm is easily computable.
Example 3.2 Consider the fo llowing algorithm segment that computes f I and estimate the run-time using step count.
1= !
Algorithm sum()
Begin
sum= 0.0
for i = 1 ton do
sum = sum+ 1
End fo r
Return sum
End
•. ,1,lc stut c111c11t s, 1111d hence steps per cxcc 111 ion .
I x ,,re 1Hlll· l'Xlll 111 · . . 1or lh
ln:-tll1l·111m~ 1• .. . • [l lH ' '
1 (1
s,1/uri,m I l'tion•il itcrnlion is required lor lhc control lo c C~c ~l
, t I , f , , l ·y p ( II 4 I ns [Ill 11(( I ' ., , , , ,, IIn1 c l <ll ,
,'\'r1' . :-.lll ll'llK'llt .. l!l~ !1 I\ qmt ( . I , ' . ·x nc;11tcd '11' times. 1 his IS shown Ill ruhl t: 11 1u1 ()I ~'r1
. l ,·
~tnti:nwnt ~ I~ " 11h111 I lC nr oop. 1c1 l I l'I ·
, · ·ltll'C t H.:y ,Il l,; C, i.; .. ' [L \
• •1~ 1, '
Table 3.3 Step count analysis •1
I2 I Begin 0 0
8 End 0 0 0
Total 2n + 3
Therefore, the step count of these instructions is n. The total count is obtained by multiplying the steps per e .
~
. 1unct10n. of thts
. aIgont. hm segment. Xecution1
frequency. It can be observed that T(n) = 2n + 3 is the time complexity
As evident from Examples 3.1 and 3.2, time complexity is not a single number but a function. More impo~
polynomials. It can be observed that th1s. 1s. a po Iynom1a. I wt'th an mpu
. t size
. n an d can be so Ive d east·1 y, as pol Y,tne
.
algorithms are easily solvable. Ynorn1a1
Advantages and disadvantages of step count Evaluation of time complexity using step count is easy How
. . · ever c
the major disadvantages of this method 1s that step count 1s not dependent on operands. In other words, the size of l~e
operand is not significant. For example, a = a + 10 and a = a + I0,000,000,000 have the same step count.
Operation Count
Operation count is another way of performing algorithm analysis. The idea is to count the number of operationsir
of steps for algorithm analysis. Operations can be divided into two categories: elementary (or basic) and non-elem,
(non-basic) operations.
Elementary (or basic) operations are primitive operations and are often implemented directly with less effort. The follr
are some of the commonly used basic operations:
I. Assignment operations
2. Comparison operations
3. Arithmetic operations
4. Logical operations
Non-elementary (or non-basic) operations are not atomic in nature and involve many elementary operations; e\J
incl ude sorting, and finding the maximum or minimum of an array.
Co~nting Lh_e number of elementary or basic operations is helpful in measuring the run-time of an algorithm. An1
operation on bits, characters, nodes, or integers can be a basic operation for most of the algorithms. Thus, the stepsr(,
for performing an operation count are as follows:
Sl'QU<'m'l':
End
Here, C is the condition. P and Q are the statements. It can be observed that the worst-case running time of the selection
structure is the maximum of the worst-case running time of its statements (refer to Table 3.5).
Structure
, c.;..,.,..., Ope~ation Count
for i = j to k do '
while ( C) do
T5 = Tpx n
p
repeat P
until C
'
Here, Tp is the worst case running time of operation P and n is the worst case iterations involved in looping. It can be
observed that it is the multiplication of n and Tp. This is called multiplication principle (refer to Table 3.6).
Thus, it can be observed that algorithm analysis is reduced to counting of steps or operations (refer to Box 3.3).
; .,,, ,/l\1 1 ,,f 1/i:1)//(/11111
I l, ,,, , ,,,,
th8
➔
1 "2 + 3 • • • + 200 - ~
Johann Fnednch Gauss is considered .
grdoAI·
ftenofreferre
d one to 200 + 199 + 198 + . . ~ 1 s
°
I est mathematicians of all ~,m.es an 1777 In -------
as the 'prince of mathemattcs . He was born In 201 +201 +201 + · · · +201 2S
Bru nswick, Germany. He is credited with carrying out
st In the sequence 2S, the number 201 is a
mathematical work that influenced many fields of udy.
He was a genius since his childhood. When Gauss was a 10-ye~r-old Therefore, s
= 200 x 20 1/2 and the sum is 80 ~ 8 aririg
school student his mathematics teacher Mr Buttner asked him to leads to a formula n(n+1 )/2. The book Dis ·. ?O. l"h· ~Or,
th . Th qu,s,,.1 ta1
compute the s~m of natural numbers from 1 to 100. He produced e (In vestigations m Number , , eory) authored by G %J4 ~ ~•,
result in a flash of a second, surprising his teacher. The formula for important role in the number theory. Gauss is aus8 Pr,,Iqrr,,
. k d. an all t· ii,,..
computing the sum of numbers is as follows: ematlcian, and h,s wor s are use m very d' • 'rtie , . i
. . d ivers Qr~,
astronomy, acoustics, optics, geo esy, magnetism e areasil-,,
,oo n(n+1 ) tics, and calculus. The bell-shaped curve (for 81 •61eC1ri . i.,_
1 + 2 + 3 + --- + 100 =I i= - - . How did he compute that? The · d' 'b 1· d Gaussian eliminanda. rd distrCij,/
l=O 2 hyper-geometric ,stn u ,on, an i'.
Gauss technique that is applied to a simple illustrative example of very useful for the study of algorithms. He died ~t,o~ rtlet~~-
counting 1 to 200 = 1 + 2 + ... + 200 is described as follows . Write the Gottingen in 1855, at the age of 77.
sequence (say S), reverse it, and add it to get 2$, in the following manner:
.
in his ho '>'.;
~.
Example 3.3 Consider the fo llowing algorithm segment and perform complex ity analysis using operati
on count·
Algorithm swap(a, b) ·
Begin
temp = a
a = b
b = temp
End
Solu~ion Table 3.7 .illustrates all the operations that are perfo rmed as part of an algorith m and is fi -
run-tune of the algonthm. . nally expressed,
As evident from Table 3.7 steps I ' 2' and 6 are aII non-executable t t
~
or t he other statements are as' fo llows: ' s a ements and hence the cost is zero. Operation er.
T(n) = c,+c2+c,0
This is a constant-time algorithm The d' .
machine-dependent. · prece mg relation shows that the exact cost estimation and the con5t1JJ~·
f/11.11r·v nlA /w1rilh111 ;1 110/y.~is 53
I2 Begin - 0 0 0
3 I for k = 1 to n do Assigning, testing, and C1 n+1 (n + 1)
'
X C1
increment
4 A=Ax k Perform multiplication C2 n n X Ci ,;
5 End for - -
6 End - - - 1,,
- - -~-- .. - - - -~~-- -"'- --= -
Simply, the total cost reduces to
for i = 1 ton do
for j = 1 ton do
A= B x C
End for
End for
'
Therefore, it can be infe rred that the run-tim ' 2
e 1s 11 .
Many rules of summ ation are often required for algorithm analys
is (refer to Box 3.4).
<, /1, ,,, ,, .,,,,/ 111, ,11 11 1 P/ 1/i:, 1111'11111
llfi1J Counting
Many times . algorithm analysis is reduced to counling of steps or Some of the additional useful rule
operat,ons s are a
II $ fo11()
The following are some of the common summations that are ~ ~ -
k, k = 1 + 2 + 3 + .. . + n == n(n + 1, ·
encountered in algorithm analysis: k= 1 ~
n
n- 1
L.1 = n-1
I k2 = 12 + 22 + 32 + · • . + n2 _ n(n + ,
1
,= 0 k= 1 - ~ ( 211 ~
n 6"" , 1)
n- 1 L k3 = 13 + 23 + 33 + ... + n3- n2(n + 1 .
I c=
k=O
en k=1 -~
4
More details are provided in Appendix A.
2
Example 3.6 Let us assume that the analysis of an algorithm yields t(n) as follows:
II
t(n) = L 6i(i + I )
i= I
What is the estimate of the total number of operations?
Solution The aforementioned summation is equi valent (refer to Box 3.3) to the fo llowing relation :
11 ../!-,
1(n) = I 6i2 + 2...,6i
i= l i= I
=6 x n (n + I )(2n + I) t-
6 x n (n + I)
6 2
= n(n + l )(2n + I ) + 3n (n + I )
= 2n 3 + 6n 2 + 4n
Therefore, the time complexity function t(n) of the given algorithm is 2n 3 + 6n 2 + 4n . The
· · Box 3 .5 . reasons of al or· h
are given 10 g it rn an·'
I
r lJ 'rPJ!" ?irf i 4ft
Example 3.8 Find the best, worst, and average case complexity of the linear search.
Solution Consider a simple problem of linear search for a list of
numbers given in Table 3.9. Table 3.9 Linear search
Formall y stated, a linear search problem is given an array of num- Index 1 2 3 4 5 "' 100
bers and a target key ; the problem is about checking the presence or
Elements 23 34 12 8 9 ... 100
absence of the target key. The worst-case complexity of linear search
occurs when the item is either present as the last item or not in the
list. For example, let us consider a scenario of searching for the number 170. To decide
whether this is the last element of an
array or to infer that it is not present in the array, n comparisons are required. Therefor
e, the worst-case complexity of this
algorithm can be given as T(n) = n.
Toe best case oflinear search occurs when the item is present as the first element of
the list. Say we are searching for an
element ' 1', which happens to be the first element. Therefore,
T(n) =n
The average-case complexity for a linear search problem can be calculated as follows
: The item can be present anywhere
in the list. Thus, the probability of the presence of the item in the given array is equally
likely [p = : } Hence, the item can
be present anywhere in the array. Therefore, the number of comparisons required is as
follows:
1 I I
T(n) = 1 . - + 2 · -+ 3 · - + · · · + n · -I = -(1
I
+ 2 + 3 + · · · + n)
n n n n n
_l. n (n + 1)_ B..±1_ "" ..!!_
-n 2 - 2 2
Thus, the average-case complexity of this algorithm is also of the order of its input size
n.
. J
l.').Hft.p\tt~ 1·111 IIW lw~1, ".1,r·t nml :1, crn11c
s ·· ' 0
case rn111plcxi1y of bubble sort.
S1tfuthm l 1'1 \\~ .,~~umc \he array a~ ~hown in Tabk 3. 10. . Table 3_ 10
. .l , •s when the lisl is already 111 the soned order as
1he bc~t case l,\ m111 ' ' l'. so 11 t, Index
,r 12. 2.1. J4} . D1ws it help') No. The algorithm for bubble sort ts. presented Ill. Chapter 7
Elements
· ·
\,1 th is ho\,k. Bubble sort still · ~ · parisons So the complex ity of the
n:qmres com · .
2
. . , ' d
r1 1g,,n 11 1m 1s 11 , ennte
asd O( 2))
11 . But tlie good thing is si nce the array ts already sorted,
~
Example 3.10 Consider three algorithm s A, B, and C. Their respective run-time complex
ity is given as follows: r1=·
T8 = 7 log 10 n, and Tc = n2
Compare the rates of growth when the input is scaled from 11 = J00 to n = 10,000.
Solution Initially, the number of instances is JOO. Later it is increased (or scaled)
ton = 10,000. For the first algorilhrr.
whose complexity is given as 7n, the rate of growth is as fo llows:
Simihrl~ · thr rnt c of gro,, th ti.-ir lllher alt::ori thms cnn br obtained ns fol lows:
500 - f(n) = n
g(n) = logi(n)
h(n) = n2
400 - k(n ) = 2"
~
·x(l)
t0
(.)
300 -
(l)
E
~
200 -
100 -
-· - -·
0-
I I I I I I I I
2 3 4 5 6 7 8 9
Values of n
Example 3.11 Let A and B be two algorithms with the following complexity functions : l.4 - n2, ta - 40n + 1200.
Wh ich algorithm is better?
Solution The starting point of this problem, is to find an instance of n where l.4 =ta.This instance can be obtained as fo llows:
n2 = 40n + 1200
:;,;ji}F.
~1,c-" ,m,I 4,,,,/, 111 ,1/ ~lg,1111lm1,
~n) = n*n
g(n) = 40n + 1200
4000
~ 3000
·xQ)
C.
E
8 2000
Q)
E
~
1000
0 10 20 30 40 50 60 70
Values of n
Fig. 3.4 Plot of functions
/111 </I In/ f /~()// //r/11 fl llOfl'l'II ~9
. . two algorithms:
Let there be the tollow 1ng
Example 3.1 2 J
. t. 1e + 11
1. r\lgorithtn A with runn,_ng ,_n f - 11-
, A.12.orithm B with runnmg time 111 -
"= ,,2
-. . - k for 11 == t 00. How long w ill it take for n
. . takes one second to compute a tas
Assume that each ope1ation
ti t algorithm can be compute d by retaming ont
. :::: IOoo~
. Each operarion takes one secon d. Time for the ,rs 2 . . d T.
SoI 1111011 . I 112 + n only n is retame . ,me computatio Y th
n t e dor
degree of the polynomial. Therefore, 111 ti,e given polynomia
. . , or al
is as follows: go~
2
1000 + lO00::::: 99.1
TimeA = 1 x 1002+ 100
. ti
Similarly, time computatio 'thm B is as follows:
n or a1gon
10002 - 100
Time 8 = I X 100 2-
. . .fti ' --....
It can be observed that no s1gmficant d1 erence ex1·sts between TimeA and Time 8 (here, the d1ffere nce is 100 _ 991
Thus , in asymptotic analysis, constants can be ignor .~
ed.
3.6.1 Asymptotic Notations
• · I .
Asym ptotic notations help to c asst'fy aIgon·thms and specify the upper and lower bounds of algorithms.
Example 3.17 Prove that 210 is 0(1) (Refer to section 3.6.2 for limits).
Solution For
lim f(n) = c
g(n)
n---,~
i10
lim- = limi 0 = i 0
n ➔ oo } n ➔ oo
Example 3.18 Consider an algorithm that is assumed to run in time O(n 2) and that takes only five seconds to compute results
fo r an instance of size 30. How long will the algorithm take to compute the results if the instance size is increased to 50?
Solution Recollect that O(n 2) implies that t(n) $ c x g(n), that is, t(n) 5 c x n2. Here, c is a constant that depends
on the
machine. As mentioned in the problem, the algorithm takes five seconds to compute 30 instances. Therefore, one can calculate
the values of c as follows:
cn2 steps = 5 seconds
c(30)2 = 900c steps executed in 5 seconds.
5-
. c=-
.. 900
I · ' of 41\!0111/1111 1
f.2 Jlr,ign mi, 1411•1 ' ' '· • •
sily be calcul ated. If the algorithrn i
. . , II other instances can ca
0nl'.c the' n\uc s of c arc dec1'dd lltn
c., e lor,1 be calculated as iO
"II 'Is· nstan1: .
. rogram can ov . ~ 1~-
10 50. then the tnnc 1akcn by t1 ,e P ,\
c(S0)2 = 250 0c
Example 3.24 Suppose that an algorithm takes eight seconds to run on an input size n= 12. Estimate the instances that can
be processed in 56 seconds. Assume that the algorithm complexity is O(n).
Solution Assume that the time complexity is O(n), then cn 8seconds. Here, the instance nis given as 12. Therefore,
12c = 8; hence, c = 8/12 = 2/3.
The problem is to determine the value of nthat can be processed in 56 seconds. This implies that c Xn= 56. The value of
c has already been determined as 2/3. Therefore, 2/3 Xn = 56. This implies that n = 56 x 3/2 = 84.
Hence, the maximum input that is possible is 84.
o-nototion (Little-oh Notation) Table 3.13 Differences between
The httleoh notion is used very rarely. 0-notation can be used
instead of the (-notation, as the o-notation represents a loose
O-notatlon O- nota
0-notation
ion
Tight bound
ound The o-notationcan be detined as follows.
letrandgewotunetions that map aset of natural numbers
0-\o0senotatiOofnnon-thelpsh re
For some values of c, the following
0 a sct ofpositive real numbers, that is, t: N’R. Let olg) be For all
Condilionvalues
condition should be satisfied
the set ofall functions with a similar rate of growth. The relation
r)= Og)) holds good, if for all c given , such that tn) s cg(n)
tn) <cxgln)
nn)cxn). situations where tight bounds In
The ditferences betwveen O-notation and o-notation are given
in the following Table 3.13.
need to be used, the O-notation is
prefered. s-notitautaiotionneednsis ploWhrefeeberreeAuBt.
hounds
w-notation (Little-omega Notation)
The formal definition of the little-omega notation is as follows:
Let r and g be two functions that map a set of natural numbers to a set of positive real numbers.
be the set of all functions with asimilar rate of growth. The relation (n) = o(g(n) holds, if
there exist
that is, t: N-
cand n, such that r(n) >cXgn). two
The differences between l-notation and w-notation are similar to the
differences between positve o
0-notation is aasymptoticallyloose bound andthe condition should be satisfied for all values ofc.
are very useful when loose bounds are needed. The O-o-nnototataiotinon andand o-
on
Tilde Notation
This notation is useful when the functions (n) and g(n) grow at
the same rate. For example, if f)
growth, then one can write that r(n) ~gln). and gn) have tie
The six popular asymptotic notations are summarized in Table 3.14.
Table 3.14 Asymptotic notations
Name of the How it is
S. no. notation What it means represented Mathematically
equivalent to
1 O-notation Growth of fp) sGrowth of gin) tn)= Olgln)
2: Q-notation Growth of fn) 2Growth of g(n) tn) =2(gn)
3 O-notation Growth of fp) =Growth of gln) tn) = (g(n) - (Roughly equal to)
4 O-notation Growth of fp) << Growth of gln) t(n) = olgn)
5 0-notation Growth of fn) >> Growth of gln) tn) =o(fn)
6 Growth of fn) is same as g(n) t(n) =gln)
O-nototion
The detinion of the 0-n0tationcan also be piven in
ferms of limits as follows:
Ii the lmt lm
*holds good, then ro) =Oe) This is called the big-Oh ratio theorem. As n ’, the
ratio will
.2+2) =0(2")
Q-notation
If the limit lim: r(n) #0 holds good, then ((n) = 2(g(n). This is
g(n) also called g(n) the 2 ratio theorem.
Example 3.27 Let us consider that r(n) = 7n +4. Prove that this is of the order of 2(n).
Solution Here t(n) = 7n +4 and g(n) =n.
Itcan be proved using the omega ratio theorem as follows:
4
7+
7n+4
lim
n’o
= lim
1 n-1+0
Therefore, one can conclude that t(n) = ln)
O-nototion
definition is as follow...
The notation can be also defined in tems of limits. The formal
fn)
lf the limit ln =C, cis positive, then I(n)= O(gn))
o-notation
The o-notation can also be expressed in terms of limits as follows:
The function r(n)= o(gn)) if lim 0. which implies that t(n) = o(g(n)).
g(n)
w-notation
:. 4" = o(4")
n! = o(2")
L'Hospital rule
This rule can be used if the functions n) and g(n) have derivatives.
lim = lim
gn) g'(n)
If lim f(n) =oand lim g(n) =oo, thatis, both converge to zero, this rule can be used.
n-o n’0
Thus, the ratio of two complexity functions is the same as that of its derivatives.
1
lim = lim -=0
n’oo
2n 2n
.:. Inne O(n')
extractingnew
notationsand information
3.6.3 AsymptoticRules
usetultor
asymptotic
manipulating d1scussedinthefollowing
are
subsections.
about he beh
Asnow nesare mportantrules
of he other
tunctons Some Notations
PropertiesofAsymptoric
properties of
O-notation:
giventwo ploynomials, the
Essentiol
Onls
ae
the
some ofthe
onlythat
In
degree of
other
Ihe tollo ng dominatingsummand matters. thepolynomial
words,the
observed
matters.
that allterms
other than the
highestpolynoma i
I. fasterand
degreegrows On'+n'+64) = Ou').Itcan
Forevample,
be
the polynomial matters.
dcgre ate,
onlythe largestdegree of
Proof follows:
generalizetheresultas
m. Then one canshow that (n) = O(n")
Onecan degreeis
be r(n) = ) an' whose
polynomial
Letthe
givenas follows:
Theproofis +laln+lao
lam-n"+...
n) s la,ln" + for n21
lamn+...+ la,ln"+la n
sla,l n" +
=cXn"= On")
9, Reflexivity Rule
For any general complexity function (), the reflexive property is given as follows:
r(n)= Ogn))
T(n)= S2(g))
I(n)= O(gn)
10. Transitivity Rule
The transitiveproperty is defined as follows: if )=
and gn) are complexity functions of two algorithms. This O(øn) and gz) = 0(h), then (n) = 0(h(n)). Here I(n), h(n).
property holds good for other notations also.
Some of the applications of these rules are
illustrated through the following numerical examples:
Example 3.36 Prove that 16 lg n! = O(n lg n)
Solution First Ig n! can be solved as follows:
lg n! = lg n + lg (n 1) + g(n-2)
+... + lg 1
< lg n + lg n + lg n +... + lg n
=n lg n
16 lg n! = 0(1) · O(n lg n) 16 = 0(1)and
Og n!)= n lg n.
= 0(1 · n lg n) (using the law of
=0(n lg n) multiplication)
Example 3.37 Prove that (n) =8n'+7n+9n+8log n is O(n).
Solutin One can use additive rule as follows:
Example 3.38 Let (n) = (3n' + 2n + 2)lgn. What is the final complexity?
Solution (n)= ((3n' + 2n' + 2))x lg n
= On) x Og n) = 0(n' lg n)