Grundlagen der funktionsorientierten Programmierung mit SCHEME/Zeitaufwand und asymptotische Ordnung
Aus ProgrammingWiki
Wiki-Quelltext dieser Seite.
<language>SchemeJS</language>
<code id="5bb86948c29b1" hidden>
(define quotient
(lambda (m n)
(floor (/ m n))))
</code>
Wir haben bisher den Zeitaufwand der sequenziellen und binären Suche empirisch untersucht.<br />
Nun wollen wir durch allgemeine Überlegungen die Funktionen $T:\mathbb{N} \rightarrow \mathbb{R}^+$ bestimmen, die den Zeitaufwand der Algorithmen analytisch beschreiben. Vergleichbare Aufwandsfunktionen sollen zu einer ''Aufwandsklasse'' zusammengefasst werden.<br />
== Zeitaufwand bei der sequenziellen Suche ==
Die empirischen Untersuchungen unserer sequenziellen Suche ergaben: $T(n) \approx 0,64 \cdot n$.<br/ >
Wir beachten dabei, dass die gesuchte Zufallszahl nicht in jedem Fall Element der Zufallsliste gewesen sein muss.<br />
Bei der Suche eines Elements, das stets im Suchraum enthalten ist, gilt: $T(n) = \tfrac{n}{2}$.<br/ >
Für sehr große $n$ wird es aber nebensächlich, wie groß der Faktor $k$ in der Funktion $T(n) = k \cdot n$ ist. In beiden Fällen wächst der Zeitaufwand ''linear'' mit der Problemgröße.<br />
Da wir bei Komplexitätsuntersuchungen an Aussagen wie:
<blockquote>
''"Wenn $n$ ver-$x$-facht wird, dann ver-$y$-facht sich der Zeitaufwand."''
</blockquote>
interessiert sind, sollen deshalb konstante Faktoren und Summanden keine Rolle spielen.
== Asymptotische Ordnung ==
'''Definition:'''
<blockquote>
Die asymptotische Ordnung $\mathcal{O} (f)$ einer Funktion $f:\mathbb{N} \rightarrow \mathbb{R}^+$ beschreibt die Menge aller Funktionen $t:\mathbb{N} \rightarrow \mathbb{R}^+$, für die bei hinreichend großem $n$ gilt:<br />
$t(n) \le c \cdot f(n), \hspace{0.3em} c \in \mathbb{R}^+$.
</blockquote>
Mit der so genannten $\mathcal{O}$-Notation lassen sich also Algorithmen hinsichtlich ihres Zeitaufwandes klassifizieren. $\mathcal{O}$ heißt Landau-Symbol, benannt nach dem deutschen Zahlentheoretiker <b>Edmund Georg Hermann Landau</b> (* 14. Februar 1877; † 19. Februar 1938).
'''Beispiel:'''
<blockquote>
Für die sequenzielle Suche schreibt man: $T(n) = \mathcal{O}(n)$.
</blockquote>
== Zeitaufwand bei der binären Suche ==
Bei der binären Suche vermuten wir einen ''logarithmischen Zeitaufwand''. Um auch hier eine anschauliche Erklärung zu finden, konzentrieren wir uns zunächst auf die ''maximale'' Anzahl der notwendigen Teilungen (''Worst-case-Fall''):
<blockquote>[[Bild:BinSuche_Worst_Case.jpg]]</blockquote>
Bereits nach wenigen Beispielen lässt sich für den Worst-case-Fall vermuten: $2^{T-1}=n$.<br />
Wir wollen diese Vermutung mit der Prozedur <tt>binsuche-worst-case</tt> untermauern. Diese Prozedur zählt die in einer vorgegebenen Intervallgröße möglichen Halbierungenschritte bis zur elementaren Intervallgröße $1$.<br />
Mit der variabelstelligen Prozedur <tt>tabausgabe</tt> wird das entsprechende Werkzeug zur Bildschirmausgabe bereit gestellt:
<code id="5bb86948c2a6f">
(define binsuche-worst-case
(lambda (max)
(letrec
([teile
(lambda (li re maxanz)
(let ([mi (quotient (+ li re) 2)])
(cond
[(= (- re li) 1) maxanz]
[(> (- re mi) (- mi li)) (teile li mi (+ maxanz 1))]
[else
(teile mi re (+ maxanz 1))])))])
(teile 1 (+ max 1) 1))))
(define tabausgabe
(lambda (ls . lss)
(let ([ausgabe
(lambda (ls)
(string-append (number->string (car ls)) (string #\tab)))])
(if (null? ls)
'ok
(begin
(apply display (map ausgabe (cons ls lss)))
(apply tabausgabe (cdr ls) (map cdr lss)))))))
(define ls '(1 2 3 4 5 6 7 8))
</code>
<run id="508d57d77a582">
(set! ls '(1 2 3 4 5 6 7 8 15 16 31 32 63 64 127 128 255 256 511 512 1023 1024))
(define T-wc (map (lambda (n) (binsuche-worst-case n)) ls))
(tabausgabe (cons "n" ls) (cons "T-wc" T-wc))
</run>
Für den ''Worst-case-Fall'' können wir unsere Vermutung bestätigen. Es gilt offensichtlich:
<blockquote>$2^{T-1}=n$ und damit:</blockquote>
<blockquote>$T(n) = \log_2 n + 1$</blockquote>
Doch wie verhält sich die binäre Suche im Mittel (''Average-case-Fall'')?<br />
Wir definieren dazu Prozeduren, die nur noch die Anzahl der benötigten Versuche beim Raten einer Zahl bestimmen bzw. auswerten sollen:
<code id="5bb86948c2b2f">
(define zufallszahl
(lambda (max)
(+ 1 (random max))))
(define computerraten
(lambda (max)
(letrec
([suche
(lambda (li z re)
(let ([mi (quotient (+ li re) 2)])
(cond
[(= z mi) 1]
[(< z mi) (+ 1 (suche li z mi))]
[else
(+ 1 (suche mi z re))])))])
(suche 1 (zufallszahl max) (+ max 1)))))
(define computerraten-average-case
(lambda (max anzahl)
(letrec
([wiederhole
(lambda (n)
(if (< n 1)
0
(+ (computerraten max) (wiederhole (- n 1)))))])
(/ (wiederhole anzahl) anzahl))))
(define computerraten-worst-case
(lambda (max anzahl)
...))
</code>
Ergänzen Sie die Prozedur <tt>computerraten-worst-case</tt>, die im Intervall $[1;max]$ bei einer vorgegebenen Anzahl von Simulationen die höchste Anzahl der benötigten Versuche zum Raten einer Zahl ermittelt.
<check id="a4b72ac9505dd">
(and (= (computerraten-worst-case 8 100) 4) (= (computerraten-worst-case 32 100) 6))
</check>
<run id="5bb7cec050c1d">
(set! ls '(1 2 3 4 5 6 7 8 15 16 31 32 63 64 127 128 255 256 511 512 1023 1024))
(define T-ac (map (lambda (n) (computerraten-average-case n 100)) ls))
(define T-wc1 (map (lambda (n) (computerraten-worst-case n 100)) ls))
(define T-wc2 (map (lambda (n) (binsuche-worst-case n)) ls))
(tabausgabe (cons "n" ls) (cons "T-ac" T-ac) (cons "T-wc1" T-wc1) (cons "T-wc2" T-wc2))
</run>
Im ''Average-Case-Fall'' benötigt die binäre Suche bei hinreichend großem $n$ wenigstens einen Versuch weniger als im Worst-Case-Fall.<br />
Wir können also verallgemeinern:
<blockquote>$T(n) = \log_2 n$ bzw.:</blockquote>
<blockquote>$T(n) = \mathcal{O}(\log_2 n)$</blockquote>
== Zusammenfassung ==
Für Suchalgorithmen haben wir zwei Aufwandsklassen kennen gelernt:
<table border="0">
<tr>
<td><b>sequenzielle Suche: </b></td>
<td>$T(n) = \mathcal{O}(n)$</td>
</tr>
<tr>
<td><b>binäre Suche: </b></td>
<td>$T(n) = \mathcal{O}(\log_2 n)$</td>
</tr>
</table>
Wir wollen uns die funktionalen Zusammenhänge deutlich machen:
<code id="4bd433bc55be9">
(define x-von 0)
(define x-bis 100)
(define y-von 0)
(define y-bis 50)
(define log2
(lambda (x)
(/ (log x) (log 2))))
(define linsuche
(lambda (x)
x))
(define binsuche log2)
</code>
<canvas w="610" h="460"></canvas>
<code id="5bb7cec050b57" hidden>
(define nr 1)
(define genau 6)
(define breite 610)
(define hoehe 460)
(define rand 50)
(define canvas (eval (string->symbol (string-append "canvas" (number->string nr)))))
(define bez "x")
(define raster #f)
(define modulo
(lambda (m n)
(let ([k (floor (/ m n))])
(round (* (- (/ m n) k) n)))))
(define sqr
(lambda (x)
(* x x)))
(define black
(lambda (canvas)
(setcolor canvas 0 0 0)))
(define red
(lambda (canvas)
(setcolor canvas 255 0 0)))
(define brown
(lambda (canvas)
(setcolor canvas 100 50 0)))
(define orange
(lambda (canvas)
(setcolor canvas 255 100 0)))
(define yellow
(lambda (canvas)
(setcolor canvas 255 200 0)))
(define green
(lambda (canvas)
(setcolor canvas 0 255 0)))
(define blue
(lambda (canvas)
(setcolor canvas 0 0 255)))
(define pink
(lambda (canvas)
(setcolor canvas 200 50 200)))
(define n-tes-mod
(lambda (ls anz)
(list-ref ls (modulo anz (length ls)))))
(define genaurunden
(lambda (x anz)
(let ([n 0])
(if (not (= x 0))
(set! n (round (- anz (/ (log (abs x)) (log 10)) -0.5))))
(/ (round (* x (expt 10 n))) (expt 10 n)))))
(define groessenordnung
(lambda (x)
(if (= x 0)
1
(let ([pot (/ (log (abs x)) (log 10))])
(genaurunden (expt 10 (floor pot)) genau)))))
(define minimum
(lambda (x y)
(if (< x y) x y)))
(define maximum
(lambda (x y)
(if (> x y) x y)))
(define ungleich-null
(lambda (x y)
(if (= x y 0) (+ y 1) y)))
(define schaubild
(lambda (fkt-list x1 x2 y1 y2 von bis koordcolor color-list)
(clear canvas)
(setwidth canvas 1)
(let* ([textgroesse 12]
[rand 50]
[xu (minimum x1 x2)] [xo (maximum x1 x2)] [xo (ungleich-null xu xo)]
[yu (minimum y1 y2)] [yo (maximum y1 y2)] [yo (ungleich-null yu yo)]
[spmin rand] [spmax (- breite rand)] [spend (+ spmax 15)]
[zemin (- hoehe rand)] [zemax rand] [zeend (- zemax 15)]
[s00 (+ (* (/ (- xu) (- xo xu)) (- spmax spmin)) spmin)] [s0 s00]
[z00 (+ (* (/ (- yu) (- yo yu)) (- zemax zemin)) zemin)] [z0 z00]
[spvon (+ (* (/ (- von xu) (- xo xu)) (- spmax spmin)) spmin)]
[spbis (+ (* (/ (- bis xu) (- xo xu)) (- spmax spmin)) spmin)]
[mx (- xo xu)] [teilerx 1] [stepx (/ (- spmax spmin) mx teilerx)] [ex mx] [faktorx 1]
[my (- yo yu)] [teilery 1] [stepy (/ (- zemin zemax) my teilery)] [ey my] [faktory 1])
(letrec
([anpassen
(lambda (m)
(if (> m 2)
(if (<= m 20)
m
(anpassen (/ m 10)))
(if (> m 2)
m
(anpassen (* m 10)))))]
[teilen
(lambda (m)
(cond
[(< m 3) 5]
[(< m 10) 2]
[else 1]))]
[x-raster
(lambda (sx zy)
(if (> zy (- zemax 1))
(begin
(if (and (< zy (+ zemin 1)) (> (abs (- zy z0)) 1)) (line canvas sx zy sx zy))
(x-raster sx (- zy 3)))))]
[x-teilen-positiv
(lambda (sx)
(if (< sx (+ spmax 1))
(begin
(if (and (> sx (- spmin 1)) (> (abs (- sx s0)) 1))
(begin
(line canvas sx z0 sx (- z0 3))
(if raster (x-raster sx zemin))))
(x-teilen-positiv (+ sx stepx)))))]
[x-teilen-negativ
(lambda (sx)
(if (> sx (- spmin 1))
(begin
(if (and (< sx (+ spmax 1)) (> (abs (- sx s0)) 1))
(begin
(line canvas sx z0 sx (- z0 3))
(if raster (x-raster sx zemin))))
(x-teilen-negativ (- sx stepx)))))]
[y-raster
(lambda (sx zy)
(if (< sx (+ spmax 1))
(begin
(if (and (> sx (- spmin 1)) (> (abs (- sx s0)) 1)) (line canvas sx zy sx zy))
(y-raster (+ sx 3) zy))))]
[y-teilen-positiv
(lambda (zy)
(if (> zy (- zemax 1))
(begin
(if (and (< zy (+ zemin 1)) (> (abs (- zy z0)) 1))
(begin
(line canvas s0 zy (+ s0 3) zy)
(if raster (y-raster spmin zy))))
(y-teilen-positiv (- zy stepy)))))]
[y-teilen-negativ
(lambda (zy)
(if (< zy (+ zemin 1))
(begin
(if (and (> zy (- zemax 1)) (> (abs (- zy z0)) 1))
(begin
(line canvas s0 zy (+ s0 3) zy)
(if raster (y-raster spmin zy))))
(y-teilen-negativ (+ zy stepy)))))]
[faktor
(lambda (len step)
(if (<= len step)
1
(+ 1 (faktor (- len step) step))))]
[textwidth
(lambda (str)
(* textgroesse (/ 2 3) (string-length str)))]
[textout-widthcenter
(lambda (sx zy str unten)
(if unten
(if (integer? (string->number str))
(text canvas (- sx (/ (textwidth str) 2))
(+ zy (/ textgroesse (/ 2 3))) str textgroesse)
(text canvas (- sx (/ (textwidth str) 2) (- (/ textgroesse 2)))
(+ zy (/ textgroesse (/ 2 3))) str textgroesse))
(if (integer? (string->number str))
(text canvas (- sx (/ (textwidth str) 2))
(- zy textgroesse) str textgroesse)
(text canvas (- sx (/ (textwidth str) 2) (- (/ textgroesse 2)))
(- zy textgroesse) str textgroesse))))]
[textout-heightcenter
(lambda (sx zy str links)
(if links
(if (integer? (string->number str))
(if (> (string->number str) 0)
(text canvas (- sx (textwidth str) 6)
(+ zy (/ textgroesse 2)) str textgroesse)
(text canvas (- sx (textwidth str) 4)
(+ zy (/ textgroesse 2)) str textgroesse))
(text canvas (- sx (textwidth str) (- (/ textgroesse 2)) 4)
(+ zy (/ textgroesse 2)) str textgroesse))
(text canvas (+ sx textgroesse)
(+ zy (/ textgroesse 2)) str textgroesse)))]
[textout-bez-widthcenter
(lambda (sx zy str)
(text canvas (- sx (/ (textwidth str) 4))
(- zy textgroesse) str textgroesse))]
[textout-bez-heightcenter
(lambda (sx zy str)
(text canvas (+ sx (/ textgroesse 2))
(+ zy (/ textgroesse 2)) str textgroesse))]
[x-bezeichnen-positiv
(lambda (sx e unten)
(if (< sx (+ spmax 1))
(begin
(if (and (> sx (- spmin 1)) (> (abs (- sx s0)) 1))
(textout-widthcenter sx z0 (number->string (genaurunden e genau)) unten))
(x-bezeichnen-positiv (+ sx (* stepx faktorx)) (+ e (* ex faktorx)) unten))))]
[x-bezeichnen-negativ
(lambda (sx e unten)
(if (> sx (- spmin 1))
(begin
(if (and (< sx (+ spmax 1)) (> (abs (- sx s0)) 1))
(textout-widthcenter sx z0 (number->string (genaurunden e genau)) unten))
(x-bezeichnen-negativ (- sx (* stepx faktorx)) (- e (* ex faktorx)) unten))))]
[y-bezeichnen-positiv
(lambda (zy e links)
(if (> zy (- zemax 1))
(begin
(if (and (< zy (+ zemin 1)) (> (abs (- zy z0)) 1))
(textout-heightcenter s0 zy (number->string (genaurunden e genau)) links))
(y-bezeichnen-positiv (- zy (* stepy faktory)) (+ e (* ey faktory)) links))))]
[y-bezeichnen-negativ
(lambda (zy e links)
(if (< zy (+ zemin 1))
(begin
(if (and (> zy (- zemax 1)) (> (abs (- zy z0)) 1))
(textout-heightcenter s0 zy (number->string (genaurunden e genau)) links))
(y-bezeichnen-negativ (+ zy (* stepy faktory)) (- e (* ey faktory)) links))))]
[x-achse-bezeichnen
(lambda (str)
(textout-bez-heightcenter spend z0 str))]
[y-achse-bezeichnen
(lambda (str)
(let ([y-str (string-append "f(" str ")")])
(textout-bez-widthcenter s0 zeend y-str)))]
[graph-startpkt
(lambda (fkt pkt)
(let ([sx (car pkt)] [zy (cdr pkt)])
(if (< sx (+ spbis 1))
(let* ([x (+ (* (/ (- sx spmin) (- spmax spmin)) (- xo xu)) xu)] [y (fkt x)])
(if (real? y)
(begin
(set! zy (+ (* (/ (- y yu) (- yo yu)) (- zemax zemin)) zemin))
(if (> zy zemin) (set! zy zemin))
(if (< zy zemax) (set! zy zemax))
(cons sx zy))
(graph-startpkt fkt (cons (+ sx 1) zy))))
(cons sx zy))))]
[graph-zeichnen
(lambda (fkt pkt)
(let ([sx (car pkt)] [zy (cdr pkt)])
(if (< sx (+ spbis 1))
(let* ([sx-neu (+ sx 1)]
[x (+ (* (/ (- sx-neu spmin) (- spmax spmin)) (- xo xu)) xu)] [y (fkt x)])
(if (real? y)
(let ([zy-neu (+ (* (/ (- y yu) (- yo yu)) (- zemax zemin)) zemin)])
(if (> zy-neu zemin) (set! zy-neu zemin))
(if (< zy-neu zemax) (set! zy-neu zemax))
(if (and (< zy zemin) (> zy zemax) (< zy-neu zemin) (> zy-neu zemax))
(line canvas sx zy sx-neu zy-neu))
(graph-zeichnen fkt (cons sx-neu zy-neu)))
(graph-zeichnen fkt (graph-startpkt fkt (cons (+ sx 1) zy))))))))]
[graph-list-zeichnen
(lambda (fkt-ls anz color-ls)
(if (not (null? fkt-ls))
(let ([fkt (eval (car fkt-ls))])
((eval (n-tes-mod color-ls anz)) canvas)
(graph-zeichnen fkt (graph-startpkt fkt (cons spvon z0)))
(graph-list-zeichnen (cdr fkt-ls) (+ anz 1) color-ls))))])
(koordcolor canvas)
(if (< s0 spmin) (set! s0 spmin)) (if (> s0 spmax) (set! s0 spmax))
(if (> z0 zemin) (set! z0 zemin)) (if (< z0 zemax) (set! z0 zemax))
(line canvas spmin z0 spend z0)
(line canvas s0 zemin s0 zeend)
(line canvas (- spend 6) (- z0 2) spend z0)
(line canvas (- spend 6) (+ z0 2) spend z0)
(line canvas (- spend 6) (- z0 2) (- spend 6) (+ z0 2))
(line canvas (- s0 2) (+ zeend 6) s0 zeend)
(line canvas (+ s0 2) (+ zeend 6) s0 zeend)
(line canvas (- s0 2) (+ zeend 6) (+ s0 2) (+ zeend 6))
(set! mx (anpassen mx)) (set! teilerx (teilen mx))
(set! stepx (/ (- spmax spmin) mx teilerx))
(set! my (anpassen my)) (set! teilery (teilen my))
(set! stepy (/ (- zemin zemax) my teilery))
(x-teilen-positiv (+ s00 stepx)) (x-teilen-negativ (- s00 stepx))
(y-teilen-positiv (- z00 stepy)) (y-teilen-negativ (+ z00 stepy))
(set! ex (/ ex mx teilerx))
(set! faktorx (faktor
(* (/ 3 2) textgroesse (string-length (number->string (genaurunden ex genau))))
stepx))
(set! ey (/ ey my teilery))
(set! faktory (faktor
(* (/ 3 2) textgroesse)
stepy))
(if (<= (abs yu) (abs yo))
(begin
(x-bezeichnen-positiv (+ s00 (* stepx faktorx)) (* ex faktorx) #t)
(x-bezeichnen-negativ (- s00 (* stepx faktorx)) (- (* ex faktorx)) #t))
(begin
(x-bezeichnen-positiv (+ s00 (* stepx faktorx)) (* ex faktorx) #f)
(x-bezeichnen-negativ (- s00 (* stepx faktorx)) (- (* ex faktorx)) #f)))
(if (<= (abs xu) (abs xo))
(begin
(y-bezeichnen-positiv (- z00 (* stepy faktory)) (* ey faktory) #t)
(y-bezeichnen-negativ (+ z00 (* stepy faktory)) (- (* ey faktory)) #t))
(begin
(y-bezeichnen-positiv (- z00 (* stepy faktory)) (* ey faktory) #f)
(y-bezeichnen-negativ (+ z00 (* stepy faktory)) (- (* ey faktory)) #f)))
(x-achse-bezeichnen bez)
(y-achse-bezeichnen bez)
(if (< spvon spmin) (set! spvon spmin)) (if (> spbis spmax) (set! spbis spmax))
(graph-list-zeichnen fkt-list 0 color-list)
'ok))))
</code>
<run id="5bb7cec050ca3">
(schaubild '(linsuche binsuche) x-von x-bis y-von y-bis x-von x-bis black '(red blue))
</run>