Unit 2 - Analysis of Algorithm

download Unit 2 - Analysis of Algorithm

of 30

Transcript of Unit 2 - Analysis of Algorithm

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    1/30

    Analyzing the Algorithm

    Chapter 1

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    2/30

    Introduction An algorithm is an effective method for solving a

    problem expressed as a finite sequence ofinstructions.

    In world of computer science the word algorithm has

    a great significant.

    Algorithm is developed first , before writing the

    program.

    Efficiency of the program is improved by using Good

    Algorithm.

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    3/30

    Algorithm

    An algorithm is a finite set of instructions that if

    followed , accomplishes a particular task in a finite

    amount of time. Every algorithm must satisfy the following criteria.

    1)Input

    2)Output

    3)Definiteness

    4)Finiteness

    5)Effectiveness

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    4/30

    Characteristics

    1) Input: Zero or more quantities are externally

    supplied

    2) Output: At least one quantity is produced3) Definiteness: Each instruction is clear and

    unambiguous

    4) Finiteness: The algorithm terminates in a finitenumber of steps.

    5) Effectiveness: Every step in the algorithm should be

    easy to understand & can be implemented using any

    programming language.

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    5/30

    Analysis Of algorithm Analysis is the process of breaking a complex topic

    into a smaller parts to gain a better understanding ofit.

    To analyze an algorithm is to determine the amount of

    resources (such as time and storage) necessary toexecute it.

    Analysis of algorithm means developing a prediction

    of how fast the algorithm works based on the problemsize.

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    6/30

    Algorithms Performance An algorithms performance depends on

    1) Internal factors

    2) External factors

    External Factors:-

    a) Speed of the computer on which it is run

    b) Quality of the compiler

    Internal Factors:-

    a) Time Required to run

    b) Space

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    7/30

    Performance of Program

    To develop a program, we need two major factors:

    Algorithm

    Data Structure

    The choice of good data structures and algorithm design

    methods impacts theperformance of programs.

    The performance of a program can be analyzed by its

    complexity i.e. the amount of computer memory and time

    needed to run a program.

    Space Complexity

    Time Com lexit

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    8/30

    Complexity Computational complexity is a characterization of the

    time or space requirements for solving a problem by aparticular algorithm.

    The analysis of the program requires two main

    considerations.1) Time complexity.

    2)Space complexity.

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    9/30

    Time Complexity The time complexity of a program/algorithm is the

    amount o computer time that it needs to run to

    completion. OR The total amount of time taken by a program for

    execution is the time complexity of a program.

    For measuring the time complexity of an algorithm,we concentrate on developing only the frequencycount for all key statements (i.e statements that are

    important & are the basic instructions of analgorithm.)

    Complexity of any program is represented by Big Onotation.

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    10/30

    Time Complexity contd..

    Frequency count is defined as the total number oftimes statements are executed.

    Thus, Execution time is proportional to the frequencycount of an active operation.

    Eg. Algorithm A contains a= a+1

    The statement a=a+1 is independent. The number oftimes this shall be executed is 1. Therefore frequencycount of algorithm A is 1.

    Eg. 2) for(j=1; j

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    11/30

    Time Complexity contd..

    Amount of Computer time required by a program to execute

    Linear Complexity O (n)

    Quadratic Complexity O (n2)

    Cubic Complexity O (n3)

    Logarithmic Complexity O (log n)

    Exponential Complexity O (2n)

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    12/30

    Space Complexity The space complexity of an algorithm or program is

    the amount of memory that it needs to run tocompletion.

    The space needed by the algorithm is the sum of the

    following components.1) Fixed Part

    2) Variable space requirement

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    13/30

    Space Complexity contd

    Fixed Part:- Fixed part is not dependent on the characteristics of the

    inputs and outputs.

    Fixed part consist of space for fixed size structure

    variables & constants.

    Variable Space Requirement:- This includes a space, needed by variables whose size

    depends upon the particular problem being solved and

    the stack space required for recursion.

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    14/30

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

    {

    DoProcessing();for(int j= i; j< n; j++)

    {

    EnterStuffinDBMS();

    for(int p=1; p< n; p++ )

    {

    QuerySomeResults();}

    for(int l= 0; l< m; l++)

    {

    }

    }for(int k= 2; k< m; k++)

    {

    }

    }

    i

    j

    p

    l

    k

    To Calculate the Time Complexity

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    15/30

    O ( n * ( n * ( n + m ) + m ) )

    O ( n * ( n2 + nm + m))

    O (n3 + n2m + nm)

    O (n3) Time Complexity of the program

    To Calculate the Time Complexity (continued)

    i

    j

    p

    l

    k

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    16/30

    Space Complexity V/s Time Complexity

    A software which executes in less time requires more space

    and vice versa.

    This is known as tradeoff between Space and Time

    complexity.

    T (Time)

    S (Space)

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    17/30

    Best Case ,Worst Case & Average Case

    In some cases, it is important to consider the best,

    worst and/or average (or typical) performance of an

    algorithm.

    There are different types of time complexities which

    can be analyzed for an algorithm.

    1) Best Case Time Complexity

    2) Average Case Time Complexity3) Worst case Time Complexity.

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    18/30

    Best Case Time Complexity

    It is a measure of the minimum time that thealgorithm will require for an input of size n.

    If an input data of n items is presented in sorted

    order ,the operations performed by the algorithm willtake the least time.

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    19/30

    Worst Case Time Complexity

    It is a measure of the maximum time that thealgorithm will require for an input of size n.

    If n input data items are supplied in reverse orderfor any sorting algorithm, then the algorithm will

    require n^2 operations to perform the sort which will

    correspond to the worst case time complexity of thealgorithm.

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    20/30

    Average Case Time Complexity

    The average time that an algorithm will require toexecute a typical data of size n is known as average

    case time complexity.

    The value that is obtained by averaging the running

    time of an algorithm for all possible inputs of size n

    can determine average case time complexity.

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    21/30

    Example For example, consider the deterministic sorting

    algorithm quicksort .

    This solves the problem of sorting a list of integerswhich is given as the input.

    The best-case scenario is when the input is already

    sorted, and the algorithm takes time O(n log n) for suchinputs.

    The worst-case is when the input is sorted in reverse

    order, and the algorithm takes time O(n2

    ) for this case. If we assume that all possible permutations of the input

    list are equally likely, the average time taken for sorting

    is O(n log n).

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    22/30

    Asymptotic Notation Running time of an algorithm as a function

    of input size n (for large n). Expressed using only the highest-order term in the

    expression for the exact running time.

    f(n)=1+n+n^2

    Order of polynomial is the degree of the highest term.

    O(f(n))=O(n^2)

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    23/30

    Asymptotic Notation1) Big-Oh

    2) Omega

    3)Theta

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    24/30

    Big-oh Notation The time complexity of a algorithm can be expressed

    in terms of the order of magnitude of frequency countusing the Big-Oh notation

    When we have only an asymptotic upper bound , we

    use O-notation. O(g(n)) = { f(n) :there exist positive constants c & n0

    such that

    0

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    25/30

    Big-oh Notation

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    26/30

    Theta Notation () The theta notation asymptotically bounds a function

    from above & below. A function f(n) belongs to the set O(g(n)) if there

    exist positive constants c1 & c2 such that it can be

    sandwiched between c1g(n) & c2g(n) , forsufficiently large n.

    (g(n)) = {f(n) : there exist positive constants c1,

    c2,& n0 such that0 c1g(n) f(n) c2g(n) for all n>=n0 }

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    27/30

    Theta Notation

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    28/30

    Omega Notation Omega notation provides an asymptotic lower

    bound. For functiong(n), we define (g(n)),Omega of

    n, as the set:

    (g(n)) = {f(n) :there exist positive constantsc & n0 such that

    0 cg(n) f(n) for all n n0}

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    29/30

    Omega Notation

  • 7/30/2019 Unit 2 - Analysis of Algorithm

    30/30

    Complexity Of Algorithms Measure the time complexity (in terms of

    steps) as a function of the input size. f(n)=12n3+1