0% found this document useful (0 votes)
2 views

DSA_Chapter_1_Introduction_to_Data_Structures_and_Algorithms

The document introduces data structures and algorithms, focusing on data abstraction, abstract data types (ADTs), and the classification of data structures. It explains the importance of abstraction in simplifying programming and reducing errors, while also detailing the properties and analysis of algorithms, including computational and asymptotic complexity. The document emphasizes the significance of choosing appropriate data structures for efficient algorithm design and problem-solving.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DSA_Chapter_1_Introduction_to_Data_Structures_and_Algorithms

The document introduces data structures and algorithms, focusing on data abstraction, abstract data types (ADTs), and the classification of data structures. It explains the importance of abstraction in simplifying programming and reducing errors, while also detailing the properties and analysis of algorithms, including computational and asymptotic complexity. The document emphasizes the significance of choosing appropriate data structures for efficient algorithm design and problem-solving.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Data

Structures
and
Algorithms
CHAPTER ONE:
INTRODUCTION TO DATA
STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHM BY: KIBRU G.


Outline
▪Introduction to Data Structures
▪ Data Abstraction
▪ Abstract Data Types
▪ What is Data Structure?
▪ Classification of Data Structures

▪Algorithms Analysis
▪ Properties of an Algorithm
▪ Complexity Analysis
▪ Computational Complexity
▪ Asymptotic Complexity
▪Algorithm Analysis Categories
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 2
Introduction to Data Structure
▪ Data Abstraction:
▪Abstraction is the process of hiding complex implementation details
(how data is stored and manipulated) and exposing only the essential
features or behavior of a system.
▪It allows programmers to work with high-level concepts without
worrying about low-level details.
▪It allows users to interact with a system at a higher level without
worrying about how it works internally.
▪Focus on what something does, not how it does it.
▪ Example: Driving a car
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 3
Introduction to Data Structure
▪ Data Abstraction:
▪ Example: Driving a car:
▪ Abstraction: When you drive a car, you interact with the steering wheel,
accelerator pedal, brake pedal, and gear shift. You don't need to know the
intricate details of how the engine works, how the transmission shifts gears, or
how the brakes apply pressure to the wheels.
▪ Hidden Complexity: Under the hood, there's a complex system of engine parts,
fuel injectors, electrical components, and more. These details are hidden from
you, the driver.
▪ Essential Information: You only need to know the essential controls (steering,
gas, brake) to operate the car effectively.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 4
Introduction to Data Structure
▪ Data Abstraction:
Programming Example: When you use an int in C++, you don’t need to know how it’s stored in
memory .You only need to know how to perform operations like addition, subtraction, etc.

▪ Integers are a common data type used in programming, but their physical representation in
memory can vary across different systems.

▪ One computer might represent integers using binary-coded decimal. Another might
use sign-and-magnitude binary. Yet another might use one's complement or two’s
complement notation.

▪ Despite these differences, programmers can use integers without needing to


understand their underlying representation.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 5
Introduction to Data Structure
▪ Data Abstraction: Why Data Abstraction Matters?
▪Simplifies Programming: By abstracting away implementation details,
programmers can focus on solving problems rather than worrying about
how data is stored or manipulated at a low level.
▪Reduces Errors: By focusing on what's important, you're less likely to make
mistakes.
▪Portability: Code written using abstract data types (like integers) can run
on different systems without modification, even if the underlying
representation of the data differs.
▪Encapsulation: Abstraction helps encapsulate complexity, making programs
easier to understand, maintain, and extend.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 6
Introduction to Data Structure
class Integer {
▪ Data Abstraction
private:
int value; // Hidden implementation detail
public:
Integer(int v) : value(v) {} // Constructor
Examples in C++:
int getValue() const { return value; } // Public interface
void setValue(int v) { value = v; } // Public interface
};

▪Here, the internal representation of the integer (value) is hidden


from the user. The user interacts with the integer through public
methods (getValue and setValue), which provide a level of
abstraction.Data Structures and Algorithms
3/27/2025 Chapter One: Introduction to DS By: Kibru G. 7
Introduction to Data Structure
▪ Abstract Data Type (ADT):
▪ADTs provide a logical/conceptual description of how data is
organized and the operations that can be performed on that data.,
separate from their physical implementation in a specific
programming language.
▪It focuses on what the data represents and what you can do with it.
▪Think of it as a blueprint or a specification. It defines the rules and
behavior of a data structure, but it doesn't tell you the exact code to
use.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 8
Introduction to Data Structure
▪ Abstract Data Type (ADT):
▪ Abstract Data Type (ADT) components are:
▪Data: The set of all possible values (the domain) of an
encapsulated data object, (i e.g., ntegers, strings, objects) and
▪Operations: The specifications of the operations that can be
performed on the data. (e.g., add, remove, search).
▪An ADT provides a clear separation between what the data type does
(its behavior) and how it is implemented.
▪Real-World Example: A Queue (Like a Line at a Store)
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 9
Introduction to Data Structure
▪ Abstract Data Type (ADT):
▪Real-World Example: A Queue (Like a Line at a Store)
▪Concept: A queue is a data structure where items are added to the back and removed from the
front (First-In, First-Out or FIFO). Think of a line at a store or a waiting list.
▪ADT Specification:
▪Data: A collection of items (e.g., people, tasks, messages).
▪Operations:
▪enqueue(item): Adds an item to the back of the queue.
▪dequeue(): Removes and returns the item at the front of the queue.
▪peek(): Returns the item at the front of the queue without removing it
▪isEmpty(): Checks if the queue is empty.
▪ Implementation (Hidden): The queue could be implemented using an array, a linked list, or other
data structures. The user of the ADT doesn't need to know how it's implemented.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 10
Introduction to Data Structure
▪ Abstract Data Type (ADT):
▪ Examples:
▪Stack ADT:
▪ Values: A collection of elements.
▪ Operations: push (add an element), pop (remove the top
element), isEmpty (check if the stack is empty).
▪Queue ADT:
▪Values: A collection of elements.
▪Operations: enqueue (add an element to the end), dequeue (remove
an element from the front), isEmpty (check if the queue is empty).
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 11
Introduction to Data Structure
▪ Abstract Data Type (ADT): Example of an ADT: Integer
▪ Domain: The set of all possible integer values (e.g., ..., -2, -1, 0, 1, 2, ...).
▪ Operations:
▪ Creation (e.g., declaring an integer variable),
▪ Assignment (e.g., x = 5;),
▪ Arithmetic operations (e.g., +, -, *, /, %),
▪ Comparison operations (e.g., ==, !=, <, >).

▪In C++, the int data type is an example of an ADT. The user interacts with
integers through these operations without needing to know how
integers are stored in memory (e.g., binary-coded decimal, two's
complement, etc.)
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 12
Introduction to Data Structure
▪ Data Structure:
▪A data structure is a concrete implementation of an ADT.
▪ It defines how DATA is organized, stored, and manipulated inside a
computer`s memory, so that it can be used efficiently.
▪ Data structures are the "how" behind the "what" defined by ADTs.
▪ It is a logical model of a particular organization of data.
▪ Think of it as the blueprint for how data is arranged in memory. Just like a well-
organized filing cabinet makes it easy to find documents, a well-chosen data structure
makes it easy to work with data in a program.
▪ It's like choosing the right container for your items to keep them organized and easy to find
later.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 13
Introduction to Data Structure
▪ Data Structure:

▪ Data structures are essential for designing efficient algorithms and solving complex
problems in programming and software development.
▪ In other words, the possible logical and physical arrangement of DATA items to
provide an efficient solution for a problem is called Data Structures.

▪ Consider as an example books in a library: It can be stored in a shelf arbitrarily or


using some defined orders such as sorted by Title, Author and so on

▪ Operations on Data Structures?


▪ Create
▪ Destroy
▪ Access
▪ Update
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 14
Introduction to Data Structure
▪ Data Structure: Examples
▪Stack ADT can be implemented using:
▪An Array: Fixed-size, efficient but limited by size.
▪A Linked List: Dynamic size, flexible but uses more memory.
▪Queue ADT can be implemented using:
▪An Array: Requires circular array logic to handle wrapping.
▪A Linked List: Naturally supports dynamic resizing.
▪Why Data Structures Matter:
▪They determine the efficiency of operations (e.g., time and space
complexity).
▪Different data structures are suited for different use cases.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 15
Introduction to Data Structure
▪ Classification of Data Structures:
▪ Data structures can be classified based on their properties, such as linearity, memory allocation,
and usage.
1.Based on Linearity:
1.Linear Data Structures: Elements are arranged in a sequence.
Examples: Arrays, Linked Lists, Stacks, Queues.
2.Non-Linear Data Structures: Elements are not arranged in a sequence.
Examples: Trees, Graphs.
2.Based on Memory Allocation:
1.Static Data Structures: Fixed size (memory allocated at compile time).
Examples: Arrays.
2.Dynamic Data Structures: Size can grow or shrink (memory allocated at runtime).
Examples: Linked Lists, Trees.
3. Based on Usage:
1. Primitive Data Structures: Basic data types provided by the language.(built-in)
Examples: int, float, char.
2. Non-Primitive Data Structures: Built using primitive data types.
Examples: Arrays, Linked Lists, Stacks, Queues, Trees, Graphs.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 16
Algorithm Analysis:
What is Algorithm?
▪ Step by step procedure to solve a problem. E.g. baking cake, industrial
activities, Student Registration, etc, all needs an algorithm to follow.

▪Algorithm is a finite sequence of instruction, each of which has a clear


meaning and can be performed with a finite amount of effort in a finite
length of time

▪The purpose of an algorithm is to accept input values, to change a value hold


by a data structure, to re-organize the data structure itself (e.g. sorting), to
display the content of the data structure, and so on.

▪You can write algorithm using pseudo language a mixed form of ordinary
English and some programming language
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 17
Algorithm Analysis:
Properties of Algorithms
▪Finiteness: any algorithm should have finite number of steps to be
followed.

▪Absence of ambiguity: the algorithm should have one and only one
interpretation during execution.

▪Sequential: it should have a start step and a halt step for a final
result

▪Feasibility: the possibility of each algorithm to be executed.


▪Input/output: zero or more input, one or more output.
▪And so on…
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 18
Algorithm Analysis
▪There can be multiple algorithms to solve a given problem.

▪While they may all yield the same result, their efficiency can differ.

▪If we develop programs (e.g., in C++) to implement each algorithm and execute them
using the same input data, their performance will vary.

▪Some programs will run faster, while others may require more memory.

▪These differences might be insignificant for small data sets, but as the input size
increases, the variations will become more noticeable.

▪Complexity analysis is concerned with determining the efficiency of algorithms. How


do we measure the efficiency of algorithms? Computational (Empirical) complexity
and Asymptotic (Theoretical) complexity
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 19
Algorithm Analysis
▪In selecting algorithm that solves the problem in the best manner, we need to
know further about the behavior of the algorithms.

▪The two most commonly used tools to compare algorithms are Time complexity
and Space complexity of algorithms.

▪To compare the efficiency of algorithms, is to measure degree of difficulty of an


algorithm and this is called computational complexity.

▪Computational complexity indicates how much effort is needed to apply an


algorithm or how costly it is.

▪This cost commonly is measured in terms of time and space.

▪The factor of time is more important than that of space.


3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 20
Algorithm Analysis:
Computational and Asymptotic Complexity
▪In case of Computational complexity, the total running time of the program is
considered.

▪It uses the system time to calculate the running time and it can’t be used for
measuring efficiency of algorithms.

▪This is because the total running time of the program algorithm varies based on the
factors: (affecting efficiency of algorithm in terms of time)
▪ Processor speed: Speed of the machine that the algorithm run on.
▪ Current processor load
▪ Input size of the given algorithm
▪ and software environment (multitasking, single tasking,…)
▪ The language in which a given algorithm is written.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 21
Algorithm Analysis:
Computational and Asymptotic Complexity

▪Space complexity is amount of memory (RAM) space


required to run/execute an algorithm.

▪This is sum of fixed part and variable part of a program.


▪Computer memory size has been improved by many
orders of magnitude.
▪ Hence for algorithm analysis major focus will be on
time complexity.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 22
Algorithm Analysis:
Computational and Asymptotic Complexity
▪Time Complexity: Total time required to run an algorithm can be expressed as
function of input size of problem.

▪The limiting behavior of complexity as input size of a problem increases is called


Asymptotic Time Complexity.

▪It is the sum of compile time and running time.

▪Running time complexity will be under consideration for an evaluation and finding an
algorithm complexity analysis.

▪As the size of the input data get larger and larger the actual complexity computation
becomes equally important to knowing its bounding class.

▪For this reason, we study asymptotic complexity measuring.


3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 23
Algorithm Analysis:
Computational and Asymptotic Complexity
▪Before we study asymptotic complexity, what is asymptote?

▪An asymptote provides a behavior in respect of other function


for varying value of input size.

▪An asymptote is a line or curve that a graph approaches but


does not intersect.

▪In asymptotic measure of algorithm complexity, we deal the


boundaries of the algorithm’s lower and upper complexity with
respect to some known measure.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 24
Algorithm Analysis:
Computational and Asymptotic Complexity

▪The statement algorithm x has time complexity of O(n) means that


“algorithm x requires n (n being input size) executions to produce
the intended output”.
▪At the same time complexity of O(n) means n is the asymptote for
the algorithm.
▪Consequently, the algorithm runs 1 to n-1 number of times at
maximum as it never touch n.
▪Hence, asymptotic analysis of complexity is therefore measure of
bound, mean lower bound and upper bound of algorithm complexity.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 25
Algorithm Analysis:
Computational and Asymptotic Complexity
▪Below is simple definition of lower and upper bound using sets A and B.

▪You can think of sets A and B as two different complexity functions of two algorithms where one is used to
measure the other’s lower or upper bound.

▪The set A can be thought of as a collection of elements (e.g., values or functions).

▪The set B is a subset of A, and we want to find elements in A that "bound" B from below or above,
depending on the relation ≤.

▪Lower Bound: An element a є A is called lower bound of B if a ≤ x Ɐx є B.

▪ This means that a is "less than or equal to" every element in B. In other words, a sits "below" all elements
of B in the ordering defined by ≤.

▪Upper Bound: An element a є A is called upper bound of B if x ≤ a Ɐx є B.

▪ This means that a is "greater than or equal to" every element in B. In other words, a sits "above" all
elements of B in the ordering defined by ≤.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 26
Algorithm Analysis:
Computational and Asymptotic Complexity
▪Total running time of an algorithm is dependent on input size of the problem.

▪Hence complexity expression will always be a function in term of input size.

▪Problem: We couldn’t use real-time units such as microseconds to


evaluate an algorithms efficiency.

▪A better measure is to use the number of operations required to


perform an algorithm, since this is independent of the computer that
the program is run on.

▪Here, an operation can mean a single program statement such as an


assignment statement
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 27
Algorithm Analysis:
Computational and Asymptotic Complexity
▪We need to express the relationship between the size n of the input data and the
number of operations t required to process the data.
▪For example, if there is a linear relationship between the size n and the number of
operations t (that is, t = c.n where c is a constant), then an increase in the size of the
data by a factor of 5 results in an increase in number of operations by factor of 5.

▪In other words, in complexity analysis we are not interested in how


many microseconds it will take for an algorithm to execute or how
many operations it will take.

▪The important thing is how fast the number of operations grows as


the size of the data grows.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 28
Algorithm Analysis:
Computational and Asymptotic Complexity
▪In most real-world examples, the function expressing the relationship between
n and t would be much more complex.

▪BUT it is not normally necessary to determine the precise function, as many


of the terms will not be significant when the amount of data becomes large.

▪For example, consider the function t = f(n) = n2 + 5n.

▪This function consists of two terms, n2 and 5n.

▪However, for any n larger than 5 the n2 term is the most significant, and for
very large n we can effectively ignore the 5n term.

▪Therefore, we can approximate the complexity function as f(n) = n2.


3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 29
Algorithm Analysis:
Computational and Asymptotic Complexity
▪This simplified measure of efficiency is called asymptotic complexity and is
used when it is difficult or unnecessary to determine the precise computational
complexity function of an algorithm.
▪Another example: consider a function f(n) = n2 + 100n + log10n + 1000

▪We can approximate the complexity function as f(n) = n2


3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 30
Algorithm Analysis:
Computational and Asymptotic Complexity

Definition 1: The computational complexity of an


algorithm is a measure of the cost (usually in execution
time) incurred by applying the algorithm.

Definition 2: The asymptotic complexity of an


algorithm is an approximation of the computational
complexity that holds for large amounts of input data.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 31
Algorithm Analysis:
Asymptotic Complexity
▪The basic asymptotic notations used to measure
algorithm complexity are:
▪ O(Big Oh)
▪ Ώ(Big Omega)
▪ ϴ(Theta)
▪ o(Small Oh) and
▪ ῳ(Small Omega)
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 32
Algorithm Analysis:
Asymptotic Complexity
1. O(Big Oh)

▪ It is used for specifying asymptotic complexity, that is, for estimating the rate of growth of
complexity functions.

▪ Given two positive-valued functions f and g, consider the following definition:

▪ Definition: The function f(n) is O(g(n)) if there exist positive numbers c and N such that f(n) ≤
c.g(n) for all n ≥ N.

▪ This definition states that g(n) is an upper bound on the value of f(n).

▪ The notation f(n) is O(g(n)) means that the function f(n) does not grow faster than a
constant multiple of g(n) for sufficiently large n.

▪ In other words, in the long run (for large n) f grows at most as fast as g.
▪ Beyond a certain point N, the function f(n) will always be at most a constant multiple of
g(n).
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 33
Algorithm Analysis:
Asymptotic Complexity
1. O(Big Oh)

▪To illustrate this definition, consider the function:

f(n) = n2 + 5n.

▪For large values of n, we could approximate this function to n2 term only;


that is, the asymptotic complexity of f(n) is n2.

▪Therefore, we can say now that f(n) is O(n2).

▪In the definition, we substitute n2 for g(n), and we see that it is true that
f(n) ≤ 2.g(n) for all n ≥ 5 (i.e. in this case c=2, N=5).
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 34
Algorithm Analysis:
Asymptotic Complexity
1. O(Big Oh)
▪The problem with O (Big O) is that it does not tell us how to calculate c and N.
▪ f(n) ≤ c.g(n)
▪ n2 + 5n ≤ c. n2
▪ 1 + (5/n) ≤ c
▪Therefore, if we choose N=5, then c= 2; if we choose N=6, then c=1.83, and so on.
▪What are the ‘correct’ values for c and N ?
▪ The answer to this question, it should be determined for which value of N a
particular term in f(n) becomes the largest and stays the largest.
▪ In the above example, the n2 term becomes larger than the 5n term at n>5, so
N=5, c=2 is a good choice.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 35
Algorithm Analysis:
Asymptotic Complexity
1. O(Big Oh)

▪Another problem with O (Big O) is that there are actually infinitely


many functions g(n) that satisfy the definition.

▪For example, we chose n2, but we could also have chosen n3, n4, n5,
and so on.

▪All of these functions satisfy O (Big O).

▪To avoid this problem, the smallest function g is chosen, which in


this case is n2.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 36
Algorithm Analysis:
Asymptotic Complexity
O(Big Oh): Properties
Fact 1: If f(n) is O(h(n)) and g(n) is O(h(n)) then f(n) + g(n) is also O(h(n)).
▪If a program has O(n2) operation followed by another independent O(n2),
then the final program will also be O(n2).
Fact 2: The function a.nk is O(nk) for any a and k.
▪Multiplying a complexity function by a constant value (a) does not change the
asymptotic complexity.
Fact 3: The function loga n is O(logb n) for any positive numbers a and b ≠ 1
▪ It does not matter what the base of the logarithmic function is - all
logarithmic functions have the same rate of growth.
▪So, if a program is O(log2 n) it is also O(log10 n).
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 37
Algorithm Analysis:
Asymptotic Complexity
2. Ώ(Big Omega)
▪ Definition: The function f(n) is Ω(g(n)) if there exist positive
numbers c and N such that f(n) ≥ c.g(n) for all n ≥ N.

▪ g(n) is a lower bound on the value of f(n), or, in the long


run (for large n) f grows at least as fast as g.
▪ Ω notation has the same problems as big-O notation:

▪ There are many potential pairs of values for c and N, and there
are infinitely many functions that satisfy the definition.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 38
Algorithm Analysis:
Asymptotic Complexity
2. Ώ(Big Omega)
▪ When choosing one of these functions, for Ω notation we
should choose the largest function.

▪ In other words, we choose the smallest upper bound (big-O)


function and the largest lower bound (Ω) function.

▪ For example: to test if f(n) = n2 + 5n is Ω(n2) we need to find


a value for c such that n2 + 5n ≥ c.n2.

▪ For c=2 , this expression holds for all n≥5.


3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 39
Algorithm Analysis:
Asymptotic Complexity
3. ϴ(Theta)
▪ For some algorithms (but not all), the lower and upper
bounds on the rate of growth will be the same.
▪ In this case, a third notation exists for specifying asymptotic
complexity, called theta (Θ) notation.

▪ Definition: The function f(n) is Θ(g(n)) if there exist positive


numbers c1 , c2 and N such that c1.g(n) ≤ f(n) ≤ c2.g(n) for all
n ≥ N.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 40
Algorithm Analysis:
Asymptotic Complexity
3. ϴ(Theta)

▪This definition states that f(n) is Θ(g(n)) if f(n) is O(g(n)) and f(n) is
Ω(g(n)).
▪In other words, the lower and upper bounds on the rate of growth are
the same.
▪For the same example:

▪ f(n) = n2 + 5n, we can see that g(n) = n2 satisfies theta (Θ) notation, so
the function n2 + 5n is Θ(n2).

▪We have shown this already by showing that g(n) = n2 satisfies both big-O
and big-Omega definitions.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 41
Algorithm Analysis:
Asymptotic Complexity

Reading Assignment
4. o(Small Oh)
▪ You can think of little-o notation as the
opposite of Θ notation.
5. ῳ(Small Omega)
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 42
Algorithm Analysis:
Asymptotic Complexity
Summary of Asymptotic Complexity
functions:

3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 43
Algorithm Analysis:
Asymptotic Complexity
Finding Asymptotic Complexity: Examples

Analysis Rules:
1. We assume an arbitrary time unit.
2. Execution of one of the following operations takes time 1:

1. Assignment Operation
2. Single Input / Output Operation
3. Single Boolean Operations
4. Single Arithmetic Operations
5. Function Return
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 44
Algorithm Analysis:
Asymptotic Complexity
Finding Asymptotic Complexity: Examples

Analysis Rules:

3. Running time of a selection statement (if, switch) is the time for the condition
evaluation + the maximum of the running times for the individual clauses in the
selection.
4. Loops: Running time for a loop is equal to the running time for the statements inside
the loop * number of iterations.
The total running time of a statement inside a group of nested loops is the running time
of the statements multiplied by the product of the sizes of all the loops.
For nested loops, analyze inside out:
Always assume that the loop executes the maximum number of iterations possible.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 45
Algorithm Analysis:
Asymptotic Complexity
Finding Asymptotic Complexity: Examples

Analysis Rules:
5. Running time of a function call is 1 for setup + the time for any parameter calculations +
the time required for the execution of the function body.

int count(){

int k=0;

cout<< “Enter an integer”;

cin>>n;

for (i=0;i<n;i++)

k=k+1;

return 0; }
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 46
Algorithm Analysis:
Asymptotic Complexity
Step-by-Step Analysis:
int count(){
Step 1: int k = 0;
▪This is a simple assignment operation.
▪Time Complexity: O(1)(constant time). int k=0;
Step 2: cout << "Enter an integer";
▪This is an output operation. cout<< “Enter an integer”;
▪Time Complexity: O(1) (constant time).
Step 3: cin >> n; cin>>n;
▪This is an input operation.
▪Time Complexity: O(1)(constant time). for (i=0;i<n;i++)
Step 4: for (i = 0; i < n; i++)
▪This is a loop that runs n times.
k=k+1;
▪Time Complexity: O(n) (linear time).
Step 5: k = k + 1;
▪This is a simple addition operation inside the loop. return 0; }
▪Since it runs n times, its total contribution to the time complexity is O(n).
Step 6: return 0;
▪This is a return statement.
▪Time Complexity: O(1) (constant time).
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 47
Algorithm Analysis:
Asymptotic Complexity
Step-by-Step Analysis:
Total Time Complexity:
▪The dominant part of the code is the loop (Step 4 and Step 5), which runs n times.
▪All other operations (Steps 1, 2, 3, and 6) are constant time and do not depend on n.
Thus, the time complexity of the code is: O(n)
Explanation:
▪The loop runs n times, and the operations inside the loop (Step 5) are executed n
times.
▪The rest of the operations are constant time and do not affect the overall growth
rate of the algorithm.
▪Therefore, the time complexity is linear with respect to n.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 48
Algorithm Analysis:
Asymptotic Complexity int count(){

Finding Asymptotic Complexity: Examples int k=0;


Time Units to Compute
cout<< “Enter an integer”;
✓ 1 for the assignment statement: int k=0
cin>>n;
✓ 1 for the output statement.
for (i=0;i<n;i++)
✓ 1 for the input statement.
✓ In the for loop: k=k+1;

✓ 1 assignment, n+1 tests, and n increments. return 0; }

✓ n loops of 2 units for an assignment, and an addition.


✓ 1 for the return statement.

Therefore: f (n)= 1+1+1+(1+n+1+n)+2n+1 = 4n+6 = O(n)


3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 49
Algorithm Analysis Categories
▪Algorithm must be examined under different situations to
correctly determine their efficiency for accurate comparisons
1. Best Case Analysis: Assumes that data are arranged in the most
advantageous order. It also assumes the minimum input size.

▪For sorting – the best case is if the data are arranged in the required
order.

▪For searching – the required item is found at the first position.

▪ Note: Best Case computes the lower boundary of f(n)

▪ It causes fewest number of executions


3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 50
Algorithm Analysis Categories

2. Worst Case Analysis


▪Assumes that data are arranged in the disadvantageous
order. It also assumes that the input size is infinite.
▪For sorting – data are arranged in opposite required order

▪For searching – the required item is found at the end of the


item or the item is missing
▪It computes the upper bound of f(n) and causes maximum
number of execution.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 51
Algorithm Analysis Categories
3. Average Case Analysis
▪Assumes that data are found in random order.
▪It also assumes random or average input size.
▪For sorting – data are in random order.
▪For searching – the required item is found at any
position or missing.
▪It computes optimal bound of f(n)
▪It also causes average number of execution.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 52
Algorithm Analysis Categories

✓Best case and average case can not be


used to estimate (determine) complexity
of algorithms

✓Worst case is the best to determine the


complexity of algorithms.

3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 53

You might also like