Informatik II Übung 11 Gruppe 4 (Folien teils von Christian B. und Christelle G.) Lukas Burkhalter lubu@inf.ethz.ch Informatik II Übung 11 Lukas Burkhalter 16. Mai 2018 1
Alpha-Beta klarer 4 oder 2, beides ok 2
C B A Nachbesprechung letzte Übung 10 3
Merge-Sort Papier 21 15 9 63 45 44 8 88 98 67 45 6 62 21 15 63 9 45 44 88 8 98 67 45 6 62 63 21 15 9 88 45 44 8 98 67 45 6 62 88 63 45 44 21 15 9 8 98 67 62 45 6 98 88 67 63 62 45 45 44 21 15 9 8 6 4
Mergesort Pseudocode (sort) ArrayList sort (ArrayList unsorted, int begin, int end ) if ( end - begin == 0 ) return new ArrayList ( 0 ) if ( end - begin == 1 ) ArrayList result = new ArrayList ( 1 ) result.add ( unsorted[begin] ) return result // divide.. ArrayList lhs = sort ( unsorted, begin, (begin+end) / 2 ) ArrayList rhs = sort ( unsorted, (begin+end) / 2, end ) //..et impera return merge ( lhs, rhs ) 5
Mergesort Pseudocode (merge) ArrayList merge (ArrayList lhs, ArrayList rhs ) int left = 0, right = 0 ArrayList result = new ArrayList ( lhs.size + rhs.size ) loop if ( left == lhs.size ) result.addall ( rhs.sublist ( right, rhs.size ) ) break if ( right == rhs.size ) result.addall ( lhs.sublist ( left, lhs.size ) ) break result.add ( lhs[left]>rhs[right]? lhs[left++] : rhs[right++] ) return result 6
Mergesort Laufzeit 7
Türme von Hanoi Nicht benutzt wird: 3 2 1 Zusammenfassung: Anzahl der Scheiben (n): 4 Anzahl der Schritte (2 n-1 ): 15 Nicht benutzt werden: 3 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 8
Reversi Alpha-Beta Algorithmus BestMove max (int maxdepth, long timeout, GameBoard gb, int depth, int alpha, int beta) throws Timeout if (System.currentTimeMillis() > timeout) throw new Timeout(); if (depth==maxdepth) return new BestMove(eval(gb),null,true); ArrayList<Coordinates> availablemoves = new ArrayList<Coordinates>(gb.getSize()* gb.getsize()); for (int x = 1; x <= gb.getsize(); x++) for (int y = 1; y <= gb.getsize(); y++) { Coordinates coord = new Coordinates(x, y); if (gb.checkmove(mycolor, coord)) availablemoves.add(coord); } //Check available moves if (availablemoves.isempty()) if (gb.ismoveavailable(othercolor)) { BestMove result = min(maxdepth, timeout, gb, depth+1, alpha, beta); return new BestMove(result.value, null, false); } else return new BestMove(finalResult(gb), null, false); [...] //Falls keine available moves // Bewertungs funktion 9
BestMove max (int maxdepth, long timeout, GameBoard gb, int depth, int alpha, int beta) throws Timeout [...] boolean cut = false; Coordinates bestcoord = null; for (Coordinates coord : availablemoves) { GameBoard hypothetical = gb.clone(); hypothetical.checkmove(mycolor, coord); hypothetical.makemove(mycolor, coord); BestMove result = min(maxdepth, timeout, hypothetical, depth+1, alpha, beta); } if (result.value > alpha) { alpha = result.value; bestcoord = coord; } if (alpha >= beta) { return new BestMove(alpha, null, false); } cut = cut result.cut; // Passe alpha an falls nötig return new BestMove(alpha, bestcoord, cut); //Besuche Kind-Knoten //Rufe min() auf um den Kind-Wert zu erhalten // CUT wenn möglich 10
1. Sortieren mit Suchbäumen 2. Komplexitätsanalyse und O-Notation 3. Komplexität 4. Springer auf dem Schachbrett Vorbesprechung Übung 11 11
Sortieren von Suchbäumen [8, 10, 13, 4, 7, 6, 1, 3,14] Laufzeit? [1, 3, 4, 6, 7, 8, 10, 13, 14] Pre-order traversierung 12
Complexity Revisited O - Notation In der O-Notation halten wir aber nur den am schnellsten wachsende Term fest. Konstanten werden ignoriert! Beschreibt eine Menge von Funktionen 13
Komplexität von Algorithmen Was ist die Komplexität von func in Abhängigkeit von n, wenn wir davon Ausgehen, dass die Grundoperationen (+ - / *) je 1 Rechenschritt benötigen? n-mal private static int func(int n) { int out = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < i; j++) { out++; } } return out; } (0 + 1 + 2 + + n-1) = n*(n-1)/2 mal ausgeführt i++ j++ out++ Total: n + n*(n-1)/2 + n*(n-1)/2) 14
O-Notation abstrahiert private static int func(int n) { int out = 0; for(int i = 0; i < 10*(n*n); i++) { for(int j = 0; j < i; j++) { out++; } } return out; } 15
Example Java 16
Ü11.A2 Komplexität O-Notation // Fragment 1 for (int i=0; i<n; i++) a++; // Fragment 2 for (int i=0; i<2n; i++) a++; for (int j=0; j<n; j++) a++; // Fragment 4 for (int i=0; i<n; i++) for (int j=0; j<i; j++) a++; // Fragment 5 while(n >=1 ) n = n/2; // Fragment 3 for (int i=0; i<n; i++) for (int j=0; j<n; j++) a++; // Fragment 6 for (int i=0; i<n; i++) for (int j=0; j<n*n; j++) for (int k=0; k<j; k++) a++; 17
Ü11.A2 Komplexität Zeit pro Operation Max Grösse der Eingabe Totale Laufzeit 18
Ü11.A2 Komplexität (Beispiel) 19
Ü11.A4: Schachbrett Springer 20
Ü11.A4: Schachbrett Springer a) Klasse Position p = new Position(0,0); Position next = p.add(new Position(offX, offy)); Implementiert compareto, equals, etc. Methode getreachableset ArrayList<Position> getreachableset(position p, int n) p: Startposition n: Anzahl Hops returns: Knoten in der Menge 21
Ü11.A4: Schachbrett Springer b) Finde einen Weg, der...... alle Felder besucht... Jedes Feld nur 1x besucht Frühzeitiger Abbruch Falls erreichbare Felder alle besucht Backtracking: löschen der letzten Züge bis Abbruchbedingung nicht mehr erfüllt 22
Tipps Collections in Java 23
viel Spass! 24