INTRODUCTION TO DATA
STRUCTURES &
ALGORITHMS
OUTLINE
• Introduction
• Data structures
• Computational model
• Algorithms
• Time complexity
• Asymptotic analysis
• Recursion
• Divide –and- conquer strategy
• Computational problem
OBJECTIVES
• Understand computational models
• Understand asymptotic notations
• Write recurrence relation
• Understand recursion and how to trace the recursive function
• Discuss strategies for solving computational problems
DATA STRUCTURES
• Studies the way of organizing data in a computer so that it
can be used effectively
• Organizing
• Manipulation
• Effective/ efficient manner
• Types: Linear and Non Linear
• Examples: Stacks, Queues,Trees, Dictionaries, Graphs
COMPUTATIONAL MODEL
• A computational model is a mathematical model in computational science
• It requires extensive computational resources to study the behavior of a
complex system by computer simulation
• It is using mathematical model (representations) to make better sense of
data or complex systems
• Programming language
• Computer architecture
ALGORITHMS
• It a step-by-step procedure for solving a computational problem
• They are important in studying complex systems design (computational
model)
• They can be studied in a language and machine independent way
• Techniques to compare algorithms efficiency without implementing them:
• RAM model of computation
• Asymptotic analysis of worst-case complexity
RAM MODEL
• Random Access Machine suggests that each simple operation (+, -,/,*,=, if…) takes
exactly 1 sept
• Each memory access takes 1 step
• Loops and methods aren’t simple operations rather they are dependent on the size of
data and content of method
• The run time of an algorithm is measured by counting the number of steps
• The RAM model: a fixed program, unbounded memory, read only input, write only
output, memory holds arbitrary integer.
SPACE COMPLEXITY
• The amount of memory required by an algorithm to run to completion
• Fixed part:The size required to store certain data/variables, that is
independent of the size of the problem: Such as int a (2 bytes, float b (4 bytes)
etc
• Variable part: Space needed by variables, whose size is dependent on the size of
the problem: Dynamic array a[ ].
RUNNING TIME OF ALGORITHMS
• Depends on input size n.
• the input vertices and edges in a graph gives the running time
of an algorithm (bit representations)
• It is based upon the number of primitive operations performed
• primitive operation is the unit of operation that can be identified in the
pseudo-code
DETERMINING TIME COMPLEXITY
• Step-1. Determine how you will measure input size.
• Step-2. Choose the type of operation (or perhaps two operations) Comparisons,
Swaps, Copies, Additions.
• Step-3. Decide whether you wish to count operations in the
• Best case? - the fewest possible operations.
• Worst case? - the most possible operations.
• Average case? - This is harder as it is not always clear what is meant by an "average case".
NB: these calculation requires some level of probability theory
• Step-4. For the algorithm and the chosen case (best, worst, average), express the
count as a function of the input size of the problem.
RASP MACHINE OR STORED PROGRAM
MODEL
• It is same as RAM but allow program to change itself
as it is now stored in the memory
• memory of modern computers hosts both a program
and its corresponding data
CHARACTERISTICS OF ALGORITHM
• Input – It may take 0 or more inputs.
• Output – It must generate at least 1 output.
• Definiteness – Every statement must have one single and exact
meaning.
• Finiteness – Algorithm must have finite set of statements. It must
terminate at some point.
• Effectiveness – Statements must serve some purpose.
ALGORITHM WRITING
• A simple algorithm of swapping can be written as:
Algorithm swap (a, b)
{
temp = a;
a = b;
b = temp;
}
ANALYSIS OF ALGORITHMS
• There are few criteria for analysis:
• Analysis of time taken:
• every simple statement takes 1 unit of time
• in the algorithm of swapping, the time taken is 3 which is a constant
• So, F(n) = 3
• The constant is represented as O (1)
ANALYSIS OF SPACE TAKEN
• The variables taken in the algorithm are a, b, and temp
• The space taken is 3 words which is a constant
• Constant is represented as O (1)
• The analysis can be done by using the frequency count
method.
ANALYSIS OF SPACE
• The variables taken in the algorithm are a, b, and temp
• The space taken is 3 words which is a constant
• Constant is represented as O (1)
• The analysis can be done by using the frequency count
method
EXAMPLE: SUM OF NUMBERS
SPACE COMPLEXITY
• F (n) = 2n +3, so degree of polynomial = 1.
• Then the time complexity = O (n)
• Space complexity:
• Variables used = A, n, S, I
• A:n words, n:1 word, S:1 word, i:1 word
• S(n) = n + 3 words,
• so space complexity = O(n)
EXAMPLE 2:
SPACE COMPLEXITY (2)
• F(n) = 2n2
+ 2n +1, here the highest degree is 2.
• So, the time complexity is O(n2
).
• Variables: A, B, C, n, i, j.
• A n2
, B n2
, C n2
, n 1, i 1 and j 1 word.
• Space complexity S(n) is 3 n2
+ 3.
• Degree is O(n2
)
TIME COMPLEXITY
We need to check for how many times the statement inside the loop will
run. So, the degree will depend upon that
TYPES OF TIME FUNCTIONS
• O (1) Constant
🡺
• O(log n) Logarithmic
🡺
• O(n) Linear
🡺
• O(n2
) Quadratic
🡺
• O(n3
) Cubic
🡺
• O(2n
) , O(3n
), O(nn
) Exponential
🡺
COMPARISON OF CLASSES OF FUNCTIONS
• 1 < log n < (n)1/2< n < n log n < n2< n3< --- < 2n< 3n ---< nn
• to see which alg is taking less time:
• implement both the algorithms and run the two programs with different
inputs
• There are problems with this approach for analysis of algorithms
• What problems? NB: Input and machine dependent
HOW TO COMPARE ALGORITHMS
• To compare algorithms, let us define a few objective measures:
• Execution times?
• Not a good measure (specific computer).
• Number of statements executed?
• Not a good measure (programming language)
• Ideal solution?
• algorithm as a function of the input size n (i.e., f(n))
TYPES OF ANALYSIS
• know which inputs the algorithm takes less time or long time
• First case: best case
• Second case: worse case
• We need some kind of syntax = asymptotic analysis or notation
• Worse case
• Average case
• Best case
ASYMPTOTIC ANALYSIS
• evaluate the performance of an algorithm in terms of input size
• calculate, how the time (or space) taken by an algorithm increases with the input size
• Asymptotic notations are mathematical tools to represent the time complexity of
algorithms
• The notations used are:
• O (Big – Oh notation):Works as an upper bound of a function.
• (Big – Omega notation): Works as a lower bound of a function.
Ω
• (Theta notation): Works as an average bound of a function.
Θ
BIG-0 NOTATION
• It gives tight upper bound of a given function f(n)
BIG OH NOTATION
• The function f(n) = O(g(n)) iff +ve constants C and n
∃ 0. such that f(n) ≤ C * g(n) n
∀ 0
• Understanding the Statement:
📌 The function f(n) = O(g(n)) This means that f(n) grows at most as fast as g(n) when
n becomes large.
In other words, for sufficiently large values of n, f(n) is always less than or equal to
some multiple of g(n).
📌 iff +ve constants C and n
∃ ₀ This means that there exist some positive constants
C and n₀ such that the condition holds.
📌 such that f(n) ≤ C * g(n) n ≥ n
∀ ₀ This means that for all values of n greater than
or equal to n₀, the function f(n) will always be less than or equal to C times g(n).
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx
02 Introduction to Data Structures & Algorithms.pptx

02 Introduction to Data Structures & Algorithms.pptx

  • 1.
  • 2.
    OUTLINE • Introduction • Datastructures • Computational model • Algorithms • Time complexity • Asymptotic analysis • Recursion • Divide –and- conquer strategy • Computational problem
  • 3.
    OBJECTIVES • Understand computationalmodels • Understand asymptotic notations • Write recurrence relation • Understand recursion and how to trace the recursive function • Discuss strategies for solving computational problems
  • 4.
    DATA STRUCTURES • Studiesthe way of organizing data in a computer so that it can be used effectively • Organizing • Manipulation • Effective/ efficient manner • Types: Linear and Non Linear • Examples: Stacks, Queues,Trees, Dictionaries, Graphs
  • 5.
    COMPUTATIONAL MODEL • Acomputational model is a mathematical model in computational science • It requires extensive computational resources to study the behavior of a complex system by computer simulation • It is using mathematical model (representations) to make better sense of data or complex systems • Programming language • Computer architecture
  • 6.
    ALGORITHMS • It astep-by-step procedure for solving a computational problem • They are important in studying complex systems design (computational model) • They can be studied in a language and machine independent way • Techniques to compare algorithms efficiency without implementing them: • RAM model of computation • Asymptotic analysis of worst-case complexity
  • 7.
    RAM MODEL • RandomAccess Machine suggests that each simple operation (+, -,/,*,=, if…) takes exactly 1 sept • Each memory access takes 1 step • Loops and methods aren’t simple operations rather they are dependent on the size of data and content of method • The run time of an algorithm is measured by counting the number of steps • The RAM model: a fixed program, unbounded memory, read only input, write only output, memory holds arbitrary integer.
  • 8.
    SPACE COMPLEXITY • Theamount of memory required by an algorithm to run to completion • Fixed part:The size required to store certain data/variables, that is independent of the size of the problem: Such as int a (2 bytes, float b (4 bytes) etc • Variable part: Space needed by variables, whose size is dependent on the size of the problem: Dynamic array a[ ].
  • 9.
    RUNNING TIME OFALGORITHMS • Depends on input size n. • the input vertices and edges in a graph gives the running time of an algorithm (bit representations) • It is based upon the number of primitive operations performed • primitive operation is the unit of operation that can be identified in the pseudo-code
  • 10.
    DETERMINING TIME COMPLEXITY •Step-1. Determine how you will measure input size. • Step-2. Choose the type of operation (or perhaps two operations) Comparisons, Swaps, Copies, Additions. • Step-3. Decide whether you wish to count operations in the • Best case? - the fewest possible operations. • Worst case? - the most possible operations. • Average case? - This is harder as it is not always clear what is meant by an "average case". NB: these calculation requires some level of probability theory • Step-4. For the algorithm and the chosen case (best, worst, average), express the count as a function of the input size of the problem.
  • 11.
    RASP MACHINE ORSTORED PROGRAM MODEL • It is same as RAM but allow program to change itself as it is now stored in the memory • memory of modern computers hosts both a program and its corresponding data
  • 12.
    CHARACTERISTICS OF ALGORITHM •Input – It may take 0 or more inputs. • Output – It must generate at least 1 output. • Definiteness – Every statement must have one single and exact meaning. • Finiteness – Algorithm must have finite set of statements. It must terminate at some point. • Effectiveness – Statements must serve some purpose.
  • 13.
    ALGORITHM WRITING • Asimple algorithm of swapping can be written as: Algorithm swap (a, b) { temp = a; a = b; b = temp; }
  • 14.
    ANALYSIS OF ALGORITHMS •There are few criteria for analysis: • Analysis of time taken: • every simple statement takes 1 unit of time • in the algorithm of swapping, the time taken is 3 which is a constant • So, F(n) = 3 • The constant is represented as O (1)
  • 15.
    ANALYSIS OF SPACETAKEN • The variables taken in the algorithm are a, b, and temp • The space taken is 3 words which is a constant • Constant is represented as O (1) • The analysis can be done by using the frequency count method.
  • 16.
    ANALYSIS OF SPACE •The variables taken in the algorithm are a, b, and temp • The space taken is 3 words which is a constant • Constant is represented as O (1) • The analysis can be done by using the frequency count method
  • 17.
  • 18.
    SPACE COMPLEXITY • F(n) = 2n +3, so degree of polynomial = 1. • Then the time complexity = O (n) • Space complexity: • Variables used = A, n, S, I • A:n words, n:1 word, S:1 word, i:1 word • S(n) = n + 3 words, • so space complexity = O(n)
  • 19.
  • 20.
    SPACE COMPLEXITY (2) •F(n) = 2n2 + 2n +1, here the highest degree is 2. • So, the time complexity is O(n2 ). • Variables: A, B, C, n, i, j. • A n2 , B n2 , C n2 , n 1, i 1 and j 1 word. • Space complexity S(n) is 3 n2 + 3. • Degree is O(n2 )
  • 21.
    TIME COMPLEXITY We needto check for how many times the statement inside the loop will run. So, the degree will depend upon that
  • 22.
    TYPES OF TIMEFUNCTIONS • O (1) Constant 🡺 • O(log n) Logarithmic 🡺 • O(n) Linear 🡺 • O(n2 ) Quadratic 🡺 • O(n3 ) Cubic 🡺 • O(2n ) , O(3n ), O(nn ) Exponential 🡺
  • 23.
    COMPARISON OF CLASSESOF FUNCTIONS • 1 < log n < (n)1/2< n < n log n < n2< n3< --- < 2n< 3n ---< nn • to see which alg is taking less time: • implement both the algorithms and run the two programs with different inputs • There are problems with this approach for analysis of algorithms • What problems? NB: Input and machine dependent
  • 24.
    HOW TO COMPAREALGORITHMS • To compare algorithms, let us define a few objective measures: • Execution times? • Not a good measure (specific computer). • Number of statements executed? • Not a good measure (programming language) • Ideal solution? • algorithm as a function of the input size n (i.e., f(n))
  • 25.
    TYPES OF ANALYSIS •know which inputs the algorithm takes less time or long time • First case: best case • Second case: worse case • We need some kind of syntax = asymptotic analysis or notation • Worse case • Average case • Best case
  • 26.
    ASYMPTOTIC ANALYSIS • evaluatethe performance of an algorithm in terms of input size • calculate, how the time (or space) taken by an algorithm increases with the input size • Asymptotic notations are mathematical tools to represent the time complexity of algorithms • The notations used are: • O (Big – Oh notation):Works as an upper bound of a function. • (Big – Omega notation): Works as a lower bound of a function. Ω • (Theta notation): Works as an average bound of a function. Θ
  • 27.
    BIG-0 NOTATION • Itgives tight upper bound of a given function f(n)
  • 28.
    BIG OH NOTATION •The function f(n) = O(g(n)) iff +ve constants C and n ∃ 0. such that f(n) ≤ C * g(n) n ∀ 0 • Understanding the Statement: 📌 The function f(n) = O(g(n)) This means that f(n) grows at most as fast as g(n) when n becomes large. In other words, for sufficiently large values of n, f(n) is always less than or equal to some multiple of g(n). 📌 iff +ve constants C and n ∃ ₀ This means that there exist some positive constants C and n₀ such that the condition holds. 📌 such that f(n) ≤ C * g(n) n ≥ n ∀ ₀ This means that for all values of n greater than or equal to n₀, the function f(n) will always be less than or equal to C times g(n).

Editor's Notes

  • #5 Types of Computational Models Mathematical Models – Use equations to represent relationships (e.g., differential equations in physics). Agent-Based Models – Represent systems with interacting autonomous entities (e.g., traffic flow simulations). Neural Networks – Simulate human brain processes for machine learning (e.g., image recognition). Finite State Machines – Represent processes with defined states and transitions (e.g., vending machines). Monte Carlo Simulations – Use random sampling to model uncertainty (e.g., stock market predictions). Examples Weather Forecasting Uses mathematical models based on physics equations (e.g., Navier-Stokes equations) to predict temperature, wind, and precipitation patterns. Disease Spread Simulation (Epidemiology) Uses agent-based models or SIR (Susceptible-Infected-Recovered) models to predict how diseases spread in a population. Traffic Flow Simulation Uses computational models to optimize road networks, predict congestion, and improve traffic light timing. Autonomous Vehicles Use machine learning models (e.g., neural networks) and simulation environments to train self-driving cars. Computational Biology (Protein Folding) Uses simulations to understand how proteins fold, which helps in drug discovery.
  • #14 The goal of the analysis of algorithms is to compare algorithms (or solutions) mainly in terms of running lime but also in terms of other factors (e.g., memory, developer effort)
  • #23 1) It might be possible that for some inputs, first algorithm performs better than the second. And for some inputs second performs better. 2) It might also be possible that for some inputs, first algorithm performs better on one machine and the second works better on other machine for some other inputs. The solution to the above problem is the asymptotic analysis
  • #24 The rate at which the running time increases as a function of input is called the rate of growth. Total cost is approximately the cost of the highest item.
  • #27 0 - notation defined as O(g(n)) = {f(n): there exist positive constants c and n0 such that no <= f(n)<= cg(n) for all n >= n0}. g(n) is an asymptotic tight upper bound for f(n). Our objective is to give the smallest rate of growth 9(n) which is greater than or equal to the given algorithms’ rate of growth f(n). n0 is called threshold for the given function
  • #28 This statement represents Big-O notation, which describes the upper bound of a function's growth rate.