• Keine Ergebnisse gefunden

Discrete Mathematics 2. Exercise Sheet

N/A
N/A
Protected

Academic year: 2022

Aktie "Discrete Mathematics 2. Exercise Sheet"

Copied!
6
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Algorithmic

Discrete Mathematics 2. Exercise Sheet

Department of Mathematics SS 2012

PD Dr. Ulf Lorenz 2. and 3. May 2012

Dipl.-Math. David Meffert Version of June 21, 2012

Groupwork

Exercise G1 (Master-Theorem)

Determine, if possible, fixed bounds for the complexities of the recurrences (a) T(n) =4T(n2) +n3,

(b) T(n) =4T(n2) +n, (c) T(n) =4T(n2) +n2logn, (d) T(n) =4T(n2) +n2. Hint:

Solution: Throughout the whole exercise we havelogba=log24=2.

(a) We have f(n) =n3. So f(n)∈Ω(n2+"), because of0≤1·n3f(n). Furthermore4·f(2n) =4n3

8 = 12n3c·f(n), holds forc=12. So by the third case of the Master-Theorem we concludeT(n)∈Θ(n3).

(b) We have f(n) =n. So f(n)∈O(n2−")for"=1because of f(n)≤1·n. By the first case of the Master-Theorem we concludeT(n)∈Θ(n2).

(c) We havef(n) =n2logn. We immediately see f(n)6∈O(n2−")and f(n)6∈Θ(n2). We want to show that third case of the Master-Theorem can’t be used either, because the second condition can’t be fulfilled. Lec∈(0, 1). We get

f(n

2)≤c·f(n)

⇔ 4·n2

4 log(n

2)≤c·n2log(n)

n2log(n

2)≤c·n2log(n)

⇔ log(n)−log(2)≤c·log(n)

⇔ log(n)−1≤c·log(n)

⇔ −1≤log(n)(c−1)

⇔ −1

≥log(n)

(2)

This can’t hold for all n ∈N because{log(n)| n∈N} is not bounded. So c∈ (0, 1)ist not a possible choice.

By this example we can see that although the Master-Theorem is quite powerful it can’t be used for alle types of recurrences.

(d) We havef(n) =n2. Sof(n)∈Θ(n2)because of0≤1·n2f(n)≤1·n2. By the second case of the Master-Theorem we concludeT(n)∈Θ(n2logn).

Exercise G2 (Complexity)

(a) Let f,t:N→Rbe functions with fO(t). ProveO(f) +O(t)⊆O(t)andO(f) +O(f)⊆O(t).

(b) Does33+nO(3n)hold?

(c) Does33nO(3n)hold?

(d) Show thatO(fO(g) =O(f ·g)holds for f,g:N→R+.

Remark: For real valued functions f,g:N →R one just substitutes f(n), g(n)with|f(n)|,|g(n)|in the definition of O(g).

Solution:

(a) Letg,h:N→RwithgO(f)andhO(t). By definition we getng,nh∈N,cg,ch∈Rwith

|g(n)| ≤cg|f(n)| and |h(n)| ≤ch|t(n)|

for allnng,nh. Furthermore by assumption we getnf ∈N and cf ∈Rwith|f(n)| ≤cf|t(n)|for all nnf. Putting things together we conclude

|g(n) +h(n)| ≤cg|f(n)|+ch· |t(n)| ≤cgcf|t(n)|+ch· |t(n)|

= (cgcf +ch)|t(n)|

for alln≥max{ng,nh,nf}. So we getg+hO(t).

The second inclusion can be proved the same way or alternatively by showingO(f)⊆O(t).

(b) We have33+nO(3n)because of33+n=27·3n≤27·3nfor alln∈N. (c) The term33n=27nis obviously not inO(3n).

(d) By definition we have

hO(f)⇔ ∃ch,nh h(n)chf(n) ∀nnh and

kO(g)⇔ ∃ck,nk k(n)≤ckg(n) ∀nnk. So forhO(f)andkO(g)we have

(h·k)(n)≤ckch(f ·g)(n) ∀n≥max{nh,nk}. and thereforeh·kO(f ·g). This proves the first inclusion.

For the second one letlO(f ·g). This means there existnl∈Nandcl∈Rwithl(n)≤cl(f ·g)(n)for allnnl. Now setl=f ·fl. Obviously fO(f)and by dividing the last inequality by f(n)we get

l f

(n)≤clg(n)for all nnl. So we have flO(g).

Exercise G3 (Algorithms)

(a) Given two algorithmsAandB:

• AlgorithmAhas complexityO(f).

• AlgorithmBhas complexityO(g).

We want to look at two new algorithms usingAandB.

Algorithm 1 INPUT :n∈N fori=1, ..., 100do

run algorithm A end for

fori=1, ...,n2 do run algorithm B end for

(3)

Algorithm 2 if n≥30then

run algorithm A else

run algorithmus B end if

We already knowf ∈Ω(g). Determine the best possible estimates for the runtime of both algorithms.

(b) Take a look at algorithm3and determine the best possible estimate for its runtime. Justify you answer.

Algorithm 3 INPUT : n∈N m = n

whilem > 1do forj = 1,...,n

2do a=3·b c = a +b end for m =1

2·m end while

Solution:

(a) For the runtime of algorithm1we get100·O(f) +n2·O(g). Because we already knowf ∈Ω(g)we can summarize that toO(f ·h)withh(n) =n.

For algorithm2we notice that only the runtime forn→ ∞is important. So only the second case of theif-part is relevant. Hence algorithm2has runtimeO(f).

(b) We go through the outer looplogntimes. The inner loop we go throughn

2 times. Ignoring any constant factors we getO(nlogn)for the runtime of the algorithm.

Exercise G4 (Sets) Order the functions

n2,p

n,n!,nn,n

by their complexity. Start with lowest complexity and use theo-notation. Determinen0dependend onc>0in every of those cases, too.

Remark:

fo(g):⇐⇒ ∀c>0∃n0∈N∀nn0: 0≤f(n)<c g(n)

Solution:

pno(n) n01

c2

£

no(n2) n01

c

£

n2o(n!) n0=max¦

6, 1

c

£©

n!o(nn) n0=max¦

3, 1

c

£©

For explanation: In last two cases we have chosenn0≥6,n0≥3becausen!>n3holds forn≥6andnn>n·n!holds

(4)

Exercise H4 (Asymptotics) (14 points) (a) Prove that forr1,r2∈R+we havenr1O(nr2)andr1nO(r2n)iffr1r2.

(b) Prove the following statements for functions f,t:N→R: i. O(f) +O(f)⊆O(f).

ii. O(fO(t)⊆O(f ·t).

iii. max{f,t} ∈Θ(f +t)for f,t≥0.

Solution:

(a) For alln∈N the statementnr1cnr2 is equivalent tonr1r2c. The function nx is bounded iff x ≤0, which meansr1r2.

The second statement can be proved the same way. For alln∈Nthe statementr1nc r2nis equivalent tor

1 r2

n

c.

The functionxnis bounded iff x≤1, which meansr1r2.

(b) The proofs all work the same way in general by playing around with the definitions.

i. Letg,h:N→Rwithg,hO(f). By definition we havecg,ch∈Randng,nh∈Nwith

|g(n)| ≤cg|f(n)| and |h(n)| ≤ch|f(n)|

for allnng,nh. We conclude

|g(n) +h(n)| ≤ |g(n)|+|h(n)| ≤(cg+ch)|f(n) for alln≥max{ng,nh}, which meansg+hO(f).

ii. Letg,h:N→RwithgO(f)andhO(t). By definition we havecg,ch∈Randng,nh∈Nwith

|g(n)| ≤cg|f(n)| and |h(n)| ≤ch|t(n)|

for allnng,nh. We conclude

|g(n)·h(n)|=|g(n)| · |h(n)| ≤cg|f(n)| ·ch|t(n)|= (cgch)|(f ·t)(n)|. for alln≥max{ng,nh}, which meansg·hO(f ·t).

iii. We want to proof the inequality

max{f,t}(n)≥ 1

2(f(n) +t(n)) (1)

pointwise for alln∈Nand therefore distinguish two cases. For everyn∈Nwith f(n)≥t(n)we get max{f,t}(n) =f(n) = 1

2(f(n) +f(n))≥1

2(f(n) +t(n)).

For all othern∈Nwitht(n)f(n)we get max{f,t}(n) =t(n) =1

2(t(n) +t(n))≥1

2(f(n) +t(n))

the same way. So by equation (1) we concludemax{f,t} ∈ Ω(f +t). Because of the obvious inequality max{f,g}(n)≤(f +g)(n)we getmax{f,g} ∈O(f +g). Hence we havemax{f,g} ∈Θ(f +g).

Exercise H5 (A sorting algorithm) (10 points)

The algorithmSortListsorts a sequence of numbers in ascending order.

Algorithm 4SortList(list)

INPUT: sequence of numbers,list=a1, ...,an,ai∈N ifn <=1then

returnlist else

leftlist=a1, ...,adn

2e

rightlist=adn

2e+1, ...,an

return Sort(SortList(lelftlist),SortList(rightlist)) end if

(5)

Algorithm 5Sort(rightlist,leftlist) INPUT: two sequences of numbers:

rightlist=a1, ...,al,leftlist=b1, ...,bk,ai,bi∈N newlist

whilerightlistandleftlistnot emptydo

iffirst element ofleftlist<= first element ofrightlistthen

append first element ofleftlisttonewlistand delete it fromleftlist else

append first element ofrightlisttonewlistand delete it fromrightlist end if

end while

whileleftlistnot emptydo

append first element ofleftlisttonewlistand delete it fromleftlist end while

whilerightlistnot emptydo

append first element ofrightlisttonewlistand delete it fromleftlist end while

returnnewlist

(a) Sort the sequence9, 10, 7, 3, 1, 2, 12, 9, 23in ascending order by using the algorithmSortList. Make sure to include detailed steps for the algorithm in your solution to indicate that you understand how it works.

(b) What is the runtime of the algorithmSortList?

Solution:

(a) We use the short termSfor the algorithmSortand writeS(rightlist;leftlist). For the algorithmSortListwe use the short termS L. So we have

S L(9, 10, 7, 3, 1, 2, 12, 9, 23) S(S L(9, 10, 7, 3, 1);S L(2, 12, 9, 23))

 S(S(S L(9, 10, 7);S L(3, 1));S(S L(2, 12);S L(9, 23)))

 S(S(S(S L(9, 10);S L(7));S(S L(3);S L(1)));S(S(S L(2);S L(12));S(S L(9),S L(23))))  S(S(S(S(S L(9),S L(10)); 7);S(3; 1));S(S(2; 12);S(9, 23)))

 S(S(S(S(9, 10); 7);S(3; 1));S(S(2; 12);S(9, 23)))  S(S(S(9, 10; 7);S(3; 1));S(S(2; 12);S(9, 23)))  S(S(7, 9, 10; 1, 3);S(2, 12; 9, 23))

 S(1, 3, 7, 9, 10; 2, 9, 12, 23)  1, 2, 3, 7, 9, 9, 10, 12, 23.

Now we want to show the the last step in detail and thereby demonstrate how theSortalgorithm works. We use S(rightlist;leftlist;newlist)to indicate all the steps and get

S(1, 3, 7, 9, 10; 2, 9, 12, 23;;) S(3, 7, 9, 10; 2, 9, 12, 23; 1) S(3, 7, 9, 10; 9, 12, 23; 1, 2)

 S(7, 9, 10; 9, 12, 23; 1, 2, 3) S(9, 10; 9, 12, 23; 1, 2, 3, 7)  S(10; 9, 12, 23; 1, 2, 3, 7, 9) S(10; 12, 23; 1, 2, 3, 7, 9, 9)  S(;; 12, 23; 1, 2, 3, 7, 9, 9, 10) S(;; 23; 1, 2, 3, 7, 9, 9, 10, 12)  S(;;;; 1, 2, 3, 7, 9, 9, 10, 12, 23) 1, 2, 3, 7, 9, 9, 10, 12, 23.

(b) The given algorithm is calledMergeSortand is a recursive algorithm. We haveT(1) =1and get the recurrence

T(n) = T(n 2)

| {z }

Sor t List(l e f t l ist)

+ T(n 2)

| {z }

Sor t List(r i ght l ist)

+ n

|{z}

Sor t

=2T(n 2) +n.

By the Master-Theorem(second case) we concludeT(n)∈Θ(nlogn).

(6)

Given algorithm 6. What does the algorithm? Determine its runtime.

Algorithm 6 INPUT :n∈N K1 = 2;

K2 = n;

whileK2 > K1do K2 = n/K1

ifdK2e==K2then return K1 else

K1=K1+1 end if end while return 0

Solution: The algorithm tests if a given numbern∈N is prime. This is done by checking all possible divisors from 2, . . . ,bp

nc. The checking part works by dividingnbyi∈ {2, . . . ,bp

nc}and looking if this fraction is a natural number.

If a divisor is found the algorithm returns this divisor and otherwise it returns0. In the case of output0the numbernis prime. The relevant part for the runtime (while-condition) is usedp

ntimes, so the runtime isΘ(p n)).

Referenzen

ÄHNLICHE DOKUMENTE

They describe the number of possibilities to choose k objects from a given set containing n objects (without putting objects back and without respecting the order of the

Make sure to include detailed steps for the algorithm in your solution to indicate that you understand how it works. (b) What is the runtime of the

The capacities on the edges should be natural numbers and the maximal flow x from s to t is given... Let s be the source in this graph and t

They describe the number of possibilities to choose k objects from a given set containing n objects (without putting objects back and without respecting the order of the

Now keep on following the cycle C till the first node v 1 ∈ V 0 \{ V 1 } .We repeat this procedure till we went through the whole cycle C and get to the starting vertex v

Now reduce the flow on the whole path P 1 ( u, v ) P 2 by 1, so the general flow for the graph is valid again, with a maximal flow reduced by 1.. It could be the case now that this

As solving this problem exactly is time-consuming, a greedy evaluation procedure is used for approximating the quality of intermediate leader solution candidates, which is described

Users of the PACE TR-48 Analog Computer will find the High Speed Repetitive Operation Group a· valuable aid in the solution of a variety of computing