• Keine Ergebnisse gefunden

Sample Solution

N/A
N/A
Protected

Academic year: 2022

Aktie "Sample Solution"

Copied!
3
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

University of Freiburg Dept. of Computer Science Prof. Dr. F. Kuhn

Algorithms and Data Structures Summer Term 2021

Sample Solution Exercise Sheet 2

Exercise 1: O-notation

Prove or disprove the following statements. Use the set definition of the O-notation (lecture slides week 2, slides 11 and 12).

(a) 4n3+ 8n2+n∈ O(n3) (b) 2n∈o(n!)

(c) 2 logn∈Ω((logn)2)

(d) max{f(n), g(n)} ∈Θ(f(n) +g(n)) for non-negative functions f and g.

Sample Solution

(a) True. Choose n0 = 1 and c= 13. For n≥n0 we have n3 ≥n2 ≥n and hence 4n3 + 8n2+n≤ 13n3 =cn3.

(b) True. Letc >0. Choose n0 = max{1c,8}. Forn≥n0 we have c·n!

n≥1/c

≥ 1

n·n! = (n−1)!≥(n−1)·(n−2)·. . .· bn

2cn≥8≥ 4n2 = 2n (c) False. Letc >0. We have

2 logn ≥ c(logn)2

⇔ 2 ≥ clogn

2c ≥ logn

⇔ 41c ≥ n

So for a givenn0 ≥1 choosen= max{n0,41c}+1. For thisnwe haven > n0and 2 logn < c(logn)2. (d) True. Choosen0 = 1,c1 = 12 and c2 = 1. For n≥n0 we have

c1·(f(n) +g(n))≤max{f(n), g(n)}f,g≥0≤ c2(f(n) +g(n))

Exercise 2: Sorting by asymptotic growth

Sort the following functions by their asymptotic growth. Write g <O f if g ∈ O(f) and f /∈ O(g).

Write g=O f iff ∈ O(g) and g∈ O(f) (no proof needed).

√n 2n n! log(n3)

3n n100 log(√

n) (logn)2

logn 10100n (n+ 1)! nlogn

2(n2) nn

logn (2n)2

(2)

Sample Solution

plogn <O log(√

n) =O logn=O log(n3)<O(logn)2<O

√n <O 10100n <O nlogn

<On100 <O 2n<O 3n<O(2n)2<O n!<O (n+ 1)!<Onn<O 2(n2)

Exercise 3: Stable Sorting

A sorting algorithm is called stable if elements with the same key remain in the same order. E.g., assume you want to sort the following strings where the sorting key is the first letter by alphabetic order:

[“tuv”,“adr”,“bbc”,“tag”,“taa”,“abc”,“sru”,“bcb”]

A stable sorting algorithm must generate the following output:

[“adr”,“abc”,“bbc”,“bcb”,“sru”,“tuv”,“tag”,“taa”]

A sorting algorithm is not stable (with respect to the sorting key) if it outputs, e.g., the following:

[“abc”,“adr”,“bbc”,“bcb”,“sru”,“taa”,“tag”,“tuv”]

(a) Which sorting algorithms from the lecture (except CountingSort) are not stable? Prove your statement by giving an appropriate example.

(b) Describe a method to make any sorting algorithm stable, without changing theasymptoticruntime.

Explain.

Sample Solution

(a) • Selection Sort is not stable. Consider as input the array [x, y, z] withx.key=y.key= 1 and z.key= 0. In the first step,xandzare swapped, becausezhas the smallest key in the array.

So we get [z, y, x]. This array will not be altered in the second step (asy.key=x.key), i.e., it equals the output of Selection Sort. Sox and y have been swapped.

• Quicksort is not stable. Consider as input the array [x, y, z, w] with x.key = 1, y.key = z.key = 2 andw.key = 0 and assume x is taken as pivot. In the first divide step, y and w are swapped (i.e., we get [x, w, z, y]) and the array is divided into [x, w] and [z, y]. Recursive sorting yields [w, x] and [z, y] and thus [w, x, z, y] will be returned. So y and z have been swapped.

• Mergesort: If you implement Mergesort according to the pseudocode on page 26 of lecture 01, Mergesort is not stable. The reason is the condition A[i] < A[j] in line 7 of the code which may cause elements with the same key to change order. If we instead use the condition A[i]≤A[j], we make the algorithm stable.

(b) Add the number i to the key of the i-th element in the array (i.e., set A[i].key = (A[i].key, i)).

Now run the given (non-stable) sorting algorithm according to the lexocographic ordering1 on this new set of keys. That is, we sort according to the original keys and use the index in A as tie breaker.

Changing the keys takes time O(n). Additionally, each comparison between two elements is prolonged by an additional O(1) steps. As any sorting algorithm takes Ω(n), the asymptotic runtime does not change.

1Let (A, <A) and (B, <B) be ordered sets. The lexicographic ordering<lexonA×Bis defined by (a, b)<lex(a0, b0) :⇔

a <Aa0(a=a0b <Bb0)

(3)

Exercise 4: Running time

Give an asymptotically tight upper bound for the running time of the following algorithm as a function of n.

s←0

for i= 1 to ndo j= 1

while j < ido s←s+i·j j ←2·j

Sample Solution

For each i, the running time of the internal while is proportional to log2i. Hence, the total running time is proportional to Pn

i=1log2i. For an upper bound, note that this sum is upper bounded by Pn

i=1log2n = nlog2n = O(nlogn). In order to show that it is tight, note that the sum is lower bounded by Pn

i=n/2log2(n/2) = (n/2) log(n/2) = Ω(nlogn). Hence the running time is Θ(nlogn).

Referenzen

ÄHNLICHE DOKUMENTE

„c) Kraftstoff in Behältern von als Ladung beförderten mobilen Maschinen und Geräten gemäß Definition in Artikel 2 der Richtlinie 97/68/EG, wenn er für den

a) At every node there are n − 1 send and n − 1 receive events, so 2n − 2 is a lower bound on the clock value. This bound is matched if all messages are sent before any message

Assume that there exists a randomized algorithm that solves 2-coloring in o(n) with success probability at least 0.9 , this implies that for large enough n the algorithm runs in

We conclude that on combinatorial optimization problems where the solution representation is not compact, using a solution archive with linked trie results in a substantial

The algorithm is basically a lo- cal search using the swap neighborhood structure and candidate solutions are evaluated by solving the MAXCAP problem by means of integer programming

Based on an existing framework, which will be described in more detail in Section 6.1, a solution archive for the Reconstruction of Cross-Cut Shredded Text Documents (RCCSTD)

„Gasen, die in Brennstoffbehältern oder -flaschen* von Fahrzeugen, mit denen eine Beförde- rung durchgeführt wird, enthalten sind und die für deren Antrieb oder den Betrieb einer ihrer

The drawbacks which arise with this approach are, first, that the fuzzy set theory needs some additional information like the membership function of the parameters, which is often