Sonoma State University
 
Algorithm Analysis
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College

Although CS 415 has been well developed for several years, last year the CS faculty made a significant, long-term curricular change regarding SSU's Upper Division GE Area B Requirement.

Assignment on Approximation Methods

Approximation Algorithms for the Traveling Salesperson Problem

  1. Pages 438-440 of the textbook describe how to use a Branch-and-Bound algorithm to find an approximate solution to the Traveling Salesperson Problem, and pages 444-448 describe a "Twice-around-the-tree" algorithm that uses a different approach to approximate a solution.

    1. Graph A
      Graph A
      • For Graph A, consider both the Branch-and-Bound Algorithm and the "Twice-around-the-Tree" Algorithm. In each case,
        • If the algorithm can be applied to the graph, use that algorithm to approximate a solution to the Traveling Salesperson Problem.
        • if the graph does not meet the required assumptions for the algorithm, indicate what condition(s) is(are) violated, rather than applying the algorithm.
    2. Graph B
      Graph B
      • For Graph B, consider the "Twice-around-the-Tree" Algorithm. (In the interests of time, DO NOT use the Branch-and-Bound Algorithm for this graph!)
        • If this algorithm can be applied to the graph, use the algorithm to approximate a solution to the Traveling Salesperson Problem.
        • if this graph does not meet the required assumptions for the algorithm, indicate what condition(s) is(are) violated, rather than applying the algorithm.

Approximation Algorithms for Roots of a Function

Suppose we are given a continuous function f, and we want to approximate a value r where f(r)=0. (Recall from mathematics: r is called a root of the function f.)

This section considers two approximation methods for finding the roots of functions, the Bisection Method and Newton's Method.

Approximating Square Roots and Cube Roots

You can use either the Bisection Method or Newton's Method to find square roots and cube roots of numbers. For example, to find the cube root of n, solve the equation h(x) = 0 where h(x) = x3 - n.

The Bisection Method

  1. This problem and Problems 4 and 6 are based on the Reading on the Bisection Method.

    1. Write a program that uses the Bisection Method to approximate a root of a function.

      • The start of the program should define a function
                      double f (double x)
                    

        for which the program is to find a root.

      • The program should define a new function
        double find_root (double a, double b, double accuracy)
                    

        which works as follows:

        • find_root should use an assert statement to check that f(a) and f(b) have different signs. (The program should terminate if this assumption is not met.)
        • Another assert statement at the beginning of find_root should check that the accuracy is positive.
        • find_root should continue to cut the interval in half, following the Bisection Method, until either f(m)=0 for a computed midpoint m or the newly computed interval is shorter than the accuracy specified.
        • find_root should return the computed midpoint of the final interval.
      • The main program should ask the user to enter endpoints a and b and the accuracy. The main program then should call find_roots with the appropriate parameters and report the result.
    2. Find the cube root of 15 using the Bisection Method.
      • Since one may not know what to expect regarding the value of this cube root, use the interval [1, 15] as rough approximating interval, and use an accuracy of 0.00001. (This may not be a wonderful first interval, but it will get the computation started.)
      • How many iterations of the main loop are needed to obtain a good approximation?

The Newton's Method

Consult Levitin's textbook, Section 11.4 for details on Newton's Method, including the main formula.

  1. Use the program newtons-method.c to find the cube root of 15 using Newton's Method.
    (As a refresher from calculus, the derivative of x3 - 15 is 3x2.)
    • Since one may not know what to expect regarding the value of this cube root, try 15 as an initial guess (clearly a guess that is far off)!
    • How many iterations of the main loop are needed to obtain a good approximation?


For the next part of this assignment, Problems 4 and 5 are based on the function f(x), and Problems 6 and 7 are based on g(x).

Graph of f(x> Graph of f(x>
graph of f(x) = x3 - x = x (x-1) (x+1) graph of g(x) = x3 - 4x2 - 1

Working with the Function f(x) = x3 - x = x (x-1) (x+1)

Problems 4 and 5 examine the function f(x) = x3 - x = x (x-1)(x+1)

The Bisection Method

  1. Using your program from Problem 2,
    1. Approximate the roots of the function f(x) = x3 - x = x (x-1)(x+1), shown in the graph above.

      • Suppose we want to find the root near x = -1
        • After viewing the graph, which of the following might be used as the left endpoint a: -10, -2, -1.5, -0.5, -0.1, 0.75, 2.0? Explain briefly.
        • Which of the following might be used as the right endpoint b: -10, -2, -1.5, -0.5, -0.1, 0.75, 2.0?? Explain briefly.
      • After viewing the graph, what end points might you use to approximate the root near x = 0? Explain briefly.
      • After viewing the graph, what end points might you use to approximate the root near x = 1? Explain briefly.
    2. Turn in a listing of your program and the output for one of your appropriate choices for a and b to find the roots near x = -1, near x = 0, and near x = 1.

Newton's Method

  1. Use the program newtons-method.c to apply Newton's method to solve the following.

    1. Solve the equation f(x) = x3 - x = x (x-1) (x+1) = 0. Of course, from factoring, the solutions are x = -1, 0, and 1.
      • Comments in the program newtons-method.c allow easy set up for the function f(x) = x3 - x (x-1) (x+1) and derivative f'(x) = 3x2 - 1.
      • Run the program with initial guesses 10, 0.8, 0.5, and 0.4. In each case, indicate the value of the approximate root computed. Then, plotting rough tangent lines, indicate the geometry of the tangent lines that lead to the answer obtained. (For example, drawing the tangent lines may help explain why the program yields different answers for initial guesses 0.4, 0.5, and 0.8.)
    2. What, if any, conclusions do your experiments suggest regarding how to choose an initial guess when using Newton's Method?

Working with the Function x3 - 4x2 - 1

Problems 6 and 7 examine the function x3 - 4x2 - 1

The Bisection Method

  1. Use your program in Problem 2 to answer the following questions.

    1. Modify your program to use the function g(x) = x3 - 4x2 - 1, shown in the graph above.

      • After viewing the graph, which of the following might be used as the left endpoint a: -1, 1, 3, 4.5, 5? Explain briefly.
      • Which of the following might be used as the right endpoint b: -1, 1, 3, 4.5, 5? Explain briefly.
    2. Given your answers for step b above, is there any advantage to picking one of the a values and/or one of the b values when running your program? Explain briefly.
    3. Turn in a listing of your program and the output for one of your appropriate choices for a and b.

Newton's Method: Choice of Initial Guesses

  1. Consider the function g(x) = x3 - 4x2 - 1, with derivative g'(x) = 3x2 - 8x, and update the newtons-method.c to find root(s) of this function.

    The main difficulty in using Newton's Method occurs in the choice of the initial guess, x0. A poor choice can lead to a sequence x0, x1, x2, ... that does not get at all close to the solution you are seeking.

    1. Use the graph above to determine a reasonable choice. What solution does the program produce?
    2. Next, let x0 = 2. What seems to be happening? Sketch the first three iterations of Newton's Method on a graph of g(x) = x3 - 4x2 - 1 = 0.
    3. Let x0 = 0. What happens?
    4. Interesting chaotic behavior occurs when 1/sqrt(5) < x0 < 1/sqrt(3) or, by symmetry, when -1/sqrt(3) < x0 < -1/sqrt(5). Fill in the following table to convince yourself of the sensitivity of Newton's Method to the choice of x0 = 0.
      x0 Solution found
      0
      0.577
      0.578
      0.460
      0.466
      0.44722
      0.44723
    5. Extra Credit: Argue that if x0 > 1/sqrt(3), then Newton's Method will converge to the solution 1. Therefore, by symmetry, if x0 < -1/sqrt(3), Newton's Method will converge to the solution -1. Be sure to discuss what happens if x0 = -1/sqrt(3) or x0 = 1/sqrt(3).
    6. Extra Credit: Demonstrate algebraically that if we start Newton's Method with x0 = 1/sqrt(5), then x1 = -1/sqrt(5), x2 = 1/sqrt(5). Therefore, if we start with x0 = 1/sqrt(5) or x0 = -1/sqrt(5), we do not converge to a solution. In this case, -1/sqrt(5) and 1/sqrt(5) are called period 2 points.

created May 2, 2022
revised May 3, 2022
revised May 7, 2022
revised July 22, 2022
revised December 31, 2022–January 3, 2023
revised Summer 2023
revised April 9, 2024
revised November 18, 2024
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.