Grundbegriffe der Informatik Kapitel 14: Der Begriff des Algorithmus (einige grundlegende Aspekte) Thomas Worsch KIT, Institut für Theoretische Informatik Wintersemester 2015/2016 GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 1 / 37
Wo sind wir? Es war einmal... Lösen einer Sorte quadratischer Gleichungen Zum informellen Algorithmusbegriff Einführung des Hoare-Kalküls Algorithmus zur Multiplikation nichtnegativer ganzer Zahlen GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 2 / 37
Eine Zeitreise... wohin?... Wie weit in die Vergangenheit kann man reisen und findet noch etwas, was mit Informatik zu tun hat? (jenseits von Zählen und Zahlen) GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 3 / 37
... da wären wir... Zeit: circa 825 830 Ort: Bagdad, Haus der Weisheit GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 4 / 37
... da wären wir... Zeit: circa 825 830 Ort: Bagdad, Haus der Weisheit Muhammad ibn Mūsā al-khwārizmī geboren ca. 780 in Khiva (heute Usbekistan) oder Qutrubbull (heute Iran) gestorben ca. 850 Bildquelle: http://en.wikipedia.org/wiki/image: Abu_Abdullah_Muhammad_bin_Musa_al-Khwarizmi.jpg GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 4 / 37
Zwei wichtige Schriften von al-khwārizmī (1) Al-Kitāb al-mukhtaṣar fī hīsāb al-ğabr wa l-muqābala oder Al-Kitāb al-mukhtaṣar fī ḥisāb al-jabr wa-l-muqābala Buch von ca. 830 (?) deutsch: Das kurzgefasste Buch zum Rechnen durch Ergänzung und Ausgleich Aus al-ğabr bzw. al-jabr entstand später das Wort Algebra. Inhalt des Buches unter anderem: Lösen quadratischer Gleichungen mit einer Unbekannten. GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 5 / 37
Zwei wichtige Schriften von al-khwa rizmı (2) Titel vielleicht Kita b al-jam wa-l-tafrı q bi-h.isa b al-hind ca. 825 (??) Über das Rechnen mit indischen Ziffern führt die Zahl Null in das arabische Zahlensystem ein... nur noch Übersetzungen, z. B. auf Lateinisch, 12. Jhdt. (?): Titel unbekannt, Vermutung: Algorismi de numero Indorum o. ä. also ein Buch von Al-gorismi über die indischen Zahlen. Das i am Ende von Algorismi später fälschlicherweise als Pluralendung des Wortes Algorithmus angesehen. GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 6 / 37
Wo sind wir? Es war einmal... Lösen einer Sorte quadratischer Gleichungen Zum informellen Algorithmusbegriff Einführung des Hoare-Kalküls Algorithmus zur Multiplikation nichtnegativer ganzer Zahlen GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 7 / 37
Lösen einer Sorte quadratischer Gleichungen nach al-khwārizmī (1) gegeben: quadratische Gleichung der Form x 2 + bx = c mit b > 0 und c > 0 al-khwārizmī: positive Lösung findet man so: h b/2 (1) q h 2 (2) s c + q (3) w s (4) x w h (5) s nie negativ am Ende x > 0 und Lösung von x 2 + bx = c GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 8 / 37
Wo sind wir? Es war einmal... Lösen einer Sorte quadratischer Gleichungen Zum informellen Algorithmusbegriff Einführung des Hoare-Kalküls Algorithmus zur Multiplikation nichtnegativer ganzer Zahlen GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 9 / 37
Algorithmusbegriff informell Eigenschaften des eben gezeigten Algorithmus: endliche Beschreibung elementaren Anweisungen jede offensichtlich effektiv in einem Schritt ausführbar Determinismus: nächste elementare Anweisung stets eindeutig festgelegt, nur auf Grund von schon berechneten Ergebnissen und zuletzt ausgeführter elementare Anweisung endlicher Eingabe endliche Ausgabe endliche viele Schritte nur endlich oft eine elementare Anweisung ausgeführt beliebig große Eingaben bearbeitbar Nachvollziehbarkeit/Verständlichkeit des Algorithmus Algorithmus jedem (mit der Materie vertrauten) klar GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 10 / 37
Diskussion des informellen Algorithmusbegriffs obige Forderungen sind plausibel aber informell: Was heißt offensichtlich effektiv ausführbar? harte Beweise benötigen einen präziseren Algorithmusbegriff GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 11 / 37
Diskussion des informellen Algorithmusbegriffs obige Forderungen sind plausibel aber informell: Was heißt offensichtlich effektiv ausführbar? harte Beweise benötigen einen präziseren Algorithmusbegriff auch Verallgemeinerungen sind interessant randomisierte Algorithmen Zufall beeinflusst Auswahl eines Schrittes Online-Algorithmen Eingaben stehen erst nach und nach zur Verfügung nicht terminierende Berechnungen z. B. Ampelsteuerung und noch mehr... GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 11 / 37
Diskussion des informellen Algorithmusbegriffs obige Forderungen sind plausibel aber informell: Was heißt offensichtlich effektiv ausführbar? harte Beweise benötigen einen präziseren Algorithmusbegriff auch Verallgemeinerungen sind interessant randomisierte Algorithmen Zufall beeinflusst Auswahl eines Schrittes Online-Algorithmen Eingaben stehen erst nach und nach zur Verfügung nicht terminierende Berechnungen z. B. Ampelsteuerung und noch mehr... Sind Programme für die Mima Algorithmen? GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 11 / 37
Wo sind wir? Es war einmal... Lösen einer Sorte quadratischer Gleichungen Zum informellen Algorithmusbegriff Einführung des Hoare-Kalküls Algorithmus zur Multiplikation nichtnegativer ganzer Zahlen GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 12 / 37
Korrektheit eines Algorithmus wie beweist man sie? ad hoc Beispiel von al-khwārizmī systematisch verschiedene Möglichkeiten Beispiel Hoare-Tripel GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 13 / 37
Beweis von al-khwārizmī x 2 b/4 GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 14 / 37
Beweis von al-khwārizmī (b/4)x b/4 (b/4)x x 2 (b/4)x (b/4)x b/4 b/4 b/4 GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 14 / 37
Beweis von al-khwārizmī b/4 x 2 b/4 b/4 x 2 + bx = c b/4 GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 14 / 37
Beweis von al-khwārizmī b/4 x 2 b/4 b/4 x 2 + bx = c 4 b 2 /16 = b 2 /4 = q b/4 GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 14 / 37
Beweis von al-khwārizmī b/4 x 2 b/4 b/4 x 2 + bx = c 4 b 2 /16 = b 2 /4 = q b/4 c + q = (b/4 + x + b/4) 2 c + q b/2 = x GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 14 / 37
Beweis durch Nachrechnen { b > 0 c > 0 } h b/2 { h = b/2 } q h 2 { q = b 2 /4 } s c + q { s = c + b 2 /4 } w s { w = c + b 2 /4 } x w h { x = c + b 2 /4 b/2 } { x 2 + bx = ( c + b 2 /4 b/2) 2 + b( c + b 2 /4 b/2) } { x 2 + bx = c } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 15 / 37
Beweis durch Nachrechnen { b > 0 c > 0 } h b/2 { h = b/2 } q h 2 { q = b 2 /4 h = b/2 } s c + q { s = c + b 2 /4 h = b/2 } w s { w = c + b 2 /4 h = b/2 } x w h { x = c + b 2 /4 b/2 } { x 2 + bx = ( c + b 2 /4 b/2) 2 + b( c + b 2 /4 b/2) } { x 2 + bx = c } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 16 / 37
Eine einfache Programmiersprache Terminalsymbole Zuweisungssymbol Schlüsselwörter if, then, else, fi Schlüsselwörter while, do, od, for, to Symbole für Konstanten, Funktionen und Relationen Produktionen der kontextfreien Grammatik Prog Stmt Stmt Prog Stmt Var Expr if Bool then Prog else Prog fi while Bool do Prog od for Var Expr to Expr do Prog od Expr... Terme... (geeignet) Bool... Formeln... (geeignet) Var... Variablennamen... (geeignet) GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 17 / 37
Hoare-Tripel Programmstück mit Zusicherungen {P} S {Q} S Programmstück P Vorbedingung Q Nachbedingung P, Q Zusicherungen prädikatenlogische Formeln frei vorkommende Variablen Variablen eines Programms, von dem S ein Teil ist nicht notwendig Variablen, die in S vorkommen GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 18 / 37
Zusicherungen in Hoare-Tripeln Wahrheit von Zusicherungen abhängig von Interpretation und Variablenbelegung relevante Interpretationen: nur manche interessieren Grundbereich D immer fest und explizit spezifiziert Funktions- und Relationssymbole immer naheliegend interpretiert Konstantensymbole beliebig interpretierbar in D für alle Interpretationen sollen Zusicherungen wahr sein bei uns Eingaben für das Programm Variablenbelegungen beschreiben Gesamtzustand des Speichers Veränderung durch Zuweisungen GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 19 / 37
Bedeutung von Programmen Zuweisung x E für Term E wenn vorher Variablenbelegung β dann hinterher Variablenbelegung β = β val D, I, β (E) x also fast alles unverändert, nur x hat nun den Wert val D,I, β (E) Bedeutung der Kontrollstrukturen im Kapitel zur Mima angedeutet hier keine formale Definition GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 20 / 37
Gültigkeit von Hoare-Tripeln {P} S {Q} gültig, wenn für jede relevante Interpretation I und jede Variablenbelegung β gilt: wenn val D,I, β (P) = w und wenn die Ausführung von S für I und β endet und hinterher Variablenbelegung β vorliegt dann val D,I, β (Q) = w GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 21 / 37
Hoare-Kalkül Axiome und Ableitungsregeln für Hoare-Tripel Ziel Axiome und Ableitungsregeln für Hoare-Tripel so, dass genau die gültigen Hoare-Tripel ableitbar sind GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 22 / 37
Axiome für Hoare-Kalkül für Zuweisungen Voraussetzungen Zuweisung x E von Ausdruck E L Ter Q Nachbedingung zu x E σ {x /E } sei kollisionsfrei für Q HT-A: wenn σ {x /E } kollisionsfrei für Q, dann {σ {x /E } (Q)} x E {Q} ein Axiom diese Tripel sind gültig GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 23 / 37
Axiome für Hoare-Kalkül für Zuweisungen Voraussetzungen Zuweisung x E von Ausdruck E L Ter Q Nachbedingung zu x E σ {x /E } sei kollisionsfrei für Q HT-A: wenn σ {x /E } kollisionsfrei für Q, dann {σ {x /E } (Q)} x E {Q} ein Axiom diese Tripel sind gültig Man geht rückwärts vor! vorwärts «klappt nicht» andere Schreibweisen (uuuuh, ooooh) {Q[E/x]} x E {Q} oder {[E/x]Q} x E {Q} {Q[x/E]} x E {Q} (das erlauben wir) GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 23 / 37
Ableitungsregeln HT-E und HT-S im Hoare-Kalkül HT-E: Verstärkung der Vorbedingung und Abschwächung der Nachbedingung unter den Voraussetzungen P P und Q Q ist ({P} S {Q}, {P } S {Q }) eine Regel, also {P} S {Q} {P } S {Q } HT-S: Hintereinanderausführung {P} S 1 {Q} {P} S 1 ; S 2 {R} {Q} S 2 {R} Gültigkeit bleibt erhalten GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 24 / 37
Beispiel zeige Ableitbarkeit von {x = a} y x; z y {z = a} { x = a } y x; z y { z = a } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 25 / 37
Beispiel zeige Ableitbarkeit von {x = a} y x; z y {z = a} { x = a } y x z y { z = a } auseinander ziehen GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 25 / 37
Beispiel zeige Ableitbarkeit von {x = a} y x; z y {z = a} { x = a } y x { y = a } z y { z = a } auseinander ziehen HT-A: {y = a} z y {z = a} ist ableitbar GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 25 / 37
Beispiel zeige Ableitbarkeit von {x = a} y x; z y {z = a} { x = a } y x { y = a } { y = a } z y { z = a } auseinander ziehen HT-A: {y = a} z y {z = a} ist ableitbar HT-A: {x = a} y x {y = a} ist ableitbar GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 25 / 37
Beispiel zeige Ableitbarkeit von {x = a} y x; z y {z = a} { x = a } y x { y = a } { y = a } z y { z = a } auseinander ziehen HT-A: {y = a} z y {z = a} ist ableitbar HT-A: {x = a} y x {y = a} ist ableitbar HT-S: fertig GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 25 / 37
Beispiel (2) Algorithmus von al-khwārizmī { b > 0 c > 0 } h b/2 q h 2 s c + q w s x w h 1 { x = c + b 2 /4 b/2 A } { x 2 + bx = c x > 0 } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) Algorithmus von al-khwārizmī { b > 0 c > 0 } h b/2 q h 2 s c + q w s 2 { w h = c + b 2 /4 b/2 A } x w h 1 { x = c + b 2 /4 b/2 A } { x 2 + bx = c x > 0 } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) Algorithmus von al-khwārizmī { b > 0 c > 0 } h b/2 q h 2 s c + q 3 { s h = c + b 2 /4 b/2 A } w s 2 { w h = c + b 2 /4 b/2 A } x w h 1 { x = c + b 2 /4 b/2 A } { x 2 + bx = c x > 0 } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) Algorithmus von al-khwārizmī { b > 0 c > 0 } h b/2 q h 2 4 { c + q h = c + b 2 /4 b/2 A } s c + q 3 { s h = c + b 2 /4 b/2 A } w s 2 { w h = c + b 2 /4 b/2 A } x w h 1 { x = c + b 2 /4 b/2 A } { x 2 + bx = c x > 0 } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) Algorithmus von al-khwārizmī { b > 0 c > 0 } h b/2 5 { c + h 2 h = c + b 2 /4 b/2 A } q h 2 4 { c + q h = c + b 2 /4 b/2 A } s c + q 3 { s h = c + b 2 /4 b/2 A } w s 2 { w h = c + b 2 /4 b/2 A } x w h 1 { x = c + b 2 /4 b/2 A } { x 2 + bx = c x > 0 } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) Algorithmus von al-khwārizmī { b > 0 c > 0 } 6 { c + (b/2) 2 b/2 = c + b 2 /4 b/2 A } h b/2 5 { c + h 2 h = c + b 2 /4 b/2 A } q h 2 4 { c + q h = c + b 2 /4 b/2 A } s c + q 3 { s h = c + b 2 /4 b/2 A } w s 2 { w h = c + b 2 /4 b/2 A } x w h 1 { x = c + b 2 /4 b/2 A } { x 2 + bx = c x > 0 } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) Algorithmus von al-khwārizmī { b > 0 c > 0 } 7 { A : c + (b/2) 2 definiert und c + (b/2) 2 b/2 > 0 } 6 { c + (b/2) 2 b/2 = c + b 2 /4 b/2 A } h b/2 5 { c + h 2 h = c + b 2 /4 b/2 A } q h 2 4 { c + q h = c + b 2 /4 b/2 A } s c + q 3 { s h = c + b 2 /4 b/2 A } w s 2 { w h = c + b 2 /4 b/2 A } x w h 1 { x = c + b 2 /4 b/2 A } { x 2 + bx = c x > 0 } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) Algorithmus von al-khwārizmī 8 { b > 0 c > 0 } 7 { A : c + (b/2) 2 definiert und c + (b/2) 2 b/2 > 0 } 6 { c + (b/2) 2 b/2 = c + b 2 /4 b/2 A } h b/2 5 { c + h 2 h = c + b 2 /4 b/2 A } q h 2 4 { c + q h = c + b 2 /4 b/2 A } s c + q 3 { s h = c + b 2 /4 b/2 A } w s 2 { w h = c + b 2 /4 b/2 A } x w h 1 { x = c + b 2 /4 b/2 A } { x 2 + bx = c x > 0 } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) Algorithmus von al-khwārizmī 8 { b > 0 c > 0 } 7 { A : c + (b/2) 2 definiert und c + (b/2) 2 b/2 > 0 } 6 { c + (b/2) 2 b/2 = c + b 2 /4 b/2 A } h b/2 5 { c + h 2 h = c + b 2 /4 b/2 A } q h 2 4 { c + q h = c + b 2 /4 b/2 A } s c + q 3 { s h = c + b 2 /4 b/2 A } w s 2 { w h = c + b 2 /4 b/2 A } x w h 1 { x = c + b 2 /4 b/2 A } 9 { x 2 + bx = ( c + b 2 /4 b/2) 2 + b( c + b 2 /4 b/2) x > 0 } { x 2 + bx = c x > 0 } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) Algorithmus von al-khwārizmī 8 { b > 0 c > 0 } 7 { A : c + (b/2) 2 definiert und c + (b/2) 2 b/2 > 0 } 6 { c + (b/2) 2 b/2 = c + b 2 /4 b/2 A } h b/2 5 { c + h 2 h = c + b 2 /4 b/2 A } q h 2 4 { c + q h = c + b 2 /4 b/2 A } s c + q 3 { s h = c + b 2 /4 b/2 A } w s 2 { w h = c + b 2 /4 b/2 A } x w h 1 { x = c + b 2 /4 b/2 A } 9 { x 2 + bx = ( c + b 2 /4 b/2) 2 + b( c + b 2 /4 b/2) x > 0 } 10 { x 2 + bx = c x > 0 } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Regel HT-I für bedingte Anweisungen { P } if B then { P B } S 1 { Q } else { P B } S 2 { Q } fi { Q } HT-I: {P B} S 1 {Q} {P B} S 2 {Q} {P} if B then S 1 else S 2 fi {Q} Gültigkeit bleibt erhalten GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 27 / 37
Beispiel für HT-I { 0 = 0 } if x < 0 then x x else x x fi { x 0 } Grundmenge sei Z GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 28 / 37
Beispiel für HT-I { 0 = 0 } if x < 0 then else x x x x fi { x 0 } Grundmenge sei Z if B then S 1 else S 2 fi wenn {P B} S 1 {Q} ableitbar und wenn {P B} S 2 {Q} ableitbar dann {P} if B then S 1 else S 2 fi {Q} ableitbar GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 28 / 37
Beispiel für HT-I { 0 = 0 } if x < 0 then else { 0 = 0 x < 0 } x x { x 0 } x x fi { x 0 } Grundmenge sei Z if B then S 1 else S 2 fi wenn {P B} S 1 {Q} ableitbar und wenn {P B} S 2 {Q} ableitbar dann {P} if B then S 1 else S 2 fi {Q} ableitbar GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 28 / 37
Beispiel für HT-I { 0 = 0 } if x < 0 then else { 0 = 0 x < 0 } x x { x 0 } { 0 = 0 (x < 0) } x x { x 0 } fi { x 0 } Grundmenge sei Z if B then S 1 else S 2 fi wenn {P B} S 1 {Q} ableitbar und wenn {P B} S 2 {Q} ableitbar dann {P} if B then S 1 else S 2 fi {Q} ableitbar GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 28 / 37
Beispiel für HT-I { 0 = 0 } if x < 0 then else { 0 = 0 x < 0 } { x 0 } x x { x 0 } { 0 = 0 (x < 0) } { x 0 } x x { x 0 } fi { x 0 } Grundmenge sei Z if B then S 1 else S 2 fi wenn {P B} S 1 {Q} ableitbar und wenn {P B} S 2 {Q} ableitbar dann {P} if B then S 1 else S 2 fi {Q} ableitbar GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 28 / 37
Regel HT-W für while-schleifen { I } HT-W: while B do { I B } S { I } od { I B } {I B} S {I } {I } while B do S od {I B} Zusicherung I heißt Schleifeninvariante Gültigkeit bleibt erhalten GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 29 / 37
Wo sind wir? Es war einmal... Lösen einer Sorte quadratischer Gleichungen Zum informellen Algorithmusbegriff Einführung des Hoare-Kalküls Algorithmus zur Multiplikation nichtnegativer ganzer Zahlen GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 30 / 37
Erinnerung: div und mod x mod y Rest der ganzzahligen Division von x durch y 0 x mod y < y x div y Ergebnis der ganzzahligen Division von x durch y für alle x,y N 0 gilt x = y (x div y) + (x mod y) Beispiele 6 div 2 = 3 und 6 mod 2 = 0 7 div 2 = 3 und 7 mod 2 = 1 8 div 2 = 4 und 8 mod 2 = 0 GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 31 / 37
Algorithmus für die Multiplikation Grundbereich N 0 also I (a), I (b) N 0 i 0 X a Y b P 0 while X > 0 do i i + 1 P P + (X mod 2) Y X X div 2 Y 2 Y od GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 32 / 37
Beispielrechnung i 0 X a Y b P 0 while X > 0 do i i + 1 P P + (X mod 2) Y X X div 2 Y 2 Y od Es sei a = 6 und b = 9 schreibe v i für Wert von v nach i Schleifendurchläufen P i X i Y i i = 0 0 6 9 i = 1 0 3 18 i = 2 18 1 36 i = 3 54 0 72 GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 33 / 37
Beispielrechnung i 0 X a Y b P 0 while X > 0 do i i + 1 P P + (X mod 2) Y X X div 2 Y 2 Y od Es sei a = 6 und b = 9 schreibe v i für Wert von v nach i Schleifendurchläufen P i X i Y i i = 0 0 6 9 i = 1 0 3 18 i = 2 18 1 36 i = 3 54 0 72 am Ende: P 3 = 54 = a b wollen beweisen: Das klappt immer! GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 33 / 37
Algorithmus für die Multiplikation Grundbereich N 0 { 0 = 0 } also I (a), I (b) N 0 i 0 X a Y b P 0 while X > 0 do i i + 1 P P + (X mod 2) Y X X div 2 Y 2 Y od { P = a b } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 34 / 37
Algorithmus für die Multiplikation Grundbereich N 0 { 0 = 0 } also I (a), I (b) N 0 i 0 X a Y b P 0 Schleifeninvariante { X Y + P = a b } while X > 0 do i i + 1 P P + (X mod 2) Y X X div 2 Y 2 Y od { P = a b } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 34 / 37
Schleifeninvariante für Multiplikationsalgorithmus (1) Grundbereich N 0 { X Y + P = a b } also I (a), I (b) N 0 while X > 0 do i i + 1 P P + (X mod 2) Y X X div 2 Y 2 Y od { P = a b } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 35 / 37
Schleifeninvariante für Multiplikationsalgorithmus (1) Grundbereich N 0 { X Y + P = a b } also I (a), I (b) N 0 while X > 0 do { X Y + P = a b X > 0 } i i + 1 P P + (X mod 2) Y X X div 2 Y 2 Y { X Y + P = a b } od { P = a b } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 35 / 37
Schleifeninvariante für Multiplikationsalgorithmus (1) Grundbereich N 0 { X Y + P = a b } also I (a), I (b) N 0 while X > 0 do { X Y + P = a b X > 0 } i i + 1 P P + (X mod 2) Y X X div 2 Y 2 Y { X Y + P = a b } od { X Y + P = a b (X > 0) } { P = a b } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 35 / 37
Schleifeninvariante für Multiplikationsalgorithmus (2) Grundbereich N 0 { X Y + P = a b X > 0 } also I (a), I (b) N 0 i i + 1 P P + (X mod 2) Y X X div 2 Y 2 Y { X Y + P = a b } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 36 / 37
Schleifeninvariante für Multiplikationsalgorithmus (2) Grundbereich N 0 also I (a), I (b) N 0 { X Y + P = a b X > 0 } { (X div 2) (2Y ) + P + (X mod 2) Y = a b } i i + 1 { (X div 2) (2Y ) + P + (X mod 2) Y = a b } P P + (X mod 2) Y { (X div 2) (2Y ) + P = a b } X X div 2 { X (2Y ) + P = a b } Y 2 Y { X Y + P = a b } GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 36 / 37
Schleifeninvariante für Multiplikationsalgorithmus (2) Grundbereich N 0 also I (a), I (b) N 0 { X Y + P = a b X > 0 } { (X div 2) (2Y ) + P + (X mod 2) Y = a b } i i + 1 { (X div 2) (2Y ) + P + (X mod 2) Y = a b } P P + (X mod 2) Y { (X div 2) (2Y ) + P = a b } X X div 2 { X (2Y ) + P = a b } Y 2 Y { X Y + P = a b } (X div 2) (2Y ) + P + (X mod 2) Y = (2(X div 2) + (X mod 2)) Y + P = X Y + P GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 36 / 37
Was ist wichtig Das sollten Sie mitnehmen: informeller Algorithmusbegriff Schleifeninvarianten Das sollten Sie üben: Schleifeninvarianten finden Wertetabellen können helfen Korrektheitsbeweise finden mit Hoare-Kalkül GBI Grundbegriffe der Informatik KIT, Institut für Theoretische Informatik 37 / 37