Determine which of the following problems can be computed in worst-case polynomial-time, i.e. time for some constant k, where n denotes the number of bits in the binary representation of the input. If you think the problem can be solved in polynomial time, give an algorithm, explain briefly why it gives the correct answer, and argue carefully why the running time is polynomial. If you think the problem cannot be solved in polynomial time, then provide a proof.
Given as input positive integer , output .
Given as input a positive integer , output True iff for some positive integer .
Given as input a positive integer , output True iff for some positive integer .
Problem 1: To represent the value of we will need bits. As shown in the course notes, . It takes at least one step to write down a single bit of the output. Therefore, writing out the whole output would take exponential time with respect to the input length , since .
Problem 2: The polynomial-time algorithm is as follows. We iteratively compute the values . In particular, at iteration , in order to compute , we take the result from previous iteration and multiply it by . In every iteration we check whether . When , we exit the loop and check whether or . If they are equal, then return true, and false otherwise.
The analysis of the algorithmic complexity is as follows: First, a crude upper bound on the number of iterations is . This is because after the second iteration, grows by a factor of at least 2 in each iteration. So after iterations, the value of exceeds . In each iteration we do a multiplication operation. If we use the quadratic-time multiplication algorithm, then the running time of the multiplication operation would be at most . Therefore the total running time of the loop is . The comparison between two numbers and will take time. Thus, the whole algorithm takes time, which is polynomial in the input length .
Problem 3: We will perform binary search to pin down the value of such that . At each iteration, we have a guess for the square root of . We compute to see if our guess is correct, or whether is above or below . Depending on that, we update our range and calculate a new guess . Here we will not give the details about the implementation of binary search (that is not the point of the exercise).
Let’s assume and are the two variables in the binary search algorithm that keep track of the range we are interested in (the midpoint of and corresponds to our guess ). At the beginning of each iteration of the while loop, the invariant will be maintained. Since and are integers, and is decreasing at each iteration, we will find if it is also an integer.
To analyze the run-time, we first look at the number of iterations, and then look at the amount of work we do in each iteration.
In terms of the number of iterations, observe that decreases by at least a half at the end of each iteration of the loop. Since we start with a range of length initially, and we decrease the size of this range by a factor of 2 at every iteration, we have at most iterations.
In terms of the work done in each iteration, note that we compute the product , and the value of is at most . Hence, we can do this computation in time (just using the grade-school multiplication algorithm). We also perform some comparisons and assignments, each of which only takes time. So the work done in each iteration is .
We can conclude that the total time is , which is polynomial in .
A connected graph with no cycles is a tree. Consider the following claim and its proof.
Claim: Any graph with vertices and edges is a tree.
Proof: We prove the claim by induction. The claim is clearly true for and . Now suppose the claim holds for . We’ll prove that it also holds for . Let be a graph with vertices and edges. By the induction hypothesis, is a tree (and therefore by definition connected). Add a new vertex to by connecting it with any other vertex in . So we create a new graph with vertices and edges. The new vertex we added is a leaf, so it does not create a cycle. Also, since was connected, is also connected. A connected graph with no cycles is a tree, so is also a tree. The claim follows by induction.
Explain why the given proof is incorrect.
The proof relies on a specific construction to obtain bigger graphs from smaller graphs. This really only proves the statement for all graphs that can be constructed in this way (taking a smaller graph with the property and connecting a new vertex to an existing vertex). In this case it is easy to see how and why this fails. Consider any counterexample to the claim and observe that it cannot be obtained by connecting a new vertex to a smaller tree.
The general format for induction of graphs is something like this. Suppose we want to prove that all graphs that satisfy hypothesis have property .
Assume what is to be proven for all graphs on vertices or less.
Consider an arbitrary graph on vertices that satisfies hypothesis .
Remove some vertices to obtain a smaller graph that still satisfies hypothesis .
Cite the induction hypothesis to conclude that the smaller graph has property .
Reconstruct the original larger graph and argue that property is maintained.
Give two proofs (one using induction and another using a degree counting argument) for the following claim: the number of leaves in a tree with vertices is
As a reminder, any graph satisfying two of the following three properties is a tree: (i) the graph is connected; (ii) the graph is acyclic; (iii) the number of edges is one less than the number of vertices.
We prove the statement by induction on , the number of vertices. For the base case, the only tree with vertices is a single edge, which has 2 leaves and no vertices with degree at least 3. Therefore, as desired.
For the inductive case, suppose that for some , the statement is true for all trees on vertices. Consider an arbitrary tree on vertices. Every tree with at least two vertices has at least 2 leaves (this is an exercise problem in the textbook). Let be a leaf in and let be its neighbor. Then is a tree on vertices since it is connected and acyclic. Note that we cannot have since has as a neighbor as well as another vertex in .
- Case 1:
. Then and so is a leaf in . It follows that and have the same number of leaves. So where the last equality holds because in both the summation for and the summation for , neither nor appear.
- Case 2:
. Then and so
We now prove the statement via a degree counting argument. Let be an arbitrary tree with vertices, edges and leaves. Since is a tree, we know . By the handshake lemma, where the last equality follows because the summation over is equivalent to (the terms are all 0). Rearranging the terms we get
We use , and notation to compare functions in computer science. In particular, corresponds to , corresponds to , and corresponds to . We now would like to define what “strictly less than” and “strictly greater than” mean with respect to comparing functions. How should we define them?
In computer science, the standard notation for “strictly less than” is , which is known as the little-o notation. We usually define “strictly less than” to mean “not greater than or equal to”. So we can try to define to mean “not ”. In other words, we cannot find constants such that for all , . Unfortunately this definition does not quite work. This is because we can have functions that are incomparable to each other in the sense that neither holds nor (try to come up with an example for this). And for such functions, we have “not ”, but we can’t say because implies .
Another way to define “strictly less than” is “less than or equal to but not equal to”. So we can try to define to mean “ but not ”. This is the definition we can indeed use. Equivalently, we want “ grows asymptotically faster than ”, or in other words, , which is how little-o notation is usually defined. As a very simple example, we can write .
The definition for “strictly greater than” is analogous, and we use little-omega for this. As a simple example, we can write .