Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 ADT Implementation:

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,
Chapter 9: Searching, Sorting, and Algorithm Analysis
Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
© 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.
Recursion Gordon College CPS212
The Efficiency of Algorithms
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
The Efficiency of Algorithms
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
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)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Data Structures and Abstractions with Java, 4e Frank Carrano
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Analysis of Algorithm Efficiency Dr. Yingwu Zhu p5-11, p16-29, p43-53, p93-96.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
© 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.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Complexity A decidable problem is computationally solvable. But what resources are needed to solve the problem? –How much time will it require? –How much.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Chapter 10 Algorithm Analysis.  Introduction  Generalizing Running Time  Doing a Timing Analysis  Big-Oh Notation  Analyzing Some Simple Programs.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Algorithm Analysis Part of slides are borrowed from UST.
Foundations of Algorithms, Fourth Edition
Algorithm Analysis (Big O)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
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 ADT Implementation:
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Algorithm Analysis 1.
Algorithm Efficiency and Sorting
Chapter 10 Recursion Instructor: Yuksel / Demirer.
ADT Implementation: Recursion, Algorithm Analysis, and Standard Algorithms Chapter 10 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second.
Chapter 17 Recursion.
Analysis of Algorithms
Algorithm Efficiency Chapter 10.
Chapter 17 Recursion.
Algorithm Efficiency and Sorting
Algorithmic complexity
Presentation transcript:

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation: Recursion, Algorithm Analysis, and Standard Algorithms Chapter 10

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Chapter Contents 10.1 Recursion 10.2 Examples of Recursion: Towers of Hanoi; Parsing 10.3 Implementing Recursion 10.4 Algorithm Efficiency 10.5 Standard Algorithms in C++

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Chapter Objectives Review recursion by looking at examples Show how recursion is implemented using a run-time stack Look at the important topic of algorithm efficiency and how it is measured Describe some of the powerful and useful standard C++ function templates in STL (Optional) Introduce briefly the topic of algorithm verification

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion A function is defined recursively if it has the following two parts An anchor or base case –The function is defined for one or more specific values of the parameter(s) An inductive or recursive case –The function's value for current parameter(s) is defined in terms of previously defined function values and/or parameter(s)

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursive Example Consider a recursive power function double power (double x, unsigned n) { if ( n == 0 ) return 1.0; else return x * power (x, n-1); } Which is the anchor? Which is the inductive or recursive part? How does the anchor keep it from going forever?

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursive Example Note the results of a call –Recursive calls –Resolution of the calls

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved A Bad Use of Recursion Fibonacci numbers 1, 1, 2, 3, 5, 8, 13, 21, 34 f 1 = 1, f 2 = 1 … f n = f n -2 + f n -1 –A recursive function double Fib (unsigned n) { if (n <= 2) return 1; else return Fib (n – 1) + Fib (n – 2); }

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved A Bad Use of Recursion Why is this inefficient? –Note the recursion tree

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Uses of Recursion Binary Search –See source codesource code –Note results of recursive call

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Uses of Recursion Palindrome checker –A palindrome has same value with characters reversed racecar Recursive algorithm for an integer –If numDigiths <= 1 return true –Else check first and last digits num/10 numDigits-1 and num % 10 if they do not match return false –If they match, check more digits Apply algorithm recursively to: num % 10 numDigits-1 and numDigits - 2

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion Example: Towers of Hanoi Recursive algorithm especially appropriate for solution by recursion Task –Move disks from left peg to right peg –When disk moved, must be placed on a peg –Only one disk (top disk on a peg) moved at a time –Larger disk may never be placed on a smaller disk

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion Example: Towers of Hanoi Identify base case: If there is one disk move from A to C Inductive solution for n > 1 disks –Move topmost n – 1 disks from A to B, using C for temporary storage –Move final disk remaining on A to C –Move the n – 1 disk from B to C using A for temporary storage View code for solution, Fig 10.4Fig 10.4

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion Example: Towers of Hanoi Note the graphical steps to the solution

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Implementing Recursion Recall section 7.4, activation record created for function call Activation records placed on run-time stack Recursive calls generate stack of similar activation records

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Implementing Recursion When base case reached and successive calls resolved –Activation records are popped off the stack

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Algorithm Efficiency How do we measure efficiency –Space utilization – amount of memory required –Time required to accomplish the task Time efficiency depends on : –size of input –speed of machine –quality of source code –quality of compiler These vary from one platform to another

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Algorithm Efficiency We can count the number of times instructions are executed –This gives us a measure of efficiency of an algorithm So we measure computing time as: T(n)= computing time of an algorithm for input of size n = number of times the instructions are executed

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Example: Calculating the Mean Task# times executed 1.Initialize the sum to 01 2.Initialize index i to 01 3.While i < n do followingn+1 4. a) Add x[i] to sumn 5. b) Increment i by 1n 6.Return mean = sum/n1 Total 3n + 4

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Computing Time Order of Magnitude As number of inputs increases  T(n) = 3n + 4 grows at a rate proportional to n Thus T(n) has the "order of magnitude" n The computing time of an algorithm on input of size n,  T(n) said to have order of magnitude f(n),  written T(n) is O(f(n)) if … there is some constant C such that  T(n) < C  f(n) for all sufficiently large values of n

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Big Oh Notation Another way of saying this: The complexity of the algorithm is O(f(n)). Example: For the Mean-Calculation Algorithm: T(n) is O(n) Note that constants and multiplicative factors are ignored.

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Big Oh Notation f(n) is usually simple: n, n 2, n 3,... 2n 1, log 2 n n log 2 n log 2 log 2 n Note graph of common computing times

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Big Oh Notation Graphs of common computing times

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Common Computing Time Functions log 2 log 2 nlog 2 nnn log 2 nn2n2 n3n3 2n2n E E E+091.8E E E+186.7E

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Computing in Real Time Suppose each instruction can be done in 1 microsecond For n = 256 inputs how long for various f(n) FunctionTime log 2 log 2 n3 microseconds Log 2 n8 microseconds n.25 milliseconds n log 2 n2 milliseconds n2n2 65 milliseconds n3n3 17 seconds 2n2n 3.7+E64 centuries!!

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved STL's Algorithms STL has a collection of more than 80 generic algorithms. –not member functions of STL's container classes –do not access containers directly. They are stand-alone functions –operate on data by means of iterators. Makes it possible to work with regular C-style arrays as well as containers.

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Example of sort Algorithm Comes in several different forms One uses the < operator to compare elements –Requires that operator<() be defined on the data type being sorted –Example program, Fig 10.7Fig 10.7 The sort algorithm can be used with arrays –See example, Fig. 10.8Fig Sort algorithm can have a third parameter –The name of a boolean function to be used for a less than –See Fig. 10.9Fig. 10.9

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved A Sample of STL Algorithms Designed to operate on a sequence of elements Designate a sequence by using two iterators –One positioned at the first element of the sequence –One positioned after the last element in the sequence Note Table 10-4, Page 583 of text

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Algorithms from Library Contains function templates that operate on sequential containers Intended for use with numeric sequences –Such as valarrays See Table 10-5, page 584

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Using Algorithms Consider the judging of figure skaters –When judges give scores the high and low score are discarded –Then the remaining scores are averaged Given the vector of scores shown below

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Using Algorithms Use min_element algorithms to find and discard minimum score scores.erase(min_element(scores.begin(), scores.end()))); Discard maximum element similarly Now use accumulate() algorithm double avg = accumulate(scores.begin(), scores.end(),0.0)/scores.size();