data structures and algorithms what the course is about data structures is concerned with the...

68
Data Structures and Algorithms

Upload: alannah-rose

Post on 26-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Data Structures and Algorithms

Page 2: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

What The Course Is About

• Data structures is concerned with the representation and manipulation of data.

• All programs manipulate data.• So, all programs represent data in some way.• Data manipulation requires an algorithm.

Page 3: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

What The Course Is About

• Algorithm design methods needed to develop programs that do the data manipulation.

• The study of data structures and algorithms is fundamental to Computer Science.

Page 4: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Software Design Process

Page 5: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Programming Life Cycle Activities• Problem analysis understand the problem

• Requirements definition specify what program will do

• High- and low-level design how it meets requirements

• Implementation of design code it

• Testing and verification detect errors, show correct

• Delivery turn over to customer

• Operation use the program

• Maintenance change the program

Page 6: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

An Algorithm Is . . .

• A logical sequence of discrete steps that describes a complete solution to a given problem computable in a finite amount of time.

Page 7: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Goals of Quality Software

• It works. • It can be read and understood.

• It can be modified.

• It is completed on time and within budget.

Page 8: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Specification: Understanding the Problem

Detailed Program Specification

• Tells what the program must do, but not how it does it.

• Is written documentation about the program.

Page 9: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Writing Detailed Specifications

Detailed Program Specification Includes:• Inputs

• Outputs

• Processing requirements

• Assumptions

Page 10: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Abstraction

• A model of a complex system that includes only the details essential to the perspective of the viewer of the system.

Page 11: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Information Hiding

• Hiding the details of a function or data structure with the goal of controlling access to the details of a module or structure.

PURPOSE: To prevent high-level designs from depending on low-level design details that may be changed.

Page 12: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Two Approaches to Building Manageable Modules

Divides the problem into more easily handled subtasks, until the functional modules (subproblems) can be coded.

Identifies various objects composed of data and operations, that can be used together to solve the problem.

FUNCTIONALDECOMPOSITION

OBJECT-ORIENTED DESIGN

FOCUS ON: processes FOCUS ON: data objects

Page 13: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

FindWeighted Average

PrintWeighted Average

Functional Design Modules

Main

Print Data

Print Heading

Get DataPrepare File for Reading

Page 14: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Object-Oriented DesignA technique for developing a program in which the solution is expressed in

terms of objects -- self- contained entities composed of data and operations on that data.

Page 15: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate
Page 16: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

More about OOD

Languages supporting OOD include: C++, Java, …

A class is a programmer-defined data type and objects are variables of that type.

In C++, cin is an object of a data type (class) named istream, and cout is an object of a class ostream. Header files iostream.h and fstream.h contain definitions of stream classes.

Page 17: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Procedural vs. Object-Oriented Code

“Read the specification of the software you want to build. Underline the verbs if you are after procedural code, the nouns if you aim for an object-oriented program.”

Brady Gooch, “What is and Isn’t Object Oriented Design,” 1989.

Page 18: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Verification of Software Correctness

• Testing• Debugging• Program verification

Page 19: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• Program Verification is the process of determining the degree to which a software product fulfills its specifications.

Program Verification

PROGRAM

SPECIFICATIONS

Inputs

Outputs

Processing Requirements

Assumptions

Page 20: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Program Testing

• Testing is the process of executing a program with various data sets designed to discover errors.

DATA SET 1

DATA SET 2

DATA SET 3

DATA SET 4

. . .

Page 21: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Origin of Bugs

Various Types of Errors:

• Design errors occur when specifications are wrong

• Compile errors occur when syntax is wrong

• Run-time errors result from incorrect assumptions, incomplete understanding of the programming language, or unanticipated user errors.

Page 22: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

22

C++ Plus Data Structures

Nell DaleChapter 2

Data Design and Implementation

Page 23: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

23

1. What do we mean by data?

2. Data abstraction and encapsulation

3. Data structure

4. Abstract data type operator categories

Different Views of Data

Page 24: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

24

2. Data Abstraction

• Separation of a data type’s logical properties from its implementation.

LOGICAL PROPERTIES IMPLEMENTATION

What are the possible values? How can this be done in C++?

What operations will be needed?

Page 25: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

25

APPLICATION

0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1

REPRESENTATION

Data Encapsulation

• is the separation of the representation of data from the applications that use the data at a logical level; a programming language feature that enforces information hiding.

int y;

y = 25;

Page 26: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

26

Encapsulated C++ Data Type int

Value range: INT_MIN . . INT_MAX

Operations: + prefix - prefix + infix - infix * infix / infix % infixRelational Operators infix

TYPE int

(inside)

Representation of

int

as 16 bits two’s complement

+

Implementation of Operations

Page 27: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

27

Abstract Data Type (ADT)

• A data type whose properties (domain and operations) are specified independently of any particular implementation.

Page 28: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

28

3. Data Structures

Defined by

• the logical arrangement of data elements

• the set of operations we need to access the elements

Page 29: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

29

• Application (or user) level: modeling real-life data in a specific context.

• Logical (or ADT) level: abstract view of the domain and operations. WHAT

• Implementation level: specific representation of the structure to hold the data items, and the coding for operations. HOW

Data from 3 different levels

Page 30: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

30

Viewing a library from 3 different levels

• Application (or user) level: Library of Congress.

• Logical (or ADT) level: domain is a collection of books; operations include: check book out, check book in, pay fine, reserve a book.

• Implementation level: representation of the structure to hold the “books”, and the coding for operations.

Page 31: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate
Page 32: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

4. ADT Operator Categories

• Constructor -- creates a new instance (object) of an ADT.

• Transformer -- changes the state of one or more of the data values of an instance.

• Observer -- allows us to observe the state of one or more of the data values without changing them.

• Iterator -- allows us to process all the components in

a data structure sequentially.32

Page 33: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• Strings are lists of characters; Many languages have strings as a built-in data type.

• The C++ standard library provides a string class, whose declarations are available in the header file <string>. – The operations given in the string class include concatenation of two

strings using the + operator, searching a string for a substring, determining the number of characters in a string, and some input/output operations.

– C++ also inherits a primitive string type from C, which is simply an array of type char, with the null character (\0) used to signal the end of the string.

– C++ also inherits from C a set of string-handling functions in <cstring>

Page 34: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• Let’s create our own abstract data type String that has general-purpose input and output functions and encapsulate it into a class.

• We will call our class StrType so as not to confuse it with the library class string.

Page 35: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

String ADT Specification

• Structure: A sequence of alphanumeric characters.

Page 36: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Operations:

• MakeEmpty– Function: Initializes string to empty.– Precondition: None.– Postcondition: String is empty.

Page 37: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• GetStringFile(Boolean skip, InType charsAllowed, ifstream& inFile)

• Function: Gets a string from the file InFile.• Assumptions: (1) InType is a data type consisting of the

following constants:– ALPHA: Only alphabetic characters are stored.– ALPHA_NUM: Only alphanumeric characters are stored.– NON_WHITE: All nonwhitespace characters are stored.– NOT_NEW: All characters excluding the newline character are stored.

• (2) If skip is true, characters not allowed are skipped until the first allowed character is found. Reading and storing begins with this character and continues until a character not allowed is encountered. That character is read but not stored. If skip is false, reading and storing begins with the current character in the stream.

Page 38: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• Precondition: inFile has been opened• Postconditions: If no allowable characters are

found, the empty string is returned; else, a string has been input according to the skip and charsAllowed parameters. inFile is left positioned following the last character read.

Page 39: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• GetString(Boolean skip, InType charsAllowed)– Function: Gets a string from the standard input

stream.– Assumptions: Same as those for GetStringFile.– Precondition: None.– Postconditions: String has been input according to

the skip and charsAllowed parameters. Input stream is left positioned following the last character read, which is a nonallowed character.

Page 40: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• PrintToScreen(Boolean newLine)– Function: Prints an end-of-line character if needed

before printing the string to the screen.– Precondition: String has been initialized.– Postcondition: If newLine is true, string has been

printed on the screen starting on a new line; otherwise, string has been printed on the current line.

Page 41: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• PrintToFile(Boolean newLine, ofstream& outFile)– Function: Prints an end-of-line character if needed

before– printing the string to outFile.– Preconditions: String has been initialized.– outFile has been opened.– Postconditions: If newLine is true, string has been

printed on outFile starting on a new line; otherwise, string has been printed on the current line of outFile. outFile is left positioned after the last character written.

Page 42: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• int LengthIs()– Function: Returns the number of characters in the

string.– Precondition: String has been initialized.– Postcondition: Function value = number of

characters in the string.

Page 43: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• CopyString(StrType& newString)– Function: Copies self into newString.– Precondition: Self has been initialized.– Postcondition: Self has been copied into

newString.

Page 44: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Application Level#include <fstream>#include "StrType.h"#include <iostream>const int MAX_WORDS = 10;int main(){

using namespace std;StrType word;ifstream inFile;StrType words[MAX_WORDS];int numWords = 0;inFile.open("words.in");

Page 45: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

word.MakeEmpty();word.GetStringFile(true, ALPHA_NUM, inFile);while (inFile && numWords < MAX_WORDS) {

word.CopyString(words[numWords]);numWords++;word.GetStringFile(true, ALPHA_NUM, inFile);

}if (inFile)

cout << "First " << MAX_WORDS << " words on the file: ";else

cout << " Words on the file: ";for (int index = 0; index < numWords; index++)

words[index].PrintToScreen(true);return 0;}

Page 46: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Implementation Level

• Now we must determine how we will represent our strings. – Recall that C++ implements strings as one-dimensional

char arrays with the null character (\0) signaling the end of the string.

– Another way of implementing a string would be a struct or a class with two data members: an array of type char and an integer variable representing the length. The string characters would be located between position 0 and position length – 1 in the array.

• Which approach shall we use?

Page 47: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• The amount of storage required for both array-based designs is nearly the same, and the amount of processing is approximately the same although the algorithms differ somewhat.

• Let’s use the null-terminated method here.

Page 48: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• To accommodate the null character, we must remember to allocate one more position than the maximum number of characters expected.

Page 49: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• The maximum number of characters—where does this number come from? Nothing in the specification hints at a limit on the number of characters allowed in a string, but our array-based implementation requires us to specify an array size.

• Let’s arbitrarily choose a reasonably large number—say, 100—for the maximum string length.– In the specification file, StrType.h, we define a constant

MAX_CHARS to be 100, letting the user know that it is the maximum length allowed.

Page 50: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

– Should the client be responsible for making sure that the string is within the allowable length,

– or should the code of GetString and GetStringFile check for this problem and discard any characters that cannot be stored?

• Both approaches have merit. We choose the latter and do the checking within StrType.

• The specifications need to be changed to reflect this decision.

Page 51: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

// Header file for class StrType, a specification for the// String ADT#include <fstream>#include <iostream>const int MAX_CHARS = 100;enum InType {ALPHA_NUM, ALPHA, NON_WHITE, NOT_NEW};

Page 52: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

class StrType{public:// Assumptions:// InType is a data type consisting of the following constants:// ALPHA: only alphabetic characters are stored;// ALPHA_NUM: only alphanumeric characters are stored;// NON_WHITE: all nonwhitespace characters are stored;// NOT_NEW: all characters excluding the newline character// are stored.// If skip is true, characters not allowed are skipped until the// first allowed character is found. Reading and storing// begins with this character and continues until a character// not allowed is encountered. This character is read but not// stored. If skip is false, reading and storing begins with// the current character in the stream.

Page 53: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

void MakeEmpty();void GetString(bool skip, InType charsAllowed);// Post: If the number of allowable characters exceeds// MAX_CHARS, the remaining allowable characters have// been read and discarded.

Page 54: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

void GetStringFile(bool skip, InType charsAllowed,std::ifstream& inFile);// Post: If the number of allowable characters exceeds// MAX_CHARS, the remaining allowable characters have been// read and discarded.

void PrintToScreen(bool newLine);void PrintToFile(bool newLine, std::ofstream& outFile);int LengthIs();void CopyString(StrType& newString);private:char letters[MAX_CHARS + 1];

};

Page 55: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

void StrType::MakeEmpty()// Post: letters is empty string.{

letters[0] = '\0';}

Page 56: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

GetStringFile(Boolean skip, InType charsAllowed, ifstream& inFile)

#include <cctype>// Prototypes of auxiliary functions./* Note: If skip is true, nonallowable leading characters are skipped. If end-of-file is encountered while skipping characters, the empty string is returned. If the number of allowable characters exceeds MAX_CHARS, the rest are read and discarded.*/

void GetAlphaNum(bool skip, char letters[], std::ifstream& inFile);// Post: letters array contains only alphanumeric characters.void GetAlpha(bool skip, char letters[], std::ifstream& inFile);// Post: letters array contains only alphabetic characters.void GetNonWhite(bool skip, char letters[], std::ifstream& inFile);// Post: letters array contains only nonwhitespace characters.void GetTilNew(bool skip, char letters[], std::ifstream& inFile);// Post: letters array contains everything up to newline character

Page 57: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

void StrType::GetStringFile(bool skip, InType charsAllowed,std::ifstream& inFile){

switch (charsAllowed){

case ALPHA_NUM : GetAlphaNum(skip, letters, inFile);break;

case ALPHA : GetAlpha(skip, letters, inFile);break;

case NON_WHITE : GetNonWhite(skip, letters, inFile);break;

case NOT_NEW : GetTilNew(skip, letters, inFile);break;

}

}

Page 58: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• We can use the functions available in <cctype> to control our reading in each of the functions.

• If charsAllowed is ALPHA_NUM, we skip characters until the function isalnum returns true, and store them until isalnum returns false or inFile goes into the fail state.

• If charsAllowed is ALPHA, we skip characters until the function isalpha returns true, and store them until isalpha returns false or inFile goes into the fail state.

• If charsAllowed is NON_WHITE, we skip characters until the function isspace returns false, and store them until isspace returns true or inFile goes into the fail state.

• If charsAllowed is NOT_NEW, we skip characters until the character is not '\n', and store them until the character is '\n' or inFile goes into the fail state.

Page 59: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

void GetAlphaNum(bool skip, char letters[], std::ifstream& inFile){using namespace std;char letter;int count = 0;if (skip) {// Skip non-alphanumeric characters.

inFile.get(letter);while (!isalnum(letter) && inFile)

inFile.get(letter);

}else

inFile.get(letter);

Page 60: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

if (!inFile || !isalnum(letter))// No legal character found; empty string returned.letters[0] = '\0';

else{// Read and collect characters.

do{letters[count] = letter;count++;inFile.get(letter);} while (isalnum(letter) && inFile && (count < MAX_CHARS));

Page 61: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

letters[count] = '\0';// Skip extra characters if necessary.if (count == MAX_CHARS && isalnum(letter))

do{

inFile.get(letter);} while (isalnum(letter) && inFile);

}}

Page 62: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

void GetTilNew(bool skip, char letters[], std::ifstream& inFile){using namespace std;char letter;int count = 0;if (skip)

{// Skip newlines.inFile.get(letter);while ((letter == '\n') &&inFile)inFile.get(letter);}

elseinFile.get(letter);

Page 63: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

if (!inFile || letter == '\n')letters[0] = '\0';

else{// Read and collect characters.

do {letters[count] = letter;count++;inFile.get(letter);} while ((letter != '\n') && inFile && (count < MAX_CHARS));letters[count] = '\0';// Skip extra characters if necessary.if (count == MAX_CHARS && letter != '\n')

do {inFile.get(letter);} while ((letter != '\n') && inFile);

}}

Page 64: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

GetString

• This operation is nearly identical to GetStringFile, with inFile changed to cin.

Page 65: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

void StrType::PrintToScreen(bool newLine)// Post: letters has been sent to the output stream.{using namespace std;if (newLine)

cout << endl;cout << letters;}

Page 66: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

• PrintToFile is nearly identical to PrintToScreen, with cout replaced with outFile.

Page 67: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

#include <cstring>void StrType::CopyString(StrType& newString)// Post: letters has been copied into newString.letters.{

std::strcpy(newString.letters, letters);}int StrType::LengthIs()// Post: Function value = length of letters string{

return std::strlen(letters);}

Page 68: Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate

Test Plan