Pascal Schärli
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
boolean | char | byte | short | int | long | float | double | |
---|---|---|---|---|---|---|---|---|
boolean | ||||||||
char | ||||||||
byte | ||||||||
short | ||||||||
int | ||||||||
long | ||||||||
float | ||||||||
double |
int
int
int
int
long
float
double
int
int
int
long
float
double
int
int
int
int
int
int
int
int
int
long
long
long
long
long
long
long
float
float
float
float
float
float
float
float
float
double
double
double
double
double
double
double
double
double
double
double
a + b = c
a
b
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
6
Wert von a
5
Output
6
Wert von a
6
Output
int a = 5;
System.out.println(a++);
int a = 5;
System.out.println(++a);
Die Variabel wird erhöht und der alte Wert wird zurückgegeben
Die Variabel wird erhöht und der neue Wert wird zurückgegeben
Post Increment
Pre Increment
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
8
8
int a = 2;
if (x < 7) {
a = 8;
System.out.println(a);
}
System.out.println(a);
8
2
int a = 2;
if (x < 7) {
int a = 8;
System.out.println(a);
}
System.out.println(a);
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
public static void printListElements(ArrayList<Integer> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public static void printListElements(ArrayList<Integer> list) {
for (Integer k : list) {
System.out.println(k);
}
}
For-Loops können kompakter notiert werden, falls man über alle Elemente einer Liste iterieren will:
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
1 2 4 8
int i = 1;
do{
System.out.println(i + " ");
i*=2;
} while(i < 10);
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
0 2 4 6 8
for (int i = 0; i < 10; i++) {
if (i % 2 != 0)
continue;
System.out.print(i + " ");
}
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
0
for (int i = 0; i < 10; i++) {
if (i % 2 != 0)
break;
System.out.print(i + " ");
}
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
boolean y = true;
String s = y ? "y was true" : "y was false";
System.out.println(s);
y was true
Pascal Schärli - PVK Informatik II 2021
int foo(int a, float b, boolean c){
if(c){
a += b;
}
else{
a-=b;
}
return a;
}
Datentyp vom Rückgabewert
Name der Funktion
Beliebig viele Übergabewerte
Funktions-Körper (Berechnung)
Rückgabe des Resultats
Voraussetzungen
Pascal Schärli - PVK Informatik II 2021
/**
* Checks wether x is within the interval [left, right]
*
* @param x
* @param left The left edge of the interval
* @param right The right edge of the interval
* @return true iff x is in the interval [left, right]
* @throws IllegalArgumentException if the interval is empty
*/
boolean inInterval(double x, double left, double right){
if(left > right){
throw new IllegalArgumentException("Empty Interval");
}
return x >= left && x <= right;
}
Voraussetzungen
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Prüfungsstatistiken
Pascal Schärli - PVK Informatik II 2021
Prüfungsstatistiken
Pascal Schärli - PVK Informatik II 2021
Prüfungsstatistiken
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public static void swap(StringBuffer sbf1, StringBuffer sbf2) {
StringBuffer temp = sbf1;
sbf1 = sbf2;
sbf2 =temp;
}
StringBuffer sbf1 = new StringBuffer("Hello");
StringBuffer sbf2 = new StringBuffer("World");
System.out.println(sbf1+" "+sbf2);
swap(sbf1,sbf2);
System.out.println(sbf1+" "+sbf2);
Hello World
Hello World
Output:
Objektorientierung
Trotzdem dass beim Funktionsaufruf eine Referenz übergeben wird, werden die Stringbuffer nicht getauscht, warum?
Pascal Schärli - PVK Informatik II 2021
public static void swap(StringBuffer s1, StringBuffer s2){
StringBuffer temp = s1;
s1 = s2;
s2 =temp;
}
StringBuffer sbf1 = new StringBuffer("Hello");
StringBuffer sbf2 = new StringBuffer("World");
System.out.println(sbf1+" "+sbf2);
swap(sbf1,sbf2);
System.out.println(sbf1+" "+sbf2);
Output:
sbf1
address1
address1
"Hello"
address2
"World"
sbf2
address2
Hello World
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public static void swap(StringBuffer s1, StringBuffer s2){
StringBuffer temp = s1;
s1 = s2;
s2 =temp;
}
StringBuffer sbf1 = new StringBuffer("Hello");
StringBuffer sbf2 = new StringBuffer("World");
System.out.println(sbf1+" "+sbf2);
swap(sbf1,sbf2);
System.out.println(sbf1+" "+sbf2);
Hello World
Output:
sbf1
address1
address1
"Hello"
address2
"World"
sbf2
address2
s1
address1
s2
address2
swap
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public static void swap(StringBuffer s1, StringBuffer s2){
StringBuffer temp = s1;
s1 = s2;
s2 =temp;
}
StringBuffer sbf1 = new StringBuffer("Hello");
StringBuffer sbf2 = new StringBuffer("World");
System.out.println(sbf1+" "+sbf2);
swap(sbf1,sbf2);
System.out.println(sbf1+" "+sbf2);
Hello World
Output:
sbf1
address1
address1
"Hello"
address2
"World"
sbf2
address2
s1
address1
s2
address2
swap
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public static void swap(StringBuffer s1, StringBuffer s2){
StringBuffer temp = s1;
s1 = s2;
s2 =temp;
}
StringBuffer sbf1 = new StringBuffer("Hello");
StringBuffer sbf2 = new StringBuffer("World");
System.out.println(sbf1+" "+sbf2);
swap(sbf1,sbf2);
System.out.println(sbf1+" "+sbf2);
Output:
sbf1
address1
address1
"Hello"
address2
"World"
sbf2
address2
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Hello World
Hello World
public static void swap(StringBuffer s1, StringBuffer s2) {
StringBuffer temp = new StringBuffer(s1);
s1.delete(0, s1.length());
s1.append(s2);
s2.delete(0, s2.length());
s2.append(temp);
}
StringBuffer sbf1 = new StringBuffer("Hello");
StringBuffer sbf2 = new StringBuffer("World");
System.out.println(sbf1+" "+sbf2);
swap(sbf1,sbf2);
System.out.println(sbf1+" "+sbf2);
Hello World
Output:
sbf1
address1
address1
"Hello"
address2
"World"
sbf2
address2
Hello World
World Hello
s1
address1
s2
address2
swap
"Hello"
"World"
\(\rightarrow\) Man kann das zu Grunde liegende Objekt verändern!
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Objektorientierung
{1, 4, 21, 8} → “1, 4, 21, 8, null“
Lists.java
package u5a1;
import node.Node;
public class Lists {
public static String toString(Node node) {
if (node == null) return "null";
StringBuffer buf = new StringBuffer();
buf.append(node.value).append(", ").append(toString(node.next));
return buf.toString();
}
}
Node.java
package node;
public class Node {
public int value;
public Node next;
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
}
Pascal Schärli - PVK Informatik II 2021
Objektorientierung
public class Impedance {
private final String name;
protected double real, imag;
int i;
public Impedance(String name, double real,
double imag) {
this.name = name;
this.real = real;
this.imag = imag;
}
public static Impedance add(String name,
Impedance z1, Impedance z2) {
double real = z1.getReal() + z2.getReal();
double imag = z1.getImag() + z2.getImag();
return new Impedance(name, real, imag);
}
public String toString() {
return getName() + " = " + getReal() +
" + i" + getImag();
}
public String getName() {
return this.name;
}
public double getImag() {
return this.imag;
}
public double getReal() {
return this.real;
}
}
Pascal Schärli - PVK Informatik II 2021
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public class Fahrzeug{
int radzahl;
public Fahrzeug(int radzahl){
this.radzahl = radzahl;
}
}
public class Auto extends Fahrzeug{
protected float hubraum;
public Auto(float hubraum){
super(4);
this.hubraum = hubraum;
}
public float getHubraum(){
return hubraum;
}
public void setHubraum(float hubraum){
if(hubraum>0) this.hubraum = hubraum;
}
}
public class Lastwagen extends Auto{
float capacity;
public Lastwagen(float hubraum,
float capacity){
super(hubraum);
this.capacity = capacity;
}
}
public class Fahrrad extends Fahrzeug{
public Fahrrad(){
super(2);
}
}
Fahrzeug
Auto
Lastwagen
Fahrrad
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Fahrzeug myFahrrad = new Fahrrad();
System.out.println(myFahrrad.radzahl);
myFahrrad.setHubraum(300);
Fahrzeug myLastwagen = new Lastwagen(80000,50);
System.out.println(myLastwagen.radzahl);
myLastwagen.setHubraum(300);
Output: 4
Output: 2
Compile Error:
The method setHubraum(int) is undefined for the type Fahrzeug
Compile Error:
The method setHubraum(int) is undefined for the type Fahrzeug
Fahrzeug
Auto
Lastwagen
Fahrrad
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Fahrzeug myLastwagen = new Lastwagen(80000,50);
((Lastwagen)myLastwagen).setHubraum(300);
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Fahrzeug myLastwagen = new Lastwagen(80000,50);
System.out.println(myLastwagen.radzahl);
((Lastwagen)myLastwagen).setHubraum(300);
Fahrzeug myFahrrad = new Fahrrad();
System.out.println(myFahrrad.radzahl);
((Lastwagen)myFahrrad).setHubraum(300);
Runtime Error:
Exception in thread "main" java.lang.ClassCastException: Fahrzeug cannot be cast to Lastwagen
Fahrzeug
Auto
Lastwagen
Fahrrad
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Fahrzeug myLastwagen = new Lastwagen(80000, 50);
myLastwagen.setHubraum(300);
Fahrzeug myFahrrad = new Fahrrad();
((Lastwagen)myFahrrad).setHubraum(300);
Objektorientierung
Fahrrad
Lastwagen
Auto
Fahrzeug
Pascal Schärli - PVK Informatik II 2021
class Mensch { ... }
class ETHStudent extends Student { String legiNr; }
class ITETStudent extends ETHStudent { ... }
class Student extends Mensch { ... }
Student tim = new ETHStudent();
Object obj = tim;
Mensch mensch = tim;
Student student = tim;
ETHStudent ethStud = (ETHStudent) tim;
ITETStudent itetStud = (ITETStudent) tim;
Java denkt es ist ein Student
Wir wissen, es ist ein ETHStudent
ohne cast
mit cast
Runtime Error
zuweisung möglich ohne Cast
zuweisung möglich ohne Cast
zuweisung möglich mit Cast
Runtime Error
jede Java Klasse erbt von Object
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
class Mensch { ... }
class ETHStudent extends Student { String legiNr; }
class ITETStudent extends ETHStudent { ... }
class Student extends Mensch { ... }
Student tim = new ETHStudent();
Mensch mensch = tim;
Student student = tim;
ETHStudent ethStud = (ETHStudent) tim;
ITETStudent itetStud = (ITETStudent) tim;
System.out.println(mensch.legiNr);
System.out.println(student.legiNr);
System.out.println(ethStud.legiNr);
System.out.println(itetStud.legiNr);
Compile Error
Compile Error
Kein Compile Error
Kein Compile Error
haben legiNr
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Student tim = new ETHStudent();
System.out.println(tim instanceof Mensch);
System.out.println(tim instanceof Student);
System.out.println(tim instanceof ETHStudent);
System.out.println(tim instanceof ITETStudent);
true
true
true
false
Output:
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public abstract class AbstractStack {
// declare abstract functions
public abstract void push();
public abstract int pop();
public abstract int peek();
public abstract int size();
// already implement some functions
public boolean isEmpty(){
return size() == 0;
}
}
public class Stack extends AbstractStack {
// declare abstract functions
public void push(){/*TODO*/}
public int pop(){/*TODO*/}
public int peek(){/*TODO*/}
public int size(){/*TODO*/}
}
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public Interface IStack {
// declare abstract functions
public void push();
public int pop();
public int peek();
public int size();
}
public class Stack implements IStack {
// declare abstract functions
public int push(){/*TODO*/}
public int pop(){/*TODO*/}
public int peek(){/*TODO*/}
public int size(){/*TODO*/}
}
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Carina
Julia
Benutze ArrayTree
Meinung geändert, benutze ListTree
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
interface ITree{
public ITree leftChild();
public ITree rightChild();
public int getValue();
public int setValue();
}
public class TreeFactory {
public static ITree makeTree(){
return new ListTree();
}
}
Tree Interface
Tree Factory
public class FancyCode {
public ArrayTree important(){
ArrayTree myRoot = new ArrayTree();
myRoot.setValue(4);
return myRoot;
}
}
Julias Code Vorher
public class FancyCode {
public ITree important(){
ITree myRoot = TreeFactory.makeTree();
myRoot.setValue(4);
return myRoot;
}
}
Julias Code Jetzt
ArrayList myList = new ArrayList();
myList.add(42);
myList.add(“I <3 Info II”);
int i = (Integer)myList.get(0);
String s = (String)myList.get(1);
ArrayList Kann beliebige Typen speichern:
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
ArrayList<Integer> myList = new ArrayList<Integer>();
myList.add(42);
int i = myList.get(0);
myList.add(“Hello World”);
Man kann einer ArrayList sagen, dass sie nur einen bestimmten Typ speichern darf:
Compile-Error
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public class Tree<T> {
T value;
Tree right;
Tree left;
public Tree(T value) {
right = null;
left = null;
this.value = value;
}
public Tree<T> leftChild() {
return left;
}
public Tree<T> rightChild() {
return right;
}
}
Tree<Integer> tree1 = new Tree<Integer>(5);
tree1.left = new Tree<Integer>(3);
tree1.right = new Tree<Integer>(6);
Tree<String> tree2 = new Tree<String>("root");
tree2.right = new Tree<String>("right");
tree2.left = new Tree<String>("left");
5
3
6
"root"
"left"
"right"
tree1
tree2
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Herbst 2017, 1.17 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 1.17 Notenpunkte
public class Activity{
private String name;
private double duration;
private double rating;
public Activity(String name, double duration, double rating) {
if(duration <= 0) {
throw new IllegalArgumentException("The duration can not be negative!");
}
if(rating < 0 || rating > 5) {
throw new IllegalArgumentException("The rating has to be between 0 and 5!");
}
this.name = name;
this.duration = duration;
this.rating = rating;
}
public String getName() {
return name;
}
public double getDuration() {
return duration;
}
public double getRating() {
return rating;
}
}
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 1.17 Notenpunkte
public class Hike extends Activity {
private double difficulty;
public Hike(String name, double duration, double rating,
double difficulty) {
super(name, duration, rating);
if(difficulty < 1 || difficulty > 5) {
throw new IllegalArgumentException("The difficulty has to "
+ "be between 1 and 5!");
}
this.difficulty = difficulty;
}
public double getDifficulty() {
return difficulty;
}
}
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 1.17 Notenpunkte
import java.util.ArrayList;
public class ActivityManager{
ArrayList<Activity> activities;
public ActivityManager() {
activities = new ArrayList<Activity>();
}
public void addActivitiy(Activity activity){
activities.add(activity);
}
public Hike findBestHike(double maxDuration, double maxDifference){
// [..]
}
}
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 1.17 Notenpunkte
public Hike findBestHike(double maxDuration, double maxDifficulty){
Hike bestHike = null;
for (Activity activity : activities) {
if (activity instanceof Hike) {
Hike hike = (Hike) activity;
if (hike.getDuration() <= maxDuration
&& hike.getDifficulty() <= maxDifficulty) {
if (bestHike == null
|| hike.getRating() > bestHike.getRating()) {
bestHike = hike;
}
}
}
}
return bestHike;
}
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 1.17 Notenpunkte
public class Main{
public static void main(String[] args){
ActivityManager = new ActivityManager();
// Adding activities
// Assume the parameter order for the Activity constructor is
// [name, duration, rating] and for the Hike constructor
// [name, duration, rating, difficulty]
manager.addActivitiy(new Activity("Spaziergang am See", 1.2, 2.5));
manager.addActivitiy(new Hike("Brunni - Kl. Mythen", 2, 4.5, 4.5));
manager.addActivitiy(new Activity("Velofahren Zuerich", 0.5, 1));
manager.addActivitiy(new Hike("Brunni - Gr. Mythen", 3, 3.5, 3.5));
manager.addActivitiy(new Activity("Schwimmen am Letten", 1, 4.5));
manager.addActivitiy(new Activity("Nichtstun", 0, 2.5));
manager.addActivitiy(new Hike("Weggis - Rigi Kulm", 5, 4, 3.5));
manager.addActivitiy(new Hike("Alpnach - Pilatus Kulm", 5, 5, 2));
double maxDuration = 4;
double maxDifficulty = 3.5;
Hike bestHike = manager.findBestHike(maxDuration, maxDifference);
System.out.println("Die beste Wanderung: " + bestHike.getName());
}
}
Die beste Wanderung: Brunni - Gr. Mythen
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Komplexität
Zeige, dass \(\frac{n^2 + 100}{2} \in O(n^2)\):
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Komplexität
Zeile 5 Wird n-Mal Ausgeführt.
private static int f1(int n) {
int out = 0;
for(int i = 0; i < n; i++) {
out++;
}
return out;
}
\(O(n)\)
Pascal Schärli - PVK Informatik II 2021
Komplexität
Zeile 5 Wird ca. \(\lfloor \frac{n}{5} \rfloor \)-Mal Ausgeführt.
\(O(n)\)
private static int f2(int n) {
int out = 0;
for(int i = 0; i * 5 < n; i++) {
out++;
}
return out;
}
Pascal Schärli - PVK Informatik II 2021
Komplexität
Zeile 6 Wird \(n^2\)-Mal Ausgeführt.
\(O(n^2)\)
private static int f3(int n) {
int out = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++){
out++;
}
}
return out;
}
Pascal Schärli - PVK Informatik II 2021
Komplexität
Zeile 6 Wird \(1000 * n\)-Mal Ausgeführt.
\(O(n)\)
private static int f4(int n) {
int out = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < 1000; j++){
out++;
}
}
return out;
}
Pascal Schärli - PVK Informatik II 2021
Komplexität
Zeile 6 Wird \(\sum_{i=0}^{n-1}(i) = \frac{n*(n-1)}{2}\)-Mal Ausgeführt.
\(O(n^2)\)
private static int f5(int n) {
int out = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++){
out++;
}
}
return out;
}
Pascal Schärli - PVK Informatik II 2021
Komplexität
\(O(n^6)\)
private static int f5(int n) {
int out = 0;
for(int i = 0; i < n*n; i++) {
for(int j = 0; j*j < i; j++){
for(int k = j*i; k > 0; k--){
out++;
}
}
}
return out;
}
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Komplexität
public static int fibo(int n) {
if (n <= 1) return 1;
return fibo1(n - 1) + fibo1(n - 2);
}
Pascal Schärli - PVK Informatik II 2021
fibo(1) + fibo(0)
fibo(2) + fibo(1)
fibo(1) + fibo(0)
fibo(3) + fibo(2)
fibo(4)
Die Funktion ruft sich normalerweise ca. 2-mal rekursiv auf, der Resultierende Baum hat ungefähr Höhe \(n\) und somit ca \(2^n\) Knoten.
\(\rightarrow\) Zeit: \(O(2^n)\)
Der Baum wird "Depth First" traversiert, also ein Ast nach dem anderen, somit ist zu jedem Zeitpunkt höchstens ein Pfad von Wurzel bis Blatt im Speicher.
\(\rightarrow\) Speicher: \(O(n)\)
Speicher:
\(O(n)\)
Zeit:
\(O(2^n)\)
Komplexität
public static int fibo1(int n) {
if (n <= 1) return 1;
return fibo1(n - 1) + fibo1(n - 2);
}
public static int fibo2(int n) {
if(n <= 1) return 1;
int[] memory = new int[n + 1];
memory[0] = 1;
memory[1] = 1;
for (int i = 2; i <= n; i++) {
memory[i] = memory[i - 1] + memory[i - 2];
}
return memory[n];
}
public static int fibo3(int n) {
int last1 = 1;
int last2 = 1;
for (int i = 2; i <= n; i++) {
int temp = last1 + last2;
last1 = last2;
last2 = temp;
}
return last2;
}
Speicher:
\(O(n)\)
Zeit:
\(O(2^n)\)
Zeit:
\(O(n)\)
Speicher:
\(O(n)\)
Speicher:
\(O(1)\)
Zeit:
\(O(n)\)
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Herbst 2017, 0.67 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 0.67 Notenpunkte
public static int algorithm1(int[] input){
assert input != null && input.length > 0;
int maxSum = input[0];
for(int i = 0; i < input.length; i++){
for(int j = 0; j < input.length; j++){
int sum = 0;
for(int k = i; k <= j; k++){
sum += input[k];
}
if(sum > maxSum){
maxSum = sum;
}
}
}
return maxSum;
}
Zeit:
\(O(input.length^3)\)
Zusätzlicher Speicher:
\(O(1)\)
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 0.67 Notenpunkte
public static int algorithm2(int[] input){
assert input != null && input.length > 0;
// calculate partial sums
int[] p = new int[input.length];
for(int i = 0; i < input.length; i++){
if(i == 0) p[i] = input[i];
else p[i] = p[i-1] + input[i];
}
// search for the maximum partial sum
int maxSum = input[0];
for(int i = 0; i < input.length; i++){
for(int j = i; j < input.length; j++){
int sum = (i == j) ? p[j] : p[j] - p[i];
if(sum > maxSum){
maxSum = sum;
}
}
}
return maxSum;
}
Zeit:
\(O(input.length^2)\)
Zusätzlicher Speicher:
\(O(input.length)\)
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Konzept
Vorteile
Wichtigste Operationen
Nachteile
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Konzept
Vorteile
Wichtigste Operationen
Nachteile
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Konzept
Wichtigste Operationen
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Konzept
Anwendungen
Wichtigste Operationen
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Kanten
Knoten
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Kanten haben eine Richtung
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Zusammenhängender, Gerichteter Graph ohne Zyklen
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Knoten ohne Kindknoten
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
jeder Knoten besitzt höchstens zwei Kindknoten, die Reihenfolge der Kindknoten ist relevant.
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
jeder Knoten besitzt entweder zwei oder keine Kinder
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Alle Blätter haben die selbe Tiefe
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Ungleich verteilter Baum
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Anzahl “Levels” ohne Wurzel
→ hier: 3
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Weg durch Graph (Nur in Pfeilrichtung)
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
12 5 3 1 9 10 19 14 20 22
14
Gib den Wert vom Knoten aus
Gib den linken Teilbaum in Pre-Order aus
Gib den rechten Teilbaum in Pre-Order aus
22
20
19
12
10
9
5
3
1
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
1 3 5 9 10 12 14 19 20 22
14
Gib den linken Teilbaum in In-Order aus
Gib den Wert vom Knoten aus
Gib den rechten Teilbaum in In-Order aus
22
20
19
12
10
9
5
3
1
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
1 3 10 9 5 14 22 20 19 12
14
Gib den linken Teilbaum in Post-Order aus
Gib den rechten Teilbaum in Post-Order aus
Gib den Wert vom Knoten aus
22
20
19
12
10
9
5
3
1
Pascal Schärli - PVK Informatik II 2021
[ , , , , , , ]
Datenstrukturen
left child: 2*i+1
right child: 2*i+2
parent: ⌊(i-1)/2⌋
'H'
0
'B '
1
'J'
2
'A '
3
'E'
4
' '
5
'M'
6
0
1
3
4
5
6
2
Pascal Schärli - PVK Informatik II 2021
Indent = 1
Indent = 2
Indent = 3
Indent = 0
5
5
.8
5
.8
..3
5
.8
..3
..2
5
.8
..3
..2
...1
5
.8
..3
..2
...1
...9
5
.8
..3
..2
...1
...9
.7
5
.8
..3
..2
...1
...9
.7
..3
5
8
3
2
1
9
7
3
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
5( )
5(8( ) )
5(8(3 ) )
5(8(3,2( )) )
5(8(3,2(1 )) )
5(8(3,2(1,9)) )
5(8(3,2(1,9)),7( ))
5(8(3,2(1,9)),7(3))
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
5
8
7
3
2
9
3
1
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
1
3
5
6
9
10
12
18
20
19
22
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
1
3
5
6
9
10
12
18
20
19
22
Gesucht: 9
Datenstrukturen
Element Finden
Pascal Schärli - PVK Informatik II 2021
1
3
5
6
9
10
12
18
20
19
22
Gesucht: 9
Element Finden
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
1
3
5
6
9
10
12
18
20
19
22
Gesucht: 9
Element Finden
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
1
3
5
6
9
10
12
18
20
19
22
Gesucht: 9
Element Finden
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
1
3
5
6
9
10
12
18
20
19
22
Gesucht: 15
Element Finden
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
1
3
5
6
9
10
12
18
20
19
22
Gesucht: 15
Element Finden
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
1
3
5
6
9
10
12
18
20
19
22
\(\rightarrow\) Nicht vorhanden
Gesucht: 15
Element Finden
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
1
3
5
6
9
10
12
18
20
19
22
14
Datenstrukturen
Element Einfügen
Pascal Schärli - PVK Informatik II 2021
1
3
5
6
9
10
12
18
20
19
22
14
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Element Einfügen
1
3
5
6
9
10
12
18
20
19
22
14
Pascal Schärli - PVK Informatik II 2021
Element Einfügen
Datenstrukturen
1
3
5
6
9
10
12
18
20
19
22
14
Element Entfernen
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
1
3
5
6
9
10
12
20
22
19
14
Pascal Schärli - PVK Informatik II 2021
Element Entfernen
Datenstrukturen
1
3
5
6
9
10
12
19
20
22
14
Element Entfernen
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
1
3
5
9
10
12
20
22
19
14
Element Entfernen
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Laufzeiten
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
6
11
5
3
2
Pascal Schärli - PVK Informatik II 2021
Element Einfügen
2
5
8
4
3
9
12
6
11
7
5
4
1
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Element Einfügen
2
5
8
4
3
9
12
6
11
7
5
4
1
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Element Einfügen
2
5
8
4
3
9
12
6
11
7
5
4
1
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Wurzel Entfernen
4
5
8
3
2
9
12
6
11
7
5
4
1
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Wurzel Entfernen
4
5
8
3
2
9
12
6
11
7
5
4
1
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
4
5
8
3
2
9
12
6
11
7
5
4
1
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Wurzel Entfernen
4
5
8
3
2
9
12
6
11
7
5
4
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Wurzel Entfernen
4
5
8
3
2
9
12
6
11
7
5
4
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Wurzel Entfernen
Frühling 2016, 0.67 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Frühling 2016, 0.67 Notenpunkte
Inorder Traversierung:
2 4 3 7 6 15 10 13 5
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Frühling 2016, 0.67 Notenpunkte
4
5
8
1
2
3
7
0
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
Java Bytecode
Stack
Bytecode
iconst_4
iconst_2
iconst_3
imul
iconst_1
isub
iadd
Pascal Schärli - PVK Informatik II 2021
iconst_4
iconst_2
iconst_3
imul
iconst_1
isub
iadd
Bytecode
Stack
4
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
iconst_4
iconst_2
iconst_3
imul
iconst_1
isub
iadd
Bytecode
Stack
4
2
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
iconst_4
iconst_2
iconst_3
imul
iconst_1
isub
iadd
Bytecode
Stack
4
2
3
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
iconst_4
iconst_2
iconst_3
imul
iconst_1
isub
iadd
Bytecode
Stack
4
6
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
iconst_4
iconst_2
iconst_3
imul
iconst_1
isub
iadd
Bytecode
Stack
4
6
1
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
iconst_4
iconst_2
iconst_3
imul
iconst_1
isub
iadd
Bytecode
Stack
4
5
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
iconst_4
iconst_2
iconst_3
imul
iconst_1
isub
iadd
Bytecode
Stack
9
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
}
Bytecode
Übersetzung
Stack
a
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
a
}
Bytecode
Übersetzung
Stack
a
1
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
a 1
}
Bytecode
Übersetzung
Stack
a
1
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
}
}
Bytecode
Übersetzung
Stack
a
1
a > 1
1
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
1
}
}
Bytecode
Übersetzung
Stack
1
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
return 1;
}
}
Bytecode
Übersetzung
Stack
1
1
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
return 1;
}
a
}
Bytecode
Übersetzung
Stack
a
1
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
return 1;
}
a 1
}
Bytecode
Übersetzung
Stack
a
1
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
return 1;
}
a-1
}
Bytecode
Übersetzung
Stack
a
1
a-1
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
return 1;
}
f(a-1)
}
Bytecode
Übersetzung
Stack
a
1
f(a-1)
a
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
return 1;
}
f(a-1) a
}
Bytecode
Übersetzung
Stack
a
1
f(a-1)
a
2
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
return 1;
}
f(a-1) a 2
}
Bytecode
Übersetzung
Stack
a
1
f(a-1)
a
2
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
return 1;
}
f(a-1) a-2
}
Bytecode
Übersetzung
Stack
a
1
f(a-1)
a
2
a-2
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
return 1;
}
f(a-1) f(a-2)
}
Bytecode
Übersetzung
Stack
a
1
f(a-1)
f(a-2)
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
return 1;
}
f(a-1) + f(a-2)
}
Bytecode
Übersetzung
Stack
a
1
f(a-1)
f(a-2)
f(a-1) + f(a-2)
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
return 1;
}
return f(a-1) + f(a-2);
}
Bytecode
Übersetzung
Stack
a
1
f(a-1)
f(a-2)
f(a-1) + f(a-2)
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
static int f(int a);
0 iload_0 [a]
1 iconst_1
2 if_icmpgt 7
5 iconst_1
6 ireturn
7 iload_0 [a]
8 iconst_1
9 isub
10 invokestatic test.Main.f(int) : int [16]
13 iload_0 [a]
14 iconst_2
15 isub
16 invokestatic test.Main.f(int) : int [16]
19 iadd
20 ireturn
static int f(int a) {
if(a <= 1){
return 1;
}
return f(a-1) + f(a-2);
}
Bytecode
Übersetzung
Stack
Java Bytecode
Pascal Schärli - PVK Informatik II 2021
Herbst 2017, 0.34 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 0.34 Notenpunkte
public class IncrementingInteger{
public static void main(){
System.out.println(expr1(1));
System.out.println(expr2(1));
}
public static int expr1(int value){
int x = (value++)*2;
return x;
}
public static int expr2(int value){
int x = (++value)*2;
return x;
}
}
2
4
Code A:
0: iload_0
1: iinc 0, 1
2: iconst_2
3: imul
4: istore_1
5: iload_1
6: ireturn
Code B:
0: iinc 0, 1
1: iload_0
2: iconst_2
3: imul
4: istore_1
5: iload_1
6: ireturn
expr1
expr2
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Algorithmen
Pascal Schärli - PVK Informatik II 2021
1
3
6
12
24
49
98
320
160
80
40
20
10
5
f(5,98) = 320 + 160 + 10 = 490
f(320,1) = 320
f(160,3) = f(320,1) + 160
f(80,6) = f(160,3)
f(40,12) = f(80,6)
f(20,24) = f(40,12)
f(10,49) = f(20,24) + 10
f(5,98) = f(10,49)
a | b | f(a,b) |
---|---|---|
Algorithmen
Pascal Schärli - PVK Informatik II 2021
\(f(a,n) = f(2a,\frac{n-1}{2}) + a\)
\(\frac{n-1}{2} \in [1 \ldots n-1]\)
\(f(2a,\frac{n-1}{2}) + a=2a \cdot \frac{n-1}{2} + a\)
\(=a \cdot n \)
\(n\) ungerade:
\(f(a,n) = f(2a,\frac{n}{2})\)
\(\frac{n}{2} \in [1 \ldots n-1]\)
\(f(2a,\frac{n}{2})=2a \cdot \frac{n}{2}\)
\(=a \cdot n \)
\(n\) gerade:
Gegeben, dass \(f(a,b) = a \cdot b \) für \(b \in [1 \dots n-1] \).
Zeige daraus, dass \(f(a,n) = a \cdot n\).
Induktionsschritt:
Induktionsverankerung: \(b = 1 \)
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Weitere Beispiele:
\(A(0,m) = m + 1\)
\(A(n,0) = A(n-1,1)\)
\(A(n,m) = A(n-1,A(n,m-1))\)
\(2^{65536} - 3 \)
\(65533\)
\(13\)
\(\Rightarrow A(1,1) = 3\)
A(1,1) // A(1,1)
A(1,0) // A(1,1) = A(0,A(1,0))
A(0, 1) // A(1,0) = A(0,1)
<- 2 // A(0,1) = 2
<- 2 // A(1,0) = 2
A(0, 2) // A(1,1) = A(0,2)
<- 3 // A(0,2) = 3
<- 3 // A(1,1) = 3
A(1,1) // A(1,1)
A(1,0) // A(1,1) = A(0,A(1,0))
A(0, 1) // A(1,0) = A(0,1)
<- 2 // A(0,1) = 2
<- 2 // A(1,0) = 2
A(0, 2) // A(1,1) = A(0,2)
<- 3 // A(0,2) = 3
A(1,1) // A(1,1)
A(1,0) // A(1,1) = A(0,A(1,0))
A(0, 1) // A(1,0) = A(0,1)
<- 2 // A(0,1) = 2
<- 2 // A(1,0) = 2
A(0, 2) // A(1,1) = A(0,2)
A(1,1) // A(1,1)
A(1,0) // A(1,1) = A(0,A(1,0))
A(0, 1) // A(1,0) = A(0,1)
<- 2 // A(0,1) = 2
<- 2 // A(1,0) = 2
A(1,1) // A(1,1)
A(1,0) // A(1,1) = A(0,A(1,0))
A(0, 1) // A(1,0) = A(0,1)
<- 2 // A(0,1) = 2
A(1,1) // A(1,1)
A(1,0) // A(1,1) = A(0,A(1,0))
A(0, 1) // A(1,0) = A(0,1)
A(1,1) // A(1,1)
A(1,0) // A(1,1) = A(0,A(1,0))
A(1,1) // A(1,1)
Beispiel: \(A(1,1)\)
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Binäre Suche:
Gesuchter Wert mit der Mitte/Pivot vergleichen.
Gleich wie die Mitte?
-> Gefunden!
Grösser als die Mitte?
-> rechts weitersuchen
Kleiner als die Mitte?
-> links weitersuchen
26
1
2
4
6
9
10
14
16
17
20
23
11
0
1
2
3
4
5
6
7
8
9
10
pivot
10
Vergleich:
16
?
>
Algorithmen
Pascal Schärli - PVK Informatik II 2021
11
0
1
2
3
4
5
6
7
8
9
10
17
Vergleich:
16
?
<
Binäre Suche:
Gesuchter Wert mit der Mitte/Pivot vergleichen.
Gleich wie die Mitte?
-> Gefunden!
Grösser als die Mitte?
-> rechts weitersuchen
Kleiner als die Mitte?
-> links weitersuchen
pivot
23
20
17
16
14
10
9
6
4
2
1
26
Algorithmen
Pascal Schärli - PVK Informatik II 2021
14
Vergleich:
16
?
>
Binäre Suche:
Gesuchter Wert mit der Mitte/Pivot vergleichen.
Gleich wie die Mitte?
-> Gefunden!
Grösser als die Mitte?
-> rechts weitersuchen
Kleiner als die Mitte?
-> links weitersuchen
pivot
23
20
17
16
14
10
9
6
4
2
1
26
Algorithmen
11
0
1
2
3
4
5
6
7
8
9
10
Pascal Schärli - PVK Informatik II 2021
16
Vergleich:
16
?
=
pivot
23
20
17
16
14
10
9
6
4
2
1
26
Binäre Suche:
Gesuchter Wert mit der Mitte/Pivot vergleichen.
Gleich wie die Mitte?
-> Gefunden!
Grösser als die Mitte?
-> rechts weitersuchen
Kleiner als die Mitte?
-> links weitersuchen
Algorithmen
11
0
1
2
3
4
5
6
7
8
9
10
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Knoten
Unterbäume
Baum
(
)
Baum
Unterbäume
,
Knoten
B
A
C
Z
A(B(C,D))
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Algorithmen
+
)
(
Clause
Var
neuen offset zurückgeben
parst ')'
sonst offset erhöhen ('+' parsen)
falls kein '+' folgt, Schleife abbrechen
parst Var
parst '('
private static int parseClause(String kd, int offset) throws ParseException {
if (!checkNext('(', kd, offset)) {
throw new ParseException("expected '('", offset);
}
offset++;
while(true){
offset = parseVar(kd, offset);
if (!checkNext('+', kd, offset)) {
break;
}
offset++;
}
if (!checkNext(')', kd, offset)) {
throw new ParseException("expected ')'", offset);
}
offset++;
return offset;
}
Pascal Schärli - PVK Informatik II 2021
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Algorithmen
3
2
1
1
4
7
2
5
6
8
3
1
4
7
2
5
6
8
3
8
7
6
5
4
3
2
1
1
4
7
2
5
6
8
3
Pascal Schärli - PVK Informatik II 2021
Algorithmen
3
2
1
1
4
7
2
5
6
8
3
1
4
7
2
5
6
8
3
8
7
6
5
4
3
2
1
1
4
7
2
5
6
8
3
Pascal Schärli - PVK Informatik II 2021
1
2
4
7
ptr1
ptr2
3
8
6
5
1
2
4
7
3
8
6
5
Um zwei Listen zu Mergen kann man:
• einen Zeiger auf den Anfang von beiden Teillisten setzen
• Das kleinere Element dem Resultat hinzufügen und diesen Zeiger vergrössern
Algorithmen
Pascal Schärli - PVK Informatik II 2021
1
2
4
7
ptr1
ptr2
3
8
6
5
1
2
4
7
3
8
6
5
Um zwei Listen zu Mergen kann man:
• einen Zeiger auf den Anfang von beiden Teillisten setzen
• Das kleinere Element dem Resultat hinzufügen und diesen Zeiger vergrössern
Algorithmen
Pascal Schärli - PVK Informatik II 2021
1
2
4
7
ptr1
ptr2
3
8
6
5
1
2
4
7
3
8
6
5
Um zwei Listen zu Mergen kann man:
• einen Zeiger auf den Anfang von beiden Teillisten setzen
• Das kleinere Element dem Resultat hinzufügen und diesen Zeiger vergrössern
Algorithmen
Pascal Schärli - PVK Informatik II 2021
1
2
4
7
ptr1
ptr2
3
8
6
5
1
2
4
7
3
8
6
5
Um zwei Listen zu Mergen kann man:
• einen Zeiger auf den Anfang von beiden Teillisten setzen
• Das kleinere Element dem Resultat hinzufügen und diesen Zeiger vergrössern
Algorithmen
Pascal Schärli - PVK Informatik II 2021
1
2
4
7
ptr1
ptr2
3
8
6
5
1
2
4
7
3
8
6
5
Um zwei Listen zu Mergen kann man:
• einen Zeiger auf den Anfang von beiden Teillisten setzen
• Das kleinere Element dem Resultat hinzufügen und diesen Zeiger vergrössern
Algorithmen
Pascal Schärli - PVK Informatik II 2021
1
2
4
7
ptr1
ptr2
3
8
6
5
1
2
4
7
3
8
6
5
Um zwei Listen zu Mergen kann man:
• einen Zeiger auf den Anfang von beiden Teillisten setzen
• Das kleinere Element dem Resultat hinzufügen und diesen Zeiger vergrössern
Algorithmen
Pascal Schärli - PVK Informatik II 2021
1
2
4
7
ptr1
ptr2
3
8
6
5
1
2
4
7
3
8
6
5
Um zwei Listen zu Mergen kann man:
• einen Zeiger auf den Anfang von beiden Teillisten setzen
• Das kleinere Element dem Resultat hinzufügen und diesen Zeiger vergrössern
Algorithmen
Pascal Schärli - PVK Informatik II 2021
1
2
4
7
ptr1
ptr2
3
8
6
5
1
2
4
7
3
8
6
5
Um zwei Listen zu Mergen kann man:
• einen Zeiger auf den Anfang von beiden Teillisten setzen
• Das kleinere Element dem Resultat hinzufügen und diesen Zeiger vergrössern
Algorithmen
Pascal Schärli - PVK Informatik II 2021
1
2
4
7
ptr1
ptr2
3
8
6
5
1
2
4
7
3
8
6
5
Um zwei Listen zu Mergen kann man:
• einen Zeiger auf den Anfang von beiden Teillisten setzen
• Das kleinere Element dem Resultat hinzufügen und diesen Zeiger vergrössern
Algorithmen
Pascal Schärli - PVK Informatik II 2021
1
2
4
7
ptr1
ptr2
3
8
6
5
1
2
4
7
3
8
6
5
Um zwei Listen zu Mergen kann man:
• einen Zeiger auf den Anfang von beiden Teillisten setzen
• Das kleinere Element dem Resultat hinzufügen und diesen Zeiger vergrössern
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
6
5
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
6
5
5
6
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
6
5
5
6
3
5
6
8
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
6
5
5
6
3
5
6
8
2
7
4
1
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
6
5
5
6
3
5
6
8
2
7
4
1
2
7
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
6
5
5
6
3
5
6
8
2
7
4
1
2
7
2
7
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
6
5
5
6
3
5
6
8
2
7
4
1
2
7
2
7
4
1
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
6
5
5
6
3
5
6
8
2
7
4
1
2
7
2
7
4
1
1
4
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
6
5
5
6
3
5
6
8
2
7
4
1
2
7
2
7
4
1
1
4
1
2
4
7
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
6
5
5
6
3
5
6
8
2
7
4
1
2
7
2
7
4
1
1
4
1
2
4
7
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
6
5
5
6
3
5
6
8
2
7
4
1
2
7
2
7
4
1
1
4
1
2
4
7
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
Algorithmen
Algorithmen
Pascal Schärli - PVK Informatik II 2021
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
3
8
6
5
3
8
3
8
6
5
5
6
3
5
6
8
2
7
4
1
2
7
2
7
4
1
1
4
1
2
4
7
3
8
6
5
2
7
4
1
3
8
6
5
2
7
4
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Algorithmen
Es ist möglich mit Heaps einen Array in-place zu sortieren, also ohne zusätzlichen Speicherplatz zu verwenden.
Dabei nutzen wir die Array-Darstellung von Binärbäumen.
Der Algorithmus läuft in zwei Phasen:
Zuerst erstellen wir eine Heapstruktur im Array
Danach können wir immer das grösste bzw kleinste Element aus dem Heap entfernen
Pascal Schärli - PVK Informatik II 2021
7
3
9
1
2
7
3
9
1
2
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
3
9
1
2
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
3
9
1
2
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
3
1
2
7
3
9
1
2
min heap (nur als visualisierung vom Array)
9
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
3
9
1
2
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
3
9
1
2
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
3
9
1
2
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
unstrukturiert
im heap
sortiert
7
3
9
1
2
7
3
9
1
2
min heap (nur als visualisierung vom Array)
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
3
9
1
2
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
3
9
1
2
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
3
9
2
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
1
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
3
9
2
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
3
9
2
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
3
9
2
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
3
9
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
9
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
7
9
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
9
7
3
9
1
2
min heap (nur als visualisierung vom Array)
unstrukturiert
im heap
sortiert
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Algorithmen
Pascal Schärli - PVK Informatik II 2021
Algorithmen
Pascal Schärli - PVK Informatik II 2021
000
001
010
011
100
101
110
111
0kg
0.-
2kg
200.-
1kg
50.-
3kg
250.-
4kg
100.-
6kg
300.-
5kg
150.-
7kg
350.-
00X
0kg
0.-
01X
1kg
50.-
10X
4kg
100.-
11X
5kg
150.-
0XX
0kg
0.-
1XX
4kg
100.-
XXX
0kg
0.-
Kapazität: 3kg
Alter Laptop: 4kg, 100.-
Michaels: 1kg, 50.-
Katze: 2kg, 200.-
Algorithmen
Pascal Schärli - PVK Informatik II 2021
000
001
010
011
100
101
110
111
0kg
0.-
2kg
200.-
1kg
50.-
3kg
250.-
4kg
100.-
6kg
300.-
5kg
150.-
7kg
350.-
Zu schwer
Beste Lösung:
Michaels & Katze mit Wert 250.-
Algorithmen
Katze: 2kg, 200.-
Michaels: 1kg, 50.-
Alter Laptop: 4kg, 100.-
Kapazität: 3kg
Pascal Schärli - PVK Informatik II 2021
000
001
010
011
0kg
0.-
2kg
200.-
1kg
50.-
3kg
250.-
00X
0kg
0.-
01X
1kg
50.-
0XX
0kg
0.-
1XX
4kg
100.-
XXX
0kg
0.-
Zu schwer
Viel Arbeit erspart
Algorithmen
Katze: 2kg, 200.-
Michaels: 1kg, 50.-
Alter Laptop: 4kg, 100.-
Kapazität: 3kg
Sobald wir wissen, dass der Zustand illegal ist, können wir zurück gehen und müssen dort nicht mehr weiterrechnen.
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2020
Frühling 2016, 0.35 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Frühling 2016, 0.35 Notenpunkte
{XYZ}
{X{Y{Z}}}
{X,Y,{Z}}
XYZ}
{XYZ
{
{}
{XY,{}}
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Parallele Programmierung
Pascal Schärli - PVK Informatik II 2021
Parallele Programmierung
Pascal Schärli - PVK Informatik II 2021
Parallele Programmierung
k: 4‘000‘000, j: 3‘752‘884 k: 4‘000‘000, j: 10‘806‘936 k: 4‘000‘000, j: 7‘307‘897 k: 4‘000‘000, j: 5‘322‘116 k: 4‘000‘000, j: 4‘366‘084
public class Worker extends Thread {
static int j = 0;
int k = 0;
@Override
public void run() {
for (int i = 0; i < 4000000; i++) {
j++;
k++;
}
System.out.println("k: " + k + " j:" + j);
}
}
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++){
new Worker().start();
}
}
}
Pascal Schärli - PVK Informatik II 2021
Parallele Programmierung
k: 4‘000‘000, j: 3‘752‘884 k: 4‘000‘000, j: 10‘806‘936 k: 4‘000‘000, j: 7‘307‘897 k: 4‘000‘000, j: 5‘322‘116 k: 4‘000‘000, j: 4‘366‘084
Pascal Schärli - PVK Informatik II 2021
Parallele Programmierung
j = 7
Thread 1
Thread 2
7
+1
=8
=8
+1
7
1
2
3
4
5
6
j = 8
Pascal Schärli - PVK Informatik II 2021
Parallele Programmierung
k: 4‘000‘000, j: 3‘752‘884 k: 4‘000‘000, j: 10‘806‘936 k: 4‘000‘000, j: 7‘307‘897 k: 4‘000‘000, j: 5‘322‘116 k: 4‘000‘000, j: 4‘366‘084
Pascal Schärli - PVK Informatik II 2021
Parallele Programmierung
k: 4‘000‘000, j: 15‘786‘100 k: 4‘000‘000, j: 18‘432‘690 k: 4‘000‘000, j: 19‘408‘717 k: 4‘000‘000, j: 19‘344‘207 k: 4‘000‘000, j: 20‘000‘000
public class Worker extends Thread {
static int j = 0;
int k = 0;
static Object lock = new Object();
@Override
public void run() {
for (int i = 0; i < 4000000; i++) {
synchronized(lock){
j++;
}
k++;
}
System.out.println("k: " + k + " j:" + j);
}
}
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++){
new Worker().start();
}
}
}
Pascal Schärli - PVK Informatik II 2021
Parallele Programmierung
k: 4‘000‘000, j: 15‘786‘100 k: 4‘000‘000, j: 18‘432‘690 k: 4‘000‘000, j: 19‘344‘207 k: 4‘000‘000, j: 19‘408‘717 k: 4‘000‘000, j: 20‘000‘000 Done!
public static void main(String[] args) {
Worker workers[] = new Worker[5];
for (int i = 0; i < 5; i++) {
workers[i] = new Worker();
workers[i].start();
}
for (int i = 0; i < 5; i++) {
try {
workers[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Done!");
}
Pascal Schärli - PVK Informatik II 2021
Frühling 2018, 0.75 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Frühling 2018, 0.75 Notenpunkte
public class ParIncr extends Thread {
int i; // individuelle Variable
static int j; // gemeinsame Var.
static Object Sperre = new Object();
public void run() {
for (i = 0; i < 400000000; i++) {//400'000'000
synchronized (Sperre) {
j++;
}
}
System.out.println("i: " + i + " j: " + j);
}
public static void main(String[] args) {
ParIncr[] workers = new ParIncr[5];
for (int k = 0; k < 5; k++) {
workers[k] = new ParIncr();
workers[k].start();
}
for (int k = 0; k < 5; k++) {
try {
workers[k].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Frühling 2018, 0.75 Notenpunkte
public class ParIncr extends Thread {
int i; // individuelle Variable
static int j; // gemeinsame Var.
static Object Sperre = new Object();
public void run() {
for (i = 0; i < 400000000; i++) {//400'000'000
synchronized (Sperre) {
j++;
}
}
System.out.println("i: " + i + " j: " + j);
}
public static void main(String[] args) {
ParIncr[] workers = new ParIncr[5];
for (int k = 0; k < 5; k++) {
workers[k] = new ParIncr();
workers[k].start();
}
for (int k = 0; k < 5; k++) {
try {
workers[k].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Programmverifikation
Schleifen Körper
Schleife
Definition:
Schleifeninvarianten sind ein Weg um die Korrektheit einer Funktion zu beweisen.
static int f(int i, int j) {
assert(i >= 0 && j >= 0);
int u = i;
int z = 0;
// [Vor Schleife]
while (u > 0){
// [Vor Schleifenkörper]
z = z + j;
u = u - 1;
// [Nach Schleifenkörper]
}
// [Nach Schleife]
return z;
}
Pascal Schärli - PVK Informatik II 2021
1. Beweise, dass dies eine korrekte Scheifeninvariante ist:
\(z + u * j − i * j = 0 \text{ und }u \geq 0\)
static int f(int i, int j) {
assert(i >= 0 && j >= 0);
int u = i;
int z = 0;
// [Vor Schleife]
while (u > 0){
// [Vor Schleifenkörper]
z = z + j;
u = u - 1;
// [Nach Schleifenkörper]
}
// [Nach Schleife]
return z;
}
\(z_n + u_n * j - i * j = 0\)
(Schleifeninvariante)
(Sonst wären wir nicht in den while-loop gekommen)
\(\Rightarrow\) Schleifeninvariante gilt auch nach Schleifenkörper
Programmverifikation
Pascal Schärli - PVK Informatik II 2021
2. Zeige, dass die Schleifeninvariante vor der Schleife gilt
\(z + u * j − i * j = 0 \text{ und }u \geq 0\)
static int f(int i, int j) {
assert(i >= 0 && j >= 0);
int u = i;
int z = 0;
// [Vor Schleife]
while (u > 0){
// [Vor Schleifenkörper]
z = z + j;
u = u - 1;
// [Nach Schleifenkörper]
}
// [Nach Schleife]
return z;
}
\(z + u * j − i * j = 0\)
\(0 + i * j − i * j = 0\)
\(0 = 0\) ✓
u >= 0
i >= 0 ✓
1.
2.
Programmverifikation
Pascal Schärli - PVK Informatik II 2021
static int f(int i, int j) {
assert(i >= 0 && j >= 0);
int u = i;
int z = 0;
// [Vor Schleife]
while (u > 0){
// [Vor Schleifenkörper]
z = z + j;
u = u - 1;
// [Nach Schleifenkörper]
}
// [Nach Schleife]
return z;
}
\(z + u * j − i * j = 0\)
\(u \geq 0\)
\(u \leq 0\)
Daraus folgt:
\(0 \leq u \leq 0 \Rightarrow u = 0\)
\(z + 0*j - i*j = 0\)
\(\Rightarrow z = i * j\)
(Schleifeninvariante)
(Sonst wären wir nicht aus dem while-loop gekommen)
Programmverifikation
\(z + u * j − i * j = 0 \text{ und }u \geq 0\)
3. Daraus folgt, dass sie auch nach der Schleife gilt.
Pascal Schärli - PVK Informatik II 2021
Programmverifikation
Pascal Schärli - PVK Informatik II 2021
Wie findet man eine Schleifeninvariante?
Herbst 2017, 0.58 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 0.58 Notenpunkte
public static int mult(int i, int j) {
int a = i;
int b = j;
int z = 0;
while(b > 0) {
if(b%2 != 0) {
z = z + a;
b = b - 1;
}
b = b / 2;
a = 2 * a;
}
return z;
}
public static int power(int i, int j) {
int a = i;
int b = j;
int z = 1;
while(b > 0) {
if(b%2 != 0) {
z = z * a;
b = b - 1;
}
b = b / 2;
a = a * a;
}
return z;
}
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 0.58 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 0.58 Notenpunkte
Schleifeninvariante \(z \cdot a^b = i^j\)
public static int power(int i, int j) {
int a = i;
int b = j;
int z = 1;
while(b > 0) {
if(b%2 != 0) {
z = z * a;
b = b - 1;
}
b = b / 2;
a = a * a;
}
return z;
}
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 0.58 Notenpunkte
Schleifeninvariante \(z \cdot a^b = i^j\)
public static int power(int i, int j) {
int a = i;
int b = j;
int z = 1;
while(b > 0) {
if(b%2 != 0) {
z = z * a;
b = b - 1;
}
b = b / 2;
a = a * a;
}
return z;
}
\(\Rightarrow\) somit haben wir in beiden Fällen gezeigt, dass falls die Schleifeninvariante vor dem Schleifenkörper gilt, sie auch nach dem Schleifenkörper gelten muss.
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 0.58 Notenpunkte
public static int power(int i, int j) {
int a = i;
int b = j;
int z = 1;
while(b > 0) {
if(b%2 != 0) {
z = z * a;
b = b - 1;
}
b = b / 2;
a = a * a;
}
return z;
}
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 0.58 Notenpunkte
public static int power(int i, int j) {
int a = i;
int b = j;
int z = 1;
while(b > 0) {
if(b%2 != 0) {
z = z * a;
b = b - 1;
}
b = b / 2;
a = a * a;
}
return z;
}
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Spieltheorie
Grün
gewinnt
Grün
gewinnt
Grün
gewinnt
Blau
gewinnt
Blau
gewinnt
Spielregeln:
Pascal Schärli - PVK Informatik II 2021
Spielregeln:
Blau
gewinnt
Blau
gewinnt
Grün
gewinnt
Grün
gewinnt
Grün
gewinnt
Blau
gewinnt
Blau
gewinnt
Grün
gewinnt
Grün
gewinnt
Grün
gewinnt
Grün
gewinnt
Grün
gewinnt
\(\Rightarrow\) Grün gewinnt!
Pascal Schärli - PVK Informatik II 2021
Spieltheorie
Spieltheorie
0
0
0
1
1
AND
AND
AND
OR
OR
OR
OR
1
1
0
0
0
0
0
\(\Rightarrow\) Blau verliert!
Pascal Schärli - PVK Informatik II 2021
Spieltheorie
Pascal Schärli - PVK Informatik II 2021
6
6
2
9
8
9
5
2
7
9
5
3
4
6
7
3
7
Max
Max
Min
Min
Spieltheorie
Pascal Schärli - PVK Informatik II 2021
6
9
\(\geq\)9
\(\leq\)6
Max
Min
Min
\([\ldots]\)
Da Min den Wert aller Kinder minimiert, muss dieser Knoten \(\leq 6\) sein
Da Max den Wert aller Kinder maximiert, muss dieser Knoten \(\geq 9\) sein
Da Min sowieso den Weg A wählen wird, müssen wir den Wert der restlichen Knoten nicht mehr auswerten
Weg A
Weg B
Spieltheorie
Pascal Schärli - PVK Informatik II 2021
Spieltheorie
Pascal Schärli - PVK Informatik II 2021
6
9
9
6
Max
Min
Min
\([- \infty, \infty]\)
Am Anfang gibt es noch keine Einschränkungen
\(\alpha = -\infty, \beta = \infty\)
Jetzt wissen wir, dass der Wert \(\leq 6\) ist.
\(\beta = 6\)
\([- \infty, 6]\)
\([- \infty, 6]\)
\([9, 6]\)
Wir wissen, der blaue Knoten hat einen Wert \(\geq 9\), daher gilt
\(\alpha = 9\)
Da \(\alpha \geq \beta\) müssen wir nicht mehr weiterrechnen
Spieltheorie
Pascal Schärli - PVK Informatik II 2021
6
6
2
9
8
9
5
2
7
\(\geq\)8
5
3
4
6
7
\(\leq\)5
7
Max
Max
Min
Min
\([- \infty, \infty]\)
\([- \infty, \infty]\)
\([- \infty, \infty]\)
\([6, \infty]\)
\([-\infty, 6]\)
\([6, \infty]\)
\([6, \infty]\)
\([6, \infty]\)
\([6, \infty]\)
\([6, 5]\)
\([6, \infty]\)
\([6, 7]\)
\([6, 7]\)
\([8, 7]\)
\([6, \infty]\)
Spieltheorie
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Frühling 2016, 0.58 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Frühling 2016, 0.58 Notenpunkte
Min
Min
Max
Max
4
2
1
4
3
5
2
9
1
3
5
3
1
4
4
3
8
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Frühling 2016, 0.58 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
4
\(\leq 2\)
\(\leq 1\)
4
2
4
4
3
8
Min
Min
Max
Max
1
Prüfungsaufgabe
Frühling 2016, 0.58 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Frühling 2016, 0.58 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Simulationstechnik
Pascal Schärli - PVK Informatik II 2021
Simulationstechnik
Pascal Schärli - PVK Informatik II 2021
Simulationstechnik
class Ball{
double posX, posY, velX, velY;
final static double g = 9;
public Ball(double posX, double posY, double velX, double velY) {
this.posX = posX;
this.posY = posY;
this.velX = velX;
this.velY = velY;
}
public void update(double dt) {
velY -= g * dt;
posX += velX * dt;
posY += velY * dt;
}
}
Ball ball = new Ball(0,0,10,20);
double dt = 1;
for(int i = 0; i < 5; i++) {
System.out.println("x:"+ball.posX+" y:"+ball.posY);
ball.update(dt);
}
Zustand
Aktualisierung um \(\Delta t\)
x:0.0 y:0.0
x:10.0 y:11.0
x:20.0 y:13.0
x:30.0 y:6.0
x:40.0 y:-10.0
Pascal Schärli - PVK Informatik II 2021
Simulationstechnik
Pascal Schärli - PVK Informatik II 2021
Simulationstechnik
0.3571428571428572
4.642857042857144
Zustand zu beliebiger Zeit aktualisieren
Zustand
Promille p = new Promille();
p.addDrink(0.5, 0.05); // drink a beer
System.out.println(p.getPromille());
p.addDrink(0.75, 0.4); // drink a bottle of rum
System.out.println(p.getPromille());
class Promille {
private double promille;
private long last_update;
final static double process_speed = 2. / 100000000;
final static double absorb_speed = 100. / 7;
public Promille() {
promille = 0;
last_update = System.currentTimeMillis();
}
public double getPromille() {
long now = System.currentTimeMillis();
double processed = (now - last_update) * process_speed;
promille = Math.max(0, promille - processed);
last_update = now;
return promille;
}
public void addDrink(double amount, double strength) {
promille = getPromille() + amount * strength * absorb_speed;
}
}
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021