Folienmaster ETH Zürichwolffk/teaching/17infoII/Stunde11.pdf · U10.A3: Reversi...
Transcript of Folienmaster ETH Zürichwolffk/teaching/17infoII/Stunde11.pdf · U10.A3: Reversi...
||
Blatt 10
1) Merge sort
2) Türme von Hanoi
3) Reversi: alpha-beta-Suche
Außerdem: Sortierverfahren und ihre Komplexität
17.05.2017Informatik II – Übung 11 2
||
U10.A1: Merge sort (manuell)
17.05.2017Informatik II – Übung 11 3
21 63915 845 44 88 98 4567 6 62
21 15 63 9 45 44 888 98 67 45 626
2163 15 9 4588 44 458 6798 626
45 44 21 15 9 988 67 62 45 688 63
45456263678898 152144 89 6
||
U10.A1: Merge sort (Pseudo-Code: sort)
17.05.2017Informatik II – Übung 11 4
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 )
||
U10.A1: Merge sort (Pseudo-Code: merge)
17.05.2017Informatik II – Übung 11 5
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
||
U10.A1: Merge sort (Measure)
17.05.2017Informatik II – Übung 11 6
ms
Elemente
||
U10.A2: Türme von Hanoi
17.05.2017Informatik II – Übung 11 7
3
2
2
3
1
1
3…
Zusammenfassung:
Anzahl der Scheiben (n): 4
Anzahl der Schritte (2n-1): 15
Nicht benutzt werden:
3 2 1 3 2 1 3 2 1 3 2 1 3 2 1
Nicht
benutzt
wird:
||
U10.A2: Türme von Hanoi (n Scheiben)
17.05.2017Informatik II – Übung 11 8
Zusammenfassung:
5 Scheiben (31 Schritte):
2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2
4 Scheiben (15 Schritte):
3 2 1 3 2 1 3 2 1 3 2 1 3 2 1
3 Scheiben (7 Schritte):
2 3 1 2 3 1 2
||
U10.A2: Türme von Hanoi (Pseudocode)
17.05.2017Informatik II – Übung 11 9
moves = 2^n-1;
counter = 0;
if n even then
while (counter < moves)
make possible move between tower 1 and tower 2
make possible move between tower 1 and tower 3
make possible move between tower 2 and tower 3
increment counter by 3 units
else [n is odd]
while (counter < moves-1)
make possible move between tower 1 and tower 3
make possible move between tower 1 and tower 2
make possible move between tower 3 and tower 2
increment counter by 3 units
make available move between tower 1 and tower 3
make possible move es gibt immer nur eine Möglichkeit (die
kleinere Scheibe, oder die einzige Scheibe)
||
U10.A3: Reversi (Alpha-Beta-Spieler)
▪ Realisieren Sie eine Bewertungsfunktion, die nach dem α-β-Verfahren
arbeitet, ansonsten jedoch das gleiche leistet wie die reine Minmax-Methode
des letzten Übungsserie.
▪ Am Einfachsten:
▪ 2 Funktionen: min und max, die sich abwechselnd aufrufen.
▪ Die eine ändert die Beta-Schranke, die andere die Alpha-Schranke
17.05.2017Informatik II – Übung 11 10
||
U10.A3: Reversi (Alpha-Beta-Spieler)
17.05.2017Informatik II – Übung 11 11
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);
}
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);
[...]
||
U10.A3: Reversi (Alpha-Beta-Spieler)
17.05.2017Informatik II – Übung 11 12
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;
}
return new BestMove(alpha, bestCoord, cut);
||
Reversi-Turnier!
▪ Mittwoch, 31.05.2017, ab 12:30 Uhr, im
CABinett (Stuz2) http://www.vs.inf.ethz.ch/edu/FS2017/I2/reversi/
▪ Abgabe
▪ Bis Mittwoch, den 23.05.2017, 23:59 Uhr
(Zürich Time)
▪ Zusätzlich: An Leyna Sadamori
▪ Alleine oder in Zweiergruppen (Aber: die
Preise sind für Zweiergruppen ausgelegt!)
17.05.2017Informatik II – Übung 10 13
||
Blatt 11
1) Sortieren mit Suchbäumen
2) Komplexitätsanalyse und O-Notation
3) Komplexität
4) Ein Springer auf dem Schachfeld
17.05.2017Informatik II – Übung 11 14
||
Aufwand von Algorithmen
▪ Problemumfang: n
▪ Oft: Anzahl der Eingabewerte
▪ Komplexität eines Problems
▪ Geringstmöglicher Aufwand, der mit einem Lösungsalgorithmus erreicht
werden kann
▪ Aufwand
▪ Oft ist der Aufwand eines Algorithmus nicht nur von der Problemgrösse
n, sondern von den konkreten Eingabewerten bzw. deren Reihenfolge
abhängig.
▪ Dann unterscheidet man i.a. folgende Fälle:
▪ Günstigster Aufwand („best case“)
▪ Mittlerer Aufwand („average case“)
▪ Ungünstigster Aufwand („worst case“)
17.05.2017Informatik II – Übung 11 15
||
Zeitkomplexität
▪ Landau-Symbole
▪ Abschätzung durch Analyse
▪ wächst .... als
▪ O-Notation
▪ Obere Schranke
▪ Omega-Notation
▪ Untere Schranke
17.05.2017Informatik II – Übung 11 16
http://de.wikipedia.org/wiki/Landau-Symbole
Notation Anschauliche Bedeutung
f wächst nicht wesentlich schneller als g
f wächst langsamer als g
f wächst nicht wesentlich langsamer als g
f wächst schneller als g
f wächst genau so schnell wie g
gf
gf
gf
gf
gf
||
Aufwand von Sortierverfahren
17.05.2017Informatik II – Übung 11 17Quelle: wikipedia.org
||
U11.A1 – Sortieren mit Suchbäumen
▪ 3 Theoretische Fragen (u.a. Laufzeitkomplexität)
17.05.2017Informatik II – Übung 11 18
||
U11.A2: Komplexitätsanalyse (oft in Klausur)
17.05.2017Informatik II – Übung 11 19
// Fragment 4for (int i=0; i<n; i++)
for (int j=0; j<i; j++)
a++;
// Fragment 5while(n >=1 )
n = n/2;
// Fragment 6for (int i=0; i<n; i++)
for (int j=0; j<n*n; j++)
for (int k=0; k<j; k++)
a++;
// Fragment 1for (int i=0; i<n; i++)
a++;
// Fragment 2for (int i=0; i<2n; i++) a++;
for (int j=0; j<n; j++) a++;
// Fragment 3for (int i=0; i<n; i++)
for (int j=0; j<n; j++)
a++;
||
U11.A2 - Beispiel
17.05.2017Informatik II – Übung 11 20
// Fragment 1
for (int i=0; i<n; i++)
a++;
c0
c1*n
+
=
c0 + c1n
||
U11.A3: Komplexität
17.05.2017Informatik II – Übung 11 21
Grösse der Eingabe
Zeit pro Operation
Totale Laufzeit
||
U11.A3: Komplexität
17.05.2017Informatik II – Übung 11 22
||
U11.A4: Ein Springer auf dem Schachbrett
17.05.2017Informatik II – Übung 11 23
||
U11.A4 a) Erreichbare Felder
▪ Finde Menge von Feldern:
▪ Erreichbar mit n Zügen
▪ Gegeben: startPosition
17.05.2017Informatik II – Übung 11 24
||
U11.A4 a) Erreichbare Felder
▪ Vorhanden: Klasse Position▪ p = new Position(0,0);
▪ Position next = p.add(new Position(offX, offY));
▪ Implementiert compareTo, equals, etc.
▪ Implementieren: Methode getReachableSet▪ ArrayList<Position> getReachableSet(Position p,
int numberofMoves)
▪ p: Startposition
▪ numberofMoves: Anzahl Sprünge verfügbar für den Springer
▪ returns: Knoten in der Menge
17.05.2017Informatik II – Übung 11 25
||
U11.A4 b) Backtracking
▪ 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
17.05.2017Informatik II – Übung 11 26
||
▪ Feedback about Tutors
▪ Encourage your students to participate
▪ Email will go out early next week
▪ Next week is the last week for Thursday clases
▪ Ask them to go to Wednesday classes
▪ If interested coordinate with Vincent/Hossein for alternative date
and room for your group
17.05.2017 27
Last weeks
|| 17.05.2017Christian Beckel 28
…viel Spass!