TA C252 Computer Programming 13/8/03Sundar B. BITS, Pilani.1 Todays Agenda 1. Data Abstraction 2. Linear Data.

Slides:



Advertisements
Similar presentations
Slide: 1 Interra Induction Training Data Structure ADT & Complexity.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
0 - 0.
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
Chapter 7. Binary Search Trees
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
Linked Lists Chapter 4.
Linear Lists – Array Representation
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
ITEC200 Week04 Lists and the Collection Interface.
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping.
John Hurley Cal State LA
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Review Pseudo Code Basic elements of Pseudo code
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Chapter 11 Component-Level Design
Karlstad University Computer Science Design Contracts and Error Management Design Contracts and Errors A Software Development Strategy Eivind J. Nordby.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.
CHAPTER 3 COLLECTIONS SET Collection. 2 Abstract Data Types A data type consists of a set of values or elements, called its domain, and a set of operators.
Searching Arrays Linear search Binary search small arrays
Introduction - The Need for Data Structures Data structures organize data –This gives more efficient programs. More powerful computers encourage more complex.
Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules.
Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions.
Data Structure (Part I) Chapter 2 – Arrays Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 10 – September 20, 2001.
Views of Data Data – nouns of programming world the objects that are manipulated information that is processed Humans like to group information Classes,
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Generic Programming and Inner classes ge·ner·ic 1a : relating or applied to or descriptive of all members of a genus, species, class, or group : common.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
COMP Loop Statements Yi Hong May 21, 2015.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
Data Structures Arrays and Lists Part 2 More List Operations.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Announcements HW6 due this Wednesday
Design Patterns - A few examples
Announcements HW6 due this Wednesday
Chapter 16 Linked Structures
Design Patterns - An Introduction
CSE-321 Programming Languages Introduction to Functional Programming
General Condition Loop
Assistant Professor Rabia Koser Dept. of Computer Applications
Presentation transcript:

TA C252 Computer Programming 13/8/03Sundar B. BITS, Pilani.1 Todays Agenda 1. Data Abstraction 2. Linear Data

13/8/03Sundar B. BITS, Pilani.2 Course Agenda Module 2: Data-Driven Programming: Motivation and Example Data Abstraction Linear Data

13/8/03Sundar B. BITS, Pilani.3 Data Driven Programming- Example Recall the scenario: Requirement: Drama club Professor-in-charge wants an electronic list of members. Design Choice: Ordered List if more searches than additions Unordered List if more additions than searches What if data is not available or if priorities can change? Prepare for both eventualities

13/8/03Sundar B. BITS, Pilani.4 Data Abstraction - Principle Abstraction Provide a view (I.e abstract) by hiding details Data Abstraction Expose operations (View) Hide representation

13/8/03Sundar B. BITS, Pilani.5 Data Abstraction - Application What to show Prof-in-charge? List of operations: add(Member, List, ListSize) returns ListSize delete(Member, List, ListSize) returns ListSize isMember(ID, List, ListSize) returns Boolean Limitation of the Interface: ListSize needs to be passed to operations! Ideally, Prof-in-charge should not be bothered by such details

Data Abstraction – Interface Declarations /* file: dramaClubOps.h */ extern ListSize add(Member m, List ms, ListSize n); #include dramaClubList.h /* Pre-condition: - Size of ms is n. - ms is a list of Member elements. Post-condition: - m added to ms if absent; overwritten if present. - return the size of the (possibly) updated list */

13/8/03Sundar B. BITS, Pilani.7 Data Abstraction – Interface Declarations /* file: dramaClubOps.h … */ extern ListSize delete(Member m, List ms, ListSize n); /* Pre-condition: - Size of ms is n. - ms is a list of Member elements. Post-condition: - m deleted from ms if present ms unchanged if not present. - return the size of the possibly updated list */

Data Abstraction – Interface Declarations /* file: dramaClubOps.h … */ extern Boolean isMember(ID i, List ms, ListSize n); /* Pre-condition: - Size of ms is n. - Each element of ms matches against some ID Post-condition: -return TRUE if ID matches against an element in ms FALSE otherwise */ #include boolean.h

13/8/03Sundar B. BITS, Pilani.9 Data Abstraction – Types /* file: dramaClubList.h */ typedef unsigned int ID; typedef unsinged int ListSize; typedef unsigned int Member; // List is an array of at most MAX elements of type Member typedef Member List[MAX]; #define MAX 1000

13/8/03Sundar B. BITS, Pilani.10 Data Abstraction – Types Enumerated Type: Recall that a type is a set of values E.g. Boolean = { TRUE, FALSE }; E.g. DaysOfWeek = { Su, Mo, Tu, We, Th, Fr, Sa } C Syntax: typedef enum { TRUE = 1, FALSE=0 } Boolean; typedef enum { Su = 1, Mo, Tu, We, Th, Fr, Sa } DaysOfWeek;

13/8/03Sundar B. BITS, Pilani.11 Data Abstraction – Types /* file: boolean.h */ typedef enum { TRUE = 1, FALSE=0 } Boolean;

Data Abstraction - Implementation /* file: dramaClubOps.c */ #include dramaClubOps.h ListSize add(Member m, List ms, ListSize n) { } int pos, newsize = n; if (n+1 > MAX) return 0; for (pos=0; pos < n; pos++) {// pos iterations if (ms[pos] > m) break; if (ms[pos] == m) return newsize; // m present } for ( ; n > pos; n--) ms[n] = ms[n-1]; // n – pos –1 iters. ms[n] = m; return newsize +1;

13/8/03Sundar B. BITS, Pilani.13 Data Abstraction - Implementation /* file: dramaClubOps.c … */ Boolean isMember(Member m, List ms, ListSize n) { if (search(m, ms, n) >= 0) return TRUE; else return FALSE; } // Exercise: Implement delete

13/8/03Sundar B. BITS, Pilani.14 Data Abstraction - Summary Expose Data Types and Operations (through Interfaces) E.g Member and List data types E.g add, delete, and isMember interfaces Hide Representation and Implementation E.g. List is an array of up to MAX elements E.g. isMember is implemented through search.

13/8/03Sundar B. BITS, Pilani.15 Data Driven Programming - Observations Kinds of Data (used so far) Simple data elements (int, Boolean etc.) Data Collections – Linear collections or Lists Often represented as Arrays Often referred to as Random Access Lists. For any i in the range (0 to size – 1) A[i] can be accessed.

13/8/03Sundar B. BITS, Pilani.16 Lists as Arrays - Observations Operations: Access – Random element: A[i] Access each element – Idiom: for ( i=0; i<N; i++) … A[i] … Cost of (each) Access: O(1) Independent of i Insertion / Deletion : Random position i Movement of adjacent elements Cost: O(N) Limitation: Max size is a hard bound.

13/8/03Sundar B. BITS, Pilani.17 Data Driven Programming – Example 2 Consider Sets and operations on sets. Basic operation on sets: Membership testing: Is x an element of S? Dependent operations on sets: Equality testing: Is S1 same as S2? Union computation: Let S be union of S1 and S2 Intersection computation: Let S be intersection of S1 and S2. Complement computation: Let S be the complement of S1.

13/8/03Sundar B. BITS, Pilani.18 (Abstract) Data Type - Set 1. Design a representation. 2. Design operations. 3. Define interfaces for operations over the new data type Set 4. Provide a representation for this type. 5. Implement the operations using this representation.

13/8/03Sundar B. BITS, Pilani.19 Representation for Sets First attempt: List of values. E.g. S = { 5, 7, 10 } is represented as a list (or array) int S[3] = { 5, 7, 10 } Is this a good representation? Recall the criterion:Operations! Membership Cost O(n) comparsions – using unsorted lists. Cost log(n) comparisons – using sorted lists.

13/8/03Sundar B. BITS, Pilani.20 Representation for Sets First attempt: List of values. E.g. S = { 5, 7, 10 } is represented as a list (or array) int S[3] = { 5, 7, 10 } Membership Cost O(n) comparsions – using unsorted lists. Cost log(n) comparisons – using sorted lists.

13/8/03Sundar B. BITS, Pilani.21 Representation for Sets Union (ignoring duplicates) Cost: O(max(m,n)) – assuming one list can be appended (by copying elements) to another; and sizes are not known. Assuming one list can be appended to another; but sizes are known. Cost: O(m+n) – if max. size cannot accommodate the united set. If you do not want the original set(s) destroyed. Question: Can we predict the max size? Max Size: Size of the universe (of values).

13/8/03Sundar B. BITS, Pilani.22 Representation for Sets Union (considering duplicates) For each element x in S1, check whether x occurs in S2. O(mn) where m=|S1| and n=|S2| using unsorted lists i.e. O(n 2 ) for same size O(m * log(n)) using sorted lists i.e. O(n*logn) for same size Intersection Similar costs.

13/8/03Sundar B. BITS, Pilani.23 Representation for Sets Alternate Representation: Basic idea: Membership is a boolean function on the universe of values. (i.e. yes or no function). i.e. a set can be represented as a list of bits (0 or 1) corresponding to each element in the universe. Simplifications: Assume elements are integers and universe is finite. So, each set is a binary string of fixed length |U|.

13/8/03Sundar B. BITS, Pilani.24 Representation for Sets Simplistic Example: Universe U = { } Each set is a bit string of length 32. Each position j in the bit string corresponds to membership of j in the set. E.g. S1 = { 1 } can be represented by E.g. S2 = { 2, 4, 31 } can be repd. by

13/8/03Sundar B. BITS, Pilani.25 Representation for Sets Consider the operations: Union: if S = S1 S2, then x S x S1 OR x S2 Union operation is Bit-wise OR operation since membership is 0 or 1. Extend the model: Intersection is Bit-wise AND operation Complement is Bit-wise NOT operation Equality is Bit-wise EQUALITY!! Question: What is S1 <> S2 ?