Erinnerung VL vom 20.04.2016 Karatsuba-Ofman-Langzahlmultiplikation Vereinbarungen: Heute: Algorithmenanalyse (Asymptotik) Maschinenmodell (Variante des RAM-Modells) Mehr Vereinbarungen (Pseudocode, Graphen) Programmanalyse (Master-Theorem) Folgen und Listen (Motivation) KIT Institut für Theoretische Informatik 1
Pseudocode just in time Beispiel: Class Complex(x, y : Number) of Number Number r:= x Number i:= y Function abs : Number return r 2 + i 2 Function add(c : Complex) : Complex return Complex(r + c.r,i + c.i) KIT Institut für Theoretische Informatik 2
Design by Contract / Schleifeninvarianten assert: Aussage über Zustand der Programmausführung Vorbedingung: Bedingung für korrektes Funktionieren einer Prozedur Nachbedingung: Leistungsgarantie einer Prozedur, falls Vorbedingung erfüllt Invariante: Aussage, die an vielen Stellen im Programm gilt Schleifeninvariante: gilt vor / nach jeder Ausführung des Schleifenkörpers Datenstrukturinvariante: gilt vor / nach jedem Aufruf einer Operation auf abstraktem Datentyp Hier: Invarianten als zentrales Werkzeug für Algorithmenentwurf und Korrektheitsbeweis. KIT Institut für Theoretische Informatik 3
Beispiel (Ein anderes als im Buch) Function power(a : R; n 0 : N) : R p=a : R; r=1 : R; n=n 0 : N while n > 0 do return r if n is odd then n ; r:= r p else (n, p):= (n/2, p p) KIT Institut für Theoretische Informatik 4
Beispiel (Ein anderes als im Buch) Function power(a : R; n 0 : N) : R assert n 0 0 and (a = 0 n 0 = 0) // Vorbedingung p=a : R; r=1 : R; n=n 0 : N // p n r = a n 0 while n > 0 do invariant p n r = a n 0 // Schleifeninvariante (*) if n is odd then n ; r:= r p else (n, p):= (n/2, p p) assert r = a n 0 // (*) n = 0 Nachbedingung return r KIT Institut für Theoretische Informatik 5
Rechenbeispiel: 2 5 p=a = 2 : R; r=1 : R; n=n 0 = 5 : N // 2 5 1 = 2 5 while n > 0 do if n is odd then n ; r:= r p else (n, p):= (n/2, p p) Iteration p r n p n r 0 2 1 5 32 1 2 2 4 32 2 4 2 2 32 3 16 2 1 32 4 16 32 0 32 KIT Institut für Theoretische Informatik 6
Beispiel Function power(a : R; n 0 : N) : R assert n 0 0 and (a = 0 n 0 = 0) // Vorbedingung p=a : R; r=1 : R; n=n 0 : N // p n r = a n 0 while n > 0 do invariant p n r = a n 0 // Schleifeninvariante (*) if n is odd then n ; r:= r p else (n, p):= (n/2, p p) assert r = a n 0 // (*) n = 0 Nachbedingung return r neues n {}}{ Fall n ungerade: Invariante erhalten wegen p n n r = 1 p pr }{{} neues r KIT Institut für Theoretische Informatik 7
Beispiel Function power(a : R; n 0 : N) : R assert n 0 0 and (a = 0 n 0 = 0) // Vorbedingung p=a : R; r=1 : R; n=n 0 : N // p n r = a n 0 while n > 0 do invariant p n r = a n 0 // Schleifeninvariante (*) if n is odd then n ; r:= r p else (n, p):= (n/2, p p) assert r = a n 0 // (*) n = 0 Nachbedingung return r neues n {}}{ Fall n gerade: Invariante erhalten wegen p n n/2 = (p p) }{{} neues p KIT Institut für Theoretische Informatik 8
Programmanalyse Die fundamentalistische Sicht: Ausgeführte RAM-Befehle zählen einfache Übersetzungsregeln {}}{ Pseudocode Maschinenbefehle Idee: O( )-Notation vereinfacht die direkte Analyse des Pseudocodes. T (I ; I ) = T (I ) + T (I ). T (if C then I else I ) O(T (C) + max(t (I ), T (I ))). T (repeat I until C) O( i T (i-te Iteration)) Rekursion Rekurrenzrelationen KIT Institut für Theoretische Informatik 9
Schleifenanalyse Summen ausrechnen Das lernen Sie in Mathe Beispiel: Schulmultiplikation KIT Institut für Theoretische Informatik 10
Eine Rekurrenz für Teile und Herrsche Für positive Konstanten a, b, c, d, sei n = b k für ein k N. r(n) = { a cn + dr(n/b) falls n = 1 Basisfall falls n > 1 teile und herrsche. n cn 1 2 d n/b n/b... n/b............ 1 1 1 1... 1 1 a a a a... a a... 1 2 k KIT Institut für Theoretische Informatik 11
Master Theorem (Einfache Form) Für positive Konstanten a, b, c, d, sei n = b k für ein k N. r(n) = { a cn + dr(n/b) falls n = 1 Basisfall falls n > 1 teile und herrsche. Es gilt Θ(n) falls d < b r(n) Θ(n log n) falls d = b Θ ( n log d) b falls d > b. Übung: dasselbe gilt, wenn wir beliebige n N zulassen und r(n) = cn + dr( n/b ) für n > 1 setzen. KIT Institut für Theoretische Informatik 12
Beweisskizze Auf Ebene i haben wir d i Probleme @ n/b i = b k i d i c n b i = cn ( ) i d b ad k KIT Institut für Theoretische Informatik 13
Beweisskizze Fall d < b geometrisch schrumpfende Reihe erste Rekursionsebene kostet konstanten Teil der Arbeit ( ) k 1 i r(n) = a d }{{ k d } + cn Θ(n) b i=0 o(n) }{{} O(1) d=2, b=4 KIT Institut für Theoretische Informatik 14
Beweisskizze Fall d = b gleich viel Arbeit auf allen k = log b (n) Ebenen. r(n) = an + cn log b n Θ(n log n) d=b=2 KIT Institut für Theoretische Informatik 15
Beweisskizze Fall d > b geometrisch wachsende Reihe letzte Rekursionsebene kostet konstanten Teil der Arbeit ( ) i ) r(n) = ad k d + cn Θ(n logb d k 1 i=0 beachte: d k = 2 k log d = 2 k logb logb log d = b k logd logb = b k log b d = n log b d b d=3, b=2 KIT Institut für Theoretische Informatik 16
Master Theorem Beispiele Für positive Konstanten a, b, c, d, sei n = b k für ein k N. r(n) = { a cn + dr(n/b) falls n = 1 Basisfall schon gesehen, kommt noch, allgemeinerer Fall d < b: Median bestimmen d = b: mergesort, quicksort falls n > 1 teile und herrsche. d > b: rekursive Multiplikation, Karatsuba-Ofman-Multiplikation KIT Institut für Theoretische Informatik 17
Analyse im Mittel später an Beispielen Randomisierte Algorithmen später an Beispielen KIT Institut für Theoretische Informatik 18
Graphen Sie kennen schon (?): Relationen, Knoten, Kanten, (un)gerichtete Graphen, Kantengewichte, Knotengrade, knoteninduzierte Teilgraphen. Pfade (einfach, Hamilton-), Kreise, DAGs self loop s 1 1 2 t u s x K 5 H w z 2 2 w 1 1 y 1 x 1 w u v U t u K 3,3 v 1 v 1 1 G 2 u w v undirected w v bidirected KIT Institut für Theoretische Informatik 19
Bäume Zusammenhang, Bäume, Wurzeln, Wälder, Kinder, Eltern,... undirected s t undirected rooted r r directed r expression + r s t u s t u s t u a / u v v v rooted v 2 b KIT Institut für Theoretische Informatik 20
Ein erster Graphalgorithmus Ein DAG (directed acyclic graph, gerichteter azyklischer Graph) ist ein gerichteter Graph, der keine Kreise enthält. Function isdag(g = (V, E)) while v V : outdegree(v) = 0 do invariant G is a DAG i the input graph is a DAG V := V \ {v} E:= E \ ({v} V V {v}) return V =0 Analyse: kommt auf Repräsentation an (Kapitel 8), geht aber in O( V + E ). KIT Institut für Theoretische Informatik 21
Beispiel Function isdag(g = (V, E)) while v V : outdegree(v) = 0 do V := V \ {v} E:= E \ ({v} V V {v}) return V = 0 KIT Institut für Theoretische Informatik 22
Beispiel Function isdag(g = (V, E)) while v V : outdegree(v) = 0 do V := V \ {v} E:= E \ ({v} V V {v}) return V = 0 KIT Institut für Theoretische Informatik 23
Beispiel Function isdag(g = (V, E)) while v V : outdegree(v) = 0 do V := V \ {v} E:= E \ ({v} V V {v}) return V = 0 KIT Institut für Theoretische Informatik 24
Beispiel Function isdag(g = (V, E)) while v V : outdegree(v) = 0 do V := V \ {v} E:= E \ ({v} V V {v}) return V = 0 KIT Institut für Theoretische Informatik 25
Beispiel Function isdag(g = (V, E)) while v V : outdegree(v) = 0 do V := V \ {v} E:= E \ ({v} V V {v}) return V = 0 KIT Institut für Theoretische Informatik 26
Beispiel Function isdag(g = (V, E)) while v V : outdegree(v) = 0 do V := V \ {v} E:= E \ ({v} V V {v}) return V = 0 KIT Institut für Theoretische Informatik 27
Beispiel Function isdag(g = (V, E)) while v V : outdegree(v) = 0 do V := V \ {v} E:= E \ ({v} V V {v}) return V = 0 KIT Institut für Theoretische Informatik 28
Beispiel Function isdag(g = (V, E)) while v V : outdegree(v) = 0 do V := V \ {v} E:= E \ ({v} V V {v}) return V = 0 KIT Institut für Theoretische Informatik 29
Beispiel Function isdag(g = (V, E)) while v V : outdegree(v) = 0 do V := V \ {v} E:= E \ ({v} V V {v}) return V = 0 KIT Institut für Theoretische Informatik 30
Beispiel Function isdag(g = (V, E)) while v V : outdegree(v) = 0 do V := V \ {v} E:= E \ ({v} V V {v}) return V = 0 Leerer Graph. KIT Institut für Theoretische Informatik 31
P und NP das kommt in Theoretische Grundlagen der Informatik Ganz kurz: Es gibt einigermaÿen gute Gründe, ezient mit polynomiell gleichzusetzen (d. h. Laufzeit n O(1) ). Es gibt viele algorithmische Probleme (NP-vollständig/-schwer), bei denen es SEHR überraschend wäre, wenn sie in Polynomialzeit lösbar wären. KIT Institut für Theoretische Informatik 32
Folgen als Felder und Listen KIT Institut für Theoretische Informatik 33
Folgen spielen in der Informatik eine überragende Rolle. Das sieht man schon an der Vielzahl von Begrien: Folge, Feld, Schlange, Liste, Datei, Stapel, Zeichenkette, Log... (sequence, array, queue, list, le, stack, string, log... ). Wir unterscheiden: abstrakter Begri 2,3,5,7,9,11,... Funktionalität (stack,... ) Repräsentation und Implementierung Mathe Softwaretechnik Algorithmik KIT Institut für Theoretische Informatik 34
Anwendungen Ablegen und Bearbeiten von Daten aller Art Konkrete Repräsentation abstrakterer Konzepte wie Menge, Graph (Kapitel 8),... KIT Institut für Theoretische Informatik 35
Form Follows Function Operation List SList UArray CArray explanation ` ' [ ] n n 1 1 1 1 1 1 not with inter-list splice rst 1 1 1 1 last 1 1 1 1 insert 1 1 n n insertafter only remove 1 1 n n removeafter only pushback 1 1 1 1 amortized pushfront 1 1 n 1 amortized popback 1 n 1 1 amortized popfront 1 1 n 1 amortized concat 1 1 n n splice 1 1 n n ndnext,... n n n n cache-ecient KIT Institut für Theoretische Informatik 36