# Designing Algorithm to Solve Problems

1. Some of the goals of this course are:

• Teach new programming concepts (like objects)
• Develop problem solving skills that uses the computer.

2. "Problem solving using a computer" means:

• Develop an "algorithm" that will find a solution
• An "algorithm" is an unambigious finite series of operations
• Unambigious means that you must spell out the (ONE) operation that the program will do at any moment
• The finite requirement makes sure that you don't have to wait an eternaty for the solution...

3. An important problem solving technique is top-down design or stepwise refinement:

1. First, Specify the solution using abstract (high level) operations
2. Then, figure out how to perform the abstract operations

 The solution starts out very coarse and will be "stepwise refined".

4. Example problem: find the solution for x3 + 4 x2 + 5 x - 150 = 0 that lies in [0,10]

• x = 0:   03 + 4 x 02 + 5 x 0 - 150 =   -150
• x = 10:   103 + 4 x 102 + 5 x 10 - 150 =   1300
• So there is a value in interval [0,10] for which x3 + 4 x2 + 5 x - 150 = 0
• Problem: find a value (say in 6 demical places) that solves this equation.

5. A solution that uses abstract operations:

 Start with the interval [0, 10] that contains a solution Split the interval into 2 smaller intervals [0, 5) and [5, 10] Pick the interval that contains a solution Repeat this process until the length of the interval < 0.000001

The solution starts out very coarse

6. Refinement 1: cast algorithm using program constructs (if, while, etc) - still with abstract operations...

 Re-writing algorithm with program constructs ``` leftPoint = 0; rightPoint = 10; while ( rightPoint - leftPoint >= 0.000001 ) { if ( solution is in left halft of interval ) { leftPoint = leftPoint; // This statement is unnecessary... rightPoint = (leftPoint + rightPoint)/2; } else { leftPoint = (leftPoint + rightPoint)/2; rightPoint = rightPoint; // Also unnecessary... } } ```

7. Further Refinement: "solution is in left halft of interval"

• What do you need to do to determine which interval contains a solution ?
• Knowing Java or any programming language will NOT help you...
• You have to use your wits.... (math !)

Consider:

 Re-writing algorithm using sign difference to characterize location of solution: ``` leftPoint = 0; rightPoint = 10; while ( rightPoint - leftPoint >= 0.000001 ) { midPoint = (leftPoint + rightPoint)/2; if ( function value of midPoint & leftPoint have different sign ) { // Solution is in left half of interval rightPoint = midPoint; // new rightPoint } else { // Solution is in right half of interval leftPoint = midPoint; // new leftPoint } } ```

8. Further refinement: use your discoveries !

• We discovered that we need the function values to determine if they have different signs.

 Re-writing algorithm with function value computation: ``` leftPoint = 0; rightPoint = 10; while ( rightPoint - leftPoint >= 0.000001 ) { midPoint = (leftPoint + rightPoint)/2; y_midPoint = f(midPoint); y_leftPoint = f(leftPoint); if ( (y_midPoint < 0 && y_leftPoint > 0) || (y_midPoint > 0 && y_leftPoint < 0) ) { // Solution is in left half of interval rightPoint = midPoint; // new rightPoint } else { // Solution is in right half of interval leftPoint = midPoint; // new leftPoint } } ```

Here is a DEMO program: click here
Notice the static method I used to compute function value