Algorithmen und Datenstrukturen

Slides:



Advertisements
Ähnliche Präsentationen
Algorithmen und Datenstrukturen
Advertisements

LS 2 / Informatik Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)
WS 03/041 Algorithmentheorie 01 - Einleitung Prof. Dr. S. Albers Prof. Dr. Th. Ottmann.
Algorithmen und Datenstrukturen
Lineare Suche Divide-and-Conquer-Suche Kombinationssuche
Kapitel 6: Klassifizierung von Sortiertechniken
G.Heyer Algorithmen und Datenstrukturen II 1 Algorithmen und Datenstrukturen II Wintersemester 2000 / 2001 Prof. Dr. Gerhard Heyer Institut für Informatik.
Kapitel 6. Suchverfahren
5. Sortier-Algorithmen Vorbemerkungen:
Synonyme: Stapel, Keller, LIFO-Liste usw.
Sortieren mit Binären Bäumen
Sortierverfahren Richard Göbel.
Sortierverfahren Richard Göbel.
WS 03/04 Algorithmentheorie 03 - Randomisierung Prof. Dr. S. Albers Prof. Dr. Th. Ottmann.
WS Algorithmentheorie 05 - Treaps Prof. Dr. Th. Ottmann.
Dynamische Programmierung (2) Matrixkettenprodukt
Funktionenklassen zur Messung der Komplexität
1 Vorlesung Informatik 2 Algorithmen und Datenstrukturen (02 – Funktionenklassen) Prof. Dr. Th. Ottmann.
Algorithmen und Datenstrukturen
Vorlesung Informatik 2 Algorithmen und Datenstrukturen (04 – Entwurfsverfahren) Prof. Th. Ottmann.
1 Vorlesung Informatik 2 Algorithmen und Datenstrukturen (03 – Verschiedene Algorithmen für dasselbe Problem) Prof. Dr. Th. Ottmann.
Vorlesung Informatik 2 Algorithmen und Datenstrukturen (13 – Offenes Hashing) Prof. Th. Ottmann.
WS Algorithmentheorie 01 – Divide and Conquer (Segmentschnitt) Prof. Dr. Th. Ottmann.
Vorlesung Informatik 2 Algorithmen und Datenstrukturen (10 - Suchverfahren) T. Lauer.
Algorithmen und Datenstrukturen
Algorithmen und Datenstrukturen
Halbzeit: Kurze Wiederholung
Sortieren vorsortierter Daten
Vorlesung Informatik 2 Algorithmen und Datenstrukturen (02 – Funktionenklassen) Tobias Lauer.
1 Vorlesung Informatik 2 Algorithmen und Datenstrukturen (03 – Verschiedene Algorithmen für dasselbe Problem) Prof. Dr. Th. Ottmann.
Vorlesung Informatik 2 Algorithmen und Datenstrukturen (02 – Funktionenklassen) Prof. Dr. Th. Ottmann.
Vorlesung Informatik 2 Algorithmen und Datenstrukturen Halbzeit: Was haben wir bisher gelernt? Prof. Th. Ottmann.
Algorithmen und Datenstrukturen
WS Algorithmentheorie 08 – Dynamische Programmierung (2) Matrixkettenprodukt Prof. Dr. Th. Ottmann.
Vorlesung Informatik 2 Algorithmen und Datenstrukturen (27 – Kürzeste Wege) Prof. Th. Ottmann.
Informatik II, SS 2008 Algorithmen und Datenstrukturen Vorlesung 9 Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik Fakultät.
Informatik II, SS 2008 Algorithmen und Datenstrukturen Vorlesung 4 Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik Fakultät.
Anwendungen von Stapeln und Schlangen
Vorlesung Informatik 2 Algorithmen und Datenstrukturen 09 - Weitere Sortierverfahren Heapsort-Nachtrag Prof. Th. Ottmann.
Informatik II, SS 2008 Algorithmen und Datenstrukturen Vorlesung 2 Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik Fakultät.
Vorlesung Informatik 2 Algorithmen und Datenstrukturen (11-1 –Selbstanordnende lineare Listen) Prof. Th. Ottmann.
Vorlesung Informatik 2 Algorithmen und Datenstrukturen (27-Selbstanordnende lineare Listen) Prof. Th. Ottmann.
1 Vorlesung Informatik 2 Algorithmen und Datenstrukturen (21 – Kürzeste Wege) T. Lauer.
Informatik II, SS 2008 Algorithmen und Datenstrukturen Vorlesung 13 Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik Fakultät.
Algorithmen und Datenstrukturen
WS 06/07 Algorithmentheorie 01 - Einleitung Prof. Dr. Th. Ottmann Tobias Lauer.
Vorlesung Informatik 2 Algorithmen und Datenstrukturen (07 - Skiplisten) Prof. Th. Ottmann.
Informatik II, SS 2008 Algorithmen und Datenstrukturen Vorlesung 12 Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik Fakultät.
1 Vorlesung Informatik 2 Algorithmen und Datenstrukturen (03 – Verschiedene Algorithmen für dasselbe Problem) Prof. Dr. Th. Ottmann.
Vorlesung Informatik 2 Algorithmen und Datenstrukturen (09 - Weitere Sortierverfahren) Prof. Th. Ottmann.
Informatik II, SS 2008 Algorithmen und Datenstrukturen Vorlesung 16 Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik Fakultät.
Algorithmen und Datenstrukturen
Vorlesung Informatik 2 Algorithmen und Datenstrukturen (05 – Elementare Datenstrukturen) Prof. Th. Ottmann.
Informatik II, SS 2008 Algorithmen und Datenstrukturen Vorlesung 6 Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik Fakultät.
Geometrisches Divide and Conquer
Friedhelm Meyer auf der Heide 1 HEINZ NIXDORF INSTITUTE University of Paderborn Algorithms and Complexity Algorithmen und Komplexität Teil 1: Grundlegende.
4 Sortierverfahren 4.1 Einführung 4.2 Naive Sortierverfahren
Sortieralgorithmen Sortieren von Arrays.
Einführung in die Programmierung
Einführung in die Informatik für Naturwissenschaftler und Ingenieure (alias Einführung in die Programmierung) (Vorlesung) Prof. Dr. Günter Rudolph Fachbereich.
Einführung in die Programmierung Wintersemester 2009/10 Prof. Dr. Günter Rudolph Lehrstuhl für Algorithm Engineering Fakultät für Informatik TU Dortmund.
Einführung in die Informatik für Naturwissenschaftler und Ingenieure (alias Einführung in die Programmierung) (Vorlesung) Prof. Dr. Günter Rudolph Fachbereich.
Algorithmen und Datenstrukturen SS 2005 Mag.Th. Hilpold u. Dr. A.Stritzinger Institut für Wirtschaftsinformatik- Software Engineering JKU Linz.
Sortieralgorithmen Greedy Sortieren: Sortieren durch Auswahl, Einfügen und Austauschen Divide-and-Conquer-Sortieren: Quicksort und merge sort Foliensatz.
Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 1 Algorithmen und Datenstrukturen 1 SS 2002 Mag.Thomas.
Institut für Kartographie und Geoinformation Prof. Dr. Lutz Plümer Diskrete Mathematik II Vorlesung Datenstrukturen für den Algorithmus von.
Algorithmen und Datenstrukturen
Algorithmen und Datenstrukturen
Algorithmen und Datenstrukturen
Algorithmen und Datenstrukturen
 Präsentation transkript:

Algorithmen und Datenstrukturen Suchen Informatik II, SS 2008 Algorithmen und Datenstrukturen Vorlesung 11 Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik Fakultät für Angewandte Wissenschaften Albert-Ludwigs-Universität Freiburg

Problemstellung Problem: Gegeben Folge F = (a1,...,an). Aufgabe: Finde das Element mit Schlüssel k. Rahmen: class SearchAlgorithm { static int search(Orderable A[], Orderable k) { return search(A,k); } } „To my mother: Who told me the value of order!“ (D. Knuth) Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

Sequentielle Suche Einfachstes Verfahren: Sequentielle, lineare Suche class SequentialSearch extends SearchAlgorithm { public static int search(Orderable A[],Orderable k){ /* Durchsucht A[1], .., A[n] nach Element k und liefert den Index i mit A[i] = k; -1 sonst */ A[0] = k; // Stopper int i = A.length; do i--; while (!A[i].equal(k)); if (i != 0) // A[i] ist gesuchtes Element return i; else // es gibt kein Element mit Schlüssel k return -1; } } Analyse: - schlechtester Fall : n + 1 - im Mittel : Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

Gegeben: Folge a1, ..., an aufsteigend sortiert: Binäre Suche Gegeben: Folge a1, ..., an aufsteigend sortiert: a1 an Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

Binäre Suche Klasse class BinarySearch extends SearchAlgorithm Hauptroutine public static int search(Orderable A[],Orderable k){ /* Durchsucht A[1], .., A[n] nach Element k und liefert Index i >= 1 mit A[i] = k; 0 sonst */ int n = A.length - 1; return search(A, 1, n, k); } Rekursiver Aufruf public static int search(Orderable A[], int l, int r, Orderable k){ /* Durchsucht A[l], .., A[r] nach Element k und liefert Index l <= i <= r mit A[i] = k; l-1 sonst */ if (l > r) // Suche erfolglos return l-1; int m = (l + r) / 2; if (k.less(A[m])) return search(A, l, m - 1, k); if (k.greater(A[m])) return search(A, m + 1, r, k); else // A[m] = k return m; } Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

Binäre Suche ohne Rekursion public static int search(Orderable A[], Orderable k){ int n = A.length-1, l = 1, r = n; while (l <= r) { int m = (l + r) / 2; if (k.less(A[m])) { r = m - 1; } else if (k.greater(A[m])) { l = m + 1; } else return m; } return l-1; } Situation: Binärer Vergleichsoperator mit 3 Ausgängen; Berechnung der mittleren Position Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

Binäre Suche ohne Rekursion Situation: Binärer Vergleichsoperator mit 3 Ausgängen; Berechnung der mittleren Position Worst case (Annahme: n=2k-1): Suche benötigt k = log(n + 1) Vergleiche Average Case (Annahme: n=2k-1) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

Analyse Auswertung: Erwartungswert: Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

Fibonacci-Suche Erinnerung: F0 =0; F1=1; Fn=Fn-1 + Fn-2 für (n ≥ 2) Verfahren: Vergleiche den Schlüssel an Position i = Fn-2 mit k. - A[i].key > k : Durchsuche linke Fn-2 - 1 Elemente - A[i].key < k : Durchsuche rechte Fn-1 -1 Elemente 1 i N Fn-2 - 1 Fn-1 - 1 Fn - 1 Analyse: Durchsuchen von Fn-1Elementen mit max. n Schlüsselvergleichen. Nun ist Ergo: Cmax(N) = O(log1.618(N+1)) = O(log2N) Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

Implementation public static int search(Orderable A[],Orderable k){ // Durchsucht A[1], .., A[n] nach Element k und liefert den Index i mit A[i] = k; -1 sonst int n = A.length-1; int fibMinus2 = 1, fibMinus1 = 1, fib = 2; while (fib - 1 < n) { fibMinus2 = fibMinus1; fibMinus1 = fib; fib = fibMinus1 + fibMinus2; } int offset = 0; while (fib > 1) { /* Durchsuche den Bereich [offset+1,offset+fib-1] nach Schluessel(Falls fib = 2, dann besteht [offset+1,offset+fib-1] aus einem Element!) */ int i = min(offset + fibMinus2,n); if (k.less(A[i])) { // Durchsuche [offset+1,offset+fibMinus2-1] fib = fibMinus2; fibMinus1 = fibMinus1 - fibMinus2; fibMinus2 = fib - fibMinus1; } else if (k.greater(A[i])) { // Durchsuche [offset+fibMinus2+1,offset+fib-1] offset = i; fib = fibMinus1; fibMinus1 = fibMinus2; fibMinus2 = fib - fibMinus1; } else // A[i] = k return i; } return -1; } } Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

Exponentielle Suche Situation: n sehr groß, i mit ai = k klein Ziel: Finde beliebigen Schlüssel mit einer Anzahl von Vergleichsoperationen, die logarithmisch in der Position i des gesuchten Schlüssels ist. Eingabe: Sortierte Folge a1,...,an , Schlüssel k Ausgabe: Index i mit ai = k j = 1; while (k > a[j]) j = 2*j; return search (a,j/2,j,k); Analyse: - aj ≥ k: [log i] - Binäre Suche Gesamtaufwand: O(log i) Index i von aj 1 2 4 8 i 16 Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

Interpolationssuche Idee: Suche von Namen im Telefonbuch, z.B. Bayer und Zimmermann Erwartete Position von k (bei Gleichverteilung aller gespeicherten Schlüssel): Analyse: - im schlechtesten Fall: O(n) - im Mittel bei Gleichverteilung: O(log log n) al ar a ? l r Suchschlüssel k Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

Das Auswahlproblem Problem: Finde das i-kleinste Element in einer (unsortierten) Liste F mit n Elementen 1. Naive Lösung j = 0 while (j < i) bestimme kleinstes Element a_{min} entferne a_{min} aus F; j = j + 1; return a_{min} Anzahl der Schritte: O(i *n) für i = n/2 (Median): O(n²) (Sortieren ist besser) 2. Verfahren mit Heap verwandle F in einen min-Heap j = 0; while (j < i) a_{min} = delete-min(F); j = j + 1; return a_{min} Anzahl der Schritte: O(n+i*log n) für i = n/2 (Median): O(n log n) Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

Divide-and-Conquer-Lösung Idee: Aufteilung von F = a1,...,an in zwei Gruppen bzgl. Pivotelement p (Quicksort) public static int selectIndex (Orderable A[],int i,int l,int r) { // Suche den Index des i-groessten Elementes // in A[l],...,A[r] if (r > l) { int p = pivotElement(A, l, r); int m = Quicksort.divide(A, l, r); if (i <= m - l) return selectIndex (A, i, l, m - 1); return selectIndex (A, i - (m - l), m, r); } else return l; } Nur eine der zwei durch Aufteilung entstandenen Folgen wird weiter betrachtet. Lin p L< p L> Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

divide-Methode von Quicksort static int divide (Orderable A [], int l, int r) { // teilt das Array zwischen l und r mit Hilfe // des Pivot-Elements in zwei Teile auf und gibt // die Position des Pivot-Elementes zurueck int i = l-1; // linker Zeiger auf Array int j = r; // rechter Zeiger auf Array Orderable pivot = A [r]; // das Pivot-Element while (true){ // "Endlos"-Schleife do i++; while (i < j && A[i].less(pivot)); do j--; while (i < j && A[j].greater(pivot)); if (i >= j) { swap (A, i, r); return i; // Abbruch der Schleife } swap (A, i, j); Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann

Wahl des Pivotelements 1. p = ar folgt T(n) ≤ T(n-1) + O(n) Laufzeit im schlimmsten Fall: O(n²) Beispiel: Auswahl des Minimums in aufsteigend sortierter Folge 2. Randomisiert p = ein zufälliges Element aus a1,...,an 3. Median-of-Median Bestimme Pivotelement als Median von Medianen von 5-er Gruppen lineare Komplexität Informatik II: Algorithmen und Datenstrukturen, SS 2008 Prof. Dr. Thomas Ottmann