Sei sulla pagina 1di 12

Table

of Contents
Introduction 1.1
Chap 1: The Role of Algorithms in Computing 1.2
1.1 Algorithms 1.2.1
1.2 Algorithms as a technology 1.2.2
Chap 2: Getting Started 1.3
2.1 Insertion Sort 1.3.1
2.2 Analyzing Algorithms 1.3.2
2.3 Designing Algorithms 1.3.3

1
Introduction

Solutions for Introduction to Algorithms


3rd Edition
This is the solutions for the book "Introduction to Algorithms", 3rd edition.

2
1.1 Algorithms

1.1 Algorithms

1.1-1
Question: Give a real-world example that requires sorting or a real-world example that
requires computing a convex hull.

Answer:

Real world examples that requires sorting:

Find the top score of the English Premier League in last 10 years.

1.1-2
Question: Other than speed, what other measures of efficiency might one use in a real-word
setting?

Answer:

Memory usage

1.1-3
Question: Select a data structure that you have seen previously, and discuss its strengths
and limitations.

Answer:

Linked list
Strengths:
"Instantly" insert to the end
Limitations:
May have to traverse across all elements to find one

1.1-4
Question: How are the shortest-path and traveling-salesman problems above similar? How
are they different? Answer:

3
1.1 Algorithms

Similarities:
Need find the most efficient solution (lowest distance)
Differencies:
Traveling-salesman is a NP-complete, that means it has no known efficient
algorithm. You may need to find a "good enough" one.

1.1-5
Question: Come up a (1) real-world problem in which only the best solution will do. Then
come up with (2) one in which a solution that is "approximately" the best is good enough.
Answer:

(1) Calculate the price of an order


(2) Suggest other products based on a customer's previous orders

4
1.2 Algorithms as a technology

1.2 Algorithms as a technology

1.2-1
Question: Give an example of an application that requires algorithmic content at the
application level, and discuss the function of the algorithms involved

1.2-2
Question: 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 $$64nlg(n)$$ steps. For which values of n does insertion sort beat merge sort?
*Answer: Insertion sort beats merge sort when:

$$8n^2 < 64nlg(n)$$

That means: $$n < 8lg(n)$$

Or:

$$ f(n) = n - 8lg(n) < 0

$$

$$ f'(n) = 1 - 8 \frac{1}{n\ln 2}

$$

$$f(n) < 0$$ with $$n = 1, 2, 3, ..., 43 $$

$$f(n) > 0$$ with $$ n = 43 $$ From $$n=43$$, $$f'(n) > 0$$, that means $$f(n) > 0$$ when
$$n > 43$$, so insertion sort beats merge sort with $$n = 1 \ to 43$$

1.2-3
Question: What is the smallest value of n such that an algorithm whose running time is
$$100n^2$$ runs faster than a algorithm whose running time is $$2^n$$ on the same
machine?

Answer: 15

5
1.2 Algorithms as a technology

6
2.1 Insertion Sort

2.1 Insertion Sort

2.1-1
Using Figure 2.2 as a model, illustrate the operation of INSERTION-SORT on the array A =
{31, 41, 59, 26, 41, 28}

Answer:

31, 41, 59, 26, 51, 28

31, 41, 59, 51, 28

31, 41, 59, 51, 28

31, 41, 51, 59, 28

28, 31, 41, 51, 59

2.1-2
Rewrite the INSERTION-SORT procedure to sort into non-increasing instead of non-
decreasing order

Answer:

(consider the array index starts from 0)

for i = 1 to A.length - 1
key = A[i]
j = i - 1
while j >= 0 and A[j] < key
A[j+1] = A[j]
j = j - 1
A[j+1] = key

2.1-3
Write precedure for linear search, which scan through the sequence, looking for value v

Answer:

7
2.1 Insertion Sort

for i = 0 to A.length - 1
if A[i] == value
return i
return nil

Loop invariant: Property: The output is either nil or an index of A

Initialization: Before first iteration, there's no element examined, so the output is nil
Maintenance: If the previous iteration is true, and we are continuing the loop, that
means the output is nil after the previous iteration. With the current iteration, the output
will also be either nil or i
Termination: The loop terminates when the value is found, or no value is found, that
means nil. So the property is correct at termination.

2.1-4
Consider the problem of adding A to B, and storing result in C, where A and B are n-bit
binaries, and C is a (n+1)-bit binary. State the problem formally and write the procedure.

Answer:

Input: Two sequences of n-binaries A and B

Output: One sequence of (n+1)-binaries C, where C = A + B

Procedure:

modular := 0
for i = A.lenth -1 to 0:
C[i+1] = A[i] + B[i] + surplus
modular = 1 if at least two in three (A[i], B[i], modular) is 1
C[0] = modular

8
2.2 Analyzing Algorithms

2.2 Analyzing Algorithms


2.2-1
Express the function $$\frac{n^3}{1000} - 100n^2 - 100n + 3$$ in terms of $$\Theta$$
notations.

Answer:

$$\Theta(n^3)$$

2.2-2
Write pseudocode for selection sort

Answer:

FOR i = 0 to n - 2:
min := A[i]
min_index =: i
FOR j = i + 1 to n -2:
if A[j] < min:
min = A[j]
min_index = j
SWAP A[i], A[min_index]

Best case: $$\Theta(n^2)$$

Worst case: $$\Theta(n^2)$$

2.2-3
Linear search

Answer: Both average case and worst case are $$\Theta(n)$$

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

Answer: Probably modify based on the 'best-case' input.

9
2.2 Analyzing Algorithms

10
2.3 Designing Algorithms

2.3 Designing Algorithms


2.3-1
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}

Answer:

2.3-2
Rewrite the MERGE procedure so that it does not use sentinels, instead stopping once
either array L or R has had all its elements copied back to A and then copying the remainder
of the other array back to A

Answer:

11
2.3 Designing Algorithms

MERGE_SORT(A, start, end):


if start < end:
med := (start + end) / 2
MERGE_SORT(A, start, med)
MERGE_SORT(A, med + 1, end)
MERGE(A[start..med], A[(med+1)..end])

MERGE(A[start..med], A[(med+1)..end]):
i := start
j := med+1
if i > med:
append A[j..end] to output
else if j > end:
append A[i..med] to output
else if A[i] < A[j]
append A[i] to output
i++
else
append A[j] to output
j++

12

Potrebbero piacerti anche