the efficiency of algorithms chapter 9 slides by steve armstrong letourneau university longview, tx ...

23
The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall

Post on 21-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

The Efficiency of Algorithms

Chapter 9

Slides by Steve ArmstrongLeTourneau University

Longview, TX2007,Prentice Hall

Page 2: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Chapter Contents

• Motivation• Measuring an Algorithm's Efficiency

Big Oh Notation

• Formalities• Picturing Efficiency• The Efficiency of Implementations of the ADT

List An Array-Based Implementation A Linked Implementation Comparing Implementations

Page 3: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Motivation 1

• Even a simple program can be noticeably inefficient

• When the 423 is changed to 100,000,000 there is a significant delay in seeing the result

Page 4: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Motivation

• We seek the design of a Java class Huge to represent extremely large integers.

• Consider an inefficient approach to add numbers that result in a large sum View source code

• Even a simple program can be noticeably inefficient

Page 5: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Measuring Algorithm Efficiency 6

• Types of complexity Space complexity Time complexity

• Analysis of algorithms The measuring of the complexity of an

algorithm

• Cannot compute actual time for an algorithm We usually measure worst-case time

Page 6: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Measuring Algorithm Efficiency 9

Fig. 9-1 Three algorithms for computing 1 + 2 + … n for an integer n > 0

Page 7: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Measuring Algorithm Efficiency

Fig. 9-2 The number of operations required by the algorithms for Fig 9-1

Page 8: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Measuring Algorithm Efficiency 10

Fig. 9-3 The number of operations required by the algorithms in Fig. 9-1 as a function of n

Page 9: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Big Oh Notation 12

• To say "Algorithm A has a worst-case time requirement proportional to n" We say A is O(n) Read "Big Oh of n"

• For the other two algorithms Algorithm B is O(n2) Algorithm C is O(1)

Page 10: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Big Oh Notation 15

Fig. 9-4 Typical growth-rate functions evaluated at increasing values of n

Page 11: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Big Oh Notation 16

Fig. 9-5 The number of digits in an integer n compared with the integer portion of log10n

Page 12: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Big Oh Notation

Fig. 9-6 The values of two logarithmic growth-rate functions for various ranges of n.

Page 13: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Formalities 17

• Formal definition of Big OhAn algorithm's time requirement f(n) is of order at most g(n) f(n) = O(g(n)) For a positive real number c and positive

integer N exist such that

f(n) ≤ c•g(n) for all n ≥ N

Page 14: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Formalities

Fig. 9-7 An illustration of the definition of Big Oh

Page 15: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Formalities 21

• The following identities hold for Big Oh notation:

O(k f(n)) = O(f(n)) O(f(n)) + O(g(n)) = O(f(n) + g(n)) O(f(n)) O(g(n)) = O(f(n) g(n))

Page 16: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Picturing Efficiency 24

Fig. 9-8 an O(n) algorithm.

Page 17: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Picturing Efficiency 25

Fig. 9-9 An O(n2) algorithm.

Page 18: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Picturing Efficiency 27

Fig. 9-10 Another O(n2) algorithm.

Page 19: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Picturing Efficiency

Fig. 9-11 The effect of doubling the problem size on an algorithm's time requirement.

Page 20: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Picturing Efficiency 28

Fig. 9-12 The time to process one million items by algorithms of various orders at the rate of one

million operations per second.

Page 21: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Comments on Efficiency

• A programmer can use O(n2), O(n3) or O(2n) as long as the problem size is small

• At one million operations per second it would take 1 second … For a problem size of 1000 with O(n2) For a problem size of 1000 with O(n3) For a problem size of 20 with O(2n)

Page 22: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Efficiency of Implementations of ADT List 29

• Reference AList from Chapter 5• For array-based implementation

Add to end of list O(1) Add to list at given position O(n)

• Reference LList from Chapter 6• For linked implementation

Add to end of list O(n) Add to list at given position O(n) Retrieving an entry O(n)

Page 23: The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Comparing Implementations 34

Fig. 9-13 The time efficiencies of the ADT list operations for two implementations, expressed in Big Oh notation