Algorithms Chapters 1 - 2

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

Consider sorting n numbers stored in array AA by first finding the smallest element of AA and exchanging it with the element in A[1]A[1]. Then find the second smallest element of AA, and exchange it with A[2]A[2]. Continue in this manner for the first n - 1n−1 elements of AA. Write pseudocode for this algorithm, which is known as selection sort. What loop invariant does this algorithm maintain? Why does it need to run for only the first n - 1n−1 elements, rather than for all n elements? Give the best-case and worst-case running times of selection sort in Θ-notation.

1. Pseudo Code n = A.length for i = 1 to n - 1 minIndex = i for j = i + 1 to n if A[j] < A[minIndex] minIndex = j swap(A[i], A[minIndex]) 2. Loop invariant: At the start of the loop in line 1, the subarray A[1..i - 1] consists of the smallest i - 1 elements in array A with sorted order. 3. Why does it need to run for only the first n - 1 elements, rather than for all n elements?: After n - 1 iterations, the subarray A[1...n - 1] consists of the smallest i - 1 elements in array A with sorted order. Therefore, A[n] is already the largest element. 4. Running time: Θ(n^2)

What is the smallest value of n such that an algorithm whose running time is 100n^2 runs faster than an algorithm whose running time is 2^n on the same machine?

100n^2 < 2^n n >= 15

Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For inputs of size n, insertion sort runs in 8n^2 steps, while merge sort runs in 64nlogn steps. For which values of n does insertion sort beat merge sort?

8n^2 < 64nlogn 2^n < n^8 n <= 43

Use figure 2.2 as a model, illustrate the operation of intersection-sort on the array A = {31, 41, 59, 26, 41, 58}

A = {31, 41, 59, 26, 41, 58} A = {31, 41, 59, 26, 41, 58} A = {31, 41, 59, 26, 41, 58} A = {26, 31, 41, 59, 41, 58} A = {26, 31, 41, 41, 59, 58} A = {26, 31, 41, 41, 58, 59}

Come up with a real-world problem in which only the best solution will do. Then come up with one in which a solution that is "approximately" the best is good enough.

Best: find the GCD of 2 positive integers Approximately: find the solutions of differential equations.

Using Figure 2.4 as a model, illustrate the operation of merge sort on the array A =⟨3,41,52,26,38,57,9,49⟩.

Draw it yourself.

Give an example of an application that requires algorithmic content at the application level, and discuss the function of the function of the algorithms involved.

Drive-navigation

Observe that the while loop of lines 5-7 of the \text{INSERTION-SORT}INSERTION-SORT procedure in Section 2.1 uses a linear search to scan (backward) through the sorted subarray A[i..j - 1]A[i..j−1]. Can we use a binary search (see Exercise 2.3-5) instead to improve the overall worst-case running time of insertion sort to Θ(nlgn)?

Each time the while loop of lines 5-7 of INSERTION-SORT scans backward through the sorted array A[1..j - 1]A[1..j−1]. The loop not only searches for the proper place for A[j]A[j], but it also moves each of the array elements that are bigger than A[j]A[j] one position to the right (line 6). These movements takes Θ(j) time, which occurs when all the j - 1j−1 elements preceding A[j]A[j] are larger than A[j]A[j]. The running time of using binary search to search is Θ(lgj), which is still dominated by the running time of moving element Θ(j). Therefore, we can't improve the overrall worst-case running time of insertion sort to Θ(nlgn).

Describe a Θ(nlgn)-time algorithm that, given a set SS of n integers and another integer xx, determines whether or not there exist two elements in SS whose sum is exactly x

First, sort SS, which takes Θ(nlgn). Then, for each element si​ in SS, i = 1, \dots, ni=1,...,n, search A[i + 1..n]A[i+1..n] for s_i' = x - s_isi′​=x−si​ by binary search, which takes Θ(lgn). If s_i'si′​ is found, return its position; otherwise, continue for next iteration. The time complexity of the algorithm is \Theta(n\lg n) + n \cdot \Theta(\lg n) = \Theta(n\lg n)Θ(nlgn)+n⋅Θ(lgn)=Θ(nlgn)

Consider linear search again (see Exercise 2.1-3). How many elements of the in- put sequence need to be checked on the average, assuming that the element being searched for is equally likely to be any element in the array? How about in the worst case? What are the average-case and worst-case running times of linear search in \ThetaΘ-notation? Justify your answers.

If the element is present in the sequence, half of the elements are likely to be checked before it is found in the average case. In the worst case, all of them will be checked. That is, n/2 checks for the average case and n for the worst case. Both of them are Θ(n).

Consider the problem of adding 2 n-nit binary integers, stored in 2 n-element arrays A and B. The sum of the 2 ints should be stored in a binary form in an (n + 1)-element array C. State the problem formally and write pseudo for adding 2 integers.

Input: An array of booleans A = {a1, a2,....., an} and an array of booleans B = {b1, b2,...., bn}, each representing an integer stored in binary format (each digit is a number, either 0 or 1, least significant digit first) and each of length n. Output: An array C = {c1, c2,...., cn} such that C(prime) = A(prime) + B(prime), where A(prime), B(prime) and C(prime) are the integers, represented by A, B and C. ADD-BINARY(A, B) C = new integer[A.length + 1] carry = 0 for i = 1 to A.length C[i] = (A[i] + B[i] + carry) % 2 carry = (A[i] + B[i] + carry) / 2 C[i] = carry return C

Rewrite the Insertion-sort procedure to sort into non-increasing instead of non-decreasing order

Insertion sort(A) for j = 2 to A.length key = A[j] i = j - 1 while i > 0 and A[i] < key A[i + 1] = A[i] i = i + 1 A[i + 1} = key

Consider the searching problem: Input: A sequence of n numbers A = {a1, a2, ......,an} and value v Output: An index i such that v = A[i] or the special value NIL if v does not appear in A. Write pseudocode for linear search, which scans through the sequence, looking for v. Using a loop invariant fulfills the three necessary properties.

Linear-search(A, v) for i = 1 to A.length if A[i] == v return i return NIL Loop invariant: At the start of each iteration of the for loop, the subarray A[1...i - 1] consists of elements that are different than v. Initialization: Initially the subarray is the empty array, so the prove is trivial. Maintenance: On each step, we know that A[1...i - 1] does not contain v. We compare it with A[i]. If they are the same, we return i, which is a correct result. Otherwise, we continue to the next step. We have already insured that A[1...i - 1] does not contain v and that A[i] is different from v, so this step preserves the invariant. Termination: The loop terminates when i > A.length. Since i increases by 1 and i > A.length, we know that all the elements in A have been checked and it has been found that v is not among them. Thus, we return NIL.

Select a data structure that you have seen previously, and discuss its strengths and limitations.

Linked-list: Strengths-insertion and deletion. Limitations-random access and reordering.

Other than speed, what other measures of efficiency might one use in a real-world setting?

Memory efficiency and code efficiency.

How are the shortest-path and travel-salesman problems given above similar? How are they different?

Similar: finding path with shortest distance Different: traveling-salesman has more constrains.

Give a real-world example that requires sorting or a real-world example that requires computing a convex hull.

Sorting: browse the price of the restaurants with ascending prices on the NTU street Convex Hull: computing the diameter of set of points

Rewrite the MERGE procedure so that it does not use sentinels, instead stopping once either array LL or RR has had all its elements copied back to AA and then copying the remainder of the other array back into AA.

Write and check online

How can we modify almost any algorithm to have a good best-case running time?

You can modify any algorithm to have a best case time complexity by adding a special case. If the input matches this special case, return the pre-computed answer.

Express the function n^3/(1000 - 100n^2 - 100n + 3) in Θ-notation

Θ(n^3)


Conjuntos de estudio relacionados

(IB Biology) 2.3 Eukaryotic Cells

View Set

CHAPTER 11. The Philippine-American War

View Set

Honors 9th grade Lit. - Poetic Devices Quiz

View Set

Vocabulary Workshop Level G Unit 7 Synonyms and Antonyms Practice

View Set

Chapter 1 Quiz- Money and Banking

View Set