220 likes | 342 Vues
This document explores the concepts of problem decomposition and abstraction, focusing on the question of whether it's easier to solve one large problem or several smaller ones. It highlights the advantages of addressing smaller problems, such as enhanced solvability and validation. Using the example of a tic-tac-toe game, it demonstrates how to break down the problem into manageable parts, emphasizing top-down decomposition and the significance of abstraction through parameters. Ultimately, the text provides insights on determining when to stop decomposing problems to ensure effective solutions.
E N D
Problem Solving • Which one is easier: • Solving one big problem, or • Solving a number of small problems?
Problem Solving • Which one is easier: • Solving one big problem, or • Solving a number of small problems? • What are the advantages of small problems?
Problem Solving • Which one is easier: • Solving one big problem, or • Solving a number of small problems? • What are the advantages of small problems? • Easier to solve • Easier to ensure the solution is correct • In order to have a number of small problems • What do we need to do first?
Common Mistake • Dive into the details • (e.g. at the level of Java instructions) • Do not consider decomposing the problem first • “in English”
Tic-tac-toe We would like to build a tic-tac-toe game that a human can play against another How would you decompose the problem?
Problem Decomposition • Display the board • Make a move • Decide winner • Can these smaller problems be independently • solved? • tested?
Decide Winner Seems to be complicated, what can we do?
Decide Winner • Seems to be complicated, what can we do? • Decompose!
Decide Winner • Seems to be complicated, what can we do? • Decompose! • Check 3 rows • Check 3 columns • Check 2 diagonals • Can they be independently • Solved? • Tested?
Top-down Decomposition • Display the board • Make a move • Decide winner • Check 3 rows • Check 3 columns • Check 2 diagonals
Make a Move Decompose:
Make a Move • Decompose: • Ask for a move • Update the board
Top-down Decomposition • Display the board • Make a move • Ask for a move • Update the board • Decide winner • Check 3 rows • Check 3 columns • Check 2 diagonals Two levels of abstraction
When to stop decomposing a problem? • Until you’re confident you can solve the smaller problems correctly • Different for different people
Make a Move Recall there are two human players Who makes a move?
Make a Move • Do we want • To break it down to • 2 smaller problems, each for a player • One problem that is • flexible for the 2 players • ?
Make a Move • Do we want • To break it down to • 2 smaller problems, each for a player • One problem that is • flexible for the 2 players • Prefer one problem that is flexible for 2 situations • The 2 smaller problems are very similar • Solving 2 similar problems is tedious • Rewriting almost the same instructions
Make a Move • makeAMove(player) • Similar to sqrt(x) • makeAMove can be used with different players • Problem abstraction • via parameters
Summary • Problem decomposition • Top-down • Decompose until you’re confident you can solve the small problems correctly.
Summary • Problem decomposition • Top-down • Decompose until you’re confident you can solve the small problems correctly. • Problem abstraction • Different levels of abstraction in top-down decomposition • More general to more specific • Increase abstraction via parameters • Allow the solution to be reused in different situations • Avoid repeatedly writing almost the same instructions