HW Assignment #2
1. Use a direct proof to show that the sum of two odd integers is even.
1. Check your answer
10. Show that x3 is O(x4) but that x4 is not O(x3).
10. Check answer
10. Use a direct proof to show that the product of two rational numbers is rational.
10. Check your answer
11. Show that 3x4 + 1 is O(x4/2) and x4/2 is O(3x4 + 1).
11. Check answer
11. Describe an algorithm that interchanges the values of the variables x and y, using only assignments. What is the minimum number of assignment statements needed to do this?
11. Check your answer
11. Prove or disprove that the product of two irrational numbers is irrational.
11. Check your answer
12. Show that x log x is O(x2) but that x2 is not O(x log x)
12. Check answer
12. Consider the following algorithm, which takes as input a sequence of n integers a1, a2,...,an and produces as output a matrix M = {mij } where mij is the minimum term in the sequence of integers ai, ai+1,...,aj for j ≥ i and mij = 0 otherwise. initialize M so that mij = ai if j ≥ i and mij = 0 otherwise for i := 1 to n for j := i + 1 to n for k := i + 1 to j mij := min(mij , ak) return M= {mij } {mij is the minimum term of ai, ai+1,...,aj } 230 3 / Algorithms a) Show that this algorithm uses O(n3) comparisons to compute the matrix M. b) Show that this algorithm uses (n3) comparisons to compute the matrix M. Using this fact and part (a), conclude that the algorithms uses (n3) comparisons. [Hint: Only consider the cases where i ≤ n/4 and j ≥ 3n/4 in the two outer loops in the algorithm.]
12. Check your answer
12. Describe an algorithm that uses only assignment statements that replaces the triple (x, y, z) with (y, z, x). What is the minimum number of assignment statements needed?
12. Check your answer
12. Prove or disprove that the product of a nonzero rational number and an irrational number is irrational.
12. Check your answer
13. Show that 2n is O(3n) but that 3n is not O(2n). (Note that this is a special case of Exercise 60.)
13. Check answer
13. Prove that if x is irrational, then 1/x is irrational.
13. Check your answer
14. Prove that if x is rational and x = 0, then 1/x is rational.
14. Check your answer
15. Describe an algorithm that inserts an integer x in the appropriate position into the list a1, a2,...,an of integers that are in increasing order
15. Check your answer
15. Use a proof by contraposition to show that if x + y ≥ 2, where x and y are real numbers, then x ≥ 1 or y ≥ 1.
15. Check your answer
16. Show that if f (x) is O(x), then f (x) is O(x2).
16. Check answer
16. Describe an algorithm for finding the smallest integer in a finite sequence of natural numbers.
16. Check your answer
16. Prove that if m and n are integers and mn is even, then m is even or n is even.
16. Check your answer
17. Describe an algorithm that locates the first occurrence of the largest element in a finite list of integers, where the integers in the list are not necessarily distinct.
17. Check your answer
17. Show that if n is an integer and n3 + 5 is odd, then n is even using a) a proof by contraposition. b) a proof by contradiction.
17. Check your answer
18. Describe an algorithm that locates the last occurrence of the smallest element in a finite list of integers, where the integers in the list are not necessarily distinct.
18. Check your answer
18. Prove that if n is an integer and 3n + 2 is even, then n is even using a) a proof by contraposition. b) a proof by contradiction.
18. Check your answer
19. Describe an algorithm that produces the maximum, median, mean, and minimum of a set of three integers. (The median of a set of integers is the middle element in the list when these integers are listed in order of increasing size. The mean of a set of integers is the sum of the integers divided by the number of integers in the set.)
19. Check your answer
19. Prove the proposition P (0), where P (n) is the proposition "If n is a positive integer greater than 1, then n2 > n." What kind of proof did you use?
19. Check your answer
2. Use a direct proof to show that the sum of two even integers is even.
2. Check your answer
20. Describe an algorithm for finding both the largest and the smallest integers in a finite sequence of integers.
20. Check your answer
20. Prove the proposition P (1), where P (n) is the proposition "If n is a positive integer, then n2 ≥ n." What kind of proof did you use?
20. Check your answer
21. Describe an algorithm that puts the first three terms of a sequence of integers of arbitrary length in increasing order.
21. Check your answer
22. Determine the least number of comparisons, or best-case performance, a) required to find the maximum of a sequence of n integers, using Algorithm 1 of Section 3.1. b) used to locate an element in a list of n terms with a linear search. c) used to locate an element in a list of n terms using a binary search.
22 Check answer
22. Describe an algorithm to find the longest word in an English sentence (where a sentence is a sequence of symbols, either a letter or a blank, which can then be broken into alternating words and blanks).
22. Check your answer
22. Show that if you pick three socks from a drawer containing just blue socks and black socks, you must get either a pair of blue socks or a pair of black socks.
22. Check your answer
23. Analyze the average-case performance of the linear search algorithm, if exactly half the time the element x is not in the list and if x is in the list it is equally likely to be in any position.
23. Check your answer
23. Describe an algorithm that determines whether a function from a finite set of integers to another finite set of integers is onto.
23. Check your answer
23. Show that at least ten of any 64 days chosen must fall on the same day of the week.
23. Check your answer
24. An algorithm is called optimal for the solution of a problem with respect to a specified operation if there is no algorithm for solving this problem using fewer operations. 3.3 Complexity of Algorithms 231 a) Show that Algorithm 1 in Section 3.1 is an optimal algorithm with respect to the number of comparisons of integers. [Note: Comparisons used for bookkeeping in the loop are not of concern here.] b) Is the linear search algorithm optimal with respect to the number of comparisons of integers (not including comparisons used for bookkeeping in the loop)?
24 -> Check answer
24. Describe an algorithm that determines whether a function from a finite set to another finite set is one-to-one.
24. Check your answer
24. Show that at least three of any 25 days chosen must fall in the same month of the year.
24. Check your answer
25. Describe the worst-case time complexity, measured in terms of comparisons, of the ternary search algorithm described in Exercise 27 of Section 3.1.
25. Check answer
25. Describe an algorithm that will count the number of 1s in a bit string by examining each bit of the string to determine whether it is a 1 bit.
25. Check your answer
25. Use a proof by contradiction to show that there is no rational number r for which r3 + r + 1 = 0. [Hint: Assume that r = a/b is a root, where a and b are integers and a/b is in lowest terms. Obtain an equation involving integers by multiplying by b3. Then look at whether a and b are each odd or even.]
25. Check your answer
26. Describe the worst-case time complexity, measured in terms of comparisons, of the search algorithm described in Exercise 28 of Section 3.1.
26. Check your answer
26. Prove that if n is a positive integer, then n is even if and only if 7n + 4 is even.
26. Check your answer
27. Analyze the worst-case time complexity of the algorithm you devised in Exercise 29 of Section 3.1 for locating a mode in a list of nondecreasing integers.
27. Check your answer
27. Prove that if n is a positive integer, then n is odd if and only if 5n + 6 is odd.
27. Check your answer
27. The ternary search algorithm locates an element in a list of increasing integers by successively splitting the list into three sublists of equal (or as close to equal as possible) size, and restricting the search to the appropriate piece. Specify the steps of this algorithm.
27. Check your answer
28. Analyze the worst-case time complexity of the algorithm you devised in Exercise 30 of Section 3.1 for locating all modes in a list of nondecreasing integers.
28. Check your answer
28. Prove that m2 = n2 if and only if m = n or m = −n.
28. Check your answer
28. Specify the steps of an algorithm that locates an element in a list of increasing integers by successively splitting the list into four sublists of equal (or as close to equal as possible) size, and restricting the search to the appropriate piece.
28. Check your answer
29. Analyze the worst-case time complexity of the algorithm you devised in Exercise 31 of Section 3.1 for finding the first term of a sequence of integers equal to some previous term.
29. Check your answer
29. Prove or disprove that if m and n are integers such that mn = 1, then either m = 1 and n = 1, or else m = −1 and n = −1.
29. Check your answer
3. Show that the square of an even number is an even number using a direct proof.
3. Check your answer
3. Use the definition of "f (x) is O(g(x))" to show that x4 + 9x3 + 4x + 7 is O(x4).
3. Check your answer
30. Show that each of these pairs of functions are of the same order. a) 3x + 7, x b) 2x2 + x − 7, x2 c) x + 1/2, x d) log(x2 + 1), log2 x e) log10 x, log2 x
30. Check answer
30. Analyze the worst-case time complexity of the algorithm you devised in Exercise 32 of Section 3.1 for finding all terms of a sequence that are greater than the sum of all previous terms.
30. Check your answer
30. Show that these three statements are equivalent, where a and b are real numbers: (i) a is less than b, (ii) the average of a and b is greater than a, and (iii) the average of a and b is less than b.
30. Check your answer
31. Show that f (x)is (g(x))if and only if f (x)is O(g(x)) and g(x) is O(f (x)).
31. Check answer
31. Analyze the worst-case time complexity of the algorithm you devised in Exercise 33 of Section 3.1 for finding the first term of a sequence less than the immediately preceding term
31. Check your answer
31. Show that these statements about the integer x are equivalent: (i) 3x + 2 is even, (ii) x + 5 is odd, (iii) x2 is even
31. Check your answer
32. Show that if f (x) and g(x) are functions from the set of real numbers to the set of real numbers, then f (x) is O(g(x)) if and only if g(x) is (f (x)).
32. Check answer
32. Determine the worst-case complexity in terms of comparisons of the algorithm from Exercise 5 in Section 3.1 for determining all values that occur more than once in a sorted list of integers.
32. Check your answer
32. Show that these statements about the real number x are equivalent: (i) x is rational, (ii) x/2 is rational, (iii) 3x − 1 is rational.
32. Check your answer
33. Show that if f (x) and g(x) are functions from the set of real numbers to the set of real numbers, then f (x) is (g(x)) if and only if there are positive constants k, C1, and C2 such that C1|g(x)|≤|f (x)| ≤ C2|g(x)| whenever x>k.
33. Check answer
33. Determine the worst-case complexity in terms of comparisons of the algorithm from Exercise 9 in Section 3.1 for determining whether a string of n characters is a palindrome.
33. Check your answer
33. Show that these statements about the real number x are equivalent: (i) x is irrational, (ii) 3x + 2 is irrational, (iii) x/2 is irrational.
33. Check your answer
34. How many comparisons does the selection sort (see preamble to Exercise 41 in Section 3.1) use to sort n items? Use your answer to give a big-O estimate of the complexity of the selection sort in terms of number of comparisons for the selection sort.
34. Check answer
34. a) Show that 3x2 + x + 1 is (3x2) by directly finding the constants k, C1, and C2 in Exercise 33. b) Express the relationship in part (a) using a picture showing the functions 3x2 + x + 1, C1 · 3x2, and C2 · 3x2, and the constant k on the x-axis, where C1, C2, and k are the constants you found in part (a) to show that 3x2 + x + 1 is (3x2).
34. Check answer
35. Find a big-O estimate for the worst-case complexity in terms of number of comparisons used and the number of terms swapped by the binary insertion sort described in the preamble to Exercise 47 in Section 3.1.
35. Check answer
36. Show that the propositions p1, p2, p3, and p4 can be shown to be equivalent by showing that p1 ↔ p4, p2 ↔ p3, and p1 ↔ p3.
36. Check your answer
37. Show that the propositions p1, p2, p3, p4, and p5 can be shown to be equivalent by proving that the conditional statements p1 → p4, p3 → p1, p4 → p2, p2 → p5, and p5 → p3 are true.
37. Check your answer
39. Describe how the number of comparisons used in the worst case changes when these algorithms are used to search for an element of a list when the size of the list doubles from n to 2n, where n is a positive integer. a) linear search b) binary search
39. Check answer
39. Show that if f and g are real-valued functions such that f (x) is O(g(x)), then for every positive integer n, f n(x) is O(gn(x)). [Note that f n(x) = f (x)n.]
39. Check answer
39. Describe how the number of comparisons used in the worst case changes when these algorithms are used to search for an element of a list when the size of the list doubles from n to 2n, where n is a positive integer. a) linear search b) binary search
39. Check your answer
39. Prove that at least one of the real numbers a1, a2,...,an is greater than or equal to the average of these numbers. What kind of proof did you use?
39. Check your answer
4. Describe an algorithm that takes as input a list of n integers and produces as output the largest difference obtained by subtracting an integer in the list from the one following it
4. Check your answer
4. Show that the additive inverse, or negative, of an even number is an even number using a direct proof.
4. Check your answer
4. Use the definition of "f (x) is O(g(x))" to show that 2x + 17 is O(3x ).
4. Check your answer
40. Describe how the number of comparisons used in the worst case changes when the size of the list to be sorted doubles from n to 2n, where n is a positive integer when these sorting algorithms are used. a) bubble sort b) insertion sort c) selection sort (described in the preamble to Exercise 41 in Section 3.1) d) binary insertion sort (described in the preamble to Exercise 47 in Section 3.1)
40. Check answer
40. Show that for all real numbers a and b with a > 1 and b > 1, if f (x) is O(logb x), then f (x) is O(loga x).
40. Check answer
40. Describe how the number of comparisons used in the worst case changes when the size of the list to be sorted doubles from n to 2n, where n is a positive integer when these sorting algorithms are used. a) bubble sort b) insertion sort c) selection sort (described in the preamble to Exercise 41 in Section 3.1) d) binary insertion sort (described in the preamble to Exercise 47 in Section 3.1)
40. Check your answer
41. Prove that if n is an integer, these four statements are equivalent: (i) n is even, (ii) n + 1 is odd, (iii) 3n + 1 is odd, (iv) 3n is even
41. Check your answer
42. Prove that these four statements about the integer n are equivalent: (i) n2 is odd, (ii) 1 − n is even, (iii) n3 is odd, (iv) n2 + 1 is even.
42. Check your answer
43. Describe an algorithm based on the linear search for determining the correct position in which to insert a new element in an already sorted list.
43. Check your answer
44. Describe an algorithm based on the binary search for determining the correct position in which to insert a new element in an already sorted list.
44. Check your answer
45. How many comparisons does the insertion sort use to sort the list 1, 2,...,n?
45. Check your answer
46. Show that if f1(x) and f2(x) are functions from the set of positive integers to the set of real numbers and f1(x) is (g1(x)) and f2(x) is (g2(x)), then (f1f2)(x) is ((g1g2)(x)).
46. Check answer
46. How many comparisons does the insertion sort use to sort the list n, n − 1,..., 2, 1?
46. Check your answer
48. Compare the number of comparisons used by the insertion sort and the binary insertion sort to sort the list 7, 4, 3, 8, 1, 5, 4, 2.
48. Check your answer
5. Show that (x2 + 1)/(x + 1) is O(x).
5. Check answer
5. Describe an algorithm that takes as input a list of n integers in nondecreasing order and produces the list of all values that occur more than once. (Recall that a list of integers is nondecreasing if each integer in the list is at least as large as the previous integer in the list.)
5. Check your answer
5. Prove that if m + n and n + p are even integers, where m, n, and p are integers, then m + p is even. What kind of proof did you use?
5. Check your answer
50. Show that if f (x) = anxn + an−1xn−1 +···+ a1x + a0, where a0, a1,...,an−1, and an are real numbers and an = 0, then f (x) is (xn). Big-O, big-Theta, and big-Omega notation can be extended to functions in more than one variable. For example, the statement f (x, y)isO(g(x, y))means that there exist constantsC, k1, and k2 such that |f (x, y)| ≤ C|g(x, y)| whenever x>k1 and y>k2.
50. Check answer
53. Show that (x2 + xy + x log y)3 is O(x6y3)
53. Check answer
53. Show that (x2 + xy + x log y)3 is O(x6y3).
53. Check answer
54. Show that x5y3 + x4y4 + x3y5 is (x3y3).
54. Check answer
55. Show that xy is O(xy).
55. Check answer
56. Show that xy is (xy)
56. Check answer
6. a) Use pseudocode to describe the algorithm that puts the first four terms of a list of real numbers of arbitrary length in increasing order using the insertion sort. b) Show that this algorithm has time complexity O(1) in terms of the number of comparisons used.
6 a) b) Check answer
6. Describe an algorithm that takes as input a list of n integers and finds the number of negative integers in the list.
6. Check your answer
6. Use a direct proof to show that the product of two odd numbers is odd.
6. Check your answer
7. Describe an algorithm that takes as input a list of n integers and finds the location of the last even integer in the list or returns 0 if there are no even integers in the list.
7. Check your answer
7. Use a direct proof to show that every odd integer is the difference of two squares.
7. Check your answer
8. Given a real number x and a positive integer k, determine the number of multiplications used to find x2k starting with x and successively squaring (to find x2, x4, and so on). Is this a more efficient way to find x2k than by multiplying x by itself the appropriate number of times?
8. Check answer
8. Describe an algorithm that takes as input a list of n distinct integers and finds the location of the largest even integer in the list or returns 0 if there are no even integers in the list.
8. Check your answer
8. Prove that if n is a perfect square, then n + 2 is not a perfect square.
8. Check your answer
9. Give a big-O estimate for the number of comparisons used by the algorithm that determines the number of 1s in a bit string by examining each bit of the string to determine whether it is a 1 bit (see Exercise 25 of Section 3.1)
9. Check answer
9. Show that x2 + 4x + 17 is O(x3) but that x3 is not O(x2 + 4x + 17).
9. Check answer
9. A palindrome is a string that reads the same forward and backward. Describe an algorithm for determining whether a string of n characters is a palindrome
9. Check your answer
9. Use a proof by contradiction to prove that the sum of an irrational number and a rational number is irrational.
9. Check your answer
EXAMPLE 11 Give a proof by contradiction of the theorem "If 3n + 2 is odd, then n is odd."
Example 11 -> Check answer