Unit 1: Preliminaries Part 1: Course Introduction, Abstraction, and ADT s Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland April 23, 2010 ENGI 4892 (MUN) Unit 1, Part 1 April 23, 2010 1 / 12
1 Introduction What is this course about? Course outline Course notes 2 Abstraction 2 Modularity 2 Abstract Data Types (ADTs) Choosing between ADTs ENGI 4892 (MUN) Unit 1, Part 1 April 23, 2010 2 / 12
What is this course about? Data structures...but what s a data structure? A data structure is a particular way of organizing computer memory to record some data e.g. an array The way in which data is organized can have a big impact on the efficiency of any operations on that data e.g. searching for a card in a sorted vs. unsorted deck The analysis of algorithms We can analyze an algorithm to predict how fast it will run as compared to another algorithm Recursion A recursive function is one which calls itself Can be very useful for solving certain problems ENGI 4892 (MUN) Unit 1, Part 1 April 23, 2010 3 / 12
Course outline Preliminaries Abstraction Specification Review of pointers in C++ The Standard Template Library Complexity analysis Linked lists Stacks and queues Recursion Sorting Trees Graphs Hash tables ENGI 4892 (MUN) Unit 1, Part 1 April 23, 2010 4 / 12
Course notes These notes will be posted to the course website. Some material will be presented on the blackboard and not provided in electronic format. It is your responsibility to take these notes down. Usually this will be clearly indicated in the electronic notes as follows: COVERED ON THE BOARD Sometimes, however, there will be other material presented in class, but not on the blackboard. Again, it is your responsibility to record this material. ENGI 4892 (MUN) Unit 1, Part 1 April 23, 2010 5 / 12
Abstraction When we think about a topic, we usually consider it at one particular level of abstraction, e.g. to cross the street we think about the velocities of the cars and how to time our movement We abstract away the details of how the cars internal combustion engines and braking systems lead to their current velocities This process, called abstraction, allows us to deal with complex situations by abstracting away the details until the situation appears simple. When developing software, abstraction usually implies a separation between how a piece of software is used, and how it is implemented. In other words, abstraction means that we separate the interface from the implementation.
Consider the following function definition: // Returns the c h a r a c t e r f o l l o w i n g i n w i t h i n the // f i r s t 128 ASCII c h a r a c t e r s ( with wrap around ). char rotate ( char in ) ; The interface of a function includes the name, return type, and arguments, plus the specification of what that function does (i.e. the comments). Here is the function s implementation: char rotate ( char in ) { char out = ( in + 1) % 1 28; return out ; } In C++ we usually separate interface from implementation by putting the interface in a header file (.h file) and the implementation in another file (.cpp file). ENGI 4892 (MUN) Unit 1, Part 1 April 23, 2010 7 / 12
Modularity Abstraction ties in with with the concept of modularity. Modularity means that the solution is broken up into modules or components. Modularity is crucial in software development. Why? Division of labour: Designing, implementing, testing and maintaining of components can be assigned to various individuals or groups Reuse and sale: The same components can be reused in multiple software systems; Components can be sold to other software developers When we have abstraction and modularity together, we separate the role of modules (given by their interfaces), from the details of their implementation. ENGI 4892 (MUN) Unit 1, Part 1 April 23, 2010 8 / 12
Abstract Data Types (ADTs) A data type is a software component that stores some data and provides operations on that data. C++ captures this idea in its built-in data types (e.g. int, char, float,...) and in its classes. When the implementation of a data type is hidden from its user, we have an abstract data type or ADT. An ADT provides data abstraction. The separation between the use of an ADT and its implementation can be pictured as a wall:
Ultimately, the ADT is implemented using some data structure (e.g. a sorted array). However, the particular data structure used is hidden from the program that uses the ADT (a.k.a. client or user). Thus, data structures and ADT s are not the same thing! The ADT is the combination of the interface to the data and the data structure itself. What is the main mechanism for hiding implementation in C++ classes? private The user of an ADT interacts with it only through its interface, which in C++ is composed of a class s public members, as well as the specification for what those members do. Note: We may not actually hide the implementation. However, it is good practise to at least inform the user about the division between the interface and the implementation. ENGI 4892 (MUN) Unit 1, Part 1 April 23, 2010 10 / 12
Consider the following two classes: class Complex1 { private : double magnitude, theta ; // t h e t a i n r a d i a n s. public : Complex1 operator +(Complex1 ) ; //... And so on... // } ; class Complex2 { private : double real, imaginary ; public : Complex2 operator +(Complex2 ) ; //... And so on... // } ; The implementation of Complex1 uses a polar representation, while Complex2 uses a cartesian representation. These classes have the same interface the public methods are all the same but they differ in their implementation.
Choosing between ADTs If you have two ADTs that differ only in implementation, how do you choose between them? Efficiency (a.k.a complexity) Time: The speed of the operations Space: The amount of space required in memory Cost of design and implementation (one time) Cost of maintanence (ongoing) Accuracy and predictability of representation: e.g. With either representation of Complex, it is unlikely that the complex number 1/3 is perfectly representable. ENGI 4892 (MUN) Unit 1, Part 1 April 23, 2010 12 / 12