CSC153, Class 54: Algorithm Design Overview: * About ADT and algorithm design * What techniques do we know? * A new/old problem: Book packing * A new technique: Dynamic programming Notes: * I imagine many of you are having a hard time right now, whether or not you knew Mr. Stefanov. I know I am. Is there anything I can do to help? + In case you haven't heard, there's a brief rememberence service of Orthodox Christian prayer today at noon in Herrick. + Early announcement: No final, lowest exam grade dropped. * I can't believe that I forgot to make the joke about class 53 in 153 yesterday. * I need e.c. summaries from Case, Gray, Guha, Kazinka, and Ventresca. * Tomorrow: Evaluate the class. * Friday: Meet at Dari Barn. ---------------------------------------- How do you design abstract data types? Usually, you have a problem you want to solve and you need to organize data for that problem. Pick an overall paradigm/theory/broad definition that guides the development. Identify the key operations that will help you solve that problem. Telling a story may help. Work out the details of those operations. Preconditions, postconditions, when you might throw exceptions, exact meaning. + May require you to think about implementation. Add the other operations the analysis revealed. (E.g., predicates for testing preconditions.) Write algorithms that use the operations to ensure that the operations seem sufficient. ---------------------------------------- How do you implement ADT as data structures? Probably based on an existing data structure or techinque. * Shove everything in to an array * Combination of "nodes" Pick an overall paradigm/broad definition that guides the development. * E.g., Heaps are trees with some special propertys ---------------------------------------- How do you design classes and objects? Tell a story. Nouns are objects, Verbs are methods, Adjectives are possibly interfaces. ---------------------------------------- How do you design an algorithm for a given problem? Start by analyzing the problem carefully to make sure you understand it. Next, try to figure out an appropriate algorithm design strategy and see if you can apply it. Can I solve it recursively? * Can I solve a very simplified version easily? (Base case) * Can I simplify the problem to a related problem? * Does the solution to the simplified problem help me solve this problem? Can I solve it iteratively? * Can I expand a simplified solution to something closer to the real solution? "Break into two halves" (Divide and Conquer) Used in * Binary search * Quick sort * Heaps and other binary trees * Merge sort Guess and Check * Exhaustive search Are there others? Certainly. You'll learn some more in CSC301. Bookshelf Packing Problem You have N linear inches of room There is a collection of books. Each book has a width in inches (whole) and a value. Assume that only certainl value/width pairs exist. Assume that for any extant value/width pair, there are arbirtraily many of that pair. Observation: Only care about one value per width. Goal: Maximize value. How? * Divide and conquer won't work. (Degenerates into taking all the 1-inch books.) * Brian's greedy solution: Pick the book with the highest value/width ratio. Take as many of those as fit. Fill the remainder of the shelf in the same way. + Fails! Why? Consider Book type 1: Width 51, value 52 Book type 2: Width 33, value 32 Shelf of size 100 * David's solution: Find the best value/width ratio for the smallest book. + Consider: Book type 1 (small): Width 2, value 1 Book type 2 (large): Width 50, value 50 New techniques: * Greed: Pick the "best" choice. * Exhaustive search: Try every combination How did we solve Fibonacci efficiently? Build an array of solutions from left to right. To build the next value, look at a combination of previous values. How can we solve shelf packing efficiently? Build an array of solutions from left to right. To build the next value, look at a combination of previous values. How do we use the previous solutions to come up with a new solution? I.e., suppose we know the optimal solution for shelf widths 0 .. K, what is the optimal solution for shelf width K+1? Look at all combinations (but there are a lot). For each book width, W, 0 < W <= K+1, compute V_w + optimal_solution(K+1-W) Take the best of those solutions To compute the best solution for N 1 + 2 + 3 + 4 + 5 + .... + N = O(n^2) Example, shelf of 10 inches, values/widths 3/2, 5/3, 14/9 0: 0 1: 0 2: 3 3: 5 4: 6 (3 + O[2]) or (5 + O[1])