Exam 3: Algorithms
Describe a brute-force algorithm for determining, given a compound proposition in variables, whether is satisfiable. It is known that this problem is -complete. If , what conclusion can be drawn about the efficiency of your algorithm compared to the efficiency of the best algorithm for solving this problem?
For each string of length of the letters and (representing true and false), evaluate the given compound proposition when the variable is assigned the truth value given by the letter in the string. If any of these values of is , then is satisfiable; otherwise it is not. This will take at least steps in the worst case (the case in which is not satisfiable), once for each such string. If , then there is a polynomial worst-case time algorithm, so the brute force algorithm is not the most efficient.
Suppose that , and are , and matrices of numbers, respectively. Is it more efficient to compute the product as or as ? Justify your answer by computing the number of multiplications of numbers needed each way.
To multiply by , we will need 3.4 multiplications. The result is a matrix. To multiply it by will require multiplications. This gives a total of steps. On the other hand, if we multiply by first, we use multiplications and then another to multiply by the matrix . This method uses a total of steps. Therefore the first method is a little faster.
(a) Describe an algorithm for finding the second largest integer in a sequence of distinct integers.
(b) Give a big- estimate of the number of comparison used by your algorithm.
(a) We first compare the first and second integers in the sequence , setting the value of the variable firstmax equal to the larger, and the value of the variable secondmax equal to the smaller. For each successive integer in the sequence, , we first compare it to firstmax. If firstmax, then we make the assignments secondmax firstmax and firstmax Otherwise, we compare to secondmax, and if , then we make the assignment secondmax := . At the end of this procedure the value of secondmax will be the second largest integer in the sequence.
(b) We do one comparison at the beginning of the algorithm to determine whether or is larger. Then for each successive term, for , we carry out at most two comparisons. Hence the largest number of comparisons used is , ignoring bookkeeping. This is .
Describe an algorithm for finding the smallest integer in a finite sequence of integers.
Determine the worst case complexity in terms of the number of comparisons used for the algorithm you described in problem 1.
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)