Data Structure (Part I) Chapter 2 – Arrays. 2.1.2 Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Outline lecture Revise arrays Entering into an array
MATH 224 – Discrete Mathematics
One Dimensional Arrays
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Array pair C++ array requires the index set to be a set of consecutive integers starting at 0 C++ does not check an array index to ensure that it belongs.
Data Abstraction and Encapsulation
© 2004 Goodrich, Tamassia Hash Tables1  
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Chapter 1 – Basic Concepts
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Arrays.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Analysis of Algorithms
Chapter 2 1. Arrays Array: a set of index and value Data structure For each index, there is a value associated with that index. Eg. int list[5]: list[0],
Data Structure (Part II) Chapter 2 – Arrays. Matrix A matrix with 5 rows and 3 columns can be represented by n = 3 m = 5 We say this is a 5×3 matrix.
Array (continue).
Hash Tables1   © 2010 Goodrich, Tamassia.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Data Structure Sang Yong Han Chung-Ang University Spring
Data Structures & Algorithm Analysis Arrays. Array: a set of pairs (index and value) data structure For each index, there is a value associated with that.
Data Structure Introduction.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
© 2004 Goodrich, Tamassia Hash Tables1  
Sparse Vectors & Matrices Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Linked List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Learners Support Publications Constructors and Destructors.
LINKED LISTS.
Hash Tables 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Data Structure Sang Yong Han
Constructors and Destructors
Outline lecture Revise arrays Entering into an array
Analysis of Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
Algorithm Analysis CSE 2011 Winter September 2018.
© 2013 Goodrich, Tamassia, Goldwasser
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
CSCE 3110 Data Structures & Algorithm Analysis
Linked List (Part I) Data structure.
Constructors and Destructors
Dictionaries 1/17/2019 7:55 AM Hash Tables   4
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Introduction to Data Structure
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Lecture 3 – Data collection List ADT
Presentation transcript:

Data Structure (Part I) Chapter 2 – Arrays

2.1.2 Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding The concealing of the implementation details of a data object from the outside world. –Data Abstraction The separation between the specification of a data object and its implementation.

2.1.2 Data Abstraction and Encapsulation in C++ Data Type –A collection of objects and a set of operations that act on those objects. Data Encapsulation –In C++, data encapsulation is enforced Declaring all data members of a class to be private or protected. External access to data members can be achieved by defining public member functions that get and set data members.

2.1.2 Data Abstraction and Encapsulation in C++ Data Abstraction –Abstract Data Type (ADT) A data type in which the specification of objects and operations on the objects is separated from the representation of and the implementation the objects. Implementation-independent.

Abstract Data Type int ReadData(int i) void WriteData(int i, int i) int ReadData(int i) void WriteData(int i, int i) ADT public: private:

2.2 The Array As an ADT From a perspective on implementation issues –An array is a consecutive set of memory locations with each containing data of the same type. –Example: int term[10]; int term In C++, to access the third element, use term[2] or *(term+2).

2.2 The Array As an ADT What is the advantage of preserving data in an array? Because it can support – __________ access, and – __________ access through indices. The index is used like an ID number for the value stored in array.

2.2 The Array As an ADT When considering array as an ADT –An array is a set of pairs,. Each index at most has a value associated with it. Correspondence / Mapping indexvalue 213

2.2 The Array As an ADT class GeneralArray1D { public: //Create an array of a given size; each element is initialized with initValue GeneralArray1D(int size, float initValue); //If the index i is valid, return the value associated with it; //otherwise, throw an exception. float Retrieve(int index); //bool Retrieve(int index, float &result); //If the index i is valid, replace the old value associated with it by x; //otherwise, throw an exception. void Store(index i, float x); //bool Store(int i, float x); };

2.2 The Array As an ADT GeneralArray1D is more flexible about the composition of the index set. –Integers in the index set is not required to be consecutive. –Range checking can be provided to ensure valid access. –Time complexity to retrieve a specific index is an issue. C++ array: –Finding the value associated with the index i: _____. GeneralArray1D: –Finding the value associated with the index i: _____.

Applications of Arrays Ordered List / Linear List Polynomials (on a single variable) Sparse Matrices

Polynomial Example a(x) = 7 x 4 – 3x The degree of a polynomial is the largest exponent. –The degree of a(x) is _________. a(x) has ______ terms. –They are _______, _______, and ________. The coefficients are _____, ______, and ______. The exponents are _____, ______, and ______. –Normally, terms with zero coefficients are not displayed. exponent coefficient

Sum and Product of Two Polynomials Example: a(x) = 3x 3 + 2x – 4 b(x) = x 8 – 10x 5 – 3x a(x) + b(x) = x 8 – 10x 5 + (3-3)x 3 + 2x + (-4+1) = x 8 – 10x 5 + 2x – 3 a(x) × b(x) = (3x 3 + 2x – 4)(x 8 – 10x 5 – 3x 3 + 1) = 3x 3  (x 8 – 10x 5 – 3x 3 + 1) + 2x  (x 8 – 10x 5 – 3x 3 + 1) + (-4)  (x 8 – 10x 5 – 3x 3 + 1)

2.3 The Polynomial ADT class Polynomial { //Suppose public: Polynomial(); ~Polynomial(); Polynomial &Add(Polynomial &poly); Polynomial &Mult(Polynomial &poly); //Evaluate the polynomial at f and return the result. float Eval(float f); } Using & to pass parameters and return value by reference.

2.3.1 Polynomial Representation Representation 1 –Represent polynomials in C++ array Index represent exponent. The coefficient of x i is stored in coef[i]. –Example: a(x) = 3x 3 + 2x – x2x 3x33x3

Polynomial Representation - 1 –Implementation: private: int degree; float coef[MaxDegree + 1]; MaxDegree: a constant that represents that largest-degree to be represented. –Advantage: Simple Fast –Disadvantage:

Polynomial Representation - 1 –Advantage: Simple. Fast. –Time complexity to retrieve a term with a specific exponent: ___________. –Disadvantage: Could be very wasteful in its use of computer memory if degree is much less than MaxDegree.

Polynomial Representation - 2 Also represented in C++ array, but use dynamical allocation. Implementation: int degree; float *coef; Define coef so that its size is degree+1. Polynomial::Polynomial(int d) { degree = d; coef = new float [degree + 1]; }

Polynomial Representation - 2 –Disadvantage: Could also be very wasteful in its use of computer memory if the polynomial is sparse. –too many zero terms. Example: b(x) = x x Consider: –At least how many elements are required? –Eventually how many elements are used to store b(x)?

Polynomial Representation - 3 To solve the problem of Representation 1 and 2, we store only the nonzero terms. The exponent now is independent of the index of the array. –A nonzero term is stored in an element of the array. –Each element has to preserve both exponent and coefficient. Example: c(x) = 3x x exp:1000 coef: 3 0 exp: 2 coef: 2 1 exp: 0 coef:

Polynomial Representation - 3 class Polynomial; class Term { friend Polynomial; private: float coef; int exp; }; class Polynomial { … private: Term *termarray; int size;//size of termArray int count;//number of nonzero terms }; count size

Polynomial Representation - 3 Requirement: –When inserting terms into Polynomial, each exponent must be unique (cannot be duplicated). –Incorrect example: exp: 3 coef: 3 0 exp: 3 coef: 2 1 exp: 2 coef: Ambiguous! What exactly is the coefficient of the term with exponent of 3?

Comparison Representation 2Representation 3 Time complexity of searching the term with the exponent i. O(1)O(count) Space ComplexityO(degree)O(count) If the polynomial has few zero terms, Representation 3 uses memory space about twice as much space as does Representation 2. Why?

2.3.2 Polynomial Addition Example: a(x) = 3x 3 + 2x 2 b(x) = 5x 4 +x 2 – 2x + 7 exp: 4 coef: 5 0 exp: 2 coef: 1 1 exp: 1 coef: exp: 3 coef: 3 exp: 2 coef: 2 exp: 0 coef: 7 3

exp: 4 coef: 5 exp: 2 coef: 1 exp: 1 coef: -2 exp: 3 coef: 3 exp: 2 coef: 2 exp: 0 coef: 7 exp: 4 coef: 5 exp: 3 coef: 3 exp: 2 coef: 3 exp: 1 coef: -2 A: B: aPos bPos C: exp: 0 coef: 7 A.termarray[0].exp = 3 < 4 = B.termarray[0].exp A.termarray[0].exp = 3 > 2 = B.termarray[1].expA.termarray[1].exp = 2 == 2 = B.termarray[1].exp →A.termarray[1].exp + B.termarray[1].exp = 3 != 0 Stopped. C(x) = 5x 4 + 3x 3 + 3x 2 – 2x + 7 Stopped.

Algorithm Polynomial Polynomial::Add(Polynomial B) { 1 Declare C as Polynomial to be the result; 2 aPos = 0, bPos = 0; 3 while aPos < count and bPos < B.count 4 if (termarray[aPos].exp > B.termarray[bPos].exp) 5 C.InsertNewTerm ; 6 aPos++; 7 else if (termarray[aPos].exp < B.termarray[bPos].exp) 8 C.InsertNewTerm ; 9 bPos++; 10 else 11 NewCoef = termarray[aPos].coef +B.termarray[bPos].coef; 12 if NewCoef > 0 13 C.InsertNewTerm ; 14 end if 15 aPos++, bPos++; 16 end if 17 end while 18 for each remaining term t in this object 19 Add t to C; 20 end for 21 for each remaining term t in B 22 Add t to C; 23 end for 24 Return C; 25}

Analysis of Polynomial::Add() Steps to analyze time complexity: 1.Define instance characteristics. 2.Analysis time complexity line by line. Consider worst case. 3.Compute the total complexity.

Analysis of Polynomial::Add() Let m and n be the number of nonzero terms in A and B. –Line 1-2: O(1). –Line 3-22: aPos or bPos increase by 1 each time until aPos > m and bPos > n. The total number of iterations of the while- and for-loop is bounded by m + n. Therefore, the total time complexity is O(m + n).

Implementation Polynomial &Polynomial::Add(Polynomial &B) { Polynomial *C = new Polynomial(); int aPos = 0, bPos = 0; while (aPos < count && bPos < B.count) { if (termarray[aPos].exp > B.termarray[bPos].exp) { C->NewTerm (termarray[aPos].exp, termarray[aPos].coef); aPos++; } else if (termarray[aPos].exp < B.termarray[bPos].exp) { C->NewTerm (B.termarray[bPos].exp, B.termarray[bPos].coef); bPos++; } else { NewCoef = termarray[aPos].coef +B.termarray[bPos].coef; if (NewCoef > 0) C->NewTerm (B.termarray[aPos].exp, NewCoef); end if aPos++; bPos++; } for ( ; aPos < count; aPos++) C->NewTerm (termarray[aPos].exp, termarray[aPos].coef); for ( ; bPos < count; bPos++) C->NewTerm (termarray[bPos].exp, termarray[bPos].coef); return *C; }

Invoking Polynomial::Add() Polynomial A, B; //Construct A and B … Polynomial &C = A.Add(B); “&” tells compiler that C is exactly the object returned by Add(). Discuss: –what happens if we do not use pass-by-reference?