Todays topics Designing and Implementing Algorithms Problem solving
Today’s topics Designing and Implementing Algorithms Problem solving Pseudocode Java Syntax and Grammars Upcoming More Java Acknowledgement Marti Hearst, UC Berkeley David Smith, Georgia tech Reading Computer Science, Chapter 5 Great Ideas, Chapter 2 Comp. Sci 001 4. 1
Problem Solving Programming is a strenuous exercise in problem solving G. Polya. How to Solve It, 2 nd ed. , Princeton University Press, 1957. l Understand the problem ä What are its parts? unknown, data, condition ä Does the problem make sense? Is it feasible? ä Think about the problem, get a sense of what it needs l Make a plan ä Find the connection between givens and result ä What kind of problem is it? Is it familiar? ä Think about generalizations, specializations, variants l Carry out the plan ä Check each step l Examine the result ä Does it make sense? Comp. Sci 001 4. 2
Back of the envelope calculations Jon Bentley. Programming Pearls. 2 nd edition. A-W, 2000. http: //www. vendian. org/envelope/ l Engineering technique to approximate and check answers ä Two answers are better than one ä Quick checks ä Rules of thumb ä Practice l Ad claims that salesperson drove 100, 000 miles in a year. True? l Newspaper article states that a United States quarter dollar coin has “an average life of 30 years. ” How can you check that claim? Comp. Sci 001 4. 3
Why “back of the envelope” estimates? l l Often need to make rapid estimates ä to eliminate candidate solutions ä establish feasibility ä sketch out potential trade-offs Most remember key numbers related to their field, not every detail Hence we need to estimate ä which numbers are important ä values of numbers needed ä how to perform the calculation Emphasis is on “order of magnitude” estimates ä to nearest factor of 10 (or 2) Comp. Sci 001 4. 4
Orders of Magnitude l l l How far away is home? Is it more like 1, or 100 miles? ä Probably do not know exactly ä Is it approximately "a couple", or "a few", or "a lot” ä Estimate based on powers rather than multiples of 10 How tall is your dorm? More like 1, 100, 1000 feet? ä 1 foot tall is like a doll house, so that’s out ä What do we know that is about 10 feet big? Hmm. . . People ä If building is a couple of people high, 10 sounds good. ä But that means 1000, would be 100 people high, so that’s out ä So 10 or 100 depending on how many people tall the building is Use orders of magnitude as brackets to find reasonable range Comp. Sci 001 4. 5
Example: How many piano tuners in NYC l l l Approximately how many people are in New York City? ä 10, 000 Does every individual own a piano? ä No Reasonable to assert “individuals do not own pianos; families do”? ä Yes About how many families are there in a city of 10 million people? ä Perhaps there are 2, 000 families Does every family own a piano? ä No Perhaps one out of every five does ä That would mean there about 400, 000 pianos in NYC Comp. Sci 001 4. 6
Example: Piano Tuners continued l l l How many piano tuners are needed for 400, 000 pianos? ä Some people never get around to tuning their piano ä Some people tune their piano every month ä Assume "on the average" every piano gets tuned once a year, then there are 400, 000 every year How many piano tunings can one piano tuner do? ä Assume that average piano tuner can tune four pianos a day ä Assume that there are 200 working days per year ä That means every tuner can tune about 800 pianos per year How many piano tuners are needed in NYC? ä Number of tuners is approximately 400, 000/800 or 500 Comp. Sci 001 4. 7
Example: Piano Tuners summary l l “Back of the Envelope” estimates have ä Formulas: provide roadmap to upcoming calculations ä Estimates: brief justification of approximations in formula ä Calculations: estimates and known facts are use in formula Piano Tuner example ä Formula: # tuners = # pianos x # repairs / # repairs per day x # days ä Estimates # pianos ~= 400, 000 (20% of 2, 000 families own pianos) # repairs ~= 1 per piano (some many, some none) # repairs per day ~= 4 #working days ~= 200 (5 x 50 – vacation, sickness) ä Calculation # tuners ~= (400, 000 x 1) / (4 x 200) = 500 Comp. Sci 001 4. 8
Estimation General Principles l l l Recall Einstein's famous advice ä Everything should be made as simple as possible, but no simpler Do not worry about constant factors of 2, π, etc. ä Round to “easy” number or nearest order of magnitude Guess numbers you do not know ä Within bounds of common sense (accuracy increases with experience) Adjust geometry, etc. , to suit you ä Assume a cow is spherical if it helps Extrapolate from what you do know ä Use ratios to assume unknown value is similar to known quantity Apply a ‘plausibility’ filter ä If answer seems unbelievable, it probably is ä Can usually set range of reasonable values that indicates major mistake (e. g. , speed cannot be faster than light!) Comp. Sci 001 4. 9
Central role of algorithms in CS Comp. Sci 001 4. 10
What’s wrong with this algorithm? (From back of shampoo bottle) Directions: Wet Hair Apply a small amount of shampoo Lather Rinse Repeat Comp. Sci 001 4. 11
Properties of good algorithms l l Good algorithms must be ä Correct ä Complete ä Precise ä Unambiguous And should be ä Efficient ä Simple ä Contain levels of abstraction Comp. Sci 001 4. 12
Algorithms l Hand-waving not allowed! l Specifying algorithms requires you to say what is really involved in making it work. l Example: ä How does a computer work? ä Hand-wave: zeros & ones ä Real answer: see later part of class. You learn to know when you don’t know ä “I know nothing except the fact of my ignorance. ” l ä Socrates, from Diogenes Laertius, Lives of Eminent Philosophers Comp. Sci 001 4. 13
Describing Algorithms Pictures More easily expressed Natural language (English) More precise Pseudo-code Specific high-level programming language Comp. Sci 001 4. 14
Pseudocode l l l A shorthand for specifying algorithms Leaves out the implementation details Leaves in the essence of the algorithm < l l What does this algorithm do? How many times does it print Hello? Comp. Sci 001 4. 15
Sequential search Comp. Sci 001 4. 16
Picking courses 1. 2. 3. 4. Make a list of courses you want to register for, in order of priority Start with empty schedule. Number of courses = 0. Choose highest priority class on list. If the chosen class is not full and its class time does not conflict with classes already scheduled, then register for the class (2 steps): 1. Add the class to the schedule 2. Increment the number of classes scheduled 5. Cross that class off of your list. 6. Repeat steps 3 through 5 until the number of classes scheduled is >= 4, or until all classes have been crossed out. 7. Stop. Comp. Sci 001 4. 17
Flowcharts Begin Make list of classes you want to take Num Classes = 0 Choose highest priority class on list yes Is this class full? no Is there a time conflict? no Add the class to your schedule. Increment Num Classes. Cross the class off your list. yes End Comp. Sci 001 Num Classes >= 4? no More classes on list? no 4. 18
Programming Primitive Operations l l l l Assign a value to a variable Call a method Arithmetic operation Comparing two numbers Indexing into an array Following an object reference Returning from a method Comp. Sci 001 4. 19
Components of Computing Algorithms Any computing algorithm will have AT MOST five kinds of components: • Data structures to hold data • Instructions change data values • Conditional expressions to make decisions • Control structures to act on decisions • Modules to make the algorithm manageable by abstraction, i. e. , grouping related components Comp. Sci 001 4. 20
Java! l l l Java is a buzzword-enabled language From Sun (the developers of Java), “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. ” What do all of those terms mean? Comp. Sci 001 4. 21
“Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. ” l A programming language ä A vocabulary and set of syntactical (grammatical) rules for instructing a computer to perform specific tasks ä You can do most anything in any programming language ä A particular language encourages one to do things in a certain way l A Question for the course: What makes a good language? Comp. Sci 001 4. 22
“Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. ” l Based on popular languages called C and C++ C: old, pretty bare bones language C++: newer, more complicated language Start from C and add some of C++’s more useful features ä From Gosling, the creator, “Java omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefits. ” l Question: Is Java really all that simple? l l l Comp. Sci 001 4. 23
“Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. ” l The object-oriented paradigm ä Problems and their solutions are packaged in terms of classes ä The information in a class is the data ä The functionality in a class is the method ä A class provides the framework for building objects l Object-oriented programming (OOP) allows pieces of programs to be used in other contexts more easily Comp. Sci 001 4. 24
“Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. ” l A distributed system is one where multiple separate computer systems are involved ä Electronic card catalogs ä The web l Java was designed for the web Question: What are examples of a distributed task in your lives? l Comp. Sci 001 4. 25
“Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. ” l l l Java a high-level language High-level languages must be translated to a computer’s native tongue, machine language Interpreted high-level languages are translated to an intermediate form and then converted to machine language and run Why? We’ll learn more about this later Comp. Sci 001 4. 26
“Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. ” l l l Programs will have errors, but a good program degrades reasonably A robust program may not do exactly what it is supposed to do, but it should not bring down other unrelated programs down with it Question: Give me an example of a non-robust program you have seen? Comp. Sci 001 4. 27
“Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. ” l l l Security: techniques that ensure that data stored on a computer cannot be read or compromised A program is running on your computer. What is to stop it from erasing all of your data, accidentally or otherwise? Question: Is Java really all that secure? Comp. Sci 001 4. 28
“Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. ” l l A language is architecture-neutral if it does not prefer a particular type of computer architectures E. g. The Macintosh processor family (Power. PC) and the PC (x 86 -Pentium) family have their own respective strengths and weaknesses. It is not too hard to construct a program that will run faster on one than an other. A particular program is never entirely architecture neutral though Question: When is being architecturally neutral a bad thing? Comp. Sci 001 4. 29
“Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. ” l l l A program is portable if it will work the same (roughly) on many different computer systems HTML is also platform-independent or portable A whole lot of effort is currently spent porting non-portable code Comp. Sci 001 4. 30
“Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. ” l l Performance: speed in completing some task Performance is everything to most computer and software manufacturers. Story: ä If the transportation industry kept up with the computer industry, one would be able to now buy a Roll Royce that could drive across country in 5 minutes for $35. Rebuttal: ä It would crash once a week, killing everyone on board. Comp. Sci 001 4. 31
“Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. ” l l l A thread is a part of the program that can operate independently of its other parts Multi-threaded programs can do multiple things at once ä e. g. download a file from the web while still looking at other web pages Question: What is the problem with multiple agents working at the same time? ä Synchronization Comp. Sci 001 4. 32
“Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. ” l Dynamic refers to actions that take place at the moment they are needed rather than in advance ä Antonym: static A dynamic program can ä Ask for more or less resources as it runs ä Use the most recent version of some code that is available l Question: Why is being dynamic a good thing? l Comp. Sci 001 4. 33
A Java Program import java. awt. *; public class Hello. World extends java. applet. Applet { Text. Field m 1; public void init() { m 1 = new Text. Field(60); m 1. Set. Text(“Hello World”); add(m 1); } } Comp. Sci 001 4. 34
Definitions l l l Algorithm: ordered set of unambiguous executable steps, defining a terminating process Program: instructions executed by a computer Applet: Java program that is executed in a program such as appletviewer or a Java-enabled web browser Class: family of components sharing common characteristics consisting of: ä Data: information ä Method: functionality Object: instance of a class Variable: represent value stored in computer memory. A variable must be defined or declared before being used ä Sometimes synonymous with object Comp. Sci 001 4. 35
Grammar English and other natural languages have structure <S> => <NOUN-PHRASE> <VERB-PHRASE> => <NOUN> | <ARTICLE> <NOUN> | <PP> <VERB-PHRASE> => <VERB> | <VERB> <NOUN-PHRASE> <NOUN> => DOG | FLEAS | PERSON |. . . <VERB> => RAN | BIT |. . . l Process of taking sentence and fitting it to grammar is called parsing DOG BIT PERSON <NOUN> <VERB> <NOUN-PHRASE> <VERB-PHRASE> <S> l Parsing English is complex because of context dependence l Comp. Sci 001 4. 36
Formal specifications l l l Need a precise notation of syntax of a language Grammars can be used for generation and also can be used Context-free grammars <name> => sequence of letters and/or digits that begins with a letter <name> => guess. B <name> => msg 42 l Substitute as many times as necessary. All legal statements can be generated this way ä Want person = firstn + " " + lastn; ä How do we get this from our grammar? Comp. Sci 001 4. 37
Random Sentence Generator l l l Constructs sentences, paragraphs, and even papers that fit a prescribed format. The format is specified by a set of rules called a grammar A grammar consists of a set of definitions Each definition is a set of productions Examples of grammars ä Extension request ä College rejection ä Poem ä http: //www. cs. duke. edu/courses/fall 05/cps 001/class/03_Grammars/ Natual languages have grammars <S> => <NP> <VP> l Comp. Sci 001 4. 38
Poem Grammar l All grammars begin with start rule { <start> The <object> <verb> tonight. ; } l Nonterminals are indicated by angle brackets { <object> waves ; big yellow flowers ; slugs ; } Comp. Sci 001 4. 39
More on the poem grammar l Nonterminals can refer to other nonterminals { <verb> sigh <adverb> ; portend like <object> ; } { <adverb> warily ; grumpily and <adverb> ; } Comp. Sci 001 4. 40
Generating a poem all sentences start with <start> ä There is only one production in the definition of <start> The <object> <verb> tonight. ä ä ä Expand each grammar element from left to right "The" is a terminal, so it is simply printed – <object> is a non-terminal, so it must be expanded Choose one: • waves • big yellow flowers • slugs ä Suppose that 'slugs' is chosen Comp. Sci 001 4. 41
Generating a poem The slugs <verb> tonight. ä -<verb> is a non-terminal, so it must be expanded – ä Choose one: 1. sigh <adverb> 2. portend like <object> The slugs sigh <adverb> tonight. ä <adverb> is a non-terminal, so it must be expanded 1. warily 2. grumpily The slugs sigh grumpily tonight. ä "tonight. " is a non-terminal so it is simply printed ä There are no more non-terminals to expand! ä The grammar has generated a complete poem Comp. Sci 001 4. 42
Game l 10 coins ä ä ä You and a friend have a stack of 10 coins On each person’s turn, they remove either 1 or 2 coins from the stack The person who removes the last coin wins. Can you win? 10 coins with a twist ä 10 coins, can now ALSO place 1 or 2 coins back on the stack ä Person who removes last coin wins ä l ä Should you go first or second, and what’s your strategy Comp. Sci 001 4. 43
- Slides: 43