Upload
phamxuyen
View
216
Download
0
Embed Size (px)
Citation preview
Grundbegri�e der Informatik
Kapitel 14: Der Begri� des Algorithmus
(einige grundlegende Aspekte)
Thomas Worsch
KIT, Institut für Theoretische Informatik
Wintersemester 2015/2016
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 1 / 37
Wo sind wir?
Es war einmal . . .
Lösen einer Sorte quadratischer Gleichungen
Zum informellen Algorithmusbegri�
Einführung des Hoare-Kalküls
Algorithmus zur Multiplikation nichtnegativer ganzer Zahlen
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 2 / 37
Eine Zeitreise . . . wohin? . . .
Wie weit in die Vergangenheit kann man reisen undfindet noch etwas, was mit Informatik zu tun hat?(jenseits von Zählen und Zahlen)
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 3 / 37
. . . da wären wir . . .
Zeit: circa 825–830
Ort: Bagdad, Haus der Weisheit
Muhammad ibn Musa al-Khwarizmı
geboren ca. 780
in Khiva (heute Usbekistan)
oder �trubbull (heute Iran)
gestorben ca. 850
Bildquelle: http://en.wikipedia.org/wiki/Image:Abu_Abdullah_Muhammad_bin_Musa_al-Khwarizmi.jpg
GBI — Grundbegri�e 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 Musa al-Khwarizmı
geboren ca. 780
in Khiva (heute Usbekistan)
oder �trubbull (heute Iran)
gestorben ca. 850
Bildquelle: http://en.wikipedia.org/wiki/Image:Abu_Abdullah_Muhammad_bin_Musa_al-Khwarizmi.jpg
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 4 / 37
Zwei wichtige Schri�en von al-Khwarizmı (1)
„Al-Kitab al-mukhtas.ar fı hısab al-ğabr wa’l-muqabala“ oder
„Al-Kitab al-mukhtas.ar fı h. isab al-jabr wa-l-muqabala“
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 — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 5 / 37
Zwei wichtige Schri�en von al-Khwarizmı (2)
Titel vielleicht „Kitab al-Jam‘ wa-l-tafrıq bi-h. isab al-Hind“
ca. 825 (??)
“Über das Rechnen mit indischen Zi�ern“
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 — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 6 / 37
Wo sind wir?
Es war einmal . . .
Lösen einer Sorte quadratischer Gleichungen
Zum informellen Algorithmusbegri�
Einführung des Hoare-Kalküls
Algorithmus zur Multiplikation nichtnegativer ganzer Zahlen
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 7 / 37
Lösen einer Sorte quadratischer Gleichungen nach
al-Khwarizmı (1)
gegeben: quadratische Gleichung der Form
x2 + bx = c mit b > 0 und c > 0
al-Khwarizmı: positive Lösung findet man so:
h ← b/2 (1)
q ← h2 (2)
s ← c + q (3)
w ←√s (4)
x ← w − h (5)
s nie negativ
am Ende x > 0 und Lösung von x2 + bx = c
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 8 / 37
Wo sind wir?
Es war einmal . . .
Lösen einer Sorte quadratischer Gleichungen
Zum informellen Algorithmusbegri�
Einführung des Hoare-Kalküls
Algorithmus zur Multiplikation nichtnegativer ganzer Zahlen
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 9 / 37
Algorithmusbegri� informell
Eigenscha�en des eben gezeigten Algorithmus:
endliche Beschreibung
elementaren Anweisungen
jede o�ensichtlich e�ektiv in einem Schri� 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 Schri�e
nur endlich o� eine elementare Anweisung ausgeführt
beliebig große Eingaben bearbeitbar
Nachvollziehbarkeit/Verständlichkeit des Algorithmus
Algorithmus jedem (mit der Materie vertrauten) klar
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 10 / 37
Diskussion des informellen Algorithmusbegri�s
obige Forderungen sind plausibel aber informell:
Was heißt „o�ensichtlich e�ektiv ausführbar“ ?
harte Beweise benötigen einen präziseren Algorithmusbegri�
auch Verallgemeinerungen sind interessant
randomisierte Algorithmen
Zufall beeinflusst Auswahl eines Schri�es
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 — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 11 / 37
Diskussion des informellen Algorithmusbegri�s
obige Forderungen sind plausibel aber informell:
Was heißt „o�ensichtlich e�ektiv ausführbar“ ?
harte Beweise benötigen einen präziseren Algorithmusbegri�
auch Verallgemeinerungen sind interessant
randomisierte Algorithmen
Zufall beeinflusst Auswahl eines Schri�es
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 — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 11 / 37
Diskussion des informellen Algorithmusbegri�s
obige Forderungen sind plausibel aber informell:
Was heißt „o�ensichtlich e�ektiv ausführbar“ ?
harte Beweise benötigen einen präziseren Algorithmusbegri�
auch Verallgemeinerungen sind interessant
randomisierte Algorithmen
Zufall beeinflusst Auswahl eines Schri�es
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 — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 11 / 37
Wo sind wir?
Es war einmal . . .
Lösen einer Sorte quadratischer Gleichungen
Zum informellen Algorithmusbegri�
Einführung des Hoare-Kalküls
Algorithmus zur Multiplikation nichtnegativer ganzer Zahlen
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 12 / 37
Korrektheit eines Algorithmus — wie beweist man sie?
ad hoc
Beispiel von al-Khwarizmı
systematisch
verschiedene Möglichkeiten
Beispiel Hoare-Tripel
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 13 / 37
Beweis von al-Khwarizmı
b/4
x2
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 14 / 37
Beweis von al-Khwarizmı
(b/4)x
(b/4)x
(b/4)x
(b/4)x
x2
b/4
b/4
b/4 b/4
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 14 / 37
Beweis von al-Khwarizmı
x2
b/4
b/4
b/4 b/4
x2 + bx = c
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 14 / 37
Beweis von al-Khwarizmı
x2
b/4
b/4
b/4 b/4
x2 + bx = c
4 · b2/16 = b2/4 = q
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 14 / 37
Beweis von al-Khwarizmı
x2
b/4
b/4
b/4 b/4
x2 + bx = c
4 · b2/16 = b2/4 = q
c + q = (b/4 + x + b/4)2
√c + q − b/2 = x
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 14 / 37
Beweis durch Nachrechnen
{ b > 0 ∧ c > 0 }h ← b/2{ h = b/2 }q ← h2
{ q = b2/4 }s ← c + q
{ s = c + b2/4 }
w ←√s
{ w =√c + b2/4 }
x ← w − h
{ x =√c + b2/4 − b/2 }
{ x2 + bx = (√c + b2/4 − b/2)2 + b (
√c + b2/4 − b/2) }
{ x2 + bx = c }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 15 / 37
Beweis durch Nachrechnen
{ b > 0 ∧ c > 0 }h ← b/2{ h = b/2 }q ← h2
{ q = b2/4∧h = b/2 }s ← c + q
{ s = c + b2/4∧h = b/2 }
w ←√s
{ w =√c + b2/4∧h = b/2 }
x ← w − h
{ x =√c + b2/4 − b/2 }
{ x2 + bx = (√c + b2/4 − b/2)2 + b (
√c + b2/4 − b/2) }
{ x2 + bx = c }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 16 / 37
Eine einfache „Programmiersprache“
Terminalsymbole
Zuweisungssymbol←
Schlüsselwörter if, then, else, fiSchlüsselwörter while, do, od, for, toSymbole für Konstanten, Funktionen und Relationen
Produktionen der kontextfreien Grammatik
Prog→ Stmt | Stmt ProgStmt→ 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 — Grundbegri�e 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 — Grundbegri�e 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 seinbei uns „Eingaben“ für das Programm
Variablenbelegungen
beschreiben Gesamtzustand des „Speichers“
Veränderung durch Zuweisungen
GBI — Grundbegri�e 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
β ′ = βvalD, I ,β (E )x
also
fast alles unverändert, nur
x hat nun den Wert valD, I,β (E)
Bedeutung der Kontrollstrukturenim Kapitel zur Mima angedeutet
hier keine formale Definition
GBI — Grundbegri�e 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 valD, I,β (P ) = w und
wenn die Ausführung von S für I und β endet und
hinterher Variablenbelegung β ′ vorliegt
dann valD, I,β ′ (Q ) = w
GBI — Grundbegri�e 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 — Grundbegri�e 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 ∈ LTer
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 — Grundbegri�e 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 ∈ LTer
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 — Grundbegri�e 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 } S1 {Q } {Q } S2 {R}
{P } S1; S2 {R}
Gültigkeit bleibt erhalten
GBI — Grundbegri�e 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 }
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 — Grundbegri�e 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
HT-A: {y = a} z ← y {z = a} ist ableitbar
HT-A: {x = a} y ← x {y = a} ist ableitbar
HT-S: fertig
GBI — Grundbegri�e 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
HT-A: {x = a} y ← x {y = a} ist ableitbar
HT-S: fertig
GBI — Grundbegri�e 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 — Grundbegri�e 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 — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 25 / 37
Beispiel (2) — Algorithmus von al-Khwarizmı
{ b > 0 ∧ c > 0 }{ }
{ }
h ← b/2{ }
q ← h2
{ }
s ← c + q
{ }
w ←√s
{ }
x ← w − h
1 { x =√c + b2/4 − b/2 ∧ A }
{ }
{ x2 + bx = c ∧ x > 0 }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) — Algorithmus von al-Khwarizmı
{ b > 0 ∧ c > 0 }{ }
{ }
h ← b/2{ }
q ← h2
{ }
s ← c + q
{ }
w ←√s
2 { w − h =√c + b2/4 − b/2 ∧ A }
x ← w − h
1 { x =√c + b2/4 − b/2 ∧ A }
{ }
{ x2 + bx = c ∧ x > 0 }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) — Algorithmus von al-Khwarizmı
{ b > 0 ∧ c > 0 }{ }
{ }
h ← b/2{ }
q ← h2
{ }
s ← c + q
3 {√s − h =
√c + b2/4 − b/2 ∧ A }
w ←√s
2 { w − h =√c + b2/4 − b/2 ∧ A }
x ← w − h
1 { x =√c + b2/4 − b/2 ∧ A }
{ }
{ x2 + bx = c ∧ x > 0 }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) — Algorithmus von al-Khwarizmı
{ b > 0 ∧ c > 0 }{ }
{ }
h ← b/2{ }
q ← h2
4 {√c + q − h =
√c + b2/4 − b/2 ∧ A }
s ← c + q
3 {√s − h =
√c + b2/4 − b/2 ∧ A }
w ←√s
2 { w − h =√c + b2/4 − b/2 ∧ A }
x ← w − h
1 { x =√c + b2/4 − b/2 ∧ A }
{ }
{ x2 + bx = c ∧ x > 0 }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) — Algorithmus von al-Khwarizmı
{ b > 0 ∧ c > 0 }{ }
{ }
h ← b/25 {
√c + h2 − h =
√c + b2/4 − b/2 ∧ A }
q ← h2
4 {√c + q − h =
√c + b2/4 − b/2 ∧ A }
s ← c + q
3 {√s − h =
√c + b2/4 − b/2 ∧ A }
w ←√s
2 { w − h =√c + b2/4 − b/2 ∧ A }
x ← w − h
1 { x =√c + b2/4 − b/2 ∧ A }
{ }
{ x2 + bx = c ∧ x > 0 }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) — Algorithmus von al-Khwarizmı
{ b > 0 ∧ c > 0 }{ }
6 {√c + (b/2)2 − b/2 =
√c + b2/4 − b/2 ∧ A }
h ← b/25 {
√c + h2 − h =
√c + b2/4 − b/2 ∧ A }
q ← h2
4 {√c + q − h =
√c + b2/4 − b/2 ∧ A }
s ← c + q
3 {√s − h =
√c + b2/4 − b/2 ∧ A }
w ←√s
2 { w − h =√c + b2/4 − b/2 ∧ A }
x ← w − h
1 { x =√c + b2/4 − b/2 ∧ A }
{ }
{ x2 + bx = c ∧ x > 0 }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) — Algorithmus von al-Khwarizmı
{ 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 + b2/4 − b/2 ∧ A }
h ← b/25 {
√c + h2 − h =
√c + b2/4 − b/2 ∧ A }
q ← h2
4 {√c + q − h =
√c + b2/4 − b/2 ∧ A }
s ← c + q
3 {√s − h =
√c + b2/4 − b/2 ∧ A }
w ←√s
2 { w − h =√c + b2/4 − b/2 ∧ A }
x ← w − h
1 { x =√c + b2/4 − b/2 ∧ A }
{ }
{ x2 + bx = c ∧ x > 0 }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) — Algorithmus von al-Khwarizmı
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 + b2/4 − b/2 ∧ A }
h ← b/25 {
√c + h2 − h =
√c + b2/4 − b/2 ∧ A }
q ← h2
4 {√c + q − h =
√c + b2/4 − b/2 ∧ A }
s ← c + q
3 {√s − h =
√c + b2/4 − b/2 ∧ A }
w ←√s
2 { w − h =√c + b2/4 − b/2 ∧ A }
x ← w − h
1 { x =√c + b2/4 − b/2 ∧ A }
{ }
{ x2 + bx = c ∧ x > 0 }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) — Algorithmus von al-Khwarizmı
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 + b2/4 − b/2 ∧ A }
h ← b/25 {
√c + h2 − h =
√c + b2/4 − b/2 ∧ A }
q ← h2
4 {√c + q − h =
√c + b2/4 − b/2 ∧ A }
s ← c + q
3 {√s − h =
√c + b2/4 − b/2 ∧ A }
w ←√s
2 { w − h =√c + b2/4 − b/2 ∧ A }
x ← w − h
1 { x =√c + b2/4 − b/2 ∧ A }
9 { x2 + bx = (√c + b2/4 − b/2)2 + b (
√c + b2/4 − b/2) ∧ x > 0 }
{ x2 + bx = c ∧ x > 0 }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Beispiel (2) — Algorithmus von al-Khwarizmı
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 + b2/4 − b/2 ∧ A }
h ← b/25 {
√c + h2 − h =
√c + b2/4 − b/2 ∧ A }
q ← h2
4 {√c + q − h =
√c + b2/4 − b/2 ∧ A }
s ← c + q
3 {√s − h =
√c + b2/4 − b/2 ∧ A }
w ←√s
2 { w − h =√c + b2/4 − b/2 ∧ A }
x ← w − h
1 { x =√c + b2/4 − b/2 ∧ A }
9 { x2 + bx = (√c + b2/4 − b/2)2 + b (
√c + b2/4 − b/2) ∧ x > 0 }
10 { x2 + bx = c ∧ x > 0 }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 26 / 37
Regel HT-I — für bedingte Anweisungen
{ P }if Bthen
{ P ∧ B }S1{ Q }
else{ P ∧ ¬B }S2{ Q }
fi{ Q }
HT-I:
{P ∧ B} S1 {Q } {P ∧ ¬B} S2 {Q }
{P } if B then S1 else S2 fi {Q }
Gültigkeit „bleibt erhalten“
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 27 / 37
Beispiel für HT-I
{ 0 = 0 }if x < 0then
x ← −xelse
x ← xfi{ x ≥ 0 }
Grundmenge sei Z
if B then S1 else S2 fiwenn {P ∧ B} S1 {Q } ableitbar und
wenn {P ∧ ¬B} S2 {Q } ableitbar
dann {P } if B then S1 else S2 fi {Q }ableitbar
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 28 / 37
Beispiel für HT-I
{ 0 = 0 }if x < 0then
{ }
{ }
x ← −x{ }
else{ }
{ }
x ← x{ }
fi{ x ≥ 0 }
Grundmenge sei Z
if B then S1 else S2 fiwenn {P ∧ B} S1 {Q } ableitbar und
wenn {P ∧ ¬B} S2 {Q } ableitbar
dann {P } if B then S1 else S2 fi {Q }ableitbar
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 28 / 37
Beispiel für HT-I
{ 0 = 0 }if x < 0then
{ 0 = 0 ∧ x < 0 }{ }
x ← −x{ x ≥ 0 }
else{ }
{ }
x ← x{ }
fi{ x ≥ 0 }
Grundmenge sei Z
if B then S1 else S2 fiwenn {P ∧ B} S1 {Q } ableitbar und
wenn {P ∧ ¬B} S2 {Q } ableitbar
dann {P } if B then S1 else S2 fi {Q }ableitbar
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 28 / 37
Beispiel für HT-I
{ 0 = 0 }if x < 0then
{ 0 = 0 ∧ x < 0 }{ }
x ← −x{ x ≥ 0 }
else{ 0 = 0 ∧ ¬(x < 0) }{ }
x ← x{ x ≥ 0 }
fi{ x ≥ 0 }
Grundmenge sei Z
if B then S1 else S2 fiwenn {P ∧ B} S1 {Q } ableitbar und
wenn {P ∧ ¬B} S2 {Q } ableitbar
dann {P } if B then S1 else S2 fi {Q }ableitbar
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 28 / 37
Beispiel für HT-I
{ 0 = 0 }if x < 0then
{ 0 = 0 ∧ x < 0 }{ −x ≥ 0 }x ← −x{ x ≥ 0 }
else{ 0 = 0 ∧ ¬(x < 0) }{ x ≥ 0 }x ← x{ x ≥ 0 }
fi{ x ≥ 0 }
Grundmenge sei Z
if B then S1 else S2 fiwenn {P ∧ B} S1 {Q } ableitbar und
wenn {P ∧ ¬B} S2 {Q } ableitbar
dann {P } if B then S1 else S2 fi {Q }ableitbar
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 28 / 37
Regel HT-W — für while-Schleifen
{ I }
while B
do{ I ∧ B }
S
{ I }
od{ I∧¬B }
HT-W:
{I ∧ B} S {I }
{I } while B do S od {I ∧ ¬B}
Zusicherung I heißt Schleifeninvariante
Gültigkeit „bleibt erhalten“
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 29 / 37
Wo sind wir?
Es war einmal . . .
Lösen einer Sorte quadratischer Gleichungen
Zum informellen Algorithmusbegri�
Einführung des Hoare-Kalküls
Algorithmus zur Multiplikation nichtnegativer ganzer Zahlen
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 30 / 37
Erinnerung: div und modx mod y
Rest der ganzzahligen Division von x durch y0 ≤ x mod y < y
x div yErgebnis der ganzzahligen Division von x durch y
für alle x ,y ∈ N0 gilt
x = y · (x div y) + (x mod y)
Beispiele
6 div 2 = 3 und 6 mod 2 = 07 div 2 = 3 und 7 mod 2 = 18 div 2 = 4 und 8mod 2 = 0
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 31 / 37
Algorithmus für die Multiplikation
Grundbereich N0
also I (a), I (b) ∈ N0
i ← 0X ← aY ← bP ← 0while X > 0 do
i ← i + 1P ← P + (X mod 2) · YX ← X div 2Y ← 2 · Y
od
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 32 / 37
Beispielrechnung
i ← 0X ← aY ← bP ← 0while X > 0 do
i ← i + 1P ← P + (X mod 2) · YX ← X div 2Y ← 2 · Y
od
Es sei a = 6 und b = 9
schreibe vi für Wert von v„nach i Schleifendurchläufen“
Pi Xi Yi
i = 0 0 6 9i = 1 0 3 18i = 2 18 1 36i = 3 54 0 72
am Ende: P3 = 54 = a · bwollen beweisen: Das klappt immer!
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 33 / 37
Beispielrechnung
i ← 0X ← aY ← bP ← 0while X > 0 do
i ← i + 1P ← P + (X mod 2) · YX ← X div 2Y ← 2 · Y
od
Es sei a = 6 und b = 9
schreibe vi für Wert von v„nach i Schleifendurchläufen“
Pi Xi Yi
i = 0 0 6 9i = 1 0 3 18i = 2 18 1 36i = 3 54 0 72
am Ende: P3 = 54 = a · bwollen beweisen: Das klappt immer!
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 33 / 37
Algorithmus für die Multiplikation
Grundbereich N0
also I (a), I (b) ∈ N0
{ 0 = 0 }i ← 0X ← aY ← bP ← 0
while X > 0 doi ← i + 1P ← P + (X mod 2) · YX ← X div 2Y ← 2 · Y
od{ P = a · b }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 34 / 37
Algorithmus für die Multiplikation
Grundbereich N0
also I (a), I (b) ∈ N0
{ 0 = 0 }i ← 0X ← aY ← bP ← 0
Schleifeninvariante { X · Y + P = a · b }while X > 0 do
i ← i + 1P ← P + (X mod 2) · YX ← X div 2Y ← 2 · Y
od{ P = a · b }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 34 / 37
Schleifeninvariante für Multiplikationsalgorithmus (1)
Grundbereich N0
also I (a), I (b) ∈ N0
{ X · Y + P = a · b }while X > 0 do
{ }
i ← i + 1P ← P + (X mod 2) · YX ← X div 2Y ← 2 · Y{ }
od{ }
{ P = a · b }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 35 / 37
Schleifeninvariante für Multiplikationsalgorithmus (1)
Grundbereich N0
also I (a), I (b) ∈ N0
{ X · Y + P = a · b }while X > 0 do
{ X · Y + P = a · b ∧ X > 0 }i ← i + 1P ← P + (X mod 2) · YX ← X div 2Y ← 2 · Y{ X · Y + P = a · b }
od{ }
{ P = a · b }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 35 / 37
Schleifeninvariante für Multiplikationsalgorithmus (1)
Grundbereich N0
also I (a), I (b) ∈ N0
{ X · Y + P = a · b }while X > 0 do
{ X · Y + P = a · b ∧ X > 0 }i ← i + 1P ← P + (X mod 2) · YX ← X div 2Y ← 2 · Y{ X · Y + P = a · b }
od{ X · Y + P = a · b ∧ ¬(X > 0) }{ P = a · b }
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 35 / 37
Schleifeninvariante für Multiplikationsalgorithmus (2)
Grundbereich N0
also I (a), I (b) ∈ N0
{ 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 }
(X div 2) · (2Y ) + P + (X mod 2) · Y= (2(X div 2) + (X mod 2)) · Y + P = X · Y + P
GBI — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 36 / 37
Schleifeninvariante für Multiplikationsalgorithmus (2)
Grundbereich N0
also I (a), I (b) ∈ N0
{ 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 — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 36 / 37
Schleifeninvariante für Multiplikationsalgorithmus (2)
Grundbereich N0
also I (a), I (b) ∈ N0
{ 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 — Grundbegri�e der Informatik KIT, Institut für Theoretische Informatik 36 / 37