Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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:
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:
4
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:
Or:
$$
$$ f'(n) = 1 - 8 \frac{1}{n\ln 2}
$$
$$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-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:
2.1-2
Rewrite the INSERTION-SORT procedure to sort into non-increasing instead of non-
decreasing order
Answer:
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
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:
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
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]
2.2-3
Linear search
2.1-4
How can we modify almost any algorithm to have a good best-case running time?
9
2.2 Analyzing Algorithms
10
2.3 Designing Algorithms
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(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