CS 415, Section 002 Sonoma State University Spring, 2022
 
Algorithm Analysis
Instructor: Henry M. Walker

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

Worksheet:  Coping with Limitations of Computing

Computablity and Unsolvability

  1. The Halting Problem is said to be "unsolvable".

    1. Outline the basic argument that shows that the Halting Problem is unsolvable.
    2. How is being "unsolvable" different from stating that we have not yet found an algorithm that solves the problem?
  2. A company believes there would be a strong market for a software package that would take the specifications of a problem and the code for a program as input and would prove whether the program meets its specifications in all cases. That is, the software package would prove whether or not a program always meets its specifications. Should the company invest in the development of such a software package? Why or why not? Explain.

  3. Consider the concepts of a NP-Hard problem and an NP-Complete problem.

    1. Can a problem be NP-Hard without being NP-Complete? Explain.
    2. Can a problem be NP-Complete without being NP-Hard? Explain.
  4. State two problems that are in Class P, and justify your conclusion for each problem.

  5. Given a Boolean expression with n variables, the Satisfiability Problem asks whether there is an assignment of True or False to each of the variables, so that the resulting expression is True. Give a careful argument, using the formal definition of Class NP, that explains why the Satisfiability Problem is in Class NP.

  6. In Levitin's textbook, solve Problems 2 and 3 in Section 11.3.

Compounding of Numeric Error

  1. Suppose a loop is to start at a value start and finish at (or near) a value end in about n+1 iterations, with iterations increasing by a value increment = (end-start)/n each time.

    Two loop structures are proposed:

          // approach 1
          increment = (end - start)/n;
          for (i = 0; i <= n; i++){
               value = start + i * increment;              
              /* processing for value */
            }
        
          // approach 2
          value = start;
          increment = (end - start)/n;
          while (value <= end) {
             /* processing for value */
             value += increment;             
          }
        

    Does either of these approches have any advantages over the other and thus be preferred, or are the two approaches about the same in computing a reliably-accurate answer? Explain.

  2. In reviewing Approach 2 in the previous problem, what, if any, adjustments would you suggest to improve the code (e.g., to ensure the loop is repeated n+1 times). Briefly explain.

  3. Suppose y = f(x) is a function that decreases from x=a to x=b, on the interval [a, b], with a<b.

    Throughout this interval, assume f(x)>0, and assume the Trapezoidal Rule were to be used to approximate the area under y = f(x) on the interval [a, b].

    1. Should the main loop begin at start and go toward end or begin at end and go toward start, or is either order fine? Explain.
    2. Write the code that implements the Trapezoidal Rule for this function on this interval.

Approximation Algorithms for the Traveling Salesperson Problem

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 another solution.

  1. Apply each of these algorithms to find approximate solutions to the Traveling Salesperson Problem for each of the following graphs.

    1. Graph A
      Graph A
    2. Graph B
      Graph B

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. (Jargon: 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.

Work with these numeric methods requires you to graph functions, so that you have a visual representation of a function's graph. For this purpose, you can use any graphing software that you like. One option is Graph a function from desmos.com.

The Bisection Method

Although finding a root r of a function can be a difficult problem in general, suppose that we can guess two points a and b (perhaps from a graph) where f(a) and f(b) have opposite signs. If the graph crosses the x-axis at the midpoint m of the interval between a and b (i.e., if f(m)=0), then m is the desired root of the function. Otherwise, the four possible cases are shown below:

four cases for the bisection method

Since this setting assumes we are given a and b for which f(a) and f(b) have opposite signs, we can infer that a root r must lie in the interval [a, b]. In one step, we can cut this interval in half as follows. If f(a) and f(m) have opposite signs, then r must lie in the interval [a, m]; otherwise, r must lie in the interval [m, b].

One simple way to determine if two numbers have the same sign is to compute their product. If the product is positive, the two numbers are both positive or both negative. If the product is negative, one of the original numbers is positive and the other is negative.


Following this process of cutting the interval in half, we can continue until the interval is very narrow. At this stage, the midpoint of the interval will be a reasonable approximation to a root of the function.

  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.

Newton's Method

Acknowledgement

This section draws heavily upon a lab by a similar name in Learning by Discovery: A Lab Manual for Calculus, edited by Anita Solow, MAA Notes, 1993. In particular, the approach and many details here come from a 1993 lab by Anita Solow.

Background

Given a function f, we want to approximate a solution to f(x) = 0. To use Newton's Method, we must have an initial guess x0. The next guess, x1, is found at the intersection of the x-axis with the tangent line to y = f(x) at (x0, f(x0)), as shown in the following figure:

Newton's Method
  1. We need to find a formula for x1.

    1. Use Δ y / Δ x to find f ' (x0), the slope of the tangent line to y = f(x), at x0, in terms of x0, x1, and f(x0).
    2. Solve for x1 to get the first iteration of Newton's Method:
      x1 = x0 - (f(x0) / f ' (x0))

      Once we have x1, we repeat the process to get x2 from x1, x3 from x2, etc. If all goes well, the xi ' s get closer and closer to the zero of f that we are seeking.

    3. Write a formula for x2 from x1.
    4. Write a formula for x3 from x2.
    5. Write a formula for xn+1 from xn.
    6. On the figure below, sketch the appropriate tangent lines and show x1, x2, and x3.
      Starting Newton's Method

Graphs and Computations

Much of the rest of this worksheet requires use of graphing software to plot graphs and a program newtons-method.c to perform computations.

  1. Apply Newton's Method to solve the equation x3 - 4x2 - 1 = 0.
    1. Graph f(x) = x3 - 4x2 - 1 = 0 using Graph a function from desmos.com or other graphing software.
    2. Use the program newtons-method.c to compute successive guesses using Newton's Method. To use this program,
      • Save the program to your account. Then compile and run it.
      • The program is already set up for the function f(x) = x3 - 4x2 - 1 = 0.
      • The program will ask you for an initial guess. Use the graph from step a to determine a reasonable choice. What solution does the program produce?
  2. You can use Newton's Method to find square roots of numbers. For example, to find the square root of n, solve the equation f(x) = 0 where f(x) = x2 - n.

    1. Find the square root of 15 using Newton's Method, specifying what value you need for x0.
    2. Check your answer with a calculator. Many calculators use Newton's Method, with an initial guess of 1, to take square roots.
created May 2, 2022
revised May 3, 2022
revised May 7, 2022
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.