cs2114 lecture06 algorithmefficiency 2015f

Upload: maryam-davoodi

Post on 22-Feb-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    1/22

    The Efficiency of Algorithms

    Chapter 4

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

    Data Structures and Abstractions with Java, 4e

    Frank Carrano

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    2/22

    !"#$$ &'$()*$$

    +,-#. /01(*)1. #)- 23#14$ 5)36,

    7,'6 8,64

    9:*);2< => #)- ?6*@A#B ?6,C*13 D

    +*$3 )*E3 >*-)*$-#. 2*:3*FB*6 DG

    /H*6.3I()J 8*KH* 1,H*6*-

    5) 1"#$$ #)- 9)"()* L'"M:"* !I,(1*

    >6(N*) ?,6M,)

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    3/22

    !"(14*6 ?,""

    >I(1I !I#:3*6 #6* .,' F,$3 ()3*6*$3*- ()

    6*H(*8()JO

    #P !I Q J$

    BP

    !I D

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    4/22

    Why Efficient Code?

    Computers are faster, have larger

    memories!So why worry about efficient code?

    And how do we measure efficiency?

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    5/22

    Example

    !,)$(-*6 3I* :6,B"*F ,R $'FF()J

    U5VWX/ S@Q +I6** #"J,6(3IF$ R,6 1,F:'M)J 3I* $'F

    Q Y D Y P P P Y ) R,6 #) ()3*J*6 ) Z [

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    6/22

    Example

    Java code for the three algorithms

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    7/22

    What is best?

    An algorithm has both time and space

    constraints that is complexity

    !Time complexity

    !

    Space complexity

    This study is called analysis of

    algorithms

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    8/22

    Counting Basic Operations

    < B#$(1 ,:*6#M,) ,R #) #"J,6(3IF!+I* F,$3 $(J)(\1#)3 1,)36(B'3,6 3, (3$ 3,3#" MF*

    6*]'(6*F*)3

    U5VWX/ S@D +I* )'FB*6 ,R B#$(1 ,:*6#M,)$6*]'(6*- B. 3I* #"J,6(3IF$ () U(J'6* S@Q

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    9/22

    Counting Basic Operations

    FIGURE 4-3 The number of basic operations required by thealgorithms in Figure 4-1 as a function of n

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    10/22

    Counting Basic Operations

    FIGURE 4-4 Typical growth-rate functions evaluatedat increasing values of n

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    11/22

    Best, Worst,

    and Average Cases

    For some algorithms, execution time

    depends only on size of data set

    Other algorithms depend on the natureof the data itself

    !Here we seek to know best case, worst

    case, average case

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    12/22

    Big Oh Notation

    A function f(n) is of order at most g(n)

    That is, f(n) is O(g(n))if

    !A positive real number c and positive

    integer N exist

    !

    Such that f(n) "c xg(n) for all n #N

    !That is, c xg(n) is an upper bound on f(n)

    when n is sufficiently large

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    13/22

    Picturing Efficiency

    FIGURE 4-6 An O(n) algorithm

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    14/22

    Picturing Efficiency

    FIGURE 4-7 An O(n2) algorithm 2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    15/22

    Picturing Efficiency

    FIGURE 4-8 Another O(n2) algorithm 2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    16/22

    Picturing Efficiency

    FIGURE 4-9 The effect of doubling the problemsize on an algorithms time requirement

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    17/22

    Picturing Efficiency

    FIGURE 4-10 The time required to process one million itemsby algorithms of various orders at the rate of one million

    operations per second

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    18/22

    Efficiency of

    Implementations of ADT Bag

    FIGURE 4-11 The time efficiencies of the ADT bagoperations for two implementations, |

    expressed in Big Oh notation

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    19/22

    X*#-()J ^'(_ G

    DP 7,' $I,'"- *E:6*$$ 3I* 1,F:"*E(3. ,R #)#"J,6(3IF () 3*6F$ ,R (3

    #P $:#1* 6*]'(6*F*)3$

    BP

    *E*1'M,) MF*

    1P :6,B"*F $(_*

    -P ,H*6#"" 1,F:"*E(3.

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    20/22

    X*#-()J ^'(_ G

    TP 5R #) #"J,6(3IF 6*]'(6*$ ` B#$(1 ,:*6#M,)$ R,6

    #) #"J,6(3IF 8(3I # :6,B"*F $(_* ,R )a 3I*

    #"J,6(3IF(1 1,F:"*E(3. ($

    #P 9bQc

    BP 9b`c

    1P

    9b)c

    -P 9b`)c

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    21/22

    End

    Chapter 4

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.

  • 7/24/2019 CS2114 Lecture06 AlgorithmEfficiency 2015F

    22/22

    A*13'6* [d e [` ,) 23#14$

    2015 Pearson Education, Inc., UpperSaddle River, NJ. All rights reserved.