Data Structures and Algorithm Design

Ähnliche Dokumente
Data Structures and Algorithm Design

Data Structures and Algorithm Design

Data Structures and Algorithm Design

Data Structures and Algorithm Design

Bayesian Networks. Syntax Semantics Parametrized Distributions Inference in Bayesian Networks. Exact Inference. Approximate Inference

Ressourcenmanagement in Netzwerken SS06 Vorl. 12,

Seeking for n! Derivatives

Ein Stern in dunkler Nacht Die schoensten Weihnachtsgeschichten. Click here if your download doesn"t start automatically

Computational Models

Martin Luther. Click here if your download doesn"t start automatically

Tube Analyzer LogViewer 2.3

Geometrie und Bedeutung: Kap 5

Magic Figures. We note that in the example magic square the numbers 1 9 are used. All three rows (columns) have equal sum, called the magic number.

Finite Difference Method (FDM)

Level 2 German, 2015

Introduction FEM, 1D-Example

Rev. Proc Information

Introduction FEM, 1D-Example

Einführung in die Computerlinguistik

Algorithms & Datastructures Midterm Test 1

Number of Maximal Partial Clones

Routing in WSN Exercise

Algorithm Theory. 16 Fibonacci Heaps. Christian Schindelhauer

VGM. VGM information. HAMBURG SÜD VGM WEB PORTAL - USER GUIDE June 2016

Einführung in die Finite Element Methode Projekt 2

Algorithm Theory 3 Fast Fourier Transformation Christian Schindelhauer

v+s Output Quelle: Schotter, Microeconomics, , S. 412f

Datenstrukturen. Ziele

Wie man heute die Liebe fürs Leben findet

FEM Isoparametric Concept

Die einfachste Diät der Welt: Das Plus-Minus- Prinzip (GU Reihe Einzeltitel)

Priority search queues: Loser trees

DYNAMISCHE GEOMETRIE

Aus FanLiebe zu Tokio Hotel: von Fans fã¼r Fans und ihre Band

Wer bin ich - und wenn ja wie viele?: Eine philosophische Reise. Click here if your download doesn"t start automatically

Algorithmen und Komplexität Teil 1: Grundlegende Algorithmen

Algorithmen und Datenstrukturen Musterlösung 5

Mixed tenses revision: German

Unit 4. The Extension Principle. Fuzzy Logic I 123

Englisch-Grundwortschatz

VGM. VGM information. HAMBURG SÜD VGM WEB PORTAL USER GUIDE June 2016

SAMPLE EXAMINATION BOOKLET

Algorithms I. Markus Lohrey. Wintersemester 2017/2018. Universität Siegen. Markus Lohrey (Universität Siegen) Algorithms WS 2016/ / 168

Bayesian Networks. Syntax Semantics Parametrized Distributions Inference in Bayesian Networks. Exact Inference. Approximate Inference

Repetitive Strukturen

GERMAN LANGUAGE Tania Hinderberger-Burton, Ph.D American University

Unit 1. Motivation and Basics of Classical Logic. Fuzzy Logic I 6

Zu + Infinitiv Constructions

Handbuch der therapeutischen Seelsorge: Die Seelsorge-Praxis / Gesprächsführung in der Seelsorge (German Edition)

Analyse und Interpretation der Kurzgeschichte "Die Tochter" von Peter Bichsel mit Unterrichtsentwurf für eine 10. Klassenstufe (German Edition)

BISON Instantiating the Whitened Swap-Or-Not Construction September 6th, 2018

Willkommen zur Vorlesung Komplexitätstheorie

Information Flow. Basics. Overview. Bell-LaPadula Model embodies information flow policy. Variables x, y assigned compartments x, y as well as values

Sinn und Aufgabe eines Wissenschaftlers: Textvergleich zweier klassischer Autoren (German Edition)

Unterspezifikation in der Semantik Hole Semantics

PONS DIE DREI??? FRAGEZEICHEN, ARCTIC ADVENTURE: ENGLISCH LERNEN MIT JUSTUS, PETER UND BOB

How-To-Do. Communication to Siemens OPC Server via Ethernet

Prof. S. Krauter Kombinatorik. WS Blatt03.doc

Taxation in Austria - Keypoints. CONFIDA Klagenfurt Steuerberatungsgesellschaft m.b.h

Application Note. Import Jinx! Scenes into the DMX-Configurator

FEBE Die Frontend-Backend-Lösung für Excel

Killy Literaturlexikon: Autoren Und Werke Des Deutschsprachigen Kulturraumes 2., Vollstandig Uberarbeitete Auflage (German Edition)

Algorithms for graph visualization

Ewald s Sphere/Problem 3.7

Im Fluss der Zeit: Gedanken beim Älterwerden (HERDER spektrum) (German Edition)

Exercise (Part XI) Anastasia Mochalova, Lehrstuhl für ABWL und Wirtschaftsinformatik, Kath. Universität Eichstätt-Ingolstadt 1

Automatentheorie und formale Sprachen reguläre Ausdrücke

Grade 12: Qualifikationsphase. My Abitur

Level 2 German, 2016

Konkret - der Ratgeber: Die besten Tipps zu Internet, Handy und Co. (German Edition)

19. STL Container Programmieren / Algorithmen und Datenstrukturen 2

D-BAUG Informatik I. Exercise session: week 1 HS 2018

Mock Exam Behavioral Finance

Funktion der Mindestreserve im Bezug auf die Schlüsselzinssätze der EZB (German Edition)

Introduction to Python. Introduction. First Steps in Python. pseudo random numbers. May 2016

VORANSICHT. Halloween zählt zu den beliebtesten. A spooky and special holiday Eine Lerntheke zu Halloween auf zwei Niveaus (Klassen 8/9)

Übungsblatt 6. Analysis 1, HS14

Informatik für Mathematiker und Physiker Woche 7. David Sommer

Was heißt Denken?: Vorlesung Wintersemester 1951/52. [Was bedeutet das alles?] (Reclams Universal-Bibliothek) (German Edition)

Exam Algorithm Theory. Do not open or turn until told so by the supervisor!

Quadt Kunststoffapparatebau GmbH

Weather forecast in Accra

Scheduling. chemistry. math. history. physics. art

Informatik - Übungsstunde

Produktdifferenzierung und Markteintritte?

PONS DIE DREI??? FRAGEZEICHEN, ARCTIC ADVENTURE: ENGLISCH LERNEN MIT JUSTUS, PETER UND BOB

Statistics, Data Analysis, and Simulation SS 2015

!! Um!in!ADITION!ein!HTML51Werbemittel!anzulegen,!erstellen!Sie!zunächst!ein!neues! Werbemittel!des!Typs!RichMedia.!!!!!!

Fachübersetzen - Ein Lehrbuch für Theorie und Praxis

KTdCW Artificial Intelligence 2016/17 Practical Exercises - PART A

Logik für Informatiker Logic for computer scientists

FEM Isoparametric Concept

Big Data Analytics. Fifth Munich Data Protection Day, March 23, Dr. Stefan Krätschmer, Data Privacy Officer, Europe, IBM

Algorithmische Bioinformatik II WS2004/05 Ralf Zimmer Part III Probabilistic Modeling IV Bayesian Modeling: Algorithms, EM and MC Methods HMMs

Algebra. 1. Geben Sie alle abelschen Gruppen mit 8 und 12 Elementen an. (Ohne Nachweis).

Cycling and (or?) Trams

Informatik II, SS 2018

How-To-Do. Hardware Configuration of the CC03 via SIMATIC Manager from Siemens

Die Bedeutung neurowissenschaftlicher Erkenntnisse für die Werbung (German Edition)

Transkript:

- University of Applied Sciences - Data Structures and Algorithm Design - CSCI 340 - Friedhelm Seutter Institut für Angewandte Informatik Contents 1. Analyzing Algorithms and Problems 2. Data Abstraction 3. Recursion and Induction 4. Sorting 6. Dynamic Sets and Searching 7. Graphs and Graph Traversals 8. Optimization and Greedy Algorithms 10. Dynamic Programming 13. NP-Complete Problems Institut für Angewandte Informatik 2 1

4. Sorting Insertion Sort Quicksort Mergesort Heapsort Institut für Angewandte Informatik 3 Sorting Problem Let A = (a 1, a 2,..., a n ) be an array of (nonnegative) integers, called keys. The problem is to find a permutation π such that the integers are sorted in nondecreasing order. Solution: A = (a π(1), a π(2),..., a π(n) ) with a π(1) a π(2)... a π(n) Institut für Angewandte Informatik 4 2

Analysis of Complexity General strategy: Sorting by comparison of keys Time: Number of key comparisons (basic operations) Space: Amount of extra space (in addition to the input) Institut für Angewandte Informatik 5 Insertion Sort Let some elements at the left side of the array be sorted. Take the first element from the unexamined elements and insert it at the right position of the sorted elements. To get a vacant space the greater elements must be shifted one position to the right. Institut für Angewandte Informatik 6 3

Insertion Sort - Example < sorted > < not examined > 2 4 12 14 15 7 19 11 2 4 12 14 15 19 11 7 2 4 7 12 14 15 19 11 < sorted > <not exam.> Institut für Angewandte Informatik 7 Insertion Sort Institut für Angewandte Informatik 8 4

Worst-case Complexity Basic operation: Key comparison in line 4 W(n) = (2 j n) (j 1) = (1 j n-1) j = ½ n (n 1) Θ(n 2 ) Institut für Angewandte Informatik 9 Average-case Complexity Basic operation: Key comparison in line 4 Assumptions: All permutations are equally likely as input and the keys are distinct. A(n) ¼n 2 Θ(n 2 ) Institut für Angewandte Informatik 10 5

Best-case Complexity Basic operation: Key comparison in line 4 B(n) = n 1 Θ(n) Institut für Angewandte Informatik 11 Space Complexity Insertion Sort sorts in-place. The additional amount of space is independent of the number of elements to sort. Institut für Angewandte Informatik 12 6

Divide and Conquer Institut für Angewandte Informatik 13 Quicksort Divide: Choose one element to be the pivot. Divide the array in two subarrays corresponding to the pivot. Less or equal elements to the left, greater elements to the right, the pivot in between. Conquer: An array of length 1 is sorted. Combine: Append two sorted subarrays with the pivot in between. Institut für Angewandte Informatik 14 7

Quicksort Institut für Angewandte Informatik 15 Quicksort-Partition Institut für Angewandte Informatik 16 8

Quicksort-Partition f l unexamined pivot f i j l pivot > pivot unexamined pivot f q l pivot pivot > pivot Institut für Angewandte Informatik 17 Quicksort-Partition Institut für Angewandte Informatik 18 9

Divide Institut für Angewandte Informatik 19 Combine Institut für Angewandte Informatik 20 10

Complexity Basic operation: Key comparison in line 4 of Partition W(n) Θ(n 2 ) A(n) Θ(n log n) B(n) Θ(n log n) Institut für Angewandte Informatik 21 Space Complexity Amount of space needed: Θ(n) The exchange of keys is in-place, but there are in the worst case n recursive procedure calls and they need that space for storing their local variables. A tricky implementation may reduce the space complexity to Θ(log n). Institut für Angewandte Informatik 22 11

Mergesort Divide: Divide the array in two halves, recursively. Conquer: An array of length 1 is sorted. Combine: Two sorted subarrays are merged to a sorted array. Institut für Angewandte Informatik 23 Mergesort Institut für Angewandte Informatik 24 12

Mergesort-Merge Institut für Angewandte Informatik 25 Divide Institut für Angewandte Informatik 26 13

Combine Institut für Angewandte Informatik 27 Complexity Basic operation: Key comparison in line 6 of Merge W(n) Θ(n log n) A(n) Θ(n log n) B(n) Θ(n log n) Institut für Angewandte Informatik 28 14

Space Complexity Amount of space needed: Θ(n) There is no exchange of keys, but all n keys are copied and merged to an extra array. A tricky implementation may reduce the space needed to n/2, but this still is in Θ(n). Institut für Angewandte Informatik 29 Lower Bounds for Sorting by Comparison of Keys What is the minimum number of key comparisons for sorting algorithms based on comparisons of keys? Given an array of n distinct keys. The solution of sorting the keys is a permutation of the keys. Thus there are n! possible solutions. Institut für Angewandte Informatik 30 15

Decision Tree for Sorting All possible sorting solutions may be represented in a decision tree. The inner nodes represent a comparison of two keys. The possible outcomes are true or false. If false, the keys have to be exchanged. Inner nodes have two successors. The leaves are the possible sorting solutions. Institut für Angewandte Informatik 31 Decision Tree for Sorting Institut für Angewandte Informatik 32 16

Decision Tree for Sorting Sorting by comparison corresponds to a path in the decision tree from the root to a leaf. The length of the longest path corresponds to the number of comparisons in the worst case. Therefore the lower bound of the height of a decision tree is a worst case lower bound for the number of key comparisons. Institut für Angewandte Informatik 33 Lower Bounds for Sorting Institut für Angewandte Informatik 34 17

Lower Bounds for Sorting Institut für Angewandte Informatik 35 Heapsort The algorithm uses a data structure called heap, which is a binary tree and some special properties. Heap-structure: Complete binary tree with some of the rightmost leaves removed. Partial tree order property: The key at any node is greater (less) than or equal to the keys at each of its children. Institut für Angewandte Informatik 36 18

Heap Institut für Angewandte Informatik 37 Heap Institut für Angewandte Informatik 38 19

Heap Implementation As a linked structure with each node containing pointers (references) to the roots of its subtrees. As an array: The root is in A[1] Let i be the index of a node, except the root, then the index of the parent is i/2. Let i be the index of a node, except a leaf, then 2i is the index of the left child and 2i+1 is the index of the right child. Institut für Angewandte Informatik 39 Heap: Array-Implementation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 14 13 8 7 11 12 2 4 1 heapsize[a] length[a] Institut für Angewandte Informatik 40 20

Heapsort Strategy The root contains the largest key in the heap. Build a sorted sequence in reverse order by repeatedly removing the root element from the heap. After each removing step the heap properties have to be reestablished by bringing the next largest key to the root. Institut für Angewandte Informatik 41 Fixing a Heap A node violates the partial order tree property, i. e. its key is less than at least one of the keys of its children. This node must be exchanged with the child, which has the largest key, recursively. Institut für Angewandte Informatik 42 21

Fixing a Heap Institut für Angewandte Informatik 43 Fixing a Heap Institut für Angewandte Informatik 44 22

Fixing a Heap Institut für Angewandte Informatik 45 Fixing a Heap Institut für Angewandte Informatik 46 23

Complexity of FixHeap Basic operation: Key comparisons in lines 3 and 6 W(n) = 2h = 2 lg n Θ( log n) (h height of the heap, n number of nodes) Institut für Angewandte Informatik 47 Constructing a Heap Given an unordered array of keys. The corresponding binary tree has heap structure, but the partial order tree property is violated. The leaves A[ n/2+1 ],..., A[n] are heaps. The subtrees with roots from A[ n/2 ] down to A[1] must establish their partial order tree property. Institut für Angewandte Informatik 48 24

Constructing a Heap A = (4, 1, 12, 2, 16, 11, 13, 14, 8, 7) Institut für Angewandte Informatik 49 Constructing a Heap A = (16, 14, 13, 8, 7, 11, 12, 2, 4, 1) Institut für Angewandte Informatik 50 25

Constructing a Heap Institut für Angewandte Informatik 51 Complexity of ConstructHeap Basic operation: Call of FixHeap in line 3 W(n) n lg n Θ(n log n) But this upper bound is poor! Institut für Angewandte Informatik 52 26

Heights of subtrees for FixHeap Institut für Angewandte Informatik 53 Complexity of ConstructHeap Basic operation: Call of FixHeap in line 3 W(n) (0 k h) 2 k (h k) 2n -lgn + 2 Θ(n) Institut für Angewandte Informatik 54 27

Heapsort Institut für Angewandte Informatik 55 Heapsort Institut für Angewandte Informatik 56 28

Heapsort Institut für Angewandte Informatik 57 Heapsort Institut für Angewandte Informatik 58 29

Heapsort Given: A = (4, 1, 12, 2, 16, 11, 13, 14, 8, 7) ConstructHeap: A = (16, 14, 13, 8, 7, 11, 12, 2, 4, 1) HeapSort: A = (1, 2, 4, 7, 8, 11, 12, 13, 14, 16) Institut für Angewandte Informatik 59 Complexity of Heapsort Add up the complexities of ConstructHeap and FixHeap in the loop: W(n) = Θ(n) + (n-1) Θ(lg n) Θ(n lg n) Institut für Angewandte Informatik 60 30

Space Complexity Heapsort sorts in-place. The space needed for recursion is limited to a depth of about lg n. But these procedures can be recoded in iterative procedures. Institut für Angewandte Informatik 61 Comparison of Sorting Algorithms Algorithm Worst case Average Extra space Insertion Sort n 2 /2 Θ(n 2 ) Θ(1) Quicksort n 2 /2 Θ(n log n) Θ(log n) Mergesort n lg n Θ(n log n) Θ(n) Heapsort 2n lg n Θ(n log n) Θ(1) Institut für Angewandte Informatik 62 31