0% found this document useful (0 votes)
11 views5 pages

Time Complexity

Uploaded by

2023ugcs040
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Time Complexity

Uploaded by

2023ugcs040
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

3.

3 ASYMPTOTIC NOTATION

Asymptoic notatin is the most simple and casiestway of describing the running time of an algorith
lt represents the efliciency and performance of an algorithm in asystematic and meaningful mannegy
Asymptotic notations describe time complexity in terms of three common measures, bestcase (or 'fastes
possible), worst case (or 'slowest possible'), and average case (or 'average time').
The three most important asymptotic notations are:
1. Big-Oh notation
2. Omega notation
3. Theta notation

3.3.1 Big-Oh Notation


The big-oh notation is a method that is used to express the upper bound of the running time of an
algorithm. It is denoted by O'. Using this notation, we can compute the maximum possible amount of
time that an algorithm will take for its completion.

Definition Considerfn) and g(n) to be two positive functions ofn, where n is the size of the input data.
Then, f(n) is big-oh of g(n), if and only if there exists a positive constant Cand an integer n, such that
fn) s Cgn) and n > no
Here.f(n) -Og(m)
Figure 3.2 shows the graphical representation of big-oh notation.
Cgln)

{n)

fAn) =0(gln))
Fig. 3.2 Graphical representation of big-ohnotation
big-oh notation are:
Some of the typical complexities (computing time) represented by
1. O(1) ’ Constant
2. O(n)’ Linear
3. On) ’Quadratic

70 Data Structures Using C


4 On') (ubic
Ixponcnial
<(2") -+ Loganthmic
6 Ologn) -’
Example3.4 Derivethe big-oh notaton, if n) Sn 7aid gin)n
integet n schthat
Solution To show fn) is O(gn), we mmust consder stive constants (C and
allnn
nfor all >n
Let C 15,
New,
wemustshow that 8n +7s 15n.
or7sn
orlsn
Theretore, n) =8S +7s 1Sn for all n>l, where C 15 and n 1.
Hence. n)=0g).

Example3.5 Derive the big-oh notation, if fn) =2n +2 and gín)


Solution Given. fn) =2n +2 and gn) = n
For n=1,
in) =2(1)+2
=4
gn)=(1F
=1
ie.fn) >gn)
For n=2,
fin) =2(2) +2
=6
gn)=(2)?
=4
ie.fin) >gn)
For n=3,
An) =2(3) + 2
=8
gn) =(3)
=9
ie.,fn) <gn)
Therefore,fn) < Cgln) is true if n >2.

3.3.2 Omega Notation


algorithm.
a method that is used to express the lower bound of the running time of an
Ihe omega notation is of time
is denoted by '2'. Using this notation, you can compute the minimum amount
Umega notation
that an algorithm will take for its completion.
Introduction to Algorithm and Data Structures 71
Definition Consider fn) and go)to betwo positive functions of n, where nis the size offthe input
Then, fon) is omega of gon), if and only if there exists a positive constant Cand an integer n, such data
An)gn)and n>n, thva
Here, fn) = 2(g)
Figure 3.3shows the graphical
representationof omega notation.
f(n)

Cg(n)

n
no

f(n) =o (g(n)
Fig. 3.3 Graphical representation of
0mega notation
Example 3.6 Deduce the omega notation iffn) = 2n² + 4 and g(n) = 6n.
Given, fn) = 2n + 4 and g(n) = 6n.
For n = 0,
fn) = 2(0 +4
=4
gn) = 6(0)
=0
i.e.,fn) >gn)
For n = 1,
An) = 2(1) +4
=2+4
=6
gn) = 6(1)
=6
i.e.,fn) =gn)
For n=2,
An)= 2(2) +4

72 Data Structures Using C


C h P
loweraverage inputthat, 73
the the such Structures
the
between of
compute sizeC,
and Data
algorithm the
is C and
can constants
nwhere Algorithm
2n.
Example we
and
= an notation,
g(n) of n, positive to
time of Introduction
functions
running
this
6 two
+ Using
2n exists
positive
= the
fn)
if n>1. "0.completion.there
express
can
2. by two
> notation
to denoted be only
ifn for if
Cgon), Cg(n), used to
3.7
omega its g(n) and
2n. is is for
fn)
> > thatnotation andif
= f(n) Notation
Theta
3.3.3 amethod take gn),
the gn) will fn)
that Deduce
and that Theta Considerof
algorithmtheta
8+4 Rn)=6(2)
=12=g)
ic.Au)
+4
=2/3)°
An)
+4
18= 6(3)
=gn) >gn)
say
ie.fn)
+6
2n
6
+
2(0) 2(0)
=g(n) >gln)
ie,fn)
+6
=2(1)
An) =2+6
say
can is bounds.
notation is s
Czg(n)
O(g(n).
12 =3,nFor 22= 18= we Therefore, = n
fn) =0,
For
2(1)
=
g(n) gln)
>
ie.fon)
we
An)sfn)
an Definition
Then, =
fin) =6 =0 1=n,
= =8 =2 Therefore, thetaupperthat gln) fn)
Giyen, For data.C; Here,
The and time
Figure 3.4 shows the graphical representation of theta notation.
C-gn)

Crgln)

fn) = (g(n)
Fig. 3.4 Graphical representation of Theta notation

Example 3.8 Deduce the theta notation iffn) =2n +8.


Let fn) = 2n +8> 5n where n>2
Similarly, fn) =2n +8>6n where n>2
and fn) = 2n + 8<7n where n>2
Thus, 5n < 2n +8<7n, for n>2,
Here C=5 and C, =7
Hence, fin) - 2n + 8= (n)

3.4 INTRODUCTION TO DATASTRUCTURES


representation of data along with its associated
In simple terms, data structure can be defined as a
that it can be used
operations. It is the way of organizing and storing data in a computer system so under one name.
elements stored
efficiently. This organization can be in the form of a group of data
data strúcture.
Here, the data elements are referred as members of the
different types and lengths. Some
Depending on the type of data structures, the members can be of trees, stacks,etc. Algorithms are
of the examples of data structures include arrays,
linked lists,binary
of different ways, Iike sorting the data elements or
used to manipulate the data structures in a number
searching a particular data item. structure is associated with the definition of the
The design and implementation of a typical data
specification of these data structure operations
operations that can be performed on the data structure. The
is done with the help of algorithms.

74 Data Structures Using C

You might also like