Einführung in die Informatik: Programmierung und Software-Entwicklung Zentralübung 6: Lösung Aufgabe 5-4, Tipps zu Blatt 6, Einfache Klassen Christian Kroiß 25.11.2009 http://www.pst.ifi.lmu.de/Lehre/wise-09-10/infoeinf/
Aufgabe 5-4 Zunächst: Entschuldigung! Die Aufgabe doch etwas sehr schwer. Aber: bei der Korrektur wird auf die Grundideen Wert gelegt statt auf die genaue Rechnung. Wir werden versuchen, das nächste Bonus-Blatt einfacher zu machen!
Aufgabe 5-4 a/c) static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true;
Simulation a ungleich b, ungleiche Länge static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true Ergebnis: FALSE a = 1 2 3 b = 1 2 3 4
Simulation a ungleich b, a[0] nicht in b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; a = 1 2 3 b = 4 2 3 VERGLEICHE = 0
Simulation a ungleich b, a[0] nicht in b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 0 a = 1 2 3 b = 4 2 3 VERGLEICHE = 1
Simulation a ungleich b, a[0] nicht in b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 0 j = 0 a = 1 2 3 b = 4 2 3 VERGLEICHE = 2
Simulation a ungleich b, a[0] nicht in b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true true i = 0 j = 0 a = 1 2 3 b = 4 2 3 VERGLEICHE = 2
Simulation a ungleich b, a[0] nicht in b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 0 j = 0 a = 1 2 3 b = 4 2 3 VERGLEICHE = 4
Simulation a ungleich b, a[0] nicht in b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true true i = 0 j = 1 a = 1 2 3 b = 4 2 3 VERGLEICHE = 4
Simulation a ungleich b, a[0] nicht in b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 0 j = 1 a = 1 2 3 b = 4 2 3 VERGLEICHE = 6
Simulation a ungleich b, a[0] nicht in b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true true i = 0 j = 2 a = 1 2 3 b = 4 2 3 VERGLEICHE = 6
Simulation a ungleich b, a[0] nicht in b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 0 j = 2 a = 1 2 3 b = 4 2 3 VERGLEICHE = 8
Simulation a ungleich b, a[0] nicht in b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; false i = 0 j = 3 a = 1 2 3 b = 4 2 3 VERGLEICHE = 8
Simulation a ungleich b, a[0] nicht in b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true i = 0 j = 3 a = 1 2 3 b = 4 2 3 VERGLEICHE = 9
Simulation a ungleich b, a[0] nicht in b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 0 j = 3 a = 1 2 3 b = 4 2 3 VERGLEICHE = 10
Simulation a ungleich b, a[0] nicht in b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; Ergebnis: FALSE Vergleiche: 1 + 1 + 2n + 1 + 1 = 4 + 2n = 10 i = 0 j = 3 a = 1 2 3 b = 4 2 3
Simulation a gleich b 1 VERGLEICHE = 0 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; a = 1 2 3 b = 1 2 3 VERGLEICHE = 0
Simulation a gleich b VERGLEICHE = 1 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 1
Simulation a gleich b VERGLEICHE = 2 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 0 j = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 2
0 Durchläufe der WHILE-Schleife für i == 0 Aber: 2 Vergleiche Simulation a gleich b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true 0 Durchläufe der WHILE-Schleife für i == 0 Aber: 2 Vergleiche false i = 0 j = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 2
Simulation a gleich b false VERGLEICHE = 4 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 0 j = 0 a = 1 2 3 b = 1 2 3 false VERGLEICHE = 4
Simulation a gleich b true VERGLEICHE = 5 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true i = 1 j = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 5
Simulation a gleich b VERGLEICHE = 6 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 1 j = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 6
Simulation a gleich b true true VERGLEICHE = 6 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true true i = 1 j = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 6
Simulation a gleich b VERGLEICHE = 8 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 1 j = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 8
Simulation a gleich b true false VERGLEICHE = 8 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true false i = 1 j = 1 a = 1 2 3 b = 1 2 3 VERGLEICHE = 8
1 Durchlauf der WHILE-Schleife für i == 1 Aber: 4 Vergleiche Simulation a gleich b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true 1 Durchlauf der WHILE-Schleife für i == 1 Aber: 4 Vergleiche false i = 1 j = 1 a = 1 2 3 b = 1 2 3 VERGLEICHE = 8
Simulation a gleich b false VERGLEICHE = 10 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 1 j = 1 a = 1 2 3 b = 1 2 3 false VERGLEICHE = 10
Simulation a gleich b true VERGLEICHE = 11 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true i = 2 j = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 11
Simulation a gleich b VERGLEICHE = 12 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 2 j = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 12
Simulation a gleich b true true VERGLEICHE = 12 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true true i = 2 j = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 12
Simulation a gleich b VERGLEICHE = 14 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 2 j = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 14
Simulation a gleich b true true VERGLEICHE = 14 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true true i = 2 j = 1 a = 1 2 3 b = 1 2 3 VERGLEICHE = 14
Simulation a gleich b VERGLEICHE = 16 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 2 j = 1 a = 1 2 3 b = 1 2 3 VERGLEICHE = 16
Simulation a gleich b true false VERGLEICHE = 16 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true false i = 2 j = 2 a = 1 2 3 b = 1 2 3 VERGLEICHE = 16
2 Durchläufe der WHILE-Schleife für i == 2 Aber: 6 Vergleiche Simulation a gleich b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; true 2 Durchläufe der WHILE-Schleife für i == 2 Aber: 6 Vergleiche false i = 2 j = 2 a = 1 2 3 b = 1 2 3 VERGLEICHE = 16
Simulation a gleich b false VERGLEICHE = 18 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 2 j = 2 a = 1 2 3 b = 1 2 3 false VERGLEICHE = 18
Simulation a gleich b false VERGLEICHE = 19 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; false i = 3 j = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 19
Simulation a gleich b VERGLEICHE = 20 static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; i = 3 j = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 20
Ergebnis: TRUE Vergleiche: n2 + 3n + 2 = 20 Simulation a gleich b static boolean compareSets(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { int j = 0; while (j < b.length && b[j] != a[i]) j++; if (j == b.length) } return true; Ergebnis: TRUE Vergleiche: n2 + 3n + 2 = 20 i = 3 j = 0 a = 1 2 3 b = 1 2 3 VERGLEICHE = 20
Ergebnis Bester Fall für ungleiche Längen: 1 Bester Fall für gleiche Längen: a[0] nicht in b TB = 4 + 2n Schlechtester Fall: Mengen sind gleich Da jedes Element nur einmal vorkommt braucht man unabhängig von der Reihenfolge 1 x keinen Durchlauf der WHILE-Schleife für i == i0 1 x 1 Durchlauf der WHILE-Schleife für i == i1 1 x 2 Durchläufe der WHILE-Schleife für i == i2 etc. Insgesamt (inklusive Schleifen-Abbrüche): n2 + 3n + 2
Herleitung TW
Herleitung TW
Herleitung TW Gaußsche Summen- formel
Tipps für Blatt 6 Aufgabe 6-1 Aufgabe 6-2 b) Noch mal genau die Definition der O-Notation im Skript lesen Insbesondere muss die <= - Beziehung erst ab einem gewissen n0 gelten Bei j) Polynomdivision mit Rest Grenzwerte für n ∞ anschauen Bedenken: in einer Summe entscheidet der Summand mit dem schnellsten Wachstum Aufgabe 6-2 b) Man kann so tun, als ob es tatsächlich n, log n, etc. viele Operationen gebe Für jeweils eine Operation denkt man sich eine Zeitkonstante Top , die natürlich unabhängig von n ist.
Tipps für Blatt 6 Aufgabe 6-2 c) Im Taschenrechner mit großen Zahlen für n ausprobieren Wenn f(n) langsamer wächst als g(n), dann gilt: Graphen zeichnen lassen Online auf http://www.wolframalpha.com Sehr interessante und mächtige Such-/Informationsmaschine mit Mathe-Funktionen von Mathematica. Relativ kompliziert, Dokumentation für Mathe-Funktionen unter http://reference.wolfram.com/mathematica/guide/Mathematica.html GeoGebra (http://www.geogebra.org) Sehr leicht zu benutzen
Einige einfache Klassen