UNIT-1
UNIT-1
Syllabus
Introduction
• Introduction to data structures; Introduction
to Algorithms Complexity
Data Structure
• A data structure is a storage that is used to store
and organize data.
• It is a way of arranging data on a computer so
that it can be accessed and updated efficiently.
• A data structure is not only used for organizing
the data. It is also used for processing, retrieving,
and storing data.
• It is a set of algorithms that we can use in any
programming language to structure the data in
the memory.
Types of Data Structures
18
Algorithmic complexity
• Algorithmic complexity is a measure of how long an
algorithm would take to complete given an input of
size n.
• If an algorithm has to scale, it should compute the
result within a finite and practical time bound even for
large values of n. For this reason, complexity is
calculated asymptotically as n approaches infinity.
• Complexity is usually computed in terms of time and
space requirements.
• Analysis of an algorithm's complexity is helpful when
comparing algorithms or seeking improvements.
How to analyze an Algorithm?
• Fixed Part: This refers to the space that is definitely required by the
algorithm. For example, input variables, output variables, program
size, etc.
• Variable Part: This refers to the space that can be different based
on the implementation of the algorithm. For example, temporary
variables, dynamic memory allocation, recursion stack space, etc.
Therefore Space complexity S(P) of any algorithm P is S(P) = C +
SP(I), where C is the fixed part and S(I) is the variable part of the
algorithm, which depends on instance characteristic I.
How to calculate Time Complexity?
•
The time complexity of an algorithm is also calculated by
determining the following 2 components:
• Constant time part: Any instruction that is executed just
once comes in this part. For example, input, output, if-else,
switch, arithmetic operations etc.
• Variable Time Part: Any instruction that is executed more
than once, say n times, comes in this part. For example,
loops, recursion, etc.
Therefore Time complexity of any algorithm P is T(P) = C +
TP(I), where C is the constant time part and TP(I) is the
variable part of the algorithm, which depends on the
instance characteristic I.
Real-world examples of various
algorithmic complexities?
• Suppose you're looking for a specific item in a long unsorted list, you'll
probably compare with each item. Search time is proportional to the list
size. Here complexity is said to be linear.
• On the other hand, if you search for a word in a dictionary, the search will
be faster because the words are in sorted order, you know the order and
can quickly decide if you need to turn to earlier pages or later pages. This
is an example of logarithmic complexity.
• If you're asked to pick out the first word in a dictionary, this operation is
of constant time complexity, regardless of number of words in the
dictionary. Likewise, joining the end of a queue in a bank is of constant
complexity regardless of how long the queue is.
• Suppose you are given an unsorted list and asked to find all duplicates,
then the complexity becomes quadratic. Checking for duplicates for one
item is of linear complexity. If we do this for all items, complexity becomes
quadratic. Similarly, if all people in a room are asked to shake hands with
every other person, the complexity is quadratic
What does it mean to state best-case, worst-case and
average time complexity of algorithms?
Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n >
n0. }
Big-Omega Notation (Ω-Notation):
Let g and f be the function from the set of natural numbers to itself. The
function f is said to be Ω(g), if there is a constant c > 0 and a natural
number n0 such that c*g(n) ≤ f(n) for all n ≥ n0
Mathematical Representation of
Omega notation :
• Ω(g(n)) = { f(n): there exist positive constants c
and n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }
• Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such
that g(n) ≤ c.f(n) for all n > n0. }
Theta Notation (Θ-Notation):
• https://www.geeksforgeeks.org/analysis-of-algorithems-little-o-and-little-omega-
notations/
Common Asymptotic Notations
Example