Ch. 22 Developing Efficient Algorithms

¡Supera tus tareas y exámenes ahora con Quizwiz!

A. best-case input

An input that results in the shortest execution time is called the _____________. A. best-case input B. worst-case input C. average-case input

This is similar to bubble sort. Whenever a swap is made, it goes back to the beginning of the loop. In the worst case, there will be O(n^2) of swaps. For each swap, O(n) number of comparisons may be made in the worst case. So, the total is O(n^3) in the worst case.

Analyze the following sorting algorithm: for (int i = 0; i < list.length - 1; i++) { if (list[i] > list[i + 1]) { swap list[i] with list[i + 1]; i = -1; } }

The algorithm can be designed as follows: Maintain two variables, max and count. max stores the current max number, and count stores its occurrences. Initially, assign the first number to max and 1 to count. Compare each subsequent number with max. If the number is greater than max, assign it to max and reset count to 1. If the number is equal to max, increment count by 1. Since each element in the array is examined only once, the complexity of the algorithm is O(n).

Describe an algorithm for finding the occurrence of the max element in an array. Analyze the complexity of the algorithm.

The algorithm can be designed as follows: For each element in the input array, store it to a new array if it is new. If the number is already in the array, ignore it. The time for checking whether an element is already in the new array is O(n), so the complexity of the algorithm is O(n^2).

Describe an algorithm for removing duplicates from an array. Analyze the complexity of the algorithm.

C. to estimate their growth function

Estimating algorithm efficiency is ________. A. to measure their actual execution time B. to estimate their execution time C. to estimate their growth function

A. 11

For a sorted list of 1024 elements, a binary search takes at most _______ comparisons. A. 11 B. 100 C. 512 D. 6

A. constant time

O(1) is ________. A. constant time B. logarithmic time C. linear time D. log-linear time

B. half of the list

On an average, linear search searches A. the whole list B. half of the list C. just one element in the list D. one fourth of the list

B. O(nlogn)

The Graham?s algorithm for finding a convex hull takes ______________ time. A. O(n) B. O(nlogn) C. O(logn) D. O(n^2)

D. Backtracking

The ________ approach searches for a candidate solution incrementally, abandoning that option as soon as it determines that the candidate cannot possibly be a valid solution, and then looks for a new candidate. A. Divide-and-conquer B. Dynamic programming C. Brutal-force D. Backtracking

D

The gift-wrapping algorithm for finding a convex hull takes ______________ time. A. O(n) B. O(nlogn) C. O(logn) D. O(n^2)

C. O(logn)

The time complexity for the Euclid?s algorithm is ________. A. O(n) B. O(n^2) C. O(logn) D. O(2^n)

B.

The time complexity for the Sieve of Eratosthenes algorithm is ________. A. O(n) B. O(n^(1.5)/logn) C. O(logn) D. O(2^n)

D. O(2^n)

The time complexity for the Towers of Honoi algorithm in the text is ________. A. O(n) B. O(n^2) C. O(n^3) D. O(2^n)

A. O(n) linear

The time complexity for the algorithm using the dynamic programming approach is ________. A. O(n) B. O(n^2) C. O(logn) D. O(2^n)

B. O(n^2)

The time complexity for the insertion sort algorithm in the text is ________. A. O(nlogn) B. O(n^2) C. O(logn) D. O(2^n)

D. O(2^n)

The time complexity for the recursive Fibnacci algorithm in the text is ________. A. O(nlogn) B. O(n^2) C. O(logn) D. O(2^n)

B. O(n^2)

The time complexity for the selection sort algorithm in the text is ________. A. O(nlogn) B. O(n^2) C. O(logn) D. O(2^n)

B. O(nlogn)

The time complexity for the the closest pair of points problem using divide-and-conquer is ________. A. O(n) B. O(nlogn) C. O(logn) D. O(2^n)

E. the ceiling of (n - 5)/3

What is the number of iterations in the following loop: int count = 5; while (count < n) { count = count + 3; } A. n - 5 B. n - 3 C. n / 3 - 1 D. (n - 5) / 3 E. the ceiling of (n - 5) / 3

(a) (n^2 + 1)^2/n = O(n3) (b) (n^2 + log^2n)2 / n = O(n3) (c) n3 + 100n^2 + n = O(n3) (d) 2n + 100n2 + 45n = O(2n) (e) n2n + n22n = O(n^2*2^n)

What is the order of each of the following functions? (a) (n^2 + 1)^2/n (b) (n^2 + log^2n)^2 / n (c) n^3 + 100n^2 + n (d) 2^n + 100n^2 + 45n (e) n2^n + n^2*2^n

B and C

Which of the following complexity is O(nlogn)? A. 300n + 400n*n B. 23nlogn + 50 C. 45n + 45nlogn + 503 D. n*n*n + nlogn

The constant factor is ignored in big O notation, because it has no impact on the growth rate of the time complexity function. A nondominating term is ignored in Big O notation, because as the input size grows, the dominating term grows much faster than the nondominating term.

Why is a constant factor ignored in the Big O notation? Why is a nondominating term ignored in the Big O notation?

A, B, C

Why is the analysis often for the worst case? A. Best-case is not representative. B. Worst-case is not representative, but worst-case analysis is very useful. You can show that the algorithm will never be slower than the worst-case. C. Average-case analysis is ideal, but difficult to perform, because it is hard to determine the relative probabilities and distributions of various input instances for many problems.

A

______________ approach divides the problem into subproblems, solves the subproblems, then combines the solutions of the subproblems to obtain the solution for the entire problem. Unlike the ________ approach, the subproblems in the divide-and-conquer approach don?t overlap. A subproblem is like the original problem with a smaller size, so you can apply recursion to solve the problem. A. Divide-and-conquer/dynamic programming B. Dynamic programming/divide-and-conquer C. Brutal-force/divide-and-conquer D. Backtracking/dynamic programming

B. Dynamic programming

______________ approach is the process of solving subproblems, then combining the solutions of the subproblems to obtain an overall solution. This naturally leads to a recursive solution. However, it would be inefficient to use recursion, because the subproblems overlap. The key idea behind dynamic programming is to solve each subproblem only once and store the results for subproblems for later use to avoid redundant computing of the subproblems. A. Divide-and-conquer B. Dynamic programming C. Brutal-force D. Backtracking


Conjuntos de estudio relacionados

simple machines, Work 6.1, 6.2 & 6.3

View Set

Human Growth and Development FINAL

View Set