The Efficiency of Algorithms

Slides:



Advertisements
Similar presentations
The Efficiency of Algorithms Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
An Introduction to Sorting Chapter 8 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Analysys & Complexity of Algorithms Big Oh Notation.
Introduction to Analysis of Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
The Efficiency of Algorithms
The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
The Efficiency of Algorithms
Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Algorithm Analysis (Big O)
Spring2012 Lecture#10 CSE 246 Data Structures and Algorithms.
Week 2 CS 361: Advanced Data Structures and Algorithms
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Algorithm Efficiency Chapter 10 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
An Introduction to Sorting Chapter 8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with.
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Algorithm Analysis (Big O)
Algorithms Lecture #05 Uzair Ishtiaq. Asymptotic Notation.
Computational complexity The same problem can frequently be solved with different algorithms which differ in efficiency. Computational complexity is a.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
1 ADT Implementation: Recursion, Algorithm Analysis Chapter 10.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
The Efficiency of Algorithms Chapter 9 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights.
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Algorithm Efficiency and Sorting
Analysis of Algorithms
Introduction to Search Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Analysis of Algorithms
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Big-O notation.
Analysis of algorithms
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
An Introduction to Sorting
A Bag Implementation that Links Data
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Algorithm Analysis (not included in any exams!)
The Efficiency of Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Algorithm Efficiency Chapter 10.
CS 201 Fundamental Structures of Computer Science
Analysys & Complexity of Algorithms
Analysis of Algorithms
DS.A.1 Algorithm Analysis Chapter 2 Overview
List Implementations that Use Arrays
A List Implementation That Links Data
Analysis of Algorithms
Chapter 2.
Algorithm Efficiency Chapter 10
The Efficiency of Algorithms
Algorithm Efficiency and Sorting
Analysis of algorithms
The Efficiency of Algorithms
Analysis of Algorithms
A List Implementation That Links Data
Algorithm Efficiency and Sorting
List Implementations that Use Arrays
CMPT 225 Lecture 6 – Review of Complexity Analysis using the Big O notation + Comparing List ADT class implementations.
Presentation transcript:

The Efficiency of Algorithms Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Motivation Measuring an Algorithm’s Efficiency Counting Basic Operations Best, Worst, and Average Cases Big Oh Notation The Complexities of Program Constructs Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Picturing Efficiency The Efficiency of Implementations of the ADT Bag An Array-Based Implementation A Linked Implementation Comparing the Implementations Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Assess efficiency of given algorithm Compare expected execution times of two methods Given efficiencies of algorithms Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Motivation Contrast algorithms Figure 4-1 Three algorithms for computing the sum 1 + 2 + . . . + n for an integer n > 0 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Motivation Java code for algorithms Even a simple program can be inefficient Copyright ©2012 by Pearson Education, Inc. All rights reserved

Measuring an Algorithm’s Efficiency Complexity Space and time requirements Other issues for best solution Generality of algorithm Programming effort Problem size – number of items program will handle Growth-rate function Copyright ©2012 by Pearson Education, Inc. All rights reserved

Counting Basic Operations Figure 4-2 The number of basic operations required by the algorithms in Figure 4-1 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 4-3 The number of basic operations required by the algorithms in Figure 4-1 as a function of n Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved FIGURE 4-4 Typical growth-rate functions evaluated at increasing values of n Copyright ©2012 by Pearson Education, Inc. All rights reserved

Best, Worst, and Average Cases 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 end Average case somewhere between Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Big Oh Notation Notation to describe algorithm complexity Definition 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 or all n ≥ N. That is, is an upper bound on f(n) when n is sufficiently large. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 4-5 An illustration of the definition of Big Oh Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Big Oh Identities O(k g(n)) = O(g(n)) for a constant k O(g1(n)) + O(g2(n)) = O(g1(n) + g2(n)) O(g1(n)) x O(g2(n)) = O(g1(n) x g2(n)) O(g1(n) + g2(n) + . . . + gm(n)) = O(max(g1(n), g2(n), . . ., gm(n)) O(max(g1(n), g2(n), . . ., gm(n)) = max(O(g1(n)), O(g2(n)), . . ., O(gm(n))) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 4-6 An O(n) algorithm Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 4-7 An O(n2) algorithm Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 4-8 Another O(n2) algorithm Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 4-9 The effect of doubling the problem size on an algorithm’s time requirement Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 4-10 The time required to process one million items by algorithms of various orders at the rate of one million operations per second Copyright ©2012 by Pearson Education, Inc. All rights reserved

Array Based Implementation Adding an entry to a bag, an O(1) method, Copyright ©2012 by Pearson Education, Inc. All rights reserved

Array Based Implementation Searching for an entry, O(1) best case, O(n) worst or average case Thus an O(n) method overall Copyright ©2012 by Pearson Education, Inc. All rights reserved

A Linked Implementation Adding an entry to a bag, an O(1) method, Copyright ©2012 by Pearson Education, Inc. All rights reserved

A Linked Implementation Searching a bag for a given entry, O(1) best case, O(n) worst case O(n) overall Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 4-11 The time efficiencies of the ADT bag operations for two implementations, expressed in Big Oh notation Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved End Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved