Docsity

Data Structures Lecture 3: Order Notation and Recursion - Prof. Charles V. Stewart, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
Data Structures — Lecture 3
Order Notation and Recursion
1 Overview
•The median grade.cpp program from Lecture 2 and background on constructing and
using vectors.
•Algorithm analysis; order notation
•Recursion
The material on the order notation at the end the lecture is covered in Ford&Topp, pages
128-139.
2 Algorithm Analysis: What, Why, How?
•What?
–Analyze code to determine the time required, usually as function of the size of
the data being worked on.
•Why?
–We want to do better than just implementing and testing every idea we have.
–We want to know why one algorithm is better than another.
–We want to know the best we can do. (This is often quite hard.)
•How? There are several possibilities:
1. Don’t do any analysis; just use the first algorithm you can think of that works.
2. Implement and time algorithms to choose the best.
3. Analyze algorithms by counting operations while assigning different weights to
different types of operations based on how long each takes.
4. Analyze algorithms by assuming each operation requires the same amount of
time. Count the total number of operations, and then multiply this count by the
average cost of an operation.
•What happens in practice?
–99% of the time: rough count similar to #4 as a function of the size of the data.
Use order notation to simplify the resulting function and even to simplify the
analysis that leads to the function.
–1% of the time: implement and time.
What follows is a quick review of counting and the order notation.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Data Structures Lecture 3: Order Notation and Recursion - Prof. Charles V. Stewart and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures — Lecture 3

Order Notation and Recursion

1 Overview

  • The median grade.cpp program from Lecture 2 and background on constructing and using vectors.
  • Algorithm analysis; order notation
  • Recursion

The material on the order notation at the end the lecture is covered in Ford&Topp, pages 128-139.

2 Algorithm Analysis: What, Why, How?

  • What?
    • Analyze code to determine the time required, usually as function of the size of the data being worked on.
  • Why?
    • We want to do better than just implementing and testing every idea we have.
    • We want to know why one algorithm is better than another.
    • We want to know the best we can do. (This is often quite hard.)
  • How? There are several possibilities:
    1. Don’t do any analysis; just use the first algorithm you can think of that works.
    2. Implement and time algorithms to choose the best.
    3. Analyze algorithms by counting operations while assigning different weights to different types of operations based on how long each takes.
    4. Analyze algorithms by assuming each operation requires the same amount of time. Count the total number of operations, and then multiply this count by the average cost of an operation.
  • What happens in practice?
    • 99% of the time: rough count similar to #4 as a function of the size of the data. Use order notation to simplify the resulting function and even to simplify the analysis that leads to the function.
    • 1% of the time: implement and time.

What follows is a quick review of counting and the order notation.

2.1 Exercise: Counting Example

Suppose foo is an array of n doubles, initialized with a sequence of values.

  • Here is a simple algorithm to find the sum of the values in the vector:

double sum = 0; for ( int i=0; i<n; ++i ) sum += foo[i];

  • How do you count the total number of operations?
  • Go ahead and try. Come up with a function describing the number of operations.
  • You are likely to come up with different answers. How do we resolve these differences?

2.2 Order Notation

The following discussion emphasizes intuition. That’s all we care about in Data Struc- tures. For more details and more technical depth, see any textbook on data structures and algorithms.

  • Definition

Algorithm A is order f (n) — denoted O(f (n)) — if constants k and n 0 exist such that A requires no more than k × f (n) time units (operations) to solve a problem of size n ≥ n 0.

  • As a result, algorithms requiring 3n + 2, 5n − 3, 14 + 17n operations are all O(n) (i.e. in applying the definition of order notation f (n) = n).
  • Algorithms requiring n^2 /10 + 15n − 3 and 10000 + 35n^2 are all O(n^2 ) (i.e. in applying the definition of order notation f (n) = n^2 ).
  • Intuitively (and importantly), we determine the order by finding the asymptotically dominant term (function of n) and throwing out the leading constant. This term could involve logarithmic or exponential functions of n.
  • Implications for analysis:
    • We do not need to quibble about small differences in the numbers of operations.
    • We also do not need to worry about the different costs of different types of operations.
    • We do not produce an actual time. We just obtain a rough count of the number of operations. This count is used for comparison purposes.
  • In practice, this makes analysis relatively simple, quick and (sometimes unfortunately) rough.

2.6 Best-Case, Average-Case and Worst-Case Analysis

  • For a given fixed size vector, we might want to know:
    • The fewest number of operations (best case) that might occur.
    • The average number of operations (average case) that will occur.
    • The maximum number of operations (worst case) that can occur.
  • The last is the most common. The first is rarely used.
  • On the previous algorithm, the best case is O(1), but the average case and worst case are both O(n).

2.7 Approaching An Analysis Problem

  • Decide the important variable (or variables) that determine the “size” of the problem.
    • For arrays and other “container classes” this will generally be the number of values stored.
  • Decide what to count. The order notation helps us here.
    • If each loop iteration does a fixed (or bounded) amount of work, then we only need to count the number of loop iterations.
    • We might also count specific operations, such as comparisons.
  • Do the count, using order notation to describe the result.

2.8 Examples: Loops

In each case give an order notation estimate as a function of n which here notes the

  • Version A:

int count=0; for ( int i=0; i<n; ++i ) for ( int j=0; j<n; ++j ) ++count;

  • Version B:

int count=0; for ( int i=0; i<n; ++i ) ++count;

for ( int j=0; j<n; ++j ) ++count;

  • Version C:

int count=0; for ( int i=0; i<n; ++i ) for ( int j=i; j<n; ++j ) ++count;

  • How many operations in each?

2.9 More “O” Examples

Solutions to these will be posted on-line:

  1. Write a C++ function to remove the first item in an array of n float values. Give an “O” analysis of this function.
  2. Can you analyze binary search? Assume that the search interval is of size n = 2k^ for some positive integer k.

3 Recursion: The Basics

3.1 Recursive Definitions of Factorials and Integer Exponentiation

  • The factorial is defined for non-negative integers as

n! =

n · (n − 1)! n > 0 1 · n == 0

  • Computing integer powers is defined as:

np^ =

n · np−^1 p > 0 1 · p == 0

  • These are both examples of recursive definitions.

3.2 Recursive C++ Functions

C++, like other modern programming languages, allows functions to call themselves. This gives a direct method of implementing recursive functions.

  • Here’s the implementation of factorial:

int fact( int n ) { if ( n == 0 ) return 1; else { int result = fact( n-1 ); return n * result; } }

3.4 Iteration vs. Recursion

  • Each of the above functions could also have been written using a for loop, i.e. itera- tively.
  • For example, here is an iterative version of factorial:

int ifact( int n ) { int result = 1; for ( int i=1; i<=n; ++i ) result = result * i; return result; }

  • Iterative functions are generally faster than their corresponding recursive functions. Compiler optimizations sometimes (but not always!) can take care of this by auto- matically eliminating the recursion.
  • Sometimes writing recursive functions is more natural than writing iterative functions, however. Most of our examples will be of this sort.

3.5 Exercise

Write an iterative version of intpow.

3.6 Rules for Writing Recursive Functions

Here is an outline of five steps I find useful in writing and debugging recursive functions:

  1. Handle the base case(s) first, at the start of the function.
  2. Define the problem solution in terms of smaller instances of the problem. This defines the necessary recursive calls. It is also the hardest part!
  3. Figure out what work needs to be done before making the recursive call(s).
  4. Figure out what work needs to be done after the recursive call(s) complete(s) to finish the computation.
  5. Assume the recursive calls work correctly, but make sure they are progressing toward the base case(s)!

3.7 Example: Printing the Contents of a Vector

The following example is important for thinking about the mechanisms of recursion.

  • Here is a function for printing the contents of a vector. Actually, it is two functions: driver function, and a true recursive function.

void print_vec( vector& v ) { print_vec( v, 0 ); }

void print_vec( vector& v, unsigned int i ) { if ( i < v.size() ) { cout << i << ": " << v[i] << endl; print_vec( v, i+1 ); } }

  • Exercise: What will this print when called in the following code?

int main() { vector a; a.push_back( 3 ); a.push_back( 5 ); a.push_back( 11 ); a.push_back( 17 ); print_vec( a ); }

  • Note: the idea of a “driver function” that just initializes a recursive function call is quite common.
  • Exercise: How can you change the second print vec function as little as possible to write a recursive function to print the contents of the vector in reverse order?

3.8 Binary Search

  • Suppose you have a vector v, sorted so that

v[0] <= v[1] <= v[2] <= ...

  • Now suppose that you want to find if a particular value x is in the vector somewhere.
  • How can you do this without looking at every value in the vector?
  • The solution is a recursive algorithm called binary search, based on the idea of checking the middle item of the search interval within the vector and then looking either in the lower half or the upper half of the vector, depending on the result of the comparison:
  • Recursion is a way of defining a function and or a structure in terms of simpler instances of itself. While we have seen simple examples of recursion here, ones that are easily replaced by iterative, non-recursive functions, later in the semester when we return to recursion we will see much more sophisticated examples where recursion is not easily removed.

On Thursday we will proceed to C++ classes.