• Keine Ergebnisse gefunden

Introduction to Mathematical Software 7

N/A
N/A
Protected

Academic year: 2022

Aktie "Introduction to Mathematical Software 7"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Introduction

to Mathematical Software 7 th Exercise Sheet

Department of Mathematics winter term 2009/2010

PD Dr. Ulf Lorenz 18/01/2010

Christian Brandenburg

Exercise 7.1Maple Worksheet

Obtain the Maple worksheetIMS_2009_2010_5.mwfrom the course webpage (it is located under theLecture Slidestab.

Open the worksheet in Maple and work through it line by line to understand what it does. Consider the Maple help if you are unsure what a particular command is good for or how it works.

Exercise 6.2The O-Notation and Complexity of Algorithms

In the analysis of algorithms, one very important issue is their efficiency for arbitrarily large inputs, i.e. we are interested in theirorder of growthand hence theircomplexity. One might argue that with the speed-up of modern computers the complexity of algorithms becomes less important. However, experience shows that the problems people try to solve grow much faster than the available computing power.

O-Notation

In this exercise we introduce the so calledO-notationthat is used to compare the efficiency of algorithms, in particular for arbitrarily large inputs.

Definition 1 Letg:N→Nbe a function. The setO(g)is defined as O(g) =

f :N→N|∃c>0,∃n0∈N,∀nn0: 0≤f(n)≤c g(n) .

IffO(g)(alternatively, we writef =O(g)), we say that “f is Big O ofg”, or thatgis anasymptotic upper boundfor f. This means that f does not grow faster than g for n→ ∞. Note in particular that f is asymptotically positive, i.e.

f(n)>0fornn0.

Definition 2 Letg:N→Nbe a function. The setΩ(g)is defined as Ω(g) =

f :N→N|∃c>0,∃n0∈N,∀nn0: 0≤c g(n)f(n) .

For f ∈Ω(g),gis called anasymptotic lower bound.

This means thatf grows at least as fast asgforn→ ∞. a) Show that f =O(g)if and only ifg= Ω(f).

Definition 3 Letg:N→Nbe a function. The setΘ(g)is defined as

Θ(g) =f :N→N|∃c1,c2>0,∃n0∈N,∀nn0: 0≤c1g(n)≤f(n)≤c2g(n) . For f ∈Θ(g), f andgare calledasymptotically equivalent.

Prove the following propositions:

b) Ifc>0is constant, thenc f = Θ(f).

1

(2)

c) f = Θ(g)if and only ifg= Θ(f).

d) Ifh(n) =O(g)and f(n)≤h(n)fornlarge enough, then f =O(g).

e) Leth1= Ω(g)andh2=O(g). Ifh1(n)≤f(n)≤h2(n)fornlarge enough, thenf = Θ(g).

The way in which a function f :N→ N behaves asymptotically w.r.t a function g :N →N can often be found by considering the limit ofn→ ∞:

f) If lim

n→∞

f(n)

g(n)=L,0≤L<∞, thenf =O(g).

g) If lim

n→∞

f(n)

g(n)=L,0<L≤ ∞, thenf = Ω(g).

h) If lim

n→∞

f(n)

g(n)=L,0<L<∞, thenf = Θ(g).

Complexity of Algorithms

To characterize the complexity of an algorithm, some generic complexity classes as functions ofnare used. These include powers of n (including n0), square roots, exponentials, logarithms and factorials and multiplicative combinations of these.

a) Sort the following orders of complexity from lowest to highest:

O(n2), O(3n), O(2n), O(n2lgn), O(1),O(nlgn), O(n3), O(n!), O(lgn), O(n).

The complexities of some common situations are given in the following table:

Complexity Example

O(1) Fetching the first element from a set of data; fetching a given element from an array O(lgn) Splitting a set of data in half recursively

O(n) Traversing a set of data

O(nlgn) Splitting a set of data in half recursively and traversing each half O(n2) Matrix-vector multiplication

O(n3) Matrix inversion

O(n!) Creating all possible permutations of a set of data b) What is the time complexity of the following code fragments?

• int p = 1;

int s = 0;

for (int i = 0; i < n; i++) p = p * a[i];

for( int i = 0; i < n; i++) s = s + a[i];

• for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)

printf("%d * %d = %d\n",i, j, i*j);

• for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {

c[i][j] = 0;

for (int k = 0; k < n; k++)

c[i][j] = c[i][j] + a[i][k] * b[k][j];

}

c) Suppose you have two algorithms to solve the same problem. The first runs in timeT1(n) =400n, the second runs in time T2(n) =n2. What are the complexities of these algorithms? For which values ofnmight we consider to use the algorithm with the higher complexity?

2

Referenzen

ÄHNLICHE DOKUMENTE

1.. a) Type the Hello World program (Example 2 on page 18 of part 1 of the Lecture Slides) into the editor window and save the program as hello.c in the exercise1 folder that you

In this exercise, we implement Euclid’s algorithm for finding the greatest common divisor of two natural numbers that you have come across in your linear algebra class. This will

Modify the greatest common divisor program that you have implemented in Exercise 2.1 such that the actual computation of the gcd is done in a function.. The function should accept

In the last lecture, you saw the implementation of a class for representing larger integers, BiggerInt, with an implemen- tation of the BiggerInt *add class member function.. a)

• a two-dimensional array of type double** (i.e. an array of pointers to arrays of type double) with the first index being the row index and the second index being the column

An vielen Türen hängen Plakate mit Rätseln, deren Lösung eine Zahl zwischen eins und

In the naïve approach we iteratively find the largest power of 2 which is less than or equal to the number we want to convert, place a 1 at the corresponding position of the

The algorithm is very inefficient as the number of recursive calls to Fib(n) grows exponentially in n. It is much more efficient to compute the Fibonacci numbers iteratively, as