 # CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.

## Presentation on theme: "CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Today’s Topics More Java Review Let's put the equals method in the Card class (to be used instead of == to actually compare the data stored in the Card objects.)‏ Start Algorithm Analysis (chapter 5)‏

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 equals public boolean equals(Object o)‏ { if (o instanceof Card)‏ { Card c = (Card) o; return ((c.suit == this.suit) && (c.rank == this.rank)); } else return false; }

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 equals Things to make sure you understand: –which Card is which --- One card calls equals (this) and one card is passed in as a parameter (o). –why can I refer to private suit and rank instance variables? –what is the purpose of the if (o instanceof Card) check? –Does it really return true if the cards are equal and false if they are not? How? –Why is an equals method necessary at all --- why not just use something like (c1 == c2) instead of (c1.equals(c2))? –This method overrides the equals method in Object so it had to have had the same signature what's a signature?

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 HW Read handout on Algorithm Analysis and Chapter 5.

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis An algorithm is a specific set of instructions for solving a problem. The amount of time an algorithm takes to finish is often a function of the amount of input –sorting 1 million items vs. sorting 10 items –searching a list of 2 billion items vs. searching a list of 3 items Problem vs. algorithm --- a problem is not the same as an algorithm. Example: Sorting is a problem. An algorithm is a specific recipe for solving a problem. –bubbleSort, insertionSort, etc. are different algorithms for sorting. So, when we're analyzing the running time --- we're analyzing the running time of an algorithm, not a problem.

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis Algorithms are often analyzed for –the amount of time they take to run and/or –the amount of space used while running We also typically use the letter n to represent the input size –e.g. n is the number of items in a list to be searched, using linear search We want it to be a variable since we would like to describe the running time of an algorithm in relation to the size of the input

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis Some common functions (in increasing order) used in analysis are –constant functions (e.g. f(n) = 10 )‏ –logarithmic functions (e.g. f(n) = log(20n) )‏ –log squared (e.g. f(n) = log 2 (7n) )‏ –linear functions (e.g. f(n) = 3n – 9 )‏ –N log N (e.g. f(n) = 2n log n )‏ –quadratic functions (e.g. f(n) = 5n 2 + 3n )‏ –cubic functions (e.g. f(n) = 3n 3 - 17n 2 + (4/7)n )‏ –exponential functions (e.g. f(n) = 5 n )‏ –factorial functions (e.g. f(n) = n! )‏

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis The dominant term is what gives a function it's name among –cubic, quadratic, logarithmic, etc. It's more complex than this, but the dominant term can generally be picked out like: –if you determine a function for the running time of an algorithm to be say f(n) = log 2 n + 4n 3 it's dominant term is 4n 3 so ignoring the constant multiplier, we have n 3 –we say that f(n) is O (n 3 ) (pronounced big-Oh en cubed)‏ An example of when it's a bit harder to determine –f(n) = 3n log(n!) + (n 2 + 3)log n is O(n 2 logn)‏

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis Graphs of functions to get a more intuitive feel for the growth of functions, take a look at the handout with graphs.

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis Let's consider something I think we're all familiar with –Binary search –vs. –Linear search Binary search require a list to be sorted to work, but once it is sorted, is much more efficient than linear search. Reminder of how these work on the board.

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis Binary search is O(log n)‏ Linear search is O(n)‏ Consider how long these algorithms run with some different input sizes –n = 2, 4, 8, 1000, 1000000

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis Let's look at the tables with examples of actual times for certain running times given large inputs shows that the time complexity of an algorithm is much more important than processor speed (for large enough inputs) even though processor speeds are getting faster exponentially

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis Growth rates of functions are different than being able to say one function is less than another –e.g. x 2 + 100 is greater than x 3 for many initial values but as x increases above some value, x 3 will always be bigger The constant being multiplied by the dominant term is generally ignored (except for small amounts of input)‏ Big O notation ignores the constant multipliers of the dominant term and we say a phrase like: –linear search is big Oh en –when we mean that the linear search algorithm's time complexity grows linearly (with respect to n, the number of items in the search space).

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis When examining an algorithm, we usually count how many times a certain operation (or group of operations) is performed. See handout for reasonable choices of what operations we would count in different problems. This will lead us to determining the time complexity of the algorithm. We can consider best-case, worst-case and average-case scenarios.

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis Let's consider 1 problem and 3 ways to solve it (using 3 different algorithms) and we'll analyze the running times of each. The Maximum contiguous subsequence problem: –Given an integer sequence A 1, A 2,..., A N, find (and identify the sequence corresponding to) the maximum value of  j k=i A k. The maximum contiguous subsequence sum is zero if all are negative. Therefore, an empty sequence may be maximum. Example input: { -2, 11, -4, 13, -5, 2 } the answer is 20 and the sequence is { 11, -4, 13 } Another: { 1, -3, 4, -2, -1, 6 } the answer is 7, the sequence is { 4, -2, -1, 6 }

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis The simplest is an exhaustive search (brute force algorithm.)‏ –that is, simply consider every possible subsequence and compute its sum, keep doing this and save the greatest –so, we set the maxSum = 0 (b/c it is at least this big) and we start at the first element and consider ever subsequence that begins with the first element and sum them up... if any has a sum larger than maxSum, save this... –then start at second element and do the same... and so on until start at last element Advantages to this: not complex, easy to understand Disadvantages to this: slow Let's examine the algorithm. –decide what is a good thing to count –count that operation (in terms of the input size)‏

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis Let's consider this inefficient maximum continuous subsequence algorithm from page 171 in our text.

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis The exhaustive search has many unnecessary computations. Notice that  j k=i A k = A j +  j-1 k=i A k That is, if we know the sum of the first j-1 elements, then the sum of the first j elements is found just by adding in the jth element. Knowing that, the problem can be solved more efficiently by the algorithm that we are about to analyze. –We won't need to keep adding to up a sequence from scratch

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Algorithm Analysis The second algorithm that we'll analyze uses the improvement just mentioned and the running time improves (goes down.)‏

Download ppt "CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann."

Similar presentations