14 Data Structures using Python
clear that memory spaces ean never be ‘log n’. Even For a matrix, the space is", respective ofthe
dimensions of the function, the total elements are considered a ‘n’
bac
‘When two cost equations have different components with different powers, how can they be compare
for choosing the optimal solution?
2.3 ASYMPTOTIC NOTATIONS
The equations, discussed in Sections 2.2.1 and 2.2.2, formed in computing the running steps forte
complexity and the memory spaces used in space complexity are treated as a mathematical model, Foy
easy comparison and evaluation of the same, asymptotic notations are used. The need of asymptotic
notations can be understood well from the below scenario.
Consider a function with running steps “2n+7". Now, say the same functionality can be achieved
by another function with running steps ‘n’*, So, which implementation should be used in a realtime
application? This requires comparison of running time ofboth the functions, When ‘n’is ‘3°, function! wil
result in 13" steps and function2 will result in 9” steps. For all ‘n’ lesser than or equal to °3°, function?
will perform better than function, However, when ‘n’ crosses 3, functionl starts performing better over
function2, which means the number of running steps will be optimally less in case of function. Thus, in
real-time, the comparison becomes dificult while handling those mathematical models due to fluctuating
values. Asymptotic notations are used to represent the worst case and best case ofthe functions and many
such metrics aiding in comparison. -
The above scenarios have made the necessity of an asymptotic notation clear, however, the question
is, ‘what does asymptotic mean?”. Asymptotic means approaching a curve ora point as close as possible.
‘Therefore, when representing a function in its asymptotic notation, itis very important to keep in mind
that the notating function should be arbitrarily close enough to the original function,
eee
Big-O is represented as ‘O( ) This is the widely used asymptotic notation, which signifies the worst
case running time of a function, Formally, the Big-O
function is defined as a limiting function. It aims at cow
setting an upper limit for a function with another
function. A function, ‘ftx)"="O(e (x))as x20", when
the absolute value of‘) is lesser than or equal to
4 multiple of ‘C’ and “g(x) forall “x preater than or
equal toa threshold ‘x,’ and any constant °C”. This is
mathematically represented as:
2.3.1 Big-O
Running time
[fey] SC * g@), 2x, QA)
For visualizing “g(x)’ for any ‘f(x)', the graph in
Figure 2.1 can be referred.
tn the function, it can be clearly seen that ‘g(x)*
is always greater than f(x)’, after the threshold —Taput sie *_,
“x,'. For any function, there can be any number of ahi
“gGx)' forming an upper border but the core task in
Figure 2.1. Big-0 function representation
—EeEE adAbstract Data Type and Analysis 35
identifying the apt function is to ensure that ‘g(x)"is as close as possible to the base function ‘f(x)’ and
to maintain °x,” a8 minimum as possible
For identifying this easily in asymptotic notations, the first step is to identify the maximum impact
creating component in the equation of running steps/memory spaces. Say, in the function, “(2¥n) + (w'/2)
+ (n/2)’, the component with the highest impact is ‘(n’/2)’, This term can further be reduced, that is,
when *n'is high, ‘n** will also be very high. When the constant ‘2’ is compared with a very high ‘n”, "2°
becomes negligible. Thus, any type of independent constant in the term can be removed. The remaining
part in the component is only ‘mn. Thus, the given function is “O(n?)’. Simply, the Big-O is the largest
component in the function with all constants removed.
With this explanation, let us ty to identify the Big-O of 3*(logn)+7n’. Mathematically, “logn’ is always
lesser than ‘n’. So, the function is “O(n)’. Similarly, itis important to remember the fact that ‘nlogn’ is
lesser than ‘n’”, only because “logn*is lesser than ‘n’.
2.3.2 Big-Omega
Big-Omega is represented as “Q2()’. While *O()’ marks the upper bound for a function, ‘9 ( )° marks
the lower bound. A function, ‘/(x)’ = ‘@ (g(x))' as ‘x—>=0", when the absolute value of ‘/(x)" is greater
than or equal to a multiple of *C” and ‘g(x)’ for all ‘x’ greater than or equal to a threshold “x,” and any
constant °C’. This is mathematically represented as:
[fey] C* gC), 22%, 22)
For visualizing ‘g(x)" for any ‘ftx)’, the graph
in Figure 2.2 can be referred.
It is clearly seen in the graph that *g(x)"
is always lesser than ‘f(x’ after a threshold.
Say ‘f(xy’ is “x? + 5*x’, then “g(x can be any
function which is always lesser than or equal
to the ‘f(x)’. Even ‘f(x) = Q(g(1))"is right, as
*#cey’ can never be lesser than ‘1" at any point.
Even though by definition it is right, such a
representation will always be misleading and
hence this notation is rarely used. Even after
these shortcomings, if ‘2 notation is to be
used, itis to be maintained as close as possible
to ‘flay’. 6
sie identifies the element with the highest Meant bed pelle tnrellahine al
impact, however, Big-Omega targets towards
identifying the component with the least impact, Afler identification of the component, any constant
factor must be removed. Now, the component can be represented as Big-Omega. For ‘f(x)"in the above
example, the ideal “A” should be ‘OGY’.
cow
Running time.
xo ‘Input size x
2.3.3 Big-Theta
sd the upper bound and Big-Omega marked the lower bound,
Big- Theta is the average case. Big-O marke
but there was a problem in both the notations. Big-O ean be any high function and Big-Omega can be
any low function, but these are not mandatory the optimal functions, The optimal Big-O must be the
Closest upper bounding function and Big-Omega must be the closest lower bounding function. To ensure
the optimal function is used, Big-Theta can be considered. In Big-Theta, a function ‘g(x)'is selected to
be both the upper bound and lower bound of f(x)’. A function, ‘f(xy’ = *O(g(x))' as: ‘x—s00", when the[a
16 Data Srcaeswsing Python
hole value of 0) is within the range of °C)" and *C,e() for all x” greater than
aoe ee aay comnts C,and"C,. Thais matemaicaly epesened ag, ly
C8005 VOI SC, *ab0)x2 4,
2,
For visualizing “g(x)" for any f(x)", the
ny inFigue 23 an beefed
Tr BigeThea, the same function is
niipied by two ilferent comstans,
Tne ma the upper a lawer bounds
onepodingy based on the vale of the
Srna Tas aan C0 optimally
scpesen and mark the wpper and lower
Soin
Forth same. + 5," for
fm Homo an he Tower
voand 0) Foran atl
Select dn’ S60 hea Sem
inal Thus; he conan or lwet
Toad aed" For upe bond,
ined a 2 wiltealvay rer
thn Se, when pe
Fee Sere 2"e, forxes
‘Which ean be notated,
Foe +54) = 06%), forx2 S
eee _
fray on. ntti hy m0 wed wie? Fr eran co =
analyse ‘How the program will behave in the w "an What ‘beth soe Be
24 smali-o
Sl. ia sce ver
Biro hc geen a BO Sal. ako mas he per bound of gen finn ie
Hoke namie age gee wine sale
Eee “0 forall
Can nang) Real pear an req to eo
UE01 C* £60). 2%
Asthere has already been suficent expt
‘of Big-Omega and the difference betwee!
hd “Small” notations, i i left for the
further explore the ‘Small-Omega”notatic
2.4 RECURSION
Recursion is a type of funetion which ¢
feeursion means 2 funtion callingitsel,»
{ind that in tur, calls the prior function. 7
‘Of recusions canbe seen in the graphical
Recursion can be seen 38 a relasem
functions ean be re-written to work wit
‘Support for passing values from one sta!
eres is given in Python code 2Abstract Data Type and Analysis 17
In the above graph, *f(x) = O(g(x)). It ean be con
seen at a point when “x = k°, *f(x) = g(x)’. In spite
ofthe functions meeting ata point, Big-O definition
is still fine with “g(x)' marking the upper bound for
*f(x)', whereas Small-O definition does not allow
g(x)" to mark the upper bound for ‘f(x)’, as they
toth are equal for a particular ‘x’ greater than ‘x,
‘The Small-O along with Big-O and f(x) are shared Pye [iki
in the graph shown in Figure 2.5. ;
Just to make the function move away from ‘/ix)” ere a
at the point ‘x = k’, the constant that is multiplied
with *g(9), is increased. So, it is clearly evident that
“flx) = O(g(x))= 0(g(x))' for two constants °C, "and “
correspondingly, such that °C, >C,’
=
Running te
2.3.5 Small-Omega
c,909
‘Similar to Small-O, Small-Omega isa stricter version eau
of Big-Omega. A function ‘f(x) = o(g(x))"as ‘x2,
‘when the absolute value of ‘/(x)" is greater than a ae
multiple of ‘C” and *g(x)' for all x" greater than or
‘equal to a threshold ‘x,’ and any constant *C”. This
is mathematically represented as:
[flgl> C* g(0),x2%, 25)
As there has already been sufficient explanation ae
‘of Big-Omega and the difference between “Big.” a ‘rete
and ‘Small-’ notations, itis left for the users to are
further explore the *Small-Omega’ notation,
Figure 2.5 Big-0 and Small-0 fora function
2.4 RECURSION
Recursion is a type of function which cals itself. Recursion can either be direct or indirect. Direct
recursion means a function calling itself, whereas in indirect recursion a function calls another function,
tind that in tum, cals the prior function. This can be seen asa eycle of function calls. Both these types
“of recursions can be seen in the graphical cycle representation in Figure 2.6.
Recursion can be seen as a replacement of loops in some regular programs. Ideally, all recursive
functions can be re-written to work with iterations, but some might require additional data structure
suppor for passing valucs from one state to another. A simple recursion to iteration program for Fbonacet
series is given in Python code 2.1