/
The Efficiency of  Algorithms The Efficiency of  Algorithms

The Efficiency of Algorithms - PowerPoint Presentation

tatiana-dople
tatiana-dople . @tatiana-dople
Follow
354 views
Uploaded On 2019-03-16

The Efficiency of Algorithms - PPT Presentation

Chapter 4 Adapted from Pearson Education Inc 1 Contents Adapted from Pearson Education Inc Motivation Measuring an Algorithms Efficiency Counting Basic Operations Best Worst and Average Cases ID: 756795

pearson adapted max education adapted pearson education max case time size bag worst implementation 3n2 entry array doubling algorithms

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "The Efficiency of Algorithms" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

The Efficiency of Algorithms

Chapter 4

Adapted from Pearson Education, Inc.

1Slide2

Contents

Adapted from Pearson Education, Inc.

MotivationMeasuring an Algorithm’s Efficiency

Counting Basic Operations

Best, Worst, and Average Cases

Big Oh Notation

The Complexities of Program ConstructsPicturing EfficiencyThe Efficiency of Implementations of the ADT BagAn Array-Based ImplementationA Linked ImplementationComparing the Implementations

2Slide3

Objectives

Adapted from Pearson Education, Inc.

3

Assess efficiency of given algorithm

Compare expected execution times of two methods

Given efficiencies of algorithmsSlide4

Motivation - Even a simple program can be

inefficient

Adapted from Pearson Education, Inc.

4

Three

algorithms for

computing ∑ i

Trace each with n=5 and time yourself

Trace each with n=10 and time yourself

What do you notice after doubling n?

When

doubling n

The

time

remained

fixed

When

doubling n

The time was quadrupled

When

doubling n

The

time

was

doubledSlide5

Measuring an Algorithm’s Efficiency

Adapted from Pearson Education, Inc.

5

Complexity

Space requirements

Time requirements

Other issues for best solutionGenerality of algorithmProgramming effortProblem size – number of items program will handleGrowth-rate functionSlide6

Counting Basic Operations

Adapted from Pearson Education, Inc.

6

Independent

of n

Quadratically

related to n

Linearly

related to nSlide7

Counting Basic Operations

Adapted from Pearson Education, Inc.

7

Independent

of n

Quadratically

related to n

Linearly

related to nSlide8

(a) Typical growth-rate functions – (b) Effect of doubling the size of

n – (c) An example

Adapted from Pearson Education, Inc.

8

(a)

(b)

(c)

time to

process

10

6

items @

1

MIPSSlide9

Best, Worst, and Average Cases

Adapted from Pearson Education, Inc.

9

Some algorithms depend only on size of data set

Other algorithms depend on nature of the data

Best case search when item at beginning

Worst case when item at endAverage case somewhere betweenSlide10

Big Oh Notation

Adapted from Pearson Education, Inc.

10

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 ∙ g(n)

for all n ≥ N. That is, c ∙ g(n) is an upper bound on f(n) when

n is sufficiently large.

We can say f(n) = (n

2

+n)/2

is of order

g(n) = n

2

f(n) has a Big Oh of

n

2

O(n

2

)

This means,

w

ith c=1 and N=1

(n

2

+n

)/

2 ≤ 1∙

n

2

for all n ≥ 1Slide11

Big Oh Identities

O(

k g(n)

) = O(

g(n)

) for a constant

kO(g1(n)) + O(g

2

(n)

) = O(

g

1

(n)

+

g

2

(n))O(g1(n)) x O(g

2(n)) = O(g1(n) x g2(n))O (

g1(n) + g2(n) + . . . + gm(n) ) =

O ( max[

g

1

(n)

,

g

2

(n)

, . . .,

g

m

(n)

] ) =

max [ O(

g

1

(n)

), O(

g

2

(n)

), . . . , O(

g

m

(n)

) ]

Adapted from Pearson Education, Inc.

11Slide12

Big Oh Identities

Adapted from Pearson Education, Inc.

12

O(

k

g(n)) = O(g(n)) for a constant

k

O(

g

1

(n)

) + O(

g

2

(n)

) = O(g1(n)

+ g2(n))O(g1(n)) x O(g

2(n)) = O( g1(n) x g2(n) )

O (

g

1

(n)

+

g

2

(n)

+ . . . +

g

m

(n)

) =

O (max[

g

1

(n)

,

g

2

(n)

, . . .,

g

m

(n)

] ) =

max[O(

g

1

(n)

), O(

g

2

(n)

), . . . , O(

g

m

(n)

) ]

O(

3

g(n)

) = O(

g(n)

)

O(

n

)

+ O(3n2) = O(n + 3n2)O(n) x O(3n2) = O(n x 3n2)O (n + 3n2 + 5logn+n ) = O ( max[ n, 3n2, 5logn+n ] ) = max [ O(n), O(3n2), O(5logn+n) ]

= O(n+n2)= O( max(n, n2)) = O(n2)

= O(3n3)= O(n3)

= O(2n+3n

3

+5logn)

= max(O(n),O(n

3

),O(

logn

))

= O(n

3

)Slide13

Figure 4-6 An O(n) algorithm

Adapted from Pearson Education, Inc.

13Slide14

Figure 4-7 An O(n2

) algorithm

Adapted from Pearson Education, Inc.

14Slide15

Figure 4-8 Another O(n2

) algorithm

Adapted from Pearson Education, Inc.

15Slide16

Complexities of program constructs

Construct

Time Complexity

Adapted from Pearson Education, Inc.

16

Consecutive segments

If that choses between if-part and else-partA loop that iterates m times

Add growth-rates

 max

O(

cond

) +

max(O(if-part),O(else-part))

m * (O(

cond

) + O(body)) Slide17

Array Based Implementation (fixed size)

Adapted from Pearson Education, Inc.

17

Adding an entry to a bag, an O(1) method, Slide18

Array Based Implementation (fixed size)

Adapted from Pearson Education, Inc.

18

Adding an entry to a bag, an O(1) method,

O(1)

O(1)

O(1)

Max(O(1),O(1)) = O(1)

O(1)

+O(1)

=

O(1)

O(1)

O(1)

O(1

) +

O(1)

+O(1)

=

O(1)Slide19

Array Based Implementation

Adapted from Pearson Education, Inc.

19

Searching for an entry, O(1) best case,

O(n) worst or average case

O(n) method overallSlide20

Array Based Implementation

Adapted from Pearson Education, Inc.

20

Searching for an entry, O(1) best case,

O(n) worst or average case

O(n) method overall

O(1)

O(1)

O(1)

+O(1)

=O(1)

O(1)

n * (O(1

) + O(1

))

= n * O(1

)

= O(n

)

The size of the bag, is the size of the problem

numberOfEntries

is our

n

This loop executes

numberOfEntries

times, i.e.

n

times

O(1)

O(1)

O(1

) +O(n) +O(1)

=

O(n)Slide21

A Linked Implementation

Adapted from Pearson Education, Inc.

21

Adding an entry to a bag, an O(1) method,

O(1)

+O(1)

+O(1

)

+O(1)

=

O(1)Slide22

A Linked Implementation

Adapted from Pearson Education, Inc.

22

Searching a bag for a given entry, O(1) best case, O(n) worst case

O(n) overallSlide23

A Linked Implementation

Adapted from Pearson Education, Inc.

23

Searching a bag for a given entry, O(1) best case, O(n) worst case

O(n) overall

O(1)

O(1)

n * (O(1)+O(1))

= n * O(1

)

= O(n

)

O(1)

O(1)

The size of the bag, is the size of the problem

This

loop executes

n

times

O(1

) +O(n) +O(1)

=

O(n)Slide24

Time efficiencies of the ADT bag operations

Adapted from Pearson Education, Inc.

24Slide25

End

Chapter 4

Adapted from Pearson Education, Inc.

25