CSCI1200 Data Structures — Fall 2010
Lecture 13 — Problem Solving Techniques
Review from Lecture 12
•
Rules for writing recursive functions:
1. Handle the base case(s).
2. Define the problem solution in terms of smaller instances of the problem.
Use
wishful thinking
, i.e., if
someone else solves the problem of
fact(4)
I can extend that solution to solve
fact(5)
. This defines the
necessary recursive calls. It is also the hardest part!
3. Figure out what work needs to be done before making the recursive call(s).
4. Figure out what work needs to be done after the recursive call(s) complete(s) to finish the computation.
(What are you going to do with the result of the recursive call?)
5. Assume the recursive calls work correctly, but make sure they are progressing toward the base case(s)!
•
Merge sort
•
Nonlinear maze search
Today’s Class
•
Today we will discuss how to design and implement algorithms using three steps or stages:
1. Generating and Evaluating Ideas
2. Mapping Ideas into Code
3. Getting the Details Right
13.1
Generating and Evaluating Ideas
•
Most importantly, play with examples! Can you develop a strategy for solving the problem? You should try
any strategy on several examples. Is it possible to map this strategy into an algorithm and then code?
•
Try solving a simpler version of the problem first and either learn from the exercise or generalize the result.
•
Does this problem look like another problem you know how to solve?
•
If someone gave you a partial solution, could you extend this to a complete solution?
•
What if you split the problem in half and solved each half (recursively) separately?
•
Does sorting the data help?
•
Can you split the problem into different cases, and handle each case separately?
•
Can you discover something fundamental about the problem that makes it easier to solve or makes you able to
solve it more efficiently?
•
Once you have an idea that you think will work, you should evaluate it: will it indeed work? are there other
ways to approach it that might be better / faster? if it doesn’t work, why not?
13.2
Mapping Ideas Into Code
•
How are you going to represent the data? What is most efficient and what is easiest?
•
Can you use classes to organize the data?
What data should be stored and manipulated as a unit?
What
information needs to be stored for each object? What operations (beyond simple accessors) might be helpful?
•
How can you divide the problem into units of logic that will become functions? Can you reuse any code you’re
previously written? Will any of the logic you write now be reusable?
•
Are you going to use recursion or iteration? What information do you need to maintain during the loops or
recursive calls and how is it being “carried along”?
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
•
How effective is your solution? Is your solution general? How is the performance? (What is the order notation
of the number of operations)? Can you now think of better ideas or approaches?
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 CUTLER
 Data Structures, main program, Nonlinear Word Search

Click to edit the document details