CSElab Computational Science & Engineering Laboratory http://www.cse-lab.ethz.ch Martin Maag November 30, 2010 Rekursion Informatik für Mathematiker und Physiker - Serie 10 Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratoryhttp://www.cse-lab.ethz.ch
Martin Maag November 30, 2010
Rekursion
Informatik für Mathematiker und Physiker - Serie 10
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Übersicht• Nachbesprechung Serie 8
• Vorbesprechung Serie 10• Rekursion
2
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Serie 8, Aufgabe 1
3
Zurich¨Technische HochschuleEidgenossische¨
Swiss Federal Institute of Technology ZurichPolitecnico federale di ZurigoEcole polytechnique federale de Zurich´ ´
Informatik fur Mathematiker und Physiker Serie 8 HS10
http://www.ti.inf.ethz.ch/ew/courses/Info1 10/
Aufgabe 1 (4 Punkte)
Skript-Aufgabe 81 (4 Punkte)
inverse_matrix.cpp 3 3 A
A−1 A
AA−1
Hint:A−1
ij A−1 i j
A−1ij =
(−1)i+j (Aji)
(A),
(M) M Aji 2 2
A j i
3 3
Skript-Aufgabe 85 (4 Punkte)
string_matching.cpp m > 1,n
m s m t n
string_matching.cpp m(n − m + 1)
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Serie 8, Aufgabe 1
4
Zurich¨Technische HochschuleEidgenossische¨
Swiss Federal Institute of Technology ZurichPolitecnico federale di ZurigoEcole polytechnique federale de Zurich´ ´
Informatik fur Mathematiker und Physiker Losung 8 HS10
http://www.ti.inf.ethz.ch/ew/courses/Info1 10/
Aufgabe 1
• Erweitere 2D Array um 1 zusätzliche Dimension• {oben, rechts, unten, links}
• 4 mögliche Ziele• Ziel mit jeder Ausrichtung erreicht
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Serie 8, Aufgabe 3
5
for (bool* r=present; r<present+n; ++r) *r = false;
// read number into the array and remember that it was read
for (int* p=a; p<a+n; ++p) {std::cin >> *p;if (*p >= 0 && *p < n)
present [*p] = true;}
// check whether we have read all numbers in {0,... ,n -1}
bool ok = true;for (bool* r=present; r<present+n; ++r)
if (!*r) {std::cout << "input sequence does not encode a permutation .\n";ok = false;break;
}
if (ok) {// do the cycle decomposition . Here we reuse the array present
// and remove from it all numbers that we have already put into
// some cycle
std::cout << "cycle decomposition is ";int next = 0; // next number not yet put into a cycle
while (next < n) {// output cycle starting with next; we must come back to next
// below: assuming we would come back to some other element
// on the cycle , that element would have two preimages under
// pi , a contradiction
const int first = next;std::cout << "( ";do {
std::cout << next << " ";present[next] = false;next = a[next]; // next -> pi(next)
} while (next != first );std::cout << ") ";
// find start element of next cycle
while (! present[next]) ++next;}std::cout << "\n";
}// delete arrays
delete [] present;delete [] a;
return 0;
}
Solution to Exercise 85. s = a . . . ab m − 1 a b t = a . . . an a m
bn − m + 1 {1, . . . ,m} {n − m + 1, . . . , n}m(n − m + 1)
Solution to Exercise 86.
// Program: threedim_array .cpp
Exercise 85m > 1, n m s m t n
m(n−m+ 1)
Exercise 86
#include <iostream >
int main(){
int a[4][2][3] ={ // the 4 elements of a:
{ // the 2 elements of a[0]:
{2, 4, 5}, // the three elements of a[0][0]
{4, 6, 7} // the three elements of a[0][1]
},{ // the 2 elements of a[1]:
{1, 5, 9}, // the three elements of a[1][0]
{4, 6, 1} // the three elements of a[1][1]
},{ // the 2 elements of a[2]:
{5, 9, 0}, // the three elements of a[2][0]
{1, 5, 3} // the three elements of a[2][1]
},{ // the 2 elements of a[3]:
{6, 7, 7}, // the three elements of a[3][0]
{7, 8, 5} // the three elements of a[3][1]
}};
return 0;
}
threedim_array.cppa int
a
Exercise 87 frequencies.cpp
char{0, 1, . . . , 127}
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Programmieren• Festplattenspeicher ist nicht mehr so teuer
• Lesbarkeit wichtiger alsKompaktheit
6
//Output; invA ausgebenstd::cout<<"\n"<<"invA= \n";for(int m=0;m<3;++m){ for(int n=0;n<3;++n){std::cout <<invA[m][n]<<" ";if((n+1)%3==0)std::cout<<"\n"; }}
//Output; invA ausgebenstd::cout << "\n" << "invA= \n";for(int m = 0; m < 3; ++m){! for(int n = 0; n < 3; ++n){! ! std::cout << invA[m][n] << " ";! ! if((n+1)%3 == 0)! ! ! std::cout << "\n"; ! }}
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Rekursion• Funktionswert durch sich selbst definiert
• Beispiele:• Fakultät:
• 0! = 1, n! = n (n-1)!
• Fibonacci Reihe:• f(0) = 0, f(1) = 1, f(n) = f(n-1) + f(n-2)
• Ackermannfunktion: A(m,n):• m = 0 -> n + 1
• m > 0, n = 0 -> A(m-1,1)
• m > 0, n > 0 -> A(m-1, A(m,n-1))
7
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Beispiele
8
// POST: return value is n!int fac(int n) { ! if n<=0 return 1;! return n*fac(n-1);}
// POST: return value is the n-th Fibonacci number F_nunsigned int fib (unsigned int n){ if (n == 0) return 0; if (n == 1) return 1; return fib(n-1) + fib(n-2); // n > 1}
// POST: return value is the Ackermann function value A(m,n)unsigned int A (unsigned int m, unsigned int n) { if (m == 0) return n+1; if (n == 0) return A(m-1,1); unsigned int param = A(m, n-1); return A(m-1, param);}
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Beispiele
9
T(n)
Exercise 122 n 2
2
n
2 2
n
2= 2 n − 1.
Exercise 123(Σ, P, s)
Σ = {F,+,−} s = F + F + F + F P
F → FF + F + F + F + F + F − F.
Σ = {X, Y,+,−} s = Y P
X → Y + X + Y
Y → X − Y − X.
α = 60 X Y
X → X + Y + +Y − X − −XX − Y +
Y → −X + YY + +Y + X − −X − Y.
Exercise 1241, 2, 3
n
2(1, 2) 1 3 (1, 3)
2 3 (2, 3)hanoi.cpp
n n = 2(1, 2)(1, 3)(2, 3)
n = 3
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Türme von Hanoi
• Verschiebe Turm mit n Scheiben von a nach b• 3 Positionen ∈ {1,2,3}
• Keine Scheibe darf auf eine kleinere Scheibe gestellt werden
• Idee für n Scheiben:• Verschiebe Turm mit n-1 Scheiben von a nach c
• a+b+c = 6, c=6-a-b
• Verschiebe Scheibe n von a nach b
• Verschiebe Turm mit n-1 Scheiben von c nach b
10
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Beispiele
11
// PRE: a and b are different and both in {1,2,3}// POST: the sequence of moves necessary to transfer a stack of n// disks from peg a to peg b is written to standard outputvoid hanoi (const unsigned int n, const int a, const int b){ if (n > 0) { // move topmost n-1 disks from a to helper peg 6-a-b hanoi (n-1, a, 6-a-b); // move bottommost disk from a to b std::cout << "(" << a << "," << b << ")"; // move the n-1 disks from the helper peg to the b hanoi (n-1, 6-a-b, b); }}
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Aufgabe 10.2 (116)
12
Zurich¨Technische HochschuleEidgenossische¨
Swiss Federal Institute of Technology ZurichPolitecnico federale di ZurigoEcole polytechnique federale de Zurich´ ´
Informatik fur Mathematiker und Physiker Serie 10 HS10
http://www.ti.inf.ethz.ch/ew/courses/Info1 10/
Skript-Aufgabe 115 (5 Punkte)
n
k, n, k N
n
k:=
n!
k!(n − k)!,
n
k:=
0, n < k
1, n = k k = 0n−1
k+ n−1
k−1, n > k, k > 0
,
n
k:=
0, n < k
1, n k, k = 0n
k
n−1
k−1n k, k > 0
Skript-Aufgabe 116 (6 Punkte)
(20), (10, 10), (10, 5, 5), (5, 5, 5, 5)
4
partition.cpp
// PRE: [first , last) is a valid nonempty range that describes// a sequence of denominations d_1 > d_2 > ... > d_n > 0// POST: return value is the number of ways to partition amount// using denominations from d_1 , ..., d_nunsigned int partitions (unsigned int amount ,
const unsigned int* first ,const unsigned int* last);
Skript-Aufgabe 121 (5 Punkte)
x
// PRE: [first , last) is a valid range , and the elements *p,// p in [first , last) are in ascending order// POST: return value is a pointer p in [first , last) such// that *p = x, or the pointer last , if no such pointer// existsconst int* binary_search (const int* first , const int* last , const int x){
const int n = last - first;if (n == 0) return last; // empty rangeif (n == 1) {
if (*first == x)return first;
elsereturn last;
}// n >= 2const int* middle = first + n/2;if (* middle > x) {
// x can’t be in [middle , last)const int* p = binary_search (first , middle , x);if (p == middle)
return last; // x not foundelse
return p;} else
// *middle <= x; we may skip [first , middle)return binary_search (middle , last , x);
}
T(n) x
n T(n)
Aufgaben 125 126 Challenge Aufgaben
Abgabe:
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Aufrufstapel (Call Stack)
• Variabeln der Funktion kommen beim Aufruf auf einen Stapel
• Funktion arbeitet jeweils mit den obersten
• Nach Ende des Aufrufs werden Variabeln gelöscht
• Bsp: fac(3)
13
// POST: return value is n!int fac(int n) { ! if n<=0 return 1;! return n*fac(n-1);}
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Rekursion vs Iteration• Rekursion kann durch Iteration ersetzt werden
• Rekursive Formulierungen sind:• einfacher zu schreiben
• aber weniger effizient• Funktionswerte mehrmals ausgewertet
• Call Stack
• Beispiel: Die Fibonacci-Zahlen• (siehe Vorlesung-slides)
14
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Rekursion vs Iteration
15
// Prog: rec2it2.cpp// rewrites a recursive function in iterative form a la Fibonacci#include <iostream >
unsigned int f (const unsigned int n){
if (n <= 2) return 1;return f(n-1) + 2 * f(n-3);
}
unsigned int f_it (const unsigned int n){
if (n <= 2) return 1;unsigned int a = 1; // f(0)unsigned int b = 1; // f(1)unsigned int c = 1; // f(2)for (unsigned int i = 3; i < n; ++i) {
const unsigned int a_prev = a; // f(i -3)a = b; // f(i -2)b = c; // f(i -1)c = b + 2 * a_prev; // f(i)
}return c + 2 * a; // f(n -1) + 2 * f(n -3)
}
int main(){
std::cout << "Comparing f and f_it ...\n";for (int n = 0; n < 100; ++n)
std::cout << f(n) << " = " << f_it(n) << "\n";
return 0;}
Solution to Exercise 119.
n/2
// Prog: rec2it2.cpp// rewrites a recursive function in iterative form by using an array#include <iostream >
unsigned int f (const unsigned int n){
if (n == 0) return 1;return f(n-1) + 2 * f(n/2);
}
unsigned int f_it (const unsigned int n){
if (n == 0) return 1;unsigned int* const f_values = new unsigned int[n+1]; // f(0) ,... ,f(n)f_values [0] = 1;for (unsigned int i=1; i<=n; ++i)
f_values[i] = f_values[i-1] + 2 * f_values[i/2];const unsigned int result = f_values[n];delete [] f_values;return result;
}
// Prog: rec2it2.cpp// rewrites a recursive function in iterative form a la Fibonacci#include <iostream >
unsigned int f (const unsigned int n){
if (n <= 2) return 1;return f(n-1) + 2 * f(n-3);
}
unsigned int f_it (const unsigned int n){
if (n <= 2) return 1;unsigned int a = 1; // f(0)unsigned int b = 1; // f(1)unsigned int c = 1; // f(2)for (unsigned int i = 3; i < n; ++i) {
const unsigned int a_prev = a; // f(i -3)a = b; // f(i -2)b = c; // f(i -1)c = b + 2 * a_prev; // f(i)
}return c + 2 * a; // f(n -1) + 2 * f(n -3)
}
int main(){
std::cout << "Comparing f and f_it ...\n";for (int n = 0; n < 100; ++n)
std::cout << f(n) << " = " << f_it(n) << "\n";
return 0;}
Solution to Exercise 119.
n/2
// Prog: rec2it2.cpp// rewrites a recursive function in iterative form by using an array#include <iostream >
unsigned int f (const unsigned int n){
if (n == 0) return 1;return f(n-1) + 2 * f(n/2);
}
unsigned int f_it (const unsigned int n){
if (n == 0) return 1;unsigned int* const f_values = new unsigned int[n+1]; // f(0) ,... ,f(n)f_values [0] = 1;for (unsigned int i=1; i<=n; ++i)
f_values[i] = f_values[i-1] + 2 * f_values[i/2];const unsigned int result = f_values[n];delete [] f_values;return result;
}
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Rekursion vs Iteration• Wie bei Iterationen: Endlosschleifen möglich
16
// POST: return value is n!int fac(int n) { ! if (n<=0) return 1;! return n*fac(n-1);}
// POST: return value is n!int fac2(int n) { ! if (n==0) return 1;! return n*fac2(n-1);}
Endlosschleife für negative Zahlen
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Rekursion
17
Ackermann function
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Rekursion
18
Ackermann function
{if (n == 0) return 1;return f(f(n-1) - 1);
}
f f
ff(n) = 1 n
20
(a, b) Fn
A(m,n)
A(m,n) =
n + 1, m = 0A(m − 1, 1), m > 0, n = 0A(m − 1,A(m,n − 1)), m > 0, n > 0
A A
// POST: return value is the Ackermann function value A(m,n)
unsigned int A (const unsigned int m, const unsigned int n) {if (m == 0) return n+1;if (n == 0) return A(m-1,1);return A(m-1, A(m, n -1));
}
m 3 A(m,n) m = 4A(4, 1)
A(4, 2) 265536−320, 000 A(4,3)
A(n, n)n A
• Evaluiere A(1,2)
• A(4,3)?
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Rekursion
19
Ackermann function
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Aufgabe 115
20
Zurich¨Technische HochschuleEidgenossische¨
Swiss Federal Institute of Technology ZurichPolitecnico federale di ZurigoEcole polytechnique federale de Zurich´ ´
Informatik fur Mathematiker und Physiker Serie 10 HS10
http://www.ti.inf.ethz.ch/ew/courses/Info1 10/
Skript-Aufgabe 115 (5 Punkte)
n
k, n, k N
n
k:=
n!
k!(n − k)!,
n
k:=
0, n < k
1, n = k k = 0n−1
k+ n−1
k−1, n > k, k > 0
,
n
k:=
0, n < k
1, n k, k = 0n
k
n−1
k−1n k, k > 0
Skript-Aufgabe 116 (6 Punkte)
(20), (10, 10), (10, 5, 5), (5, 5, 5, 5)
4
partition.cpp
// PRE: [first , last) is a valid nonempty range that describes// a sequence of denominations d_1 > d_2 > ... > d_n > 0// POST: return value is the number of ways to partition amount// using denominations from d_1 , ..., d_nunsigned int partitions (unsigned int amount ,
const unsigned int* first ,const unsigned int* last);
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Aufgabe 101
21
T(n)
Exercise 122 n 2
2
n
2 2
n
2= 2 n − 1.
Exercise 123(Σ, P, s)
Σ = {F,+,−} s = F + F + F + F P
F → FF + F + F + F + F + F − F.
Σ = {X, Y,+,−} s = Y P
X → Y + X + Y
Y → X − Y − X.
α = 60 X Y
X → X + Y + +Y − X − −XX − Y +
Y → −X + YY + +Y + X − −X − Y.
Exercise 1241, 2, 3
n
2(1, 2) 1 3 (1, 3)
2 3 (2, 3)hanoi.cpp
n n = 2(1, 2)(1, 3)(2, 3)
n = 3
• ∃k: n=2k
• sonst ∃k:
�log2�n
2�� = �log2�
n
2�� = �log2n� − 1 = k − 1
2k < n < 2k+1
2k−1 <n
2< 2k 2k−1 < �n
2� ≤ 2k2k−1 ≤ �n
2� < 2k
k − 1 ≤ log2�n
2� ≤ log2�
n
2� ≤ k
�log2�n
2�� = k
�log2n� = k + 1
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Merge Sort
• divide et impera / divide and conquer• Teile
• Teile Array in zwei Hälften
• Herrsche• Sortiere eine Hälfte, dann die andere
• Führe beide Hälften zusammen (Merge)
22
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Merge Sort
23
Sort the following numbers using the merge sort algorithm.38, 27, 43, 3, 9, 82, 10
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Merge Sort
24
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Komplexitätsanalyse
• n: Länge des Arrays
• T(n): Zeit / Operationen, um Array zu sortieren
• T(n) ≤ (n-1)(⎡log2 n⎤)
• Beweis per Induktion
• T(1): 1
25
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Komplexitätsanalyse
26
!"#$"%&'#()*+,-./"0(
' 1&*$02(3 45*6*7*3*4!5 89"6*:*3 4#5 89$6*:*5 %;
7*4!5 89" %;6*#2'$ !5 89" $ :7*4!5 89" %;6*#2'$9*!5 89" $ :4#5 89$ %;6*#2'$9*#5 89$ $ :*5 %;7*4!5 89" %;6*4*#2'$9*5 $ % ;6*:4#5 89$ %;6*4*#2'$9*5 $ % ;*6:*5 %;7*45 < ;6*4*#2'$9*5 $ % ;6*:*5 %;=*45 < ;6*4*#2'$9*5 $ 6
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Beispiele
27
// PRE: [first, last) is a valid range// POST: the elements *p, p in [first, last) are in //! ascending order void merge_sort (int* first, int* last) {! int n = last - first;! if (n <= 1) return;! ! ! // nothing to do ! int* middle = first + n/2; ! merge_sort (first, middle);! // sort first half ! merge_sort (middle, last);!! // sort second half ! merge (first, middle, last);! // merge both halves}
// PRE: [first, middle), [middle, last) are valid ranges; in // both of them, the elements are in ascending order void merge (int* first, int* middle, int* last) {! int n = last - first; // total number of cards ! int* deck = new int[n]; // new deck to be built!! int* left = first;!! // top card of left deck ! int* right = middle;! // top card of right deck!! for (int* d = deck; d != deck + n; ++d)! ! // put next card onto new deck! ! if (left == middle) *d = *right++;! // left deck is empty ! ! else if (right == last)! *d = *left++;! // right deck is empty ! ! else if (*left < *right) *d = *left++;!! // smaller top card left ! ! else! *d = *right++;! ! // smaller top card right!! // copy new deck back into [first, last)! int *d = deck; ! while (first != middle) *first++ = *d++;! while (middle != last) *middle++ = *d++;!! delete[] deck;}
Tuesday, November 30, 2010
CSElabComputational Science & Engineering Laboratory
http://www.cse-lab.ethz.ch
Informatik (D-MATH, D-PHYS)
Aufgabe 121
28
Skript-Aufgabe 121 (5 Punkte)
x
// PRE: [first , last) is a valid range , and the elements *p,// p in [first , last) are in ascending order// POST: return value is a pointer p in [first , last) such// that *p = x, or the pointer last , if no such pointer// existsconst int* binary_search (const int* first , const int* last , const int x){
const int n = last - first;if (n == 0) return last; // empty rangeif (n == 1) {
if (*first == x)return first;
elsereturn last;
}// n >= 2const int* middle = first + n/2;if (* middle > x) {
// x can’t be in [middle , last)const int* p = binary_search (first , middle , x);if (p == middle)
return last; // x not foundelse
return p;} else
// *middle <= x; we may skip [first , middle)return binary_search (middle , last , x);
}
T(n) x
n T(n)
Aufgaben 125 126 Challenge Aufgaben
Abgabe:
Tuesday, November 30, 2010