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

2-Analysis of Algorithm

The document discusses algorithm analysis and efficiency. It covers: 1) Measuring time and space complexity to evaluate algorithm efficiency. Worst-case, best-case, and average-case analyses are described. 2) Asymptotic notations like O, Ω, Θ are introduced to classify efficiency orders of growth. 3) Methods for analyzing efficiencies of non-recursive and recursive algorithms are outlined, including identifying basic operations and setting recurrence relations.

Uploaded by

teklethelatter
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

2-Analysis of Algorithm

The document discusses algorithm analysis and efficiency. It covers: 1) Measuring time and space complexity to evaluate algorithm efficiency. Worst-case, best-case, and average-case analyses are described. 2) Asymptotic notations like O, Ω, Θ are introduced to classify efficiency orders of growth. 3) Methods for analyzing efficiencies of non-recursive and recursive algorithms are outlined, including identifying basic operations and setting recurrence relations.

Uploaded by

teklethelatter
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Algorithm Design and Analysis

Algorithm Analysis

Sosina Mengistu, AAIT


Fundamentals of the analysis of algorithm efficiency

❑The Analysis Framework


▪ Time efficiency, also called time complexity, indicates how fast an algorithm in question runs
▪ Space efficiency, also called space complexity, refers to the amount of memory units required by the
algorithm in addition to the space needed for its input and output
Worst-Case, Best-Case, and Average-Case Efficiencies

❑Measuring an algorithm’s efficiency as a function of a parameter indicating the size of the


algorithm’s input
▪ But there are many algorithms for which running time depends not only on an input size but also on
the specifics of a particular input
❑E.g., sequential search
▪ The running time of this algorithm can be quite different for the same list size n.
▪ In the worst case, when there are no matching elements or the first matching element happens to be
the last one on the list, the algorithm makes the largest number of key comparisons among all
possible inputs of size n
Worst-Case, Best-Case, and Average-Case Efficiencies
❑The worst-case efficiency of an algorithm is its efficiency for the worst-case input of size n,
which is an input (or inputs) of size n for which the algorithm runs the longest among all
possible inputs of that size.
▪ Guarantees that for any instance of size n, the running time will not exceed its running time on the
worst-case inputs.
❑The best-case efficiency of an algorithm is its efficiency for the best-case input of size n, which
is an input (or inputs) of size n for which the algorithm runs the fastest among all possible
inputs of that size
▪ The best-case efficiency is not nearly as important as that of the worst-case efficiency
❑The average-case efficiency seeks to provide an algorithm’s behavior on a “typical” or
“random” input
▪ we must make some assumptions about possible inputs of size n
▪ Let’s consider again sequential search
o Let the probability of a successful search=p and the average number of key comparisons =Cavg(n)
Asymptotic Notations and Basic Efficiency Classes
❑The efficiency analysis framework concentrates on the order of growth of an algorithm’s basic
operation count as the principal indicator of the algorithm’s efficiency
❑To compare and rank such orders of growth, computer scientists use three
notations: O big oh , Ω big omega , and Θ(big theta)
O notation
❑A function t (n) is said to be in O(g(n)), denoted t (n) ∈ O(g(n)), if t(n) is bounded above by
some constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and
some nonnegative integer n0 such that
𝑡 (𝑛) ≤ 𝑐𝑔(𝑛) 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥ 𝑛0.
Asymptotic Notations and Basic Efficiency Classes
𝜴 notation
❑A function t (n) is said to be in 𝛺(g(n)), denoted t (n) ∈ 𝛺(g(n)), if t(n) is bounded below by
some positive constant multiple of g(n) for all large n, i.e., if there exist some positive constant c
and some nonnegative integer n0 such that
𝑡 (𝑛) ≥ 𝑐𝑔(𝑛) 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥ 𝑛0.
Asymptotic Notations and Basic Efficiency Classes
𝜣 notation
❑A function t (n) is said to be in Θ(g(n)), denoted t (n) ∈ Θ(g(n)), if t (n) is bounded both above
and below by some positive constant multiples of g(n) for all large n, i.e., if there exist some
positive constants c1 and c2 and some nonnegative integer n0 such that
𝑐2𝑔(𝑛) ≤ 𝑡 (𝑛) ≤ 𝑐1𝑔(𝑛) 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥ 𝑛0.
Useful Property Involving the Asymptotic Notations

❑THEOREM:
If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}).
❑For an algorithm that comprises two consecutively executed parts, the algorithm’s overall
efficiency is determined by the part with a higher order of growth i.e., its least efficient part
❑E.g., we can check whether an array has equal elements by the following two-part algorithm:
▪ first, sort the array by applying some known sorting algorithm;
▪ second, scan the sorted array to check its consecutive elements for equality
Using Limits for Comparing Orders of Growth
❑Computing the limit of the ratio of two functions

❑ Note that the first case mean that t (n) ∈ O(g(n)), the second case means that t (n) ∈ Θ(g(n))
and the last mean that t (n) ∈ Ω(g(n))
❑The limit-based approach can take advantage of the powerful calculus techniques developed for
computing limits
▪ L’Hopital’s rule

▪ Stirling’s formula
Analysis of Non-recursive Algorithms
❑Consider the problem of finding the value of the largest element in a list of n numbers. For
simplicity, we assume that the list is implemented as an array. The following is pseudocode of a
standard algorithm for solving the problem.

❑C(n)=the number of times this comparison is executed


Analysis of Non-recursive Algorithms
❑General Plan for Analyzing the Time Efficiency of Non-recursive Algorithms
▪ Decide on a parameter (or parameters) indicating an input’s size.
▪ Identify the algorithm’s basic operation.
▪ Check whether the number of times the basic operation is executed depends only on the size of an
input.
o If it also depends on some additional property, the worst-case, average-case, and, if necessary, best-case
efficiencies have to be investigated separately.
▪ Set up a sum expressing the number of times the algorithm’s basic operation is executed.
▪ Using standard formulas and rules of sum manipulation, either find a closed form formula for the
count or, at the very least, establish its order of growth.
Analysis of Non-recursive Algorithms
❑Consider the element uniqueness problem: check whether all the elements in a given array of n
elements are distinct. This problem can be solved by the following straightforward algorithm.

❑The number of element comparisons depends not only on n but also on whether there are equal
elements in the array and, if there are, which array positions they occupy
❑Worst case analysis
▪ Two kinds of worst-case inputs - arrays with no equal elements and arrays in which the last two
elements are the only pair of equal elements.
Analysis of recursive Algorithms
❑E.g., Compute the factorial function F(n) = n! for an arbitrary nonnegative integer n

❑The number of multiplications M(n) needed


Analysis of recursive Algorithms

❑General Plan for Analyzing the Time Efficiency of Recursive Algorithms


▪ Decide on a parameter (or parameters) indicating an input’s size.
▪ Identify the algorithm’s basic operation.
▪ Check whether the number of times the basic operation is executed can vary on different inputs of the
same size;
o If it can, the worst-case, average-case, and best-case efficiencies must be investigated separately.
▪ Set up a recurrence relation, with an appropriate initial condition, for the number of times the basic
operation is executed.
▪ Solve the recurrence or, at least, ascertain the order of growth of its solution.

You might also like