1 - frequency count

Upload: jelvin-base

Post on 07-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 1 - Frequency Count

    1/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Data Structures and

    Algorithms

    Algorithm Analysis

  • 8/3/2019 1 - Frequency Count

    2/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Outline

    What is an Algorithm?

    Algorithm Formulation

    Algorithm Analysis

    Frequency Count

    Operation Count

  • 8/3/2019 1 - Frequency Count

    3/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    What is an Algorithm?

    An organized sequence or list of clearsteps or operations needed to solve a

    given programming problem 3 components:

    Inputs

    Steps or instructions

    Output

  • 8/3/2019 1 - Frequency Count

    4/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    What is an Algorithm?

    Any well-defined computational procedurethat takes some value(s) as input and

    produces some value(s) as output Sequence of computational steps that

    transform the input to output

  • 8/3/2019 1 - Frequency Count

    5/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    What is an Algorithm?

    A means to solve a problem

    A problem can be solved in many different ways:

    Many algorithms can be used to solve the sameproblem

    We choose between different algorithms basedon their efficiency

    A good metric of efficiency is the computationalcomplexity of an algorithm

  • 8/3/2019 1 - Frequency Count

    6/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Algorithms and Computer Science

    Can a particular task be accomplished by acomputing device?

    What is the minimum number of stepsoroperations for any algorithm to perform acertain function?

    Concerns: effectiveness, efficiency

  • 8/3/2019 1 - Frequency Count

    7/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Algorithm Formulation

    Steps:

    1. Understand the problem

    Determine the given inputs, the desiredoutput, and the steps to reach the output

    2. Design the algorithm

    Write the algorithm that will solve theproblem given the inputs

  • 8/3/2019 1 - Frequency Count

    8/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Algorithm Formulation

    3. Analyze the algorithm

    Determine the resources that the algorithm

    requires memory, bandwidth, hardware,computational time

    4. Refine the algorithm

    5. Verification

  • 8/3/2019 1 - Frequency Count

    9/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Algorithm Analysis

    Study the behavior of the algorithm todetermine its pattern and performance

    Measured in terms ofexecution time (speed)and amount of memory (space) consumed

    Designing efficient algorithms

  • 8/3/2019 1 - Frequency Count

    10/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    How to analyze programs?

    2 phases:

    1. Priori estimates

    Obtain a function which bounds thealgorithms complexitytime

    The amount of time a single execution will

    take, or the number of times a statement isexecuted

    Space consumed may also be estimated

  • 8/3/2019 1 - Frequency Count

    11/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    How to analyze programs

    It is impossible to know the exact amountof time to execute any command unless

    the following are known: The machine for execution

    machine instruction set

    time required by each machine instruction

    translation of the compiler from source tomachine language

  • 8/3/2019 1 - Frequency Count

    12/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    How to analyze programs?

    2. Posteriori estimates

    Study of exact time & space required for

    execution

  • 8/3/2019 1 - Frequency Count

    13/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Frequency Count

    Number of statements or steps neededby the algorithm to finish

    Simple statements:

    a 10 Count: 1

    b a * 2 Count: 1

  • 8/3/2019 1 - Frequency Count

    14/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Frequency Count

    Conditional Statements

    if ()

    ;else

    ;

    Sum of the following:

    1 + Max{ Count(), Count() }

  • 8/3/2019 1 - Frequency Count

    15/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Frequency Count - Example

    if x > 1

    y 10;

    else

    { y 20;

    z 30;}

    Total = 3

    1

    1

    2

    2

  • 8/3/2019 1 - Frequency Count

    16/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Loop Statements

    for i

    to

    Frequency Count

    Example

    for i 1 to nx x + 1

    ub - lb + 2

    ub - lb + 1

    n - 1 + 2 = n+1n

    Total = 2n+1

  • 8/3/2019 1 - Frequency Count

    17/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    if x < 1

    y 10else if x < 2

    {y 20; z 30}

    else{ for i 1 to x

    print(i)}

    Frequency Count - Example1

    1

    Total = 2x+3

    1

    2

    x

    x+12x+1

  • 8/3/2019 1 - Frequency Count

    18/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Frequency Count - Exercise

    1. k 500;for i 1 to k-1

    z z + 1

    2. for k 0 to n

    { print (k);print(n-k);

    }

  • 8/3/2019 1 - Frequency Count

    19/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Nested Statements

    for i 1 to nfor j 1 to n

    x x + 1

    Frequency Count

    n - 1 + 2 = n+1(n+1)(n)

    Total = n+1 + n2+n + n2= 2n2 + 2n + 1

    (n)(n)

    (n) =

  • 8/3/2019 1 - Frequency Count

    20/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    for i 2 to n-1

    for j

    1 to nx x + 1

    Frequency Count - Examplen-1 - 2 + 2 = n-1

    (n+1)(n)

    Total = n-1 + n2-2n+n-2 + n2-2n

    = 2n2 - 2n - 3

    (n-2)(n-2)

    = (n-2)

  • 8/3/2019 1 - Frequency Count

    21/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    for i 1 to n{ x x + 1

    for j

    3 to n+1{ y y + 1z z + 1

    }

    }

    Frequency Count - Example

    Total = n+1 + n + n2 + (n-1)n + (n-1)n

    = 2n+1 + n2 + n2-n + n2-n

    = 3n2 + 1

    = (n)

    = (n-1)

    n+1n

    (n)n(n)(n)

    (n-1)(n-1)

  • 8/3/2019 1 - Frequency Count

    22/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Frequency Count - Exercise

    for i 1 to n

    for j 1 to nfor k 1 to n

    z z + 1

  • 8/3/2019 1 - Frequency Count

    23/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Loop Statementsdo

    while

    Frequency Count

    Ex.x 1 1

    dox x + 1 n-x=n-1

    while x < n n-x=n-1Total = 2n-1

    What if while x

  • 8/3/2019 1 - Frequency Count

    24/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Loop Statementswhile

    Frequency Count

    What if while x

  • 8/3/2019 1 - Frequency Count

    25/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    1. x 1while x

  • 8/3/2019 1 - Frequency Count

    26/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Frequency Count - Exercise

    1. for i 1 to n

    for j

    1 to 2nx x + 1

    2. for k 2 to n+1

    for j 3 to n-3x x + 1

  • 8/3/2019 1 - Frequency Count

    27/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Frequency Count - Exercise3. for i 2 to n+1

    for j 3 to n-3for k 4 to n-4

    x x + 1

    4. for i 1 to n{ j 2

    while j

  • 8/3/2019 1 - Frequency Count

    28/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Frequency Count - Exercise

    5. for i 1 to nfor j 1 to i

    x x + 1

    6. for i 1 to nfor j 2i downto 1

    x x + 1

    7. for i 1 to nfor j i to n

    x x + 1

  • 8/3/2019 1 - Frequency Count

    29/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue, Metro Manila 1004 Philippines

    Frequency Count - Exercise

    8. for i 1 to n-1for j 1 to ix x + 1

    9. for i 4 to nfor j 1 to ix x + 1

    10. for i 1 to nfor j 1 to i

    for k 1 to jx x + 1

  • 8/3/2019 1 - Frequency Count

    30/30

    College of Computer Studies, De La Salle University Manila2401 Taft Avenue Metro Manila 1004 Philippines

    Frequency Count - Exercise

    11. while (i < n) 13. while (i > n){ k = k+1; { k = k+1;

    i = i+1; i = i-1;

    } }

    12. while (i>=n) 14. while (b != n-10){ k= k+1; b= b+1;

    x = x+1;i = i-1; 15. do { h = h-1;

    } } while (h >= n);