You are on page 1of 19

Lecture 4

Fundamental Computer Science I (Algorithms and Data Structures)


Prof. Dr. Andreas Nchter Research I, Room 99a Jacobs University Bremen
http://www.nuechti.de andreas@nuechti.de

Last Lectures
First attempt to define the central word algorithm An algorithm is any well-defined computational procedure that takes some value, or set of values as input and produces some value, or set of values as output. Definition of algorithm used in this lecture An algorithm is a finite sequence of instructions that can be carried out by a real machine. The sequence of instructions causes a defined input/output behavior on the machine. A deterministic algorithm is described by a partial function from the set of possible inputs to the set Algorithms and Data Structures Dr. Andreas Nchter 123 of 15, 2011 possible outputs. September

The Algorithms Man

Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

124

Donald E. Knuth
Beware of bugs in the above code; I have only proved it correct, not tried it. Donald Knuth His program TeX is considered as error free. I use the version 3.1415926 (frozen in March 2008) Donald Knuth offers monetary awards to people who find
and report a bug in TeX. The award per bug started at $2.56 (one "hexadecimal dollar") and doubled every year until it was frozen at its current value of $327.68. Knuth, however, has lost relatively little money as there have been very few bugs claimed. In addition, recipients have been known to frame their check as proof that they found a bug in TeX rather than cashing it. [wikipedia]
Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

125

Last Lecture
We dicussed an Euclids algorithm for computing the gcd

1. comment

2. comment

3. comment

Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

126

Last Lecture
Definition:

is called n-th Fibonacci number


Leonardo Pisano Bigollo (~1170 ~1250) also known as Leonardo of Pisa, Leonardo Pisano, Leonardo Bonacci, Leonardo Fibonacci, or, most commonly, simply Fibonacci A tiling with squares whose sides are successive Fibonacci numbers in length Algorithms and Data Structures
Dr. Andreas Nchter September 15, 2011

127

Last Lecture
Proof that the program for computing the greatest common divisor (gcd) is correct Analyzing algorithms
Input vs. input length Testing und running the program for several inputs Counting # of operations, e.g., modulo operations (gcd), comparisons (sorting/searching), additions and multiplications (math programs)

Landau / Big-Oh-Notation

Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

128

Last Lecture Big Oh-Notation

Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

129

Landau Notation
Example: Estimation for When analyzing algorithms we need often the upper and lower bound of . Since

we have

Therefore

is in the same order as

Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

130

Landau Notation
Proposition: Proof: Lets look at the power series of the exponential function:

Therefore . With proved the proposition

we have

Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

131

Landau Notation
Application (Sketch): Lets look at sorting algorithms, that use one comparison operation in one step. Lets call the minimal number of comparison operation to sort elements. Binary tree-like search gives Hence

Later in this lecture we will even show


Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

132

Landau Notation
Theorem: Suppose to be a monotonic growing unbounded function, i.e., and assume then it holds: ,

With other words: An exponential function grows faster that a polynomial one. Proof: Exercise!

Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

133

Landau Notation
The importance of the asymptotical considerations becomes clear when looking at the following table. Run times for fixed input length are listed for differently efficient algorithms and differently fast computers

Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

134

Example Previous Larger Element


As an example of the use of summations in algorithm analysis, consider the following simple problem Given: A sequence Wanted: For each element for we want to know the index of the rightmost element of the sequence whose value is strictly larger than If no element of this subsequence is larger than then by convention the index is 0. Or if you like, you may imagine that there is a fictitious sentinel value
Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

135

Example Previous Larger Element


More formally, for , define to be

where A more visual way to understand this problem is to image that is the height of the ith telephone pole in a sequence, and if we shoot a bullet horizontally to the left from the top of the ith pole, we want to know which pole we will hit first.

Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

136

Example Previous Larger Element


There is an time solution to this problem. (You should think about it.) However, I discuss a less efficient time algorithm.

Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

137

Example Previous Larger Element


Correctness The correctness of this algorithm is almost trivial, but let us make a couple of observations. The inner while loop has two ways of terminating, one if in which case we have found a larger element, and the other if , implying that no larger element was found. Runtime The time spent in this algorithm is dominated by the time spent in the inner ( ) loop.
Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

138

Example Previous Larger Element


On the ith iteration of the outer loop, the inner loop is executed from down to either 0 or the first index whose associated value exceeds In the worst case, this loop will always go all the way to 0. (Can you see what sort of input would give rise to this case?) Thus the total running time (up to constant factors) can be expressed as:

Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

139

Example Previous Larger Element


We can solve this summation directly by applying the formula for the arithmetic series, which yields

An interesting question to consider at this point is, what would the average-case running time be if the elements of the array were random values. Note that if is large, it seems that it would be quite unlikely to go through all iterations of the inner while loop, because the chances of coming across a larger element would seem pretty high. But how many iterations would you expect on average?
Algorithms and Data Structures Dr. Andreas Nchter September 15, 2011

140

You might also like