Introduction to Programming. Lecture 13: Container data structures. Container data structures. Topics for this lecture. A basic issue with containers

Similar documents
CMPS 10 Introduction to Computer Science Lecture Notes

Sequential search. Building Java Programs Chapter 13. Sequential search. Sequential search

AADL : about scheduling analysis

High level vs Low Level. What is a Computer Program? What does gcc do for you? Program = Instructions + Data. Basic Computer Organization

Design and Analysis of Algorithms

Esc101 Lecture 1 st April, 2008 Generating Permutation

Assembler. Building a Modern Computer From First Principles.

Course Introduction. Algorithm 8/31/2017. COSC 320 Advanced Data Structures and Algorithms. COSC 320 Advanced Data Structures and Algorithms

News. Recap: While Loop Example. Reading. Recap: Do Loop Example. Recap: For Loop Example

An Optimal Algorithm for Prufer Codes *

The Greedy Method. Outline and Reading. Change Money Problem. Greedy Algorithms. Applications of the Greedy Strategy. The Greedy Method Technique

CSCI 104 Sorting Algorithms. Mark Redekopp David Kempe

Sorting Review. Sorting. Comparison Sorting. CSE 680 Prof. Roger Crawfis. Assumptions

Assembler. Shimon Schocken. Spring Elements of Computing Systems 1 Assembler (Ch. 6) Compiler. abstract interface.

CSE 326: Data Structures Quicksort Comparison Sorting Bound

Programming in Fortran 90 : 2017/2018

Problem Set 3 Solutions

Conditional Speculative Decimal Addition*

DLK Pro the all-rounder for mobile data downloading. Tailor-made for various requirements.

Virtual Memory. Background. No. 10. Virtual Memory: concept. Logical Memory Space (review) Demand Paging(1) Virtual Memory

Programming Assignment Six. Semester Calendar. 1D Excel Worksheet Arrays. Review VBA Arrays from Excel. Programming Assignment Six May 2, 2017

Mathematics 256 a course in differential equations for engineering students

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar

Priority queues and heaps Professors Clark F. Olson and Carol Zander

CSE 326: Data Structures Quicksort Comparison Sorting Bound

CE 221 Data Structures and Algorithms

Lecture 5: Multilayer Perceptrons

Notes on Organizing Java Code: Packages, Visibility, and Scope

Harvard University CS 101 Fall 2005, Shimon Schocken. Assembler. Elements of Computing Systems 1 Assembler (Ch. 6)

CS240: Programming in C. Lecture 12: Polymorphic Sorting

Midterms Save the Dates!

CS1100 Introduction to Programming

Compiler Design. Spring Register Allocation. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Loop Transformations, Dependences, and Parallelization

Announcements. Supervised Learning

Outline. CIS 110: Introduction to Computer Programming. Review: Interactive Sum. More Cumulative Algorithms. Interactive Sum Trace (2)

CS221: Algorithms and Data Structures. Priority Queues and Heaps. Alan J. Hu (Borrowing slides from Steve Wolfman)

Concurrent Apriori Data Mining Algorithms

3D vector computer graphics

Assignment # 2. Farrukh Jabeen Algorithms 510 Assignment #2 Due Date: June 15, 2009.

Today s Outline. Sorting: The Big Picture. Why Sort? Selection Sort: Idea. Insertion Sort: Idea. Sorting Chapter 7 in Weiss.

Parallelism for Nested Loops with Non-uniform and Flow Dependences

Verification by testing

A Frame Packing Mechanism Using PDO Communication Service within CANopen

Solving two-person zero-sum game by Matlab

Pass by Reference vs. Pass by Value

Machine Learning. Topic 6: Clustering

Quicksort. Part 1: Understanding Quicksort

Exercises (Part 4) Introduction to R UCLA/CCPR. John Fox, February 2005

Insertion Sort. Divide and Conquer Sorting. Divide and Conquer. Mergesort. Mergesort Example. Auxiliary Array

Parallel Solutions of Indexed Recurrence Equations

Journal of Chemical and Pharmaceutical Research, 2014, 6(10): Research Article. Study on the original page oriented load balancing strategy

Sorting. Sorting. Why Sort? Consistent Ordering

Concurrent models of computation for embedded software

Structural Optimization Using OPTIMIZER Program

an assocated logc allows the proof of safety and lveness propertes. The Unty model nvolves on the one hand a programmng language and, on the other han

Virtual Machine Migration based on Trust Measurement of Computer Node

12/2/2009. Announcements. Parametric / Non-parametric. Case-Based Reasoning. Nearest-Neighbor on Images. Nearest-Neighbor Classification

Lecture 14: More Inheritance & Genericity

Parallelization of a Series of Extreme Learning Machine Algorithms Based on Spark

Cache Memories. Lecture 14 Cache Memories. Inserting an L1 Cache Between the CPU and Main Memory. General Org of a Cache Memory

Learning the Kernel Parameters in Kernel Minimum Distance Classifier

Intra-procedural Inference of Static Types for Java Bytecode 1

NGPM -- A NSGA-II Program in Matlab

Outline. Discriminative classifiers for image recognition. Where in the World? A nearest neighbor recognition example 4/14/2011. CS 376 Lecture 22 1

Problem Definitions and Evaluation Criteria for Computational Expensive Optimization

4/11/17. Agenda. Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Storage Management.

Support Vector Machines

Active Contours/Snakes

Module Management Tool in Software Development Organizations


Giving credit where credit is due

The Research of Support Vector Machine in Agricultural Data Classification

Outline. Midterm Review. Declaring Variables. Main Variable Data Types. Symbolic Constants. Arithmetic Operators. Midterm Review March 24, 2014

CACHE MEMORY DESIGN FOR INTERNET PROCESSORS

Motivation. EE 457 Unit 4. Throughput vs. Latency. Performance Depends on View Point?! Computer System Performance. An individual user wants to:

TECHNIQUE OF FORMATION HOMOGENEOUS SAMPLE SAME OBJECTS. Muradaliyev A.Z.

Feature-Based Matrix Factorization

Performance Evaluation

SLAM Summer School 2006 Practical 2: SLAM using Monocular Vision

Product documentation. Issue:

SENSITIVITY ANALYSIS IN LINEAR PROGRAMMING USING A CALCULATOR

ON SOME ENTERTAINING APPLICATIONS OF THE CONCEPT OF SET IN COMPUTER SCIENCE COURSE

ELEC 377 Operating Systems. Week 6 Class 3

On Some Entertaining Applications of the Concept of Set in Computer Science Course

MRKOMNO. kéï=~ë=çñw= pfabufp=ud. aáöáí~ä=o~çáçöê~éüó. nìáåâ=êéñéêéååé=öìáçé==== båöäáëü

Skew Angle Estimation and Correction of Hand Written, Textual and Large areas of Non-Textual Document Images: A Novel Approach

Parallel matrix-vector multiplication

The Ins and Outs of Gradual Type Inference

The Go4 Analysis Framework Fit Tutorial v2.2

Oracle Database: SQL and PL/SQL Fundamentals Certification Course

Load Balancing for Hex-Cell Interconnection Network

RECOGNIZING GENDER THROUGH FACIAL IMAGE USING SUPPORT VECTOR MACHINE

11. APPROXIMATION ALGORITHMS

LLVM passes and Intro to Loop Transformation Frameworks

Classification / Regression Support Vector Machines

Quality Improvement Algorithm for Tetrahedral Mesh Based on Optimal Delaunay Triangulation

User Manual SAPERION Web Client 7.1

Sorting. Sorted Original. index. index

WCET-Directed Dynamic Scratchpad Memory Allocation of Data

Transcription:

1 2 Introducton to Programmng Bertrand Meyer Lecture 13: Contaner data structures Last revsed 1 December 2003 Topcs for ths lecture 3 Contaner data structures 4 Contaners and genercty Contan other objects ( s ) Statc typng Example: a metro lne s among other thngs a contaner of stops Assessng algorthm performance: Bg-Oh notaton Lnked lsts Arrays Possble operatons on a contaner: Insert an Fnd out f an element s present Remove an element Traverse the structure to apply an operaton to every Many knds: lsts (ncl. lnked lst, ubly-lnked lsts ), crcular lsts, arrays, stacks, queues, prorty queues, hash tables A basc ssue wth contaners 5 Remnder: lst conventons 6 How we handle varants of a contaner class dstngushed only by the type of ther s? before after Metro lne: lst of stops Route: lst of segments Phone lst: lst of drectory entres Aga: lst of appontments 1 start back ndex forth (The Cursor) 1

Wthout genercty 7 Wthout genercty 8 class METRO_LINE feature : METRO_STOP s put_rght (x: METRO_STOP) s ext (x: METRO_STOP) s class ROUTE feature : SEGMENT s put_rght (x: SEGMENT) s ext (x: SEGMENT) s Wthout genercty 9 A non-generc lst class 10 class AGENDA feature : APPOINTMENT s put_rght (x: APPOINTMENT) s ext (x: APPOINTMENT) s class LIST1 feature : ANY s put_rght (x: ANY) s ext (x: ANY) s Usng the general lst 11 Solutons 12 my_route: LIST1 my_aga: LIST1 my_route.ext (seg) my_aga.ext (app) seg := my_route. app := my_aga. seg: SEGMENT app: APPOINTMENT Repeat code (not really acceptable) Allow conversons, or casts Unchecked: C, C++ Checked: Java, C# app?= my_aga. f app /= Vod then app := my_route. --????????? Make type parameterzaton explct (Effel): genercty 2

Soluton: genercty 13 Usng the general lst 14 class LINKED_LIST [G] feature Formal generc parameter : G s put_rght (x: G) s ext (x: G) s my_route: LIST1 my_aga: LIST1 my_route.ext (seg) my_aga.ext (app) seg := my_route. app := my_aga. app := my_route. --????????? seg: SEGMENT app: APPOINTMENT Usng the generc lst class 15 Statc typng 16 my_route: LIST [SEGMENT] my_aga: LIST [APPOINTMENT] my_route.ext (seg) my_aga.ext (app) seg := my_route. app := my_aga. seg: SEGMENT app: APPOINTMENT Actual generc parameter Every entty of the program s declared wth a type Every assgnment and feature call must satsfy type compatblty rules Goal: never apply to an object a feature that s not defned for that object app := my_route. -- Type-wrong, rejected The most mportant plattude n software engneerng! 17 A generc class: LINKED_LIST 18 It s better to catch an error early than late Demo (see EffelStudo) Better n analyss than n desgn Better n desgn than mplementaton Better n complaton than testng Better n testng than actual operaton 3

Computng the maxmum, verson 1 19 The routne body, verson 1 20 hghest_name (lne: METRO_LINE): STRING s -- Alphabetcally greatest staton name of lne lne_exsts: lne /= Vod fancy_lne.start ; Result := " nvarant varant fancy_lne.after Result := greater (Result, lne..name) fancy_lne.forth ensure Result /= Vod and then not Result.empty forth fancy_lne.start ; Result := "" fancy_lne.after Result := greater (Result, lne..name) fancy_lne.forth The routne body, verson 2 21 The routne body, verson 3 22 := 0 ; Result := "" > n := + 1 Result := greater (Result, lne..name) := + 1 ; Result := "" = 0 := 1 Result := greater (Result, lne..name) How fast s the algorthm? 23 Estmatng essental effcency 24 Deps on the hardware, operatng system, load on the machne But most fundamentally deps on the algorthm! How es the algorthm s executon tme (and memory occupaton) behave as a functon of the sze of the data, when that sze becomes large? Verson 1: tme roughly proportonal to. Verson 2 and 3: could be proportonal to, or to 2! 1 + 2 + + = * ( + 1) / 2 4

Bg Oh notaton 25 Formally 26 O (f (n)), where n represents the sze of the nput, means on the order of f (n). f s O (g (n) means there exsts a constant K such that for all n: Tme for verson 1 s O () f (n) / g (n) <= K Tme for versons 2 and 3 may be O () or O ( 2 ). O (1) thus means constant-tme, or tme bounded by a constant. Also used: f (n) = 3 n 2 + O (g (n)) Examples 27 Wth a 1000 tmes faster machne 28 n 2 = 3 n 2 = 3 n 2 + 2 n + 1 = O (n 2 ) O (n 2 ) O (n 2 ) Four algorthms: O (log (n)) Prevous maxmum sze: N New maxmum: N 1000 3 n 2 + 2 n + 1 = O (2 n 2 ) O (n) 1000 N O (n 2 ) 32 N 3 n 2 + 2 n + 1 = 3 n 2 + O (n) O (2 n ) N + 10 Lnked lsts 29 hghest_name, verson 2 30 Haldenegg rght 3 actve Central (The Cursor) frst_element rght Hauptbahnhof rght := 0 ; Result := "" > n := + 1 Result := greater (Result, lne..name) 5

hghest_name, verson 3 31 Performance 32 := + 1 ; Result := "" = 0 := 1 Result := greater (Result, lne..name) _th s O () (n verson 3) As a result, hghest_name, and any other such traversal, s O ( 2 )! 1 + 2 + + = * ( + 1) / 2 = O ( 2 ) Arrays 33 Array class nterface 34 class ARRAY [G] feature Indexed a lower bound to an upper bound Accessng or modfyng any element s O (1) (constant tme) () lower, upper: INTEGER make (l, h: INTEGER) s -- Allocate wth bound l and h. hgh >= low hgher >= low lower upper In memory: stored n contguous locatons (): G s -- Entry of ndex >= lower <= upper put (x: G; ) s -- Replace by x the value of the entry of ndex >= lower <= upper Usng arrays 35 A varant: hash tables 36 your_array: ARRAY [REAL] drectory: HASH_TABLE [METRO_STATION, STRING] create your_array.make (1, 100) your_array.put (35.6, 7) your_array.put ( 45.0, 8) create drectory.make (100) drectory.put (Staton_balard, "BALARD") drectory.put (Staton_montrouge, "MONTROUGE") prnt (your_array. (8)) prnt (drectory. ("MONTROUGE")) 6

37 End of lecture 13 7