19
Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

Embed Size (px)

Citation preview

Page 1: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

1

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Algorithmen und Datenstrukturen

SS 2007

Helmut Schauer

Page 2: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

2

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Inhalt

Listen, Stacks und Queues

Suchen und Sortieren

Bäume

Graphen

Geometrische Algorithmen

Textverarbeitung

Page 3: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

3

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Literatur

Sedgewick, R.: Algorithms in Java (1-4) Algorithms in Java (5): Graph Algorithms Addison-Wesley, 2003

Page 4: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

4

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Literatur

Baase, S.: Computer Algorithms Addison-Wesley, 1988

Page 5: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

5

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Literatur

Ottmann, T.; Widmayer, P.: Algorithmen und Datenstrukturen

Spektrum Akad. Verlag, 1996

Page 6: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

6

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Literatur

Saake, G.; Sattler, K.: Algorithmen und DatenstrukturenEine Einführung mit Java

dpunkt.lehrbuch, 2001

Page 7: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

7

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Literatur

Weiss, M.:Data Structures andAlgorithm Analysis in Java

Pearson , 2007

Page 8: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

8

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Literatur

Drake, P.: Data Structures and Algorithms in Java

Pearson, 2006

Page 9: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

9

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Literatur

Solymosi, A.; Grude, U.: GrundkursAlgorithmen und Datenstrukturenin Java

Vieweg, 2000

Page 10: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

10

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Insertion Sort (1)

for (i=1;i<N,i++) {{All j:1≤j<i:a[j-1]≤a[j] ordered(a[0..i-1])} k=i; x=a[i];while ((k>0)&&(x<a[k-1]))

a[k--]=a[k];a[k]=x;{All j:1≤j≤i:a[j-1]≤a[j] ordered(a[0..i])}

}

{All j:1≤j<N:a[j-1]≤a[j] ordered(a[0..N-1])}

Page 11: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

11

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Insertion Sort (2)

Vergleiche:best case (sortiert): N worst case (verkehrt sortiert): N2/2 average case: N2/4

Bewegungen:best case (sortiert): 0 worst case (verkehrt sortiert): N2/2 average case: N2/4

Page 12: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

12

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Selection Sort (1)

for (i=0;i<N,i++) {{All j:1≤j<i:a[j-1]≤a[j] ordered(a[0..i-1])} min=i;for (k=i+1;k<N,k++)

if (a[k]<a[min]) min=k;swap(i,min);{All j:1≤j≤i:a[j-1]≤a[j] ordered(a[0..i])}

}

{All j:1≤j<N:a[j-1]≤a[j] ordered(a[0..N-1])}

Page 13: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

13

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Selection Sort (2)

N2/2 Vergleiche

N Vertauschungen

Page 14: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

14

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

Bubble Sort

N2/2 VergleicheN2/2 Vertauschungen

Page 15: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

15

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

QuickSort “divide et impera”

Worst Case: A(N) = N(N-1)/2 O(N2)Best Case: A(N) = N ld N O(N log N)Average Case: A(N) = 2N ln N O(N log N)

Page 16: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

16

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

MergeSort

A(N) = N ld N O(N log N)

Page 17: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

17

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

HeapSort (1)

p

q1 q2

Heap-Bedingung für Maximum Heap:All p,q: p ist predecessor von q: p≥q

Heap-Bedingung für Minimum Heap:All p,q: p ist predecessor von q: p≤q

Page 18: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

18

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

HeapSort (2)p

q1 q2

Speicherung als Array a[0..N-1]:p ... a[i]q1 ... a[2i+1]q2 ... a[2i+2]

Heap-Bedingung für Maximum Heap:heap(N) All i: 1≤i<N: a[(i-1)/2] ≥ a[i]

Page 19: 1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer

19

Helmut SchauerEducational Engineering LabDepartment for InformaticsUniversity of Zurich

HeapSort (3)

heap(N) herstellen O(N log N)for (i=N-1; i>0, i--) {

swap(0,i); O(1)heap(i) wiederherstellen; O(log N)

}

A(N) = 2N ld N O(N log N)