COP 3223H meeting -*- Outline -*- * stepwise refinement for program design ------------------------------------------ STEPWISE REFINEMENT (WIRTH 1971) Want to be able to justify a design top-down: Abstract Program | Refined Program / | \ SubP 1 SubP 2 SubP 3 ------------------------------------------ Note: steps can be refinements to a similar problem or sequential (or other) composition of subproblems (SubP 1-3) Q: How can it be helpful to have a harder problem to solve? The refinement isn't necessarily harder, but it has fewer implementations, which means we can see refinements as design choices ** Example: 8 queens ------------------------------------------ EXAMPLE: THE 8 QUEENS PROBLEM Place 8 queens on a (8x8) chessboard so that none of them attacks any other. [--|--|--|--|--|--|--|--] | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | [--|--|--|--|--|--|--|--] ------------------------------------------ Q: How do queens attack in chess? Along a row, column, or diagonal Q: So what does a solution look like? Only one queen on each row, column, and diagonal *** example development motto: think abstractly ------------------------------------------ START OF THE DEVELOPMENT Start with Example: Let A be the set of all possible placements of 8 queens on the chessboard Let p(b) be true when x is a solution Generate ------------------------------------------ ... an abstract program that is obviously correct ... b in A until p(b) or no more solutions in A if p(b) then return b Q: How would you generate all possible placements? nested loops? Q: Why is that correct? because we assume that p does the right thing. Q: What could be wrong with that? There are too many board positions, about 2**32 of them At 100 microseconds per board, takes about 7 hours to solve this *** refinements ------------------------------------------ HOW CAN WE DO BETTER? Pre-selection strategy: eliminate Abstractly: Criteria for pre-selection: 1. 2. 3. ------------------------------------------ ... obvious non-solutions ... express p(b) as: q(b) and r(b) so let B_r be: b in A such that r(b) B_r is a subset of A because B_r == {b | b in A and r(b)} \subseteq A ... 1. B_r is much smaller than A 2. B_r is easy/fast to generate 3. q(b) is easier/faster to test than p(b) ------------------------------------------ SECOND STEP, USING PRE-SELECTION generate b in B_r until q(b) or B_r is empty if q(b), then return b Why is this better? ------------------------------------------ It will be easier or faster because of the properties of B_r and r. ------------------------------------------ APPLYING PRE-SELECTION TO 8 QUEENS What could r be? Then what would q be? ------------------------------------------ ... r(b) could be: b contains exactly 1 queen in each column (or could be per row, say) ... q(b) would be: b contains at most 1 queen in each row and diagonal Q: So what does the program look like now? let b be a placement of 8 the queens in columns check q(b) // the rows and diagonals if q(b) then return b Q: How could we continue this? keep going until we have code refining data and program in parallel ** Sudoku ------------------------------------------ FOR YOU TO DO Design a program to generate Sudoku puzzles. A Sudoku puzzle is a 9 x 9 grid, consisting of 9 3x3 subgrids. A solution to a puzzle must place in each subgrid, the digits 1-9, and each digit may only appear once per subgrid and once in each column, and once in each row. The puzzle should have some empty grid squares and some numbers filled in. The solver fills in the empty squares. The puzzle should be generated to have exactly one solution (i.e., one way to fill in the values for the empty squares). ------------------------------------------