Designing Algorithm to Solve Problems

  1. Some of the goals of this course are:

  2. "Problem solving using a computer" means:

  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]

  5. A solution that uses abstract operations:

    1. Start with the interval [0, 10] that contains a solution
    2. Split the interval into 2 smaller intervals [0, 5) and [5, 10]
    3. Pick the interval that contains a solution
    4. 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"

    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 !

    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