DSA_Chapter_1_Introduction_to_Data_Structures_and_Algorithms
DSA_Chapter_1_Introduction_to_Data_Structures_and_Algorithms
Structures
and
Algorithms
CHAPTER ONE:
INTRODUCTION TO DATA
STRUCTURES AND ALGORITHMS
▪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.
▪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.
▪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
▪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.
▪The two most commonly used tools to compare algorithms are Time complexity
and Space complexity of algorithms.
▪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
▪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.
▪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 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 ≤.
▪ 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 ≤.
▪ 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.
▪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.
▪ It is used for specifying asymptotic complexity, that is, for estimating the rate of growth of
complexity functions.
▪ 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)
f(n) = n2 + 5n.
▪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)
▪For example, we chose n2, but we could also have chosen n3, n4, n5,
and so on.
▪ 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.
▪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;
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(){
▪For sorting – the best case is if the data are arranged in the required
order.
3/27/2025 Data Structures and Algorithms Chapter One: Introduction to DS By: Kibru G. 53