Problem Solving Think, Think Image courtesy: http://www.flickr.com/photos/toniblay/52445415/sizes/z/in/photostream//
Four General Strategies • There are many problem solving strategies used by computational thinkers. Four strategies are central and common. • Problem definition • Logical reasoning • Decomposition • Abstraction
Problem Definition • Scientificinvestigationbegins with a hypothesis • Software development begins with a careful problem definition. • The problem definition specifies what task(s) are to be performed by the associated software. • Without such a definition it is impossible to know whether or not the problem has been solved - impossible to say whether a computer application is correct or incorrect.
Problem Definition • Problem definition involves more than just computer scientists. • Anyone who hires a programmer must participate in creating the problem definition. • Aproblem definition is the core of the legal agreement between customer and software developer.
Problem Definition • A problem definition consists primarily of a list of requirements. • Two types of requirements • Functional: specify the particular task(s) the software must perform • Non-functional: define other characteristics and constraints related to the software. • reliability requirements • performance requirements • security requirements • delivery requirements
Functional Requirements Clicking the play button (refer to image) causes the video to begin playing. If the video was just paused, then playing the video resumes at the point of pausing. If the video has yet to start playing, then a new computer window is created and the video begins playing in the window. So long as the video is playing the pause/play button functions as a pause button, displaying a pause button image. • Consider a computer program to play videos (mpgsfor example). There are four buttons to this program: play, pause, increase and decrease volume. How would we define play?
Functional Requirements Clicking the pause button (see image) causes the video to pause at the current play location. If the video has already finished playing, then this button has no effect. Clicking the pause button causes the pause/play button to function as a play button, displaying a play button image. • Consider a computer program to play videos (mpgsfor example). There are four buttons to this program: play, pause, increase and decrease volume. How would we define pause?
Functional Requirements Clicking the increase volume button while the volume is less than the maximum 80 db. level causes the volume to increase by 5 db. Clicking the increase volume button while the volume is at the 80 db. level does nothing. • Consider a computer program to play videos (mpgsfor example). There are four buttons to this program: play, pause, increase and decrease volume. How would we define increase volume?
Functional Requirements Clicking the decrease volume button while the volume is greater than the silent (0 db.) level causes the volume to decrease by 5 db. Clicking the decrease volume button while the volume is at the silent level does nothing. • Consider a computer program to play videos (mpgsfor example). There are four buttons to this program: play, pause, increase and decrease volume. How would we define decrease volume?
Criteria for Functional Requirements • A functional requirement should be • Clear : understood the same way by customers, users, and developers. Often written as propositions. • Consistent: requirements don’t contradict each other. • Complete: all usage scenarios have been defined.
Functional Requirements The Video Player application starts executing automatically whenever the user double-clicks on an associated video file. As this execution begins, the application displays a window containing the control panel, like the image above. This control panel remains until the user chooses to quit the application. If the application is executing, other video file double-clicks are ignored Additional requirements for completeness.
Functional Requirements Anytime that the Video Player application is executing the user may double-click within the region of the video display. Such a double-click causes the application to create a pop-up window with two buttons- QUIT and CONTINUE. If the user clicks the QUIT button, then the application quits executing. If the user clicks the CONTINUE button, then the pop-up window closes and the video continues to play. Additional requirements for completeness.
Completeness : State-Activity Functional requirements may lack completeness when they fail to consider all possible combinations of situations. A state-activity table lists all possible application states against all possible user actions.
Completeness : State-Activity • Some cells of a state-activity table impossible • It is impossible for the user to click play if the application has not begun to execute for example. • When all impossible cells have been identified, every other cell should be defined by one-or-more functional requirement. • What occurs when the video has played to completion and the user clicks the play button? (This is possible, because the user could have clicked the pause button after the video finished, and Requirement V2 specifies that this would cause the control panel to display the play button.) • Perhaps the play button should restart the video; • Perhaps it should terminate the application; • Perhaps the play button should do nothing in the video finished state.
Decomposition : Introduction • An algorithmis a group of instructions (often expressed informally) for performing some task. The task presumably solves a problem. • Problem: How to make chocolate chip cookies • Preheat oven to 350° F (175° C). • Beat in 2 eggs, then stir in vanilla (2t.). Dissolve 1t. of baking soda in hot water and add to mixture. Stir in salt (1/2t.), flour (3c.), chocolate chips (2c.), and chopped walnuts (1c.). • Bake in oven for 10 min. until edges are nicely browned • Problem: How to walk from Morris Hall to the La Crosse Center? • From 16th St. walk straight east on State St. for ten blocks • Continuing, State St. turns to a northeasterly direction just before 5th Ave. Continue in this direction for three blocks after 5th Ave. • Turn left onto 2nd St. and walk south east for just under three blocks. The La Crosse Center is on your right.
Decomposition • Decomposition : identifying the parts of a whole. • Decompositions techniques can be used to solve large problems. • Divide and conquer is the most common decomposition technique for algorithms. • A single problem is divided into sub-problems • Each sub-problem is solved individually • What technique could you use? • The sub-problem solutions can be used to solve the whole
Overview • Making pizza is a “large problem” with “parts” • Make crust • Make and spread sauce… • Some of the parts could be broken into smaller pieces
Example : Outlining • Many authors use outlining as a writing strategy. • List the main ideas • Identify the sub-points for each main idea • Identify details for each sub-point
Divide and Conquer • Example Problem: Write a 200 Page Book about My Life at UW-L • Divide: • Chapter 1: The Search – Which School to Attend? • Chapter 2: The Ordeal – Financial Aid • Chapter 3: The Freshman Year – Choosing a Major! • … • Divide again: • Chapter 1: The Search – Which School to Attend? • Section 1: Mom says “Stay close” • Section 2: Dad says “Anywhere” • Section 3: School Rankings say “UW-L” • Section 4: Friends say “Come with Me” • Section 5: I wonder “Where is the best Football Stadium?” • Divide again: • Chapter 1: The Search – Which School to Attend? • Section 2: Mom says “Stay Close” • Paragraph 1: Mom says “How about living at home and going to a tech college?" • Paragraph 2: Mom cries
Top Down Design • Top down design is a software design strategy that identifies programs that can be expressed as sub-programs. • Top down design is a divide-and-conquer strategy • Example: write a graphics program to draw the following image using only lines, rectangles, and ovals
Top level 1. Draw a top hat and head 2. Draw scarf 3. Draw middle body section 4. Lowest body section & surrounding snow 5. Draw trees in the background
1. Draw a top hat and head 1.1 Draw hat 1.2 Draw purple hat ribbon 1.3 Draw head 1.4 Draw face 2. Draw scarf 2.1 Draw scarf’s knot 2.2 Draw scarf fringes 2.3 Draw main portion of scarf 3. Draw middle body section 3.1 Draw center white body 3.2 Draw three buttons 3.3 Draw arms 4. Lowest body section & surrounding snow 4.1 Draw lowest body section 4.2 Draw surrounding snow 5. Draw trees in the background 5.1 Draw trees to the left of snowman 5.2 Draw trees to the right of snowman next refinement
1. Draw a top hat 1.1 Draw main section of hat 1.2 Draw purple hat ribbon 1.3 Draw head 1.4 Draw face 1.4.1 Draw left eye 1.4.2 Draw right eye 1.4.3 Draw nose 1.4.4 Draw mouth from 3 ovals 1.4.5 Draw pink cheek ovals 2. Draw scarf 2.1 Draw scarf’s knot 3.2.1 Draw top green button (an oval) 2.2 Draw scarf fringes 3.2.2 Draw middle purple button (a square) 2.3 Draw main portion of scarf 3.2.3 Draw botom blue button with snowflake on top 3. Draw middle body section 3.1 Draw center white body 3.2 Draw three buttons 3.3 Draw arms still more refinement Q: When do you stop refining? A: When the sub-tasks are simple enough to code.
Prototype Design • In Top-Down-Design, the final software product does not work until all of the sub-parts work and are glued together. The sub-parts usually don't do anything that the 'user' could see. • People who purchase software often like to see the product and give input as to what it should look like and how it should be designed. • DEFINITIONS: • A prototype is a partially-functional program. • Successive prototyping is a staged design process moving from prototype to ever more capable prototype.
Successive Prototyping prototype 1: A new UW-L home page with no working links prototype 2: A UW-L home page with working links to a second tier of pages prototype 3: A complete set of hyperlinked web pages without interactive forms prototype 4: All prototype 3 functionality plus an online, searchable directory of students and staff prototype 5: All prototype 4 functionality plus a page to permit students and staff to communicate email securely
Decomposition : Other Uses • Consider running a program (Skyrim) on a computer. • Trillions of computations must take place every second. • Can take these computations and break them into pieces • Compute collisions • Play sound(s) • Generate picture(s) • Generate NPC movements
Decomposition : Other Uses • Distribute these computations over multiple cores • Most CPUs have multiple cores • A core is a mini-CPU • Solving one problem by simultaneously running pieces of the solution on multiple cores is known as multi-tasking
Decomposition : Grid Computing • Grid computing uses multiple computers to solve a problem • A large problem is broken into parts • Each part is run by some computer on the web • Each computer reports their solution and is then assigned another part of the problem to work on.
Grid Computing Example • The largest known prime number • 257,885,161-1 • Discovered by the Great Internet Mersenne Prime Search (GIMPS) effort. • Discovered by Curtis Cooper's (from the University of Central Missouri) • This number has 17,425,170 digits. • GIMPS is one of the longest-running grid computing efforts in history. • Approximately 360,000 CPUs peaking at 150 trillion calculations per second
Data Decomposition • Decomposition is also useful when arranging data into parts • Example: Find your exam. • You are in a course with 1000 other students. • You take an exam and would like to see the score. • You go to the teachers office and are handed a stack of exams. • How to find your exam?
Linear Search • Linear Search Solution: • Take the top exam from the stack • See if it has your name on it • If so • you are done • If not • place the exam on a discard stack • repeat step 1 until your transcript is found or the stack is empty.
Binary Search • Binary search algorithm : Assume the stack is sorted • Examine the middle transcript in the stack. • If this exam is yours, you've found it. • Else If your name alphabetically precedes the middle name from Step 1, then discard the middle and all exams after the middle. • Otherwise, if your name alphabetically follows the middle, then discard the middle and all exams before the middle. • Repeat Step 1 using the remaining stack. If there are no more exams remaining then your exam is not present. • How many exams would you expect to look at if there are 1000 exams in the stack? • If your exam is not in the stack? • If your exam is in the stack?
Binary Search Payoff How many ‘looks’ for 500 exams?
Abstraction • A dictionary definition (non CS): • a concept or idea not associated with any specific instance • Abstraction describes data and algorithms at an appropriate level of detail. • Example: I just saw a red Corvette. • Color and model are important and appropriate • license plate number, year, engine displacement were not
Abstraction • Most systems have numerous abstraction layers. Higher levels are more ‘abstract’ while lower levels are more ‘concrete’. • low-level layers expose details of the computer hardware. “Tell the CPU to multiply the contents of memory location 0x0031 with the contents of memory location 0x0032” • high-level layers deal with the logic of the program. “Multiply the cost-per-item with the quantity.
Abstraction • Software systems are extremely complex and rely heavily on abstractions • control structures : describe the sequence of operations performed by a computer • data abstraction : describes how data is stored by a computer • use case diagrams : an abstraction of user interaction
Data Abstraction • In computer science, data is often described by a class diagram. • A class is a “kind” of thing • A “kind” is described by • the attributes of that kind of thing • The attributes are the essential data descriptors • the operations performed by/on that kind of thing
Example : Thermostat • Consider the attributes that all thermostats (like those shown) possess. • Consider the operations that a thermostat either performs or is acted upon setSystemToCool() setSystemToHeat() setSystemToOff() setFanToOn() setFanToAuto() increaseTemp() decreaseTemp() readCurrentTemp() readTempSetting() system setting: (COOL / OFF / HEAT) fan setting : (AUTO / ON) temperature setting : an Integer
Class Diagram • UML is a language for describing classes • A box is a class • The box has either one, two or three divisions (vertically stacked) • The top division contains the class name • The middle division contains the attributes • The lower division contains the operations
Example : Thermostat class name Thermostat systemSetting: (COOL / OFF / HEAT) fanSetting : (AUTO / ON) temperatureSetting : an Integer attributes setSystemToCool() setSystemToHeat() setSystemToOff() setFanToOn() setFanToAuto() increaseTemp() decreaseTemp() readCurrentTemp() readTempSetting() operations
Example : Thermostat • The operations have parenthesis to suggest that the operation accepts some input. • One or more inputs can be given to an operation to control its function • Consider an operation like “setTemperature”. The name and type of the input are included Thermostat systemSetting: (COOL / OFF / HEAT) fanSetting : (AUTO / ON) temperatureSetting : an Integer setSystemToCool() setSystemToHeat() setSystemToOff() setFanToOn() setFanToAuto() increaseTemp() decreaseTemp() readCurrentTemp() readTempSetting() setTemperature( temp : integer )