22 Problem Solving Mark Dixon Page 1 Session
22 – Problem Solving Mark Dixon Page 1
Session Aims & Objectives • Aims – to provide a more explicit understanding of problem solving skills and strategies • Objectives, by end of this week’s sessions, you should be able to: – recognise the key aspects of a problem • start state • goal state • operations – be able to use typical strategies to solve unfamiliar programming problems Mark Dixon Page 2
Types of problem • There are two types of problem: – Known problems: which we have successfully dealt with before, and can remember the solution – Unknown problems: which we have never seen before, and therefore have to discover / invent a solution for ourselves Mark Dixon Page 3
What is a problem? • All problems are different • However, have key parts: – Start state – Goal state – set of available operations • Problem solving is the process of searching for a sequence of operations that will take us from the start state to the goal state Mark Dixon Page 4
Important steps • It is essential to understand the problem (start state, goal state, and operations) prior to finding a solution • It is also often essential to be able to break a problem down into smaller sub-problems, i. e. identify intermediate sub-goal states • failure to solve a problem is often due to these Mark Dixon Page 5
Example: Light • Start state: light is off • Goal state: light on • Set of operations: – Push switch up (turns light on) – Push switch down (turns light off) • Solution: 1. Push switch up Simple problems involve a small number of operations to solve Mark Dixon Page 6
More Complex Problems • More complicated problems involve – the use of multiple operations to get from the start to the goal state – conditional execution of operations (only do this if…) – repeated operations (do this until…) – abstraction (more general description) Mark Dixon Page 7
Example: First Character • Start state: the piece of text (e. g. "Hello") • Goal state: extract the first character of a piece of text • Set of operations: – Right(s, n): gives n characters from the right of s – Left(s, n): gives n characters from the left of s – Mid(s, p, n): gives n characters from s, starting at position p – Len(s): gives the number of characters in s • Solution: 1. Left(text, 1) Mark Dixon Page 8
Example: Horizontal mid point • Start state: an image on the screen • Goal state: to calculate its horizontal mid point • Set of operations: – pixel. Left: gives the position of its left edge – pixel. Top: gives the position of its top edge – Width: gives the distance between its left and right edges – Height: gives the distance between it top and bottom edges • Solution: 1. get the pixel. Left value 2. add half the Width Mark Dixon Page 9
Concrete vs. Abstract problems • People can solve concrete problems easily – what is the first letter of hello – what is the first letter of you surname – an object's left edge is at position 100 the object is 50 wide where is its mid point? • It is often difficult for them to describe the general (abstract) process they use Mark Dixon Page 10
Example: Movement • Goal state • Start state • Operations – Move Up – Move Down – Move Left – Move Right • Solution: Mark Dixon 1. 2. 3. 4. 5. 6. Move Up or Move Up Move Right 1. 2. 3. 4. 5. 6. Move Up Move Right Page 11
Example: Movement (abstract) • Abstract (general) solution: 1. If current is below goal then Move Up 2. Else. If current is above goal then Move Down 3. Else. If current is left of goal then Move Right 4. Else. If current is right of goal then Move Left 5. Else Stop (current = goal) 6. Go To 1 (loop) Mark Dixon Page 12
Flowcharts • often useful for representing algorithms: Start below goal Y Move Up N above goal Y Move Down N left of goal Y Move Right N right of goal Y Move Left N Stop Mark Dixon Page 13
- Slides: 13