Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 14: Container Data Structures.

Slides:



Advertisements
Similar presentations
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Advertisements

Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
© 2004 Goodrich, Tamassia Hash Tables1  
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Topological Sort Algorithm.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 14: Container Data Structures.
Complexity Analysis (Part I)
Hash Tables1 Part E Hash Tables  
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 12: More on references and.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Hashing General idea: Get a large array
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 7.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 11.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Week 2 CS 361: Advanced Data Structures and Algorithms
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
Lecture 10: Class Review Dr John Levine Algorithms and Complexity March 13th 2006.
Information and Computer Sciences University of Hawaii, Manoa
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
Analysis of Algorithms
Hashing Table Professor Sin-Min Lee Department of Computer Science.
Arrays Tonga Institute of Higher Education. Introduction An array is a data structure Definitions  Cell/Element – A box in which you can enter a piece.
Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSE 501N Fall ‘09 11: Data Structures: Stacks, Queues, and Maps Nick Leidenfrost October 6, 2009.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Hashing Hashing is another method for sorting and searching data.
For more notes and topics VISIT: IMPLEMENTATION OF STACKS eITnotes.com.
CS201: Data Structures and Discrete Mathematics I Hash Table.
Chapter 11 Hash Anshuman Razdan Div of Computing Studies
Data Structures & Algorithms
Elementary Data Organization. Outline  Data, Entity and Information  Primitive data types  Non primitive data Types  Data structure  Definition 
Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.
A Introduction to Computing II Lecture 11: Hashtables Fall Session 2000.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
1 i206: Lecture 12: Hash Tables (Dictionaries); Intro to Recursion Marti Hearst Spring 2012.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
DATA STRUCTURES (CS212D) Overview & Review Instructor Information 2  Instructor Information:  Dr. Radwa El Shawi  Room: 
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Analysis of Algorithms
Stacks Chapter 4.
CMSC 341 Lecture 5 Stacks, Queues
Introduction to Data Structure
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 14: Container Data Structures

2 Topics for this lecture Containers and genericity Container operations Lists Arrays Assessing algorithm performance: Big-O notation Hash tables Stacks and queues

3 The ultimate question LINKED_LIST or ARRAY?

4 Container data structures Contain other objects (“items”) Some fundamental operations on a container:  Insertion: add an item  Removal: remove an occurrence (if any) of an item  Wipeout: remove all occurrences of an item  Search: find out if a given item is present  Iteration (or “traversal”): apply a given operation to every item Various container implementations, as studied next, determine:  Which of these operations are available  Their speed  The storage requirements This lecture is just an intro; see “Data Structures and Algorithms” (second semester course) for an in-depth study

5 A familiar container: the list item Cursor forth after before back index count 1 finish start To facilitate iteration and other operations, our lists have cursors (here internal, can be external) Queries Commands

6 A standardized naming scheme Container classes in EiffelBase use standard names for basic container operations: is_empty : BOOLEAN has (v : G ): BOOLEAN count : INTEGER item : G Whenever applicable, use them in your own classes as well make put (v : G ) remove (v : G ) wipe_out start, finish forth, back

7 Bounded representations In designing container structures, avoid hardwired limits! “Don’t box me in”: EiffelBase is paranoid about hard limits  Most structures conceptually unbounded  Even arrays (bounded at any particular time) are resizable When a structure is bounded, the maximum number of items is called capacity, with an invariant count <= capacity

8 Containers and genericity How do we handle variants of a container class distinguished only by the item type? Solution: genericity allows explicit type parameterization consistent with static typing Container structures are implemented as generic classes: LINKED_LIST [G ]pl : LINKED_LIST [PERSON ] sl : LINKED_LIST [STRING ] al : LINKED_LIST [ANY ]

9 Lists A list is a container keeping items in a defined order Lists in EiffelBase have cursors item Cursor forth after before back index count 1 finish start

10 Cursor properties (all in class invariant!) The cursor ranges from 0 to count + 1: 0 <= index <= count + 1 The cursor is at position 0 if and only if before holds: before = (index = 0 ) It is at position count + 1 if and only if after holds: after = (index = count + 1 ) In an empty list the cursor is at position 0: is_empty = (count = 0 )

11 A specific implementation: (singly) linked lists

12 Caveat Whenever you define a container structure and the corresponding class, pay attention to borderline cases:  Empty structure  Full structure (if finite capacity)

13 Adding a cell

14 The corresponding command put_right (v : G) -- Add v to right of cursor position; do not move cursor. require not_after: not after local p : LINKABLE [G] do create p. make (v) if before then p. put_right (first_element) first_element := p active := p else p.put_right (active. right) active. put_right (p) end count := count + 1 ensure next_exists: active. right /= Void inserted: (not old before) implies active. right. item = v inserted_before: (old before) implies active. item = v end

15 Removing a cell

16 The corresponding command Do remove as an exercise

17 Inserting at the end: extend

18 Arrays An array is a container storing items in a set of contiguous memory locations, each identified by an integer index Valid index values lowerupper 1 item (4 )

19 Bounds and indexes Arrays are bounded: lower : INTEGER -- Minimum index upper : INTEGER -- Maximum index The capacity of an array is determined by the bounds: capacity = upper – lower + 1

20 Accessing and modifying array items item (i : INTEGER) : G -- Entry at index i, if in index interval. require valid_key: valid_index (i ) put (v : G; i : INTEGER) -- Replace i -th entry, if in index interval, by v. require valid_key: valid_index (i ) ensure inserted: item (i ) = v

21 Eiffel note: simplifying the notation Feature item is declared as item (i : INTEGER) alias ″[ ]″ : G assign put This allows the following synonym notations: a [i ] for a. item (i ) a. item (i ) := x for a. put (x, i ) a [i ] := xfor a. put (x, i ) These facilities are available to any class A class may have at most one feature aliased to “[]”

22 Resizing an array At any point in time arrays have a fixed lower and upper bound, and thus a fixed capacity Unlike most other programming languages, Eiffel allows resizing an array (resize) Feature force resizes an array if required: unlike put, it has no precondition Resizing usually requires reallocating the array and copying the old values. Such operations are costly!

23 Using an array to represent a list See class ARRAYED_LIST in EiffelBase Introduce count (number of elements in the list) The number of list items ranges from 0 to capacity : 0 <= count <= capacity An empty list has no elements: is_empty = (count = 0)

24 Linked or arrayed list? The choice of a container data structure depends on the speed of its container operations The speed of a container operation depends on how it is implemented, on its underlying algorithm

25 How fast is an algorithm? Depends on the hardware, operating system, load on the machine... But most fundamentally depends on the algorithm!

26 Algorithm complexity: “big-O” notation Defines function not by exact formula but by order of magnitude, e.g. O (1), O (log count), O (count), O (count 2 ), O (2 count ). 7count count + 4 is O (count 2 ) Let n be the size of the data structure (count ). “f is O ( g (n))” means that there exists a constant k such that  n :  n, |f (n)|  k |g (n)|

27 Examples put_right of LINKED_LIST: O (1) Regardless of the number of elements in the linked list it takes a constant time to insert an item at cursor position. force of ARRAY: O (count) At worst the time for this operation grows proportionally to the number of elements in the array.

28 Why neglect constant factors? Consider algorithms with complexity O (n ) O (n 2 ) O (2 n ) Assume your new machine (Christmas is coming!) is 1000 times faster? How much bigger a problem can you solve in one day of computation time?

29 Variants of algorithm complexity We may be interested in  Worst-case performance  Best-case performance (seldom)  Average performance (needs statistical distribution) Unless otherwise specified this discussion considers worst-case Lower bound notation:  (n )

30 Cost of singly-linked list operations OperationFeatureComplexity Insert right to cursorput_rightO (1) Insert at endextend O (count) O (1) Remove right neighborremove_rightO (1) Remove at cursor positionremoveO (count) Index-based accessi_thO (count) SearchhasO (count)

31 Cost of doubly-linked list operations OperationFeatureComplexity Insert right to cursorput_rightO (1) Insert at endextendO (1) Remove right neighborremove_rightO (1) Remove at cursor positionremoveO (1) Index-based accessi_thO (count) SearchhasO (count)

32 Cost of array operations OperationFeatureComplexity Index-based accessitemO (1) Index-based replacementputO (1) Index-based replacement outside of current bounds forceO (count) SearchhasO (count) Search in sorted array-O (log count)

33 Hash tables Both arrays and hash tables are indexed structures; item manipulation requires an index or, in case of hash tables, a key. Unlike arrays hash tables allow keys other than integers.

34 An example person, person1 : PERSON personnel_directory : HASH_TABLE [PERSON, STRING ] create personnel_directory.make (100) Storing an element: create person1 personnel_directory. put (person1, ”Annie”) Retrieving an element person := personnel_directory. item (”Annie”)

35 Hash function The hash function maps K, the set of possible keys, into an integer interval a..b. A perfect hash function gives a different integer value for every element of K. Whenever two different keys give the same hash value a collision occurs.

36 Collision handling Open hashing: ARRAY [LINKED_LIST [G]]

37 A better technique: closed hashing Class HASH_TABLE [G, H] implements closed hashing: HASH_TABLE [G, H] uses a single ARRAY [G] to store the items. At any time some of positions are occupied and some free:

38 Closed hashing If the hash function yields an already occupied position, the mechanism will try a succession of other positions (i1, i2, i3) until it finds a free one: With this policy and a good choice of hash function search and insertion in a hash table are essentially O (1).

39 Cost of hash table operations OperationFeatureComplexity Key-based accessitem O (1) O (count) Key-based insertionput, extend O (1) O (count) Removalremove O (1) O (count) Key-based replacementreplace O (1) O (count) Searchhas O (1) O (count)

40 Dispensers Unlike indexed structures, as arrays and hash tables, there is no key or other identifying information for dispenser items. Dispensers are container data structures that prescribe a specific retrieval policy:  Last In First Out (LIFO): choose the element inserted most recently  stack.  First In First Out (FIFO): choose the oldest element not yet removed  queue.  Priority queue: choose the element with the highest priority.

41 Dispensers

42 Stacks A stack is a dispenser applying a LIFO policy. The basic operations are: Push an item to the top of the stack (put) Pop the top element (remove) Access the top element (item) Top A new item would be pushed here Body, what would remain after popping.

43 An example: Polish expression evaluation from until “All terms of Polish expression have been read” loop “Read next term x in Polish expression” if “x is an operand” then s. put (x) else -- x is a binary operator -- Obtain and pop two top operands: op1 := s. item; s. remove op2 := s. item; s. remove -- Apply operator to operands and push result: s. put (application (x, op2, op1)) end

44 Applications of stacks Many! Ubiquitous in programming language implementation:  Parsing expressions (see next)  Managing execution of routines (“THE stack”) Special case: implementing recursion  Traversing trees  …

45 Evaluating 2 a b + c d - * + 22 a 2 a b 2 (a+b) 2 c 2 c d 2 (c-d) 2 (a+b)*(c-d) 2+(a+b)*(c-d)

46 The run-time stack The run-time stack contains the activation records for all currently active routines. An activation record contains a routine’s locals (arguments and local entities).

47 Implementing stacks Common stack implementations are either arrayed or linked.

48 Choosing between data structures Use a linked list if:  Order between items matters  The main way to access them is in that order  (Bonus condition) No hardwired size limit Use an array if:  Each item can be identified by and integer index  The main way to access items is through that index  Hardwired size limit (at least for long spans of execution) Use a hash table if:  Every item has an associated key  The main way to access them is through these keys  The structure is bounded Use a stack:  For a LIFO policy  Example: traversal of nested structures such as trees Use a queue:  For a FIFO policy  Example: simulation of FIFO phenomenon

49 What we have seen Container data structures: basic notion, key examples Algorithm complexity (“Big-O”) How to choose a particular kind of container