Data Structure Data Structure Software College Northeastern Universit y 1 Data Structure in C Data Structure in C ++ ++ February 2009 February 2009 software college software college Northeastern University Northeastern University
Dec 27, 2015
Data StructureData Structure Software College Northeastern University1
Data Structure in CData Structure in C ++ ++
February 2009February 2009
software college software college Northeastern UniversityNortheastern University
Data Structure Software College Northeastern University 2
Why Study Data structureWhy Study Data structure Using Computer
Want it to go faster ? Process more data? Want it to do something that would
otherwise be impossibleTechnology improves things by a constant
factor But might be costly Good algorithmic design can do much better
and might be cheap Supercomputer cannot rescue a bad
algorithm
Data Structure Software College Northeastern University 3
Why Study Data StructureWhy Study Data Structure
Present commonly used data
structureIntroduce the idea of tradeoff;
reinforce the concept of costs and
benefits associated with every
data structure.Measure the effectiveness of a
data structure or algorithm.
Data Structure Software College Northeastern University 4
What will you learn?
What are some of the common data structures
What are some ways to implement them
How to analyze their efficiencyHow to use them to solve
practical problems
Data Structure Software College Northeastern University 5
PrerequisitesPrerequisites
four important ideas Iteration - Do, While, Repeat Data Representation - variables and pointers
-class definition Subprograms and Recursion - modular design and abstraction Inheritance
discrete math
Data Structure Software College Northeastern University 6
ApproachApproach
Programming by yourself imitate - implementation of data structure such as
list etc. realize simple application - develop application using above Design large projects - project
Language C ++ (Visual C ++)
Data Structure Software College Northeastern University 7
Course OrganizationCourse Organization
Chapter 1 IntroductionChapter 2 ArrayChapter 3 ListsChapter 4 Stacks, and QueuesChapter 5 RecursionChapter 6 TreesChapter 7 Priority QueuesChapter 8 SearchingChapter 9 SortingChapter 10 Graph Algorithms
Data Structure Software College Northeastern University 8
Course work and GradingCourse work and Grading
Programming assignments 20%Written exercises 10% Exams:
Closed book Final 70%
Data Structure Software College Northeastern University 9
Course MaterialsCourse Materials
TextBook
- Data Structures and Algorithm Analysis in C++
(second Edition) Mark Allen Weiss
POSTS & TELECOM PRESS - 《数据结构与算法分析》( C 语言描述) Mark Allen Weiss 冯舜玺译 China Machine Press
Data Structure Software College Northeastern University 10
Course MaterialsCourse Materials
Recommended readings: Herbert Schildt, C++: The Complete Herbert Schildt, C++: The Complete
Reference, Fourth Edition,Reference, Fourth Edition, 周志荣等译周志荣等译 ,电子,电子工业出版社出版工业出版社出版
殷人昆,陶永雷等编著:《数据结构》(用面向对象方法与 C++描述),清华大学出版社, 2005.11
殷人昆,徐孝凯编著:《数据结构习题解析》(用面向对象方法与 C++描述) 清华大学出版社, 2005.7
Data Structure Software College Northeastern University 11
Course MaterialsCourse Materials
Useful link: Dictionary of Algorithms and Data
Structures http://www.nist.gov/dads/ Download from http://www.163.com Email :
[email protected] Password : datastructure From :网易网盘
Data StructureData Structure Software College Northeastern University12
Chapter 1Chapter 1
IntroductionIntroduction
Data Structure Software College Northeastern University 13
OverviewOverview
What’s the course about
The goal to study Data structure
Concepts about data structure
Review of C++
Algorithm
Performance and analysis of algorithm
Data Structure Software College Northeastern University 14
Before we go into details, what is a data structure exactly?
Input
Some mysterious processing
Output
What is a computer program?
What’s the course about
Program = Data structure + Algorithm
Data Structure Software College Northeastern University 15
An Example of programAn Example of program
Network connectivity Nodes
Add Connections between pairs of nodes
Is there a path from Node A to Node B?
V0
V2
V3
V1
V5
V4
V7
V6
Data Structure Software College Northeastern University 16
An Example of programAn Example of program
in out evidence 3 4 3 4 4 9 4 9 8 0 8 0 2 3 2 3 5 6 5 6 2 9 (2–3–4-9) 5 9 5 9 7 3 7 3 4 8 4 8 5 6 (5-6) 0 2 (2–3-4–8-0) 6 1 6 1
Data Structure Software College Northeastern University 17
Union–find AbstractionUnion–find Abstraction What are critical operations we needs to support?
N Objects
– city
FIND:test whether two objects are in the same set
-Is there a connection between A and B
UNION:merge two sets
-add a connection
Design efficient data structure to store connectivity information and algorithms of FIND and UNION
Number of objects and operations can be huge
Data Structure Software College Northeastern University 18
Another Example-Image Another Example-Image ProcessingProcessing
Find connected component Read in a 2D color image and find
regions of connected pixels that have the same color
Data Structure Software College Northeastern University 19
Another Example-Image Another Example-Image ProcessingProcessing
Find connected component Read in a 2D color image and find regions of
connected pixels that have the same color One pass algorithm
Initialize each pixels to be its own component Examine pixels from left to right and top to
down
-if a neighboring cell is the same color, merge current cell into the same component
Data Structure Software College Northeastern University 20
Another Example-Image Another Example-Image ProcessingProcessing
Data Structure Software College Northeastern University 21
ObjectsObjects
Elements are arbitrary objects in a network Pixels in a digital photo Computers in a network Transistors in a computer chip Web pages on the Internet When programming, convenient to name
them 0 to N-L When drawing,fun to use animals
Data Structure Software College Northeastern University 25
Quick-find AlgorithmQuick-find Algorithm
Data structure Maintain array id[] with name for each component If p and q are connected,then same id Initialize id[i] = i
FIND.to check if p and q are connected,check if they have the same id
UNION. To merge components containing p and q ,change all entries with id[p] to id[q]
analysis FIND takes constant number of operations UNION takes time proportional to N
Data Structure Software College Northeastern University 26
Another Example: Selection Another Example: Selection ProblemProblem
Description: Given a group of N numbers, determine the kth largest, where N > k .
Solution 1:
(1) read N numbers into an array,
(2) sort the array in decreasing order by some simple algorithm (such as bubblesort),
(3) return the element in position k.
Data Structure Software College Northeastern University 27
Another Example:Selection Another Example:Selection ProblemProblem
Solution 2:(1) read the first k elements into an array
and sort them in decreasing order, (2) each remaining element is read one by
one,(2.1) If it is smaller than the kth element in the array, it is ignored;(2.2) Otherwise, it is placed in its correct spot in the array, bumping one element out of the array.
(3) the element in the kth position is returned as the answer
Data Structure Software College Northeastern University 28
Another Example:Selection Another Example:Selection ProblemProblem
Which solution is better when (1) N ~ k (2) N » k ?
why?
Data Structure Software College Northeastern University 29
What do these examples What do these examples illustrate?illustrate?
more than one solutions to a given problem
Each solution has it’s advantages and disadvantages
- time - spacePerformance of AlgorithmsAlways ask: How can we do better?
Data Structure Software College Northeastern University 30
Data Structure?Data Structure?
Data structures Representations of data/Methods of
organizing data usually in memory, for better
algorithm efficiency The data structure selected has a great effect on the details and the efficiency of the algorithm.The algorithm chosen to solve a particular programming problem helps to determine which data structure should be used.
Data Structure Software College Northeastern University 32
student table
学 号 姓 名 性别 籍 贯 出生年月
1 98131 刘激扬 男 北 京 1979.12
2 98164 衣春生 男 青 岛 1979.07
3 98165 卢声凯 男 天 津 1981.02
4 98182 袁秋慧 女 广 州 1980.10
5 98203 林德康 男 上 海 1980.05
6 98224 洪 伟 男 太 原 1981.01
7 98236 熊南燕 女 苏 州 1980.03
8 98297 宫 力 男 北 京 1981.01
9 98310 蔡晓莉 女 昆 明 1981.02
10 98318 陈 健 男 杭 州 1979.12
Data Structure Software College Northeastern University 33
Course tableCourse table
课程编号 课 程 名 学时 024002 程序设计基础 64 024010 汇编语言 48 024016 计算机原理 64 024020 数据结构 64 024021 微机技术 64 024024 操作系统 48 024026 数据库原理 48
Linear structure
Data Structure Software College Northeastern University 34
UNIX file systemUNIX file system
/ (root)
bin lib user etc
math ds sw yin tao xie
Stack.cppQueue.cpp Tree.cpp
Data Structure Software College Northeastern University 36
Concepts about Data Concepts about Data StructureStructure
Data: everything can store in computers
课程编号 课 程 名 学时 024002 程序设计基础 64 024010 汇编语言 48 024016 计算机原理 64 024020 数据结构 64 024021 微机技术 64 024024 操作系统 48 024026 数据库原理 48
data element
data item
data object
Data Structure Software College Northeastern University 37
Concepts about Data Concepts about Data StructureStructure
Data structures data object relations of each other Data_Structure = {D, R}
relation nothing -set 1 to 1 -linear structure 1 to N -tree N to N -graph or network
Logical structure
Data Structure Software College Northeastern University 38
Concepts about Data Concepts about Data StructureStructure
representations of data structure in memory representations of data object representations of relations
representation of relations static dynamic
physical structure
Data Structure Software College Northeastern University 39
Concepts about Data Concepts about Data StructureStructure
Data type ADT - abstract data type A set of objects together with a set of
operations. It is mathematical abstraction ADT { data object; relations of data element; operations; }.
Data Structure Software College Northeastern University 40
Concepts about Data Concepts about Data StructureStructure
ADT NaturalNumber is Objects:An ordered subrange of the
integer,starting at zero and ending at the maximum integer(MAXINT) on the computer.
Functions:Boolean Is_Zero();Not_num Zero() ;Add(x,y)Equal(x,y)
Data Structure Software College Northeastern University 41
Concepts about Data Concepts about Data StructureStructure
The use of ADT divides the programming task into two steps: Implement the operations of the
ADT, choose a particular data structure to represent the ADT, and write the functions to implement the operations.
Write the main program which calls the functions of the ADT.
.
Data Structure Software College Northeastern University 42
ADT
select insert delete update
Records of student
Data Structure Software College Northeastern University 43
Something about C++
Dynamic Memory Management Shallow copy and deep copy Template Inheritance Exception Handling Design pattern STL
Data Structure Software College Northeastern University 44
Dynamic Memory ManagementDynamic Memory Management
C: malloc() and free()C++: new and delete
Data Structure Software College Northeastern University 45
Dynamic Memory ManagementDynamic Memory Management
Memory Allocation The new operator works for all data
types int* i_ptr = new int;char* c_ptr = new char;float* f_ptr = new float;double* d_ptr = new double;string* str_ptr = new string;
// Dynamically allocate an array of size 100float* ptr1 = new float[100];// Prompt the user for the size of the second arrayint size = 0; cin >> size;float* ptr2 = new float[size];
Data Structure Software College Northeastern University 46
Dynamic Memory ManagementDynamic Memory Management
Objects can also be dynamically allocated.
The new operator, in addition to allocating the memory for an object, will call a constructor for the object.
Data Structure Software College Northeastern University 47
#include <iostream>#include <cstdlib>using namespace std;class my_class {private: int x;public: my_class() : x(0) {} my_class(int p) : x(p) {} int value() { return x;}};
int main(int argc, char* argv[])
{ // Allocate a single object
my_class* ptr1 = new my_class(4);
// Allocate an array of objects
my_class* ptr2 = new my_class[10];
cout << ptr1->value() << endl;
cout << ptr2->value() << endl;
return EXIT_SUCCESS;
}
Data Structure Software College Northeastern University 48
Dynamic Memory ManagementDynamic Memory Management
Memory Allocation The new operator always returns a
memory address.
// Allocate a single integerint* ptr = new int;*ptr = 10;cout << "Address: " << ptr << endl;cout << "Value: " << *ptr << endl;
Data Structure Software College Northeastern University 49
Dynamic Memory ManagementDynamic Memory Management
Memory Deallocation C++ does not have garbage collection. When an object that is allocated by
new is no longer referenced, the delete operation must be applied to the object ( through a pointer)
point * p = new point[100];point * p = new point[100]; …… …… delete delete [ ][ ] p; p;
Data Structure Software College Northeastern University 50
Shallow copy and deep copy
Internal data and external data struct Teacher
{ string *FirstName;
string *LastName;
int employeeNum; };
Shallow copy ---- copy the pointerDeep copy ----- copy the resource
that the pointer pointing to
Data Structure Software College Northeastern University 51
class Teacher { private: string *FirstName; string *LastName; int EmployeeNum; public: Teacher(); ……}
teacher s,t; If we didn’t define our own copy constructor and didn’t overload = operator. t = s; How will it do?
Shallow copy and deep copy
Data Structure Software College Northeastern University 52
Deep copy Deep copy :: A special copy A special copy constructor should be defined and constructor should be defined and overload = operatoroverload = operator
FirstNameLastNameEmployeeNum
FirstNameLastNameEmployeeNum
Data Structure Software College Northeastern University 53
overload = operatoroverload = operator
Teacher::Teacher(const Teacher &rhs) { this->FirstName=new string(*(rhs.FirstName)); this->LastName= new string(*(rhs.LastName)); this->EmployeeNum = rhs.EmployeeNum; }Teacher & Teacher:: operator= (const Teacher & rhs) { *(this->FirstName) = *(rhs.FirstName); *(this->LastName)= *(rhs.LastName); this->EmployeeNum = rhs.EmployeeNum; }
Data Structure Software College Northeastern University 54
Templates
Many algorithms are type independent. We will describe how type-independent algorithms are written in C++ using the template. Template Functions Template Classes
Data Structure Software College Northeastern University 55
Template Functions
Template functions allow the programmer to create functions independent of the data types of their parameters. Function overloading
int max(int x, int y){ return x < y ? y : x;} float max(float x, float y) { return x < y ? y : x;} string max(string& x, string& y) { return x < y ? y : x;}
A template function template <class T> T my_max(T x, T y) { return x < y ? y : x; }
Data Structure Software College Northeastern University 56
Template function : Insertion sort Template function : Insertion sort fuctionfuction
template <class comparable>void insertionSort(vector <comparable> & a){ for(int p = 1;p<a.size();p++) { comparable tmp = a[p]; int j; for(j = p;j> 0 && tmp < a[j-1];j--) a[j] = a[j-1]; a[j] = tmp; }}
int main() { vector<int> array; int x; cout<< “Enter items to sort:”<<endl; while(cin >> x){ array.push_back(x);} insertionSort(array); cout<<“Sorted items are:”<<endl; for(int i = 0;i<array.size();i++) cout<<array[i]<<endl; return 0;}
Data Structure Software College Northeastern University 57
Function Objects
template <class comparable>const comparable& findMax(vector <comparable> & a){ int maxIndex = 0 ; for(int i = 1;i < a.size() ; i++)
if(a[maxIndex] < a[i]) maxIndex = i; return a[i];}
The function The function templatetemplate
It works only for It works only for objects that objects that
have an have an operator < operator <
function function defineddefined
For the class Teacher, sometimes we want to compare by Firstname, sometimes by Lastname.
How to do?
Data Structure Software College Northeastern University 58
Function objects
The solution is to pass the comparison function as a second parameter to findMax
findMax use the fuction of paramenter instead of the existence of an operator<.
findMax will now have two parameters: a vector of Object and a comparison function.
Function object – funtor –often contains no data but does contain a single method with a given name specified by the generic algorithm.
Data Structure Software College Northeastern University 59
Function objectFunction objecttemplate <class object,class comparable>Const object& findMax(const vector <object> & a, comparable isLessThan){ int maxIndex = 0 ; for(int i = 1;i < a.size() ; i++)
if( isLessThan(a[maxIndex] , a[i]) ) maxIndex = i; return a;}
if(isLessThan.operator()(a[maxIndex],a[i]) )if(isLessThan.operator()(a[maxIndex],a[i]) )
class LessThanByFirstNameclass LessThanByFirstName{{ public public Bool operator ( ) (const Teacher &lhs,Bool operator ( ) (const Teacher &lhs, const Teacher &rhs) constconst Teacher &rhs) const { return lhs.FirstName < rhs.FirstName; }{ return lhs.FirstName < rhs.FirstName; }};};main()main(){ vector<Teacher> a; { vector<Teacher> a; cout << findMax(a,cout << findMax(a,LessThanByFirstNameLessThanByFirstName())<<endl;())<<endl;}}
Data Structure Software College Northeastern University 60
The template class The template class dataList dataList #ifndef DATALIST_H #define DATALIST_H #include <iostream.h> template <class Type> class dataList { private: Type *Element; int ArraySize; void Swap (const int m1, const int m2); int MaxKey (const int low, const int high);
Template class
declaration
Template Classes
Data Structure Software College Northeastern University 61
public: dataList (int size = 10) : ArraySize (size), Element (new Type [Size]) { } ~dataList ( ) {delete [ ] Element;} void Sort ( ); friend ostream& operator << (ostream& outStream, const datalist<Type>& outList); friend istream& operator >> (istream& inStream, const datalist<Type>& inList); }; #endif
Template Classes
Data Structure Software College Northeastern University 62
dataListdataList 类中所有操作作为模板函数的实现类中所有操作作为模板函数的实现
template <class Type> void dataList <Type>:: Swap (const int m1, const int m2) { //// 交换由交换由 mm1, 1, mm22 为下标的两个数组元素的值为下标的两个数组元素的值 Type temp = Element [m1]; Element [m1] = Element [m2]; Element [m2] = temp; }
Data Structure Software College Northeastern University 63
template <class Type>
int dataList<Type>::
MaxKey (const int low, const int high) {
//// 查找数组查找数组 ElementElement[[lowlow]]~~ ElementElement[[highhigh]] 中中 //// 的最大值,函数返回其位置的最大值,函数返回其位置 int max = low;
for (int k = low+1, k <= high, k++)
if ( Element[max] < Element[k] )
max = k;
return max;
}
Data Structure Software College Northeastern University 64
template <class Type>
ostream&operator<< (ostream& OutStream, const dataList<Type> OutList) { OutStream << “Array Contents : \n”; for (int i = 0; i < OutList.ArraySize; i++) OutStream << OutList.Element[i] << ‘ ’; OutStream << endl; OuStream << “Array Current Size : ” << OutList.ArraySize << endl; return OutStream; }
Data Structure Software College Northeastern University 65
template <class Type> istream& operator >> (istream& InStream, dataList<Type> InList) { //// 输入对象为输入对象为 InListInList ,,输入流对象为输入流对象为 InStreamInStream cout << “Enter array Current Size : ”; Instream >> InList.ArraySize; cout << “Enter array elements : \n”; for (int i = 0; i < InList.ArraySize; i++) { cout << “Elememt” << i << “:” ; InStream >> InList.Element[i]; } return InStream; }
Data Structure Software College Northeastern University 66
template <class Type>
void dataList<Type>::Sort ( ) {
//// 按非递减顺序对按非递减顺序对 ArraySizeArraySize 个关键码个关键码 ////ElementElement[0][0]~~ ElementElement[[ArraySize-ArraySize-1]1] 排序。排序。 for ( int i = ArraySize -1; i > 0; i-- ) {
int j = MaxKey (0, i);
if ( j != i ) swap (j, i);
}
}
#endif
Data Structure Software College Northeastern University 67
Inheritance and derivation
we can build a new class by deriving the new class from an already existing class.
The mechanism for this derivation is inheritance, and the derived class is said to be inherited from the original class.
Base classParent class
Derived classChild class
Data Structure Software College Northeastern University 68
Inheritance and derivation
Syntax:class DerivedClass : Inheritance Mode
BaseClass{public:
//……protected:
//……private:
//……};
Data Structure Software College Northeastern University 69
Example– class PolygonExample– class Polygonclass Polygon { class Polygon { public:public: Polygon( Point ); Polygon( Point ); void move( Point );void move( Point ); void isInside( Point );void isInside( Point ); Point referencePoint( );Point referencePoint( ); virtual void draw ( ) = 0;virtual void draw ( ) = 0;private:private: Point referencePoint;Point referencePoint;};};
Pure virtual function
class Rectangle: public Polygonpublic Polygon {
public:
Rectangle( Point,Point );
int isInside( Point );
void draw( );
private:
Point vertex2;
};
Public inherite
Data Structure Software College Northeastern University 70
Corresponding interface define:Corresponding interface define:class Rectangle { class Rectangle { public:public: Rectangle( Point,Point ); Rectangle( Point,Point ); Point referencePoint( );Point referencePoint( ); int isInside( Point );int isInside( Point ); void move( Point );void move( Point ); void draw( );void draw( );private:private: Point vertex2;Point vertex2;};};
Note: private member
doesn’t be inherited
Data Structure Software College Northeastern University 71
Public, Private, and Protected InheritanceProtected Inheritance
can’t accessprivateprivate
privateprivateprotected
privateprivatepublic
can’t accessprotectedprivate
protectedprotectedprotected
protectedprotectedpublic
can’t accesspublicprivate
protectedpublicprotected
publicpublicpublic
in derived classinheritance mode members in base class
Data Structure Software College Northeastern University 72
Public, Private, and Protected InheritanceCompatible RulesAn object of derived class, which is public inherited
from the base class, can be use as a object of base class:
A) Objects of base class can be assigned by the object of derived class.
B) References of base class can be initialized by the object of derived class.
C) Pointers of base class can be assigned by the pointer of derived class.
D) Only members of base class can be accessed by the pointers and objects of base class.
Data Structure Software College Northeastern University 73
Transformation of the pointer of derived class
class Point {class Point {friend ostream &operator <<(ostream &, const Point &) friend ostream &operator <<(ostream &, const Point &) public:public: Point( float = 0, float = 0 ); Point( float = 0, float = 0 ); void setPoint ( float, float );void setPoint ( float, float ); float getX () const { return x; }float getX () const { return x; } float getY () const { return y; }float getY () const { return y; }protected:protected: float x, y;float x, y;};};
class Circle:public Point { friend ostream &operator<< (ostream &, const Circle &); public: Circle ( float r = 0.0, float x = 0, float y =0 ); void setRadius ( float ); float getRadius ( ) const; float area( ) const;protected: float radius;};
Data Structure Software College Northeastern University 74
main()main(){{ Point * pointPtr, p ( 3.5, 5.3 );Point * pointPtr, p ( 3.5, 5.3 ); Circle * circlePtr, c ( 2.7, 1.2, 8.9 );Circle * circlePtr, c ( 2.7, 1.2, 8.9 ); cout <<“Point p”<<p<<“\n Circle c:”<<c<<endl;cout <<“Point p”<<p<<“\n Circle c:”<<c<<endl; pointPtr = &c; pointPtr = &c; // the object of derived class is assigned to // the object of derived class is assigned to
// the pointer of base class// the pointer of base class circlePtr = ( Circle * ) pointPtr; circlePtr = ( Circle * ) pointPtr; //the pointer of base class is //the pointer of base class is
transformed to the pointer of derived classtransformed to the pointer of derived class cout <<“\n Area of c(via circlePtr):”<<circlePtr->area ( ) << cout <<“\n Area of c(via circlePtr):”<<circlePtr->area ( ) <<
endl;endl; pointPtr = &p;pointPtr = &p; circlePtr = ( Circle * ) pointPtr;circlePtr = ( Circle * ) pointPtr; cout <<“\n Radius of object circlePtr point to:”<< circlePtr-cout <<“\n Radius of object circlePtr point to:”<< circlePtr-
>getRadius ( ) << endl;>getRadius ( ) << endl; return 0;return 0;}}
Data Structure Software College Northeastern University 75
virtual functionIn the In the static bindingstatic binding, the decision , the decision
about which function to use to about which function to use to resolve an overload is made at resolve an overload is made at compile time.compile time.class A {
public : void f( ){ cout << “it is in a”<<endl;}}class B: public A { public : void f( ){ cout << “it is in b”<<endl; } }
1 A a;2 B b;3 …4 a.f(); b.f();
Here, the determination of which f()is used in the two calls at line4 is
computable at compile time
Data Structure Software College Northeastern University 76
virtual function
If a member function is If a member function is declared to be declared to be virtualvirtual, , dynamic bindingdynamic binding is used. The is used. The decision about which function to use to decision about which function to use to resolve an overload is made resolve an overload is made at run at run timetime, if it cannot be determined at , if it cannot be determined at compile timecompile time
Virtualness is inherited. Virtualness is inherited. It is override but not overload.It is override but not overload.A run-time decision is needed A run-time decision is needed onlyonly
when an object is accessed through a when an object is accessed through a pointer or referencepointer or reference to a base class. to a base class.
Data Structure Software College Northeastern University 77
Example:Example:#include <iostream>#include <iostream>using namespace std;using namespace std;class B0class B0 // the declaration of base class B0// the declaration of base class B0{public:{public: // public methods// public methods
virtualvirtual void void displaydisplay() () // virtual member function// virtual member function {cout<<"B0::display()"<<endl;} {cout<<"B0::display()"<<endl;}
};};class B1: public B0class B1: public B0 //public derivation//public derivation{ public:{ public: void void display()display() { cout<<"B1::display()"<<endl; } { cout<<"B1::display()"<<endl; }};};class D1: public B1class D1: public B1 //public derivation//public derivation{ public:{ public:
void void displaydisplay() { cout<<"D1::display()"<<endl; }() { cout<<"D1::display()"<<endl; }};};
Data Structure Software College Northeastern University 78
void fun(B0 *ptr)void fun(B0 *ptr) //// a simple function outside classesa simple function outside classes{ { ptr->display();ptr->display(); } }void main()void main() //main function//main function{{ B0 b0, *p;B0 b0, *p; //declare object and pointer of base class B0//declare object and pointer of base class B0
B1 b1;B1 b1; //declare object of derived class B1//declare object of derived class B1D1 d1;D1 d1; //declare object of derived class D1//declare object of derived class D1p= p= &b0;&b0;fun(p);fun(p); // call the member function of base class B0// call the member function of base class B0p= &b1;p= &b1;fun(pfun(p););//call the member function of derived class B1//call the member function of derived class B1p= p= &d1;&d1;fun(p);fun(p); //call the member function of derived class D1//call the member function of derived class D1
}}
ResultResult :: B0::B0::display()display()
B1::B1::display()display()
D1::D1::display()display()
78
Data Structure Software College Northeastern University 79
Abstract Method (Pure Virtual Function) and Abstract Method (Pure Virtual Function) and Abstract Base ClassAbstract Base Class
A method is declared A method is declared abstract abstract by specifying that it is by specifying that it is virtualvirtual and by supplying and by supplying = 0= 0 in the interface in place of an in the interface in place of an implementation.implementation.
An abstract method An abstract method has no meaningful definitionhas no meaningful definition and is thus and is thus always defined in the derived class.always defined in the derived class.
A class that has at least one abstract method is called an A class that has at least one abstract method is called an abstract classabstract class
Because the behavior of an abstract class is not completely Because the behavior of an abstract class is not completely defined, defined, abstract classes can never be instantiatedabstract classes can never be instantiated. .
When a derived class fails to override an abstract method When a derived class fails to override an abstract method with an implementation, it with an implementation, it remains abstractremains abstract, and the , and the compiler reports an error if an attempt to instantiate the compiler reports an error if an attempt to instantiate the abstract derived class is made.abstract derived class is made.
Data Structure Software College Northeastern University 80
void display( Polygon & void display( Polygon & apoly )apoly ){{ …… …… aploy.draw( );aploy.draw( ); …… ……}}
PolygonPolygon void draw ( );void draw ( );
TriangleTriangle void draw ( );void draw ( );
RectangleRectangle void draw ( );void draw ( );
void display( Polygon & void display( Polygon & apoly )apoly ){{ …… …… aploy.draw( );aploy.draw( ); …… ……}}
PolygonPolygon virtual void draw( )=0;virtual void draw( )=0;
TriangleTriangle void draw ( );void draw ( );
RectangleRectangle void draw ( );void draw ( );
Static Static dynamic
Data Structure Software College Northeastern University 81
Exception HandlingException HandlingC++ exception handling centers around C++ exception handling centers around
the use of the try keyword and the the use of the try keyword and the catch keywordcatch keyword
The C++ Standard Library provides and The C++ Standard Library provides and uses a standard exception class uses a standard exception class hierarchy.hierarchy.
Head fileHead file
#include <stdexcept>#include <stdexcept>
int main(int argc, char* argv[]) {int main(int argc, char* argv[]) { try {try { if (argc != 2) { if (argc != 2) { cerr << "Usage: " << argv[0] << " num" << endl;cerr << "Usage: " << argv[0] << " num" << endl; return EXIT_FAILURE; return EXIT_FAILURE; } } int number_fib = atoi(argv[1]); int number_fib = atoi(argv[1]); calculate_fibonacci(number_fib); calculate_fibonacci(number_fib); return EXIT_SUCCESS; return EXIT_SUCCESS; } } catch (exception& e) {catch (exception& e) { cerr << e.what() << endl; cerr << e.what() << endl; } } return EXIT_FAILURE;return EXIT_FAILURE;} }
Data Structure Software College Northeastern University 82
In C++, a variable of In C++, a variable of any data typeany data type can be thrown as an exception using can be thrown as an exception using the throw statement. This differs the throw statement. This differs from from JavaJava where only objects or where only objects or descendents of type java.lang.descendents of type java.lang.
int main(int argc, char* argv[]) {int main(int argc, char* argv[]) { try {try { srand(time(0));srand(time(0)); if (rand() % 2 == 0) { if (rand() % 2 == 0) { throw out_of_range("out of range"); throw out_of_range("out of range"); } } else { else { throw length_error("length error");throw length_error("length error"); }} return EXIT_SUCCESS;return EXIT_SUCCESS; }} catch (catch (out_of_range eout_of_range e) {) { cerr << cerr << "caught out_of_range exception""caught out_of_range exception" << endl; << endl; } } catch (catch (length_error elength_error e) {) { cerr << cerr << "caught length_error exception""caught length_error exception" << endl; << endl; }} return EXIT_FAILURE;} return EXIT_FAILURE;}
Data Structure Software College Northeastern University 83
A function can transfer execution to a A function can transfer execution to a catch statement that exists in catch statement that exists in another function. This usually another function. This usually happens when a function (that happens when a function (that throws an exception) is called from throws an exception) is called from within a try block of another within a try block of another function.function.
void display_args(int argc, char* argv[]) {void display_args(int argc, char* argv[]) { if (argc == 1) {if (argc == 1) { throw runtime_error("Please supply arguments");throw runtime_error("Please supply arguments"); }} for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) { cout << argv[i] << endl;cout << argv[i] << endl; }}}}int main(int argc, char* argv[]) { int main(int argc, char* argv[]) { try { try { display_args(argc, argv); display_args(argc, argv); return EXIT_SUCCESS;return EXIT_SUCCESS; }} catch (runtime_error& e) { catch (runtime_error& e) { cerr << e.what() << endl;cerr << e.what() << endl; }} return EXIT_FAILURE;return EXIT_FAILURE; } }
Data Structure Software College Northeastern University 84
C++ provides a mechanism to catch C++ provides a mechanism to catch exceptions of all possible types.exceptions of all possible types.
int main(int argc, char* argv[]) {int main(int argc, char* argv[]) { try {try { if (argc == 1) {if (argc == 1) { throw runtime_error("runtime error");throw runtime_error("runtime error"); }} if (argc == 2) {if (argc == 2) { throw out_of_range("out of range");throw out_of_range("out of range"); }} if (argc == 3) {if (argc == 3) { throw length_error("length error");throw length_error("length error"); }} return EXIT_SUCCESS;return EXIT_SUCCESS; }} catch (...) {catch (...) { cerr << "An exception was caught!" << endl;cerr << "An exception was caught!" << endl; }} return EXIT_FAILURE;return EXIT_FAILURE;}}
all all possible possible exceptioexceptio
nn
Data Structure Software College Northeastern University 85
The C++ Standard Exception The C++ Standard Exception HierarchyHierarchy
Data Structure Software College Northeastern University 86
The C++ Standard Exception HierarchyThe C++ Standard Exception Hierarchy
class DivideByZeroException : public logic_error {public: DivideByZeroException(const string& what_arg
) throw() : logic_error ("divide by zero: " + what_arg)
{}};
Listing 6 Extending the standard exception hierarchy
Data Structure Software College Northeastern University 87
Design PatternsDesign Patterns A design pattern describes a problem that A design pattern describes a problem that
occurs over and over in software occurs over and over in software engineering and then describes the solution engineering and then describes the solution in a sufficiently generic manner as to be in a sufficiently generic manner as to be applicable in a wide variety of contexts. applicable in a wide variety of contexts.
Like a recipe,design patterns usually Like a recipe,design patterns usually follow a format. Typically, a design follow a format. Typically, a design pattern consists ofpattern consists of pattern namepattern name problemproblem solutionsolution consequencesconsequences
Data Structure Software College Northeastern University 88
Terms:Terms:
wrapperwrapper(( 包装器包装器 )) adapteradapter (适配器)(适配器) containercontainer (容器)(容器) iteratoriterator (迭代器)(迭代器)
Data Structure Software College Northeastern University 89
WrappersWrappers ::A wrapper class stores a primitive type A wrapper class stores a primitive type
and adds operations that the primitive and adds operations that the primitive
type does not support or does not type does not support or does not
support correctly.support correctly.
Adapters Adapters ::An adapter class is used when the An adapter class is used when the
interface of a class is not exactly what interface of a class is not exactly what
is needed.is needed.
Difference between wrappers and Difference between wrappers and
adaptersadapters :: an adapter is implemented by an adapter is implemented by
private inheritance.private inheritance.
包装器有几种不同的实现方法包装器有几种不同的实现方法
Data Structure Software College Northeastern University 90
template <class Comparable>template <class Comparable>class Pointerclass Pointer{{ public:public:
explicit Pointer(Comparable *rhs =NULL):pointee(rhs) explicit Pointer(Comparable *rhs =NULL):pointee(rhs) { }{ } bool operator<(const Pointer &rhs) constbool operator<(const Pointer &rhs) const {return (*pointee < *(rhs.pointee)); }{return (*pointee < *(rhs.pointee)); } operator Comparable*() constoperator Comparable*() const {return pointee; }{return pointee; } Comparable * get() constComparable * get() const {return pointee;}{return pointee;}
private :private : Comparable * pointee;Comparable * pointee; };};
The wrapper converse the The wrapper converse the compare between pointers compare between pointers
to the compare of to the compare of resources the pointers resources the pointers
pointpoint Overload Overload operater < operater <
The method for The method for type conversetype converse
Example of wrapperExample of wrapper
Data Structure Software College Northeastern University 91
template <class Object>template <class Object>Class MemoryCellClass MemoryCell{{public:public: explicit MemoryCell(const Object & initValue = explicit MemoryCell(const Object & initValue = Object( ) );Object( ) ); const Object & read() const ;const Object & read() const ; void write(const Object & x);void write(const Object & x);private:private: Object storeValue;Object storeValue;}}
template <class Object>template <class Object>Class StorageCellClass StorageCell:private MemoryCell<Object>:private MemoryCell<Object>{{public:public: explicit StorageCell(const Object & initialValue = explicit StorageCell(const Object & initialValue = Object( ))Object( )) :MemoryCell<Object>(initialValue) {}:MemoryCell<Object>(initialValue) {} const Object & get() const const Object & get() const {return read( ) ;}{return read( ) ;} void put(const Object & x)void put(const Object & x) {write(x); }{write(x); }}}
Through private Through private inheritance, the public inheritance, the public
member function change to member function change to private functionprivate function
Adapter --- change the interfaceAdapter --- change the interface
Data Structure Software College Northeastern University 92
iiteratorterator
an iterator is an iterator is an objectan object that allows us that allows us
to to iterate iterate through all objects in a through all objects in a
collection.collection.
exampleexample :: forfor(( int i=0int i=0;; i<v.size();i++)i<v.size();i++)
cout<<v[i]<<endl;cout<<v[i]<<endl;
In this loop, i is an iterator. But we can In this loop, i is an iterator. But we can
store the collection only in an arraylike store the collection only in an arraylike
structure.structure.A A flexible alternativeflexible alternative :: design an iterator class that design an iterator class that encapsulates a position inside a encapsulates a position inside a collectioncollection
Data Structure Software College Northeastern University 93
IIterator design 1terator design 1
template <class Object>template <class Object>Class VectorIteratorClass VectorIterator ;;template MyVector:public vector<object>template MyVector:public vector<object>{{public:public: explicit MyVector(int size = 0) :vector<object>(size) {}explicit MyVector(int size = 0) :vector<object>(size) {} VectorIterator<object> VectorIterator<object> getIterator() const getIterator() const {return {return VectorIterator<object>(thisVectorIterator<object>(this);});}}}
GetIterator()GetIterator() 返回一返回一个新迭代器个新迭代器
template <class Object>template <class Object>Class VectorIteratorClass VectorIterator{{public:public: VectorIteratorVectorIterator(const MyVector<Object>*v) :owner(v),count(0) {}(const MyVector<Object>*v) :owner(v),count(0) {} bool hasNext( ) constbool hasNext( ) const { return count != { return count != owner->size( );owner->size( ); } } const Object& next( )const Object& next( ) { return (*owner)[{ return (*owner)[count++];}count++];}private:private: const MyVector<Object> *owner;const MyVector<Object> *owner; int count;int count;} }
constconst 的的作用?作用?
Data Structure Software College Northeastern University 94
#include “MyVector.h”#include “MyVector.h”#include “VectorIterator.h”#include “VectorIterator.h”int main()int main(){{ MyVector<int> v;MyVector<int> v; v.push_back(3);v.push_back(3); v.push_back(2);v.push_back(2); cout << “Vector contents:” << endl;cout << “Vector contents:” << endl;
VectorIterator<int> itr = v.getIterator();VectorIterator<int> itr = v.getIterator(); while(itr.hasNext())while(itr.hasNext()) cout << itr.next() << endl;cout << itr.next() << endl; return 0;return 0;}}
问题:问题:如果基类不是如果基类不是VectorVector ,需,需要修改代码要修改代码
ListIterator<int> itr = v.getIterator();ListIterator<int> itr = v.getIterator();
Data Structure Software College Northeastern University 95
真实类型未知的真实类型未知的迭代器对象迭代器对象
Template <class Object>Template <class Object>Class IteratorClass Iterator{{public:public: virtual ~Iterator() { }virtual ~Iterator() { } virtual bool hasNext( ) const virtual bool hasNext( ) const = 0= 0;; virtual const Object & next() virtual const Object & next() = 0= 0;;}; };
IIterator Design 2 terator Design 2 :: IInheritance-Based nheritance-Based
IteratorsIterators
Data Structure Software College Northeastern University 96
真实类型未知的真实类型未知的迭代器对象迭代器对象
Template <class Object>Template <class Object>Class VectorIterator:public Iterator<Object>Class VectorIterator:public Iterator<Object>{{public:public: VectorIterator(const MyVector<object. * v)VectorIterator(const MyVector<object. * v) :owner(v),count(0) { }:owner(v),count(0) { } bool hasNext( ) constbool hasNext( ) const {return count != owner->size();} {return count != owner->size();} const Object & next()const Object & next() {return (*owner)[count++]; }{return (*owner)[count++]; }private:private: const MyVector<Object> *owner;const MyVector<Object> *owner; int count;int count;}; };
IIterator Design 2 terator Design 2 :: IInheritance-Based nheritance-Based
IteratorsIterators
Data Structure Software College Northeastern University 97
template <class Object>template <class Object>Class Iterator;Class Iterator;Template <class Object>Template <class Object>Class VectorIterator;Class VectorIterator;
Template <class Object>Template <class Object> Class MyVector:public vector<Object>Class MyVector:public vector<Object>{{public:public: explicit MyVector(intint size=0)explicit MyVector(intint size=0) :vector<Object>(size){}:vector<Object>(size){}
Iterator<Object> *Iterator<Object> * getIterator() const getIterator() const {return {return new VectorIterator<Object>(this);new VectorIterator<Object>(this); } }}}
真实类型未知的真实类型未知的迭代器对象迭代器对象
IIterator Design 2 terator Design 2 :: IInheritance-Based nheritance-Based
IteratorsIterators
Data Structure Software College Northeastern University 98
#include “MyVector.h”#include “MyVector.h”#include “VectorIterator.h”#include “VectorIterator.h”int main()int main(){{ MyVector<int> v;MyVector<int> v; v.push_back(3);v.push_back(3); v.push_back(2);v.push_back(2); cout << “Vector contents:” << endl;cout << “Vector contents:” << endl;
Iterator<int> *itr = v.getIterator();Iterator<int> *itr = v.getIterator(); while(itr.hasNext())while(itr.hasNext()) cout << itr.next() << endl;cout << itr.next() << endl; return 0;return 0;}}
ExpecteExpectedd
Data Structure Software College Northeastern University 99
containercontainer
containercontainer :: A container represents a group of A container represents a group of
objects, known as its elements. objects, known as its elements. Container is an object. Some Container is an object. Some implementations , such as vectors implementations , such as vectors and lists, are unordered; others, and lists, are unordered; others, such as sets and maps, are ordered.such as sets and maps, are ordered.
A container can contain another A container can contain another container.container.
Data Structure Software College Northeastern University 100
STL(Standard Template Library)STL(Standard Template Library)
The STL provides general-purpose components for common programming tasks. These components are characterized by their flexibility, efficiency, and theoretical soundness.
The library is organized into three main abstractions. ContainersContainers IteratorsIterators AlgorithmsAlgorithms
Data Structure Software College Northeastern University 101
ContainersContainers
The standard containers are available via The standard containers are available via the following header files, whose names the following header files, whose names indicate the corresponding container indicate the corresponding container type.type.
Standard ContainersStandard Containers 1: 1: #include#include <string><string> // strings // strings 2: 2: #include#include <vector><vector> // arrays // arrays 3: 3: #include#include <list> <list> // cyclic doubly linked lists // cyclic doubly linked lists 4: 4: #include#include <deque> <deque> // hybrid list/array // hybrid list/array 5: 5: #include#include <queue> <queue> // queue // queue 6: 6: #include#include <stack><stack> // stack // stack 7: 7: #include#include <bitset><bitset> // bit-vectors // bit-vectors 8: 8: #include#include <set><set> // general sets // general sets 9: 9: #include#include <map><map> // associative arrays // associative arrays
Data Structure Software College Northeastern University 102
Numerical calculation headersNumerical calculation headers
1: #include <complex> // complex numbers
2: #include <valarray> // numerical arrays
3: #include <numeric> // numerical algorithms
4:#include <cmath> // math functions
Some common STL extensionsSome common STL extensions
1: #include <hash_set> // hash set
2: #include <hash_map> // hash map
3: #include <slist> // singly linked list Listing
Data Structure Software College Northeastern University 103
IteratorsIterators provide a uniform interface
between containers and algorithms in the STL. Iterators are modeled after plain C++ pointers. // // Traversing a container using iterators Traversing a container using iterators
stringstring AA = "This is a string";= "This is a string"; string::iterator itstring::iterator it; ; // create iterator// create iterator forfor ((it it = = AA..beginbegin();(); it it !!= A.end= A.end(); ++(); ++itit)) { { cout cout << *<< *it it << << endlendl;; } }
Data Structure Software College Northeastern University 104
// Traversing a range// Traversing a range
forfor ((it it == A A..beginbegin();(); it it !=!= A A..beginbegin() +() + 10 10; ++; ++itit)) {{
cout cout << *<< *it it <<<< endl endl;;
}}
Iterators can be used to get access Iterators can be used to get access to a single item, but in sequential to a single item, but in sequential containers, they also provide containers, they also provide accessing to a rangeaccessing to a range of items.of items.
Data Structure Software College Northeastern University 105
Algorithms
The core of the STL is its extensive collection of algorithms. Since iterators form the interface between containers and algorithms in the STL, the algorithms are (for the most part) implemented as free functions.
Data Structure Software College Northeastern University 106
#include#include <string><string> #include#include <cstdlib><cstdlib>#include#include <iostream><iostream> #include#include <algorithm><algorithm>using namespace std;using namespace std;int mainint main((intint argc argc,, charchar** argv argv[])[]) { { string s = string s = "This is a test";"This is a test"; coutcout << << s s <<<< endl endl;; // replace spaces with hyphens // replace spaces with hyphens replacereplace((ss..beginbegin(),(), s s..endend(), '(), ' ',', '-');'-'); cout cout <<<< s s <<<< endl endl;; // reverse the string// reverse the string reversereverse(s.begin(), s.end()); (s.begin(), s.end()); cout cout <<<< s s <<<< endl endl;; returnreturn EXIT_SUCCESS EXIT_SUCCESS;; } }
Data Structure Software College Northeastern University 107
Problem Solving: Main StepsProblem Solving: Main Steps
1. Problem definition2. Algorithm design / Algorithm specification3. Algorithm analysis4. Implementation5. Testing6. [Maintenance]
Data Structure Software College Northeastern University 108
An algorithm is a clearly specified set of instructions a computer follows to solve a problem. It takes some value as input and produces some value as output.
An algorithm is a tool for solving a well-specified computational problem.
Describe: in natural language / pseudo-code / diagrams / etc.
What is an algorithm?
Data Structure Software College Northeastern University 109
Example – sorting problem Input: A sequence of n numbers: <a1, a2, …, an> Output: A permutation (reordering) <a1’, a2’, …,
an’> of the input sequence, such that a1’ ≤ a2’ ≤ … ≤ an’.
What is an algorithm?
Data Structure Software College Northeastern University 110
An algorithm satisfies the following criteria: Input: Zero or morequabtities Output: At least one quantity Definiteness( 确定性 ): Clarity, precision of each instruction Finiteness (有穷行) : Terminates after a number of steps Effectiveness (有效性) : Every Instruction must be basic enough
What is an algorithm?
Data Structure Software College Northeastern University 111
Performance Measure of Performance Measure of AlgorithmsAlgorithms
What do we measure? How do we measure?
.
Data Structure Software College Northeastern University 112
Performance Measure of Performance Measure of AlgorithmsAlgorithms
What do we measure? Correctness Readability Robustness efficiency
Data Structure Software College Northeastern University 113
int sign(int x)int sign(int x){int y;{int y; if(x >0)if(x >0) y = 1;y = 1; else ifelse if(x = 0)(x = 0) y = 0;y = 0; else else y = -1;y = -1; return y;return y;}}
Level 1
No compile error
Data Structure Software College Northeastern University 114
int sign(int x)int sign(int x)
{int y;{int y;
if(if(x >1x >1))
y = 1;y = 1;
else if(x == 0)else if(x == 0)
y = 0;y = 0;
else else
y = -1;y = -1;
return y;return y;
}}
Level 2representative input Input 100 ooutput 1Input 0 output 0Input -100 output -1
Data Structure Software College Northeastern University 115
int sign(int x)int sign(int x)
{int y;{int y;
if(x >= 1)if(x >= 1)
y = 1;y = 1;
else if(x == 0)else if(x == 0)
y = 0;y = 0;
else else
y = -1;y = -1;
return y;return y;
}}
Level 3Strict input
Input 100 output 1
Input 1 output 1Input 0 output 0Input -1 output -1Input -100 output -1
Data Structure Software College Northeastern University 116
int sign(int x)int sign(int x)
{int y;{int y;
if(x > 0)if(x > 0)
y = 1;y = 1;
else if(x == 0)else if(x == 0)
y = 0;y = 0;
else else
y = -1;y = -1;
if(x ==0x1234)if(x ==0x1234)
y= 0;y= 0;
return y;return y;
}}
Level 4Every input
Data Structure Software College Northeastern University 117
Performance Measure of Performance Measure of AlgorithmsAlgorithms
Efficiency Little main memory Little time Little interaction delay Few disk access Few resources
In other words,we measure Time(how fast) Space(how small)
Data Structure Software College Northeastern University 118
Performance Measurement of Performance Measurement of AlgorithmsAlgorithms
Efficiencies relative;Not absolute
3 complementary methods Empirical: use real-world data with an
implemented system. simulational: use simulated data with an
implemented system or with a model system
Analytical: use theoretic-model data with a theoretic-model system
Data Structure Software College Northeastern University 119
Performance:Performance:Analytical MeasurementMeasurement
Example:Example:lineline float sum ( float a[ ], const int n ) 1 { 2 float s = 0.0; 3 for ( int i = 0; i < n; i++ ) 4 s += a [i]; 5 return s; 6 }Number of instructions: 2n+3Extre memory: a float s
Data Structure Software College Northeastern University 120
Performance:Performance:Analytical MeasurementMeasurement
Abstract time
-number of crucial instructions Most of time spent on a few crucial instruction Consider one instruction takes one unit of time Ignore most of the program’s instructions,
incrementing indices
Abstract space
-crucial requests for memory during execution
Data Structure Software College Northeastern University 121
Performance:Performance:Analytical MeasurementMeasurement
Relationship of input-data size,size, to abstract execution time
f:size →time Relationship of input-data size to abstract
space usage
f:size →space Size of data can be
number of elements in a matrix
number of elements to be sorted
Data Structure Software College Northeastern University 122
Performance:Performance:Analytical MeasurementMeasurement
Advantages: independent of Specific programming language Specific computer system
Disadvantages:
Difficulty: different “kinds” of data of the same size Which do we consider?
Worse
average
best
Data Structure Software College Northeastern University 123
Mathematical BackgroundMathematical Background
DEFINITION:
T(N) = O(f(N))
If there are positive constants c and n0 such that T(N) <= cf(N) when n>= n0
DEFINITION
T(N) = Ω(g(N))
If there are positive constants c and n0 such that T(N) >= cg(N) when n>= n0
Data Structure Software College Northeastern University 124
Mathematical BackgroundMathematical Background
DEFINITION:
T(N) = Θ(b(N))
If and only if T(N) = O(b(N)) and
T(N) = Ω(b(N))
DEFINITION
T(N) = o(p(N))
If T(N) = O(p(N)) and T(N) ≠ Θ(p(N))
Data Structure Software College Northeastern University 125
Meaning of GrowthMeaning of Growth
数学表达式 相对增长率T(N)=O(F
(N) )T(N) 的增长率≤ F(N) 增长率
T(N)=Ω(F(N)) T(N) 的增长率≥ F(N) 增长率T(N)=Θ(F(N)) T(N) 的增长率 = F(N) 增长率T(N)=o(F(N)) T(N) 的增长率< F(N) 增长率
Data Structure Software College Northeastern University 126
Mathematical BackgroundMathematical Background
Physical meaning:
- No meaning f(N) < g(N) At some points
- Relative order among functions
- Relative rates of growth
Data Structure Software College Northeastern University 127
Mathematical BackgroundMathematical Background
Data Structure Software College Northeastern University 128
b=2 b=e
b=10
Mathematical BackgroundMathematical Background
Data Structure Software College Northeastern University 129
Mathematical BackgroundMathematical Background
Function Name
c logN log2N N NlogN N2 N3 2N
Constant Logarithmic Log- squared Linear Quadratic Cubic Exponential
Typical Growth Rates
Data Structure Software College Northeastern University 130
Mathematical Background
Rule 1
))N(g)N(f(O)N(T)N(T)b(
)))N(g(O)),N(f(Omax()N(T)N(T)a(
then)),N(g(O)N(Tand))N(f(O)N(TIf
21
21
21
Examples:if T1(N) = O(N2) and T2(N)= O(N) then
(a) T1(N) + T2(N) = O(N2)
(b) T1(N) * T2(N) = O(N3)
Data Structure Software College Northeastern University 131
Mathematical Background
Rule 2
Rule 3:
)N()N(T
thenk,degreeofpolynomialais)N(TIfk
slowlyverygrowlogarithmsthattellsThis
k.constantanyfor)(logk NON
Data Structure Software College Northeastern University 132
Mathematical Background
inside a Big-Oh, ignored: Low-order terms Constants
Example: correct form:T(N) = O(N2)
bad style: T(N) = O(2N2) correct form:T(N) = O(N2) bad style: T(N) = O(N2+N)
Data Structure Software College Northeastern University 133
Performance:Performance:Analytical MeasurementMeasurement
The space needed Fixed part: independent of the
characteristics of inputs and outputs
Instruction space
Space for simple variables ,constants
Fixed-size component variables Variable part
Component variables whose size is dependent on a particular problem instance
Using recursive functions
Data Structure Software College Northeastern University 134
Performance:Performance:Analytical MeasurementMeasurement
Space Complexity
-The amount of memory a program needs to run to complete
- f:n →The number of unitsTime Complexity
-The amount of computer time a program needs to run to complete
- f:n →The number of steps
Data Structure Software College Northeastern University 135
Performance:Performance:Analytical MeasurementMeasurement
Asymptotic NotationBest-case
-The minimum number of stepsWorst-case
-The maximum number of stepsAverage
-The average number of steps
that can be executed with the given parameters
Data Structure Software College Northeastern University 136
template <class Type> template <class Type> ////algorithm of bubble sortalgorithm of bubble sort void void dataList<dataList<TypeType>>::::bubbleSort bubbleSort ( ) {( ) { //sort //sort form form ElementElement[0] [0] toto ElementElement[[ArraySizeArraySize-1]-1] /// / ArraySize ArraySize is the length of arrayis the length of array int int i i = 1; int = 1; int exchangeexchange = 1; = 1; ////if if exchangeexchange is is 00,then stop ,then stop while ( while ( ii < < ArraySize ArraySize && && exchange exchange ) { ) { BubbleExchange BubbleExchange ( ( ii, , exchangeexchange ); ); ii++;++; } } ////one step one step }}
Example:Example:Analytical Measurement Measurement
Data Structure Software College Northeastern University 137
template <class Type>template <class Type>void void dataList<dataList<TypeType>>::::BubbleExchangeBubbleExchange(const int (const int ii, int & , int & exchange exchange ){){ exchangeexchange = 0; = 0; //initialization//initialization for ( int for ( int j j == ArraySize ArraySize--1; 1; j>=ij>=i; ; jj----)) if ( if ( ElementElement[[j j --1] > 1] > ElementElement[[jj] ) {] ) {
Swap Swap ( ( j j -1, -1, jj ); ); ////if reversionif reversion////swap swap ElementElement[[jj-1] -1] and and ElementElement[[jj]]
exchangeexchange = 1; = 1; ////set the flag to 1set the flag to 1 }} }}
Example:Example:Analytical Measurement Measurement
Data Structure Software College Northeastern University 138
example:example:Analytical MeasurementMeasurement
The number of swapping: basic operationBest-case
0Worst-case
Average
?
1n
1i 21)n(n
i)(n
O(1) O(1)
O(n2)O(n2)
O(n2)O(n2)
Data Structure Software College Northeastern University 139
Ways to estimate the running timeRule 1 - For loops for (i=0;i<N;i++)
k++;
Rule 2 - Nested for loopsfor (i=0; i<N; i++)
for (j=0; j<N; j++)k++
O(N) O(N)
O(N2)O(N2)
Running Time Calculations
Data Structure Software College Northeastern University 140
Rule 3: Consecutive Statements
for (i=0;i<N;i++)k++;
for (i=0; i<N; i++)for (j=0; j<N; j++)
k++;
O(Max(N,N2)) = O(N2)O(Max(N,N2)) = O(N2)
Running Time Calculations
Data Structure Software College Northeastern University 141
Rule 4: Condition Statementif (condition)
S1else
S2
Running time of test plus larger of times s1 and s2
Running time of test plus larger of times s1 and s2
Running Time Calculations
for ( int for ( int j j == ArraySize ArraySize--1; 1; j>=ij>=i; ; jj----)) if ( if ( ElementElement[[j j --1] > 1] > ElementElement[[jj] ) {] ) {
Swap Swap ( ( j j -1, -1, jj ); ); ////if reversionif reversion////swap swap ElementElement[[jj-1] -1] and and ElementElement[[jj]]
exchangeexchange = 1; = 1; ////set the flag to 1set the flag to 1 }}
Data Structure Software College Northeastern University 142
Examples
Sum=0for (j=0;j<N;j++)
Sum++;
Sum=0for (j=0;j<N;j++)
for (k=0;k<N;k++)Sum++;
O(N) O(N)
O(N2) O(N2)
Data Structure Software College Northeastern University 143
Sum=0for (j=0;j<N;j++)
for (k=0;k<N*N;k++)Sum++;
Sum=0for (j=0;j<N;j++)
for (k=0;k<j;k++)Sum++;
Examples
O(N3) O(N3)
O(N2) O(N2)
Data Structure Software College Northeastern University 144
Sum=0for (j=0;j<N;j++)
for (k=0;k<j*j;k++)for (m=0; m<k; m++)
Sum++;
Examples
O(N5) O(N5)
Data Structure Software College Northeastern University 145
Analysis of factorialint fact(int n){ if (n<=1)
return 1;else
return (n*fact(n-1)); }
O(N) O(N)
Examples
Data Structure Software College Northeastern University 146
Analysis of Fibonacci numberint Fib (int N){ if (N<=1)
return 1;else
return ( Fib(N-1) + Fib(N-2) );}
O((5/3)N) O((5/3)N)
Examples
Data Structure Software College Northeastern University 147
# define N 100# define N 100float evaluate (float coef[ ], float x , int n )float evaluate (float coef[ ], float x , int n ) { float power[N], f; int i;{ float power[N], f; int i; for (power[ 0]=1, i = 1; i<=n; i++ ) for (power[ 0]=1, i = 1; i<=n; i++ ) power[i]=x*power[i-1]; power[i]=x*power[i-1]; for (f = 0, i=0; i<=N; i ++) for (f = 0, i=0; i<=N; i ++) f=f+coef[i]*power[i]; f=f+coef[i]*power[i]; return(f);return(f); } }
nn
2210 xa...xaxaa)x(f
Time complexity: O(n) Space complexity : O(n)
Examples
Data Structure Software College Northeastern University 148
# define N 100# define N 100
float evaluate (float coef[ ], float x , int n )float evaluate (float coef[ ], float x , int n )
{ float f; int i;{ float f; int i;
for (f = coef[n], i=n-1; i>=0; i --)for (f = coef[n], i=n-1; i>=0; i --)
f=f*x+coef[i]; f=f*x+coef[i];
return(f);return(f);
}}
x)x)..x)xaa(...aa(a)x(f n1n210
Examples
Time complexity: O(n) Space complexity : O(1)
Data Structure Software College Northeastern University 149
1.1.For each of the following six program For each of the following six program fragments:fragments:
a.Give an analysis of the running time(Big-Oh a.Give an analysis of the running time(Big-Oh will do)will do)
b.Implement the code in the language of C ,give b.Implement the code in the language of C ,give the the
running time for several values of N running time for several values of N
c.Compare your analysis with the actual running c.Compare your analysis with the actual running
timestimes
(1) sum = 0;(1) sum = 0;
for (i=0;i<N;i++)for (i=0;i<N;i++)
sum++;sum++;
Execises
(2) sum = 0;(2) sum = 0; for (i=0;i<N;i++)for (i=0;i<N;i++) for(j=0;j<N;j++)for(j=0;j<N;j++) sum++;sum++;
Data Structure Software College Northeastern University 150
Execises
(3) sum = 0;(3) sum = 0; for (i=0;i<N;i++)for (i=0;i<N;i++) for(j=0;j<N*N;j++)for(j=0;j<N*N;j++) sum++;sum++;
(4) sum = 0;(4) sum = 0; for (i=0;i<N;i++)for (i=0;i<N;i++) for(j=0;j<i;j++)for(j=0;j<i;j++) sum++;sum++;
(5) sum = 0;(5) sum = 0; for (i=0;i<N;i++)for (i=0;i<N;i++) for(j=0;j<i*i;j++)for(j=0;j<i*i;j++) for(k=0;k<j;k++)for(k=0;k<j;k++) sum++;sum++;
(6) sum = 0;(6) sum = 0; for (i=0;i<N;i++)for (i=0;i<N;i++) for(j=0;j<i*i;j++)for(j=0;j<i*i;j++) if(j%i == 0)if(j%i == 0) for(k=0;k<j;k++)for(k=0;k<j;k++) sum++;sum++;
Data Structure Software College Northeastern University 151
2.Write a program2.Write a program
a.Write a program to determine if a positive integer,N ,is a.Write a program to determine if a positive integer,N ,is prime.prime.
b.In terms of N,what is the worst-case running time time b.In terms of N,what is the worst-case running time time of your program?(you should be able to do this in of your program?(you should be able to do this in O(sqrt(N)) .O(sqrt(N)) .
c.Let B equal the number of bits in the binary c.Let B equal the number of bits in the binary representation of N ,what is the value of B?representation of N ,what is the value of B?
d.In terms of B,what is the worst-case running times if a d.In terms of B,what is the worst-case running times if a 20-bit number and a 40-bit number are prime.20-bit number and a 40-bit number are prime.
e.Is it more reasonable times to give the running time in e.Is it more reasonable times to give the running time in terms of N or B?Whyterms of N or B?Why
Execises
Data Structure Software College Northeastern University 152
2.Write a program2.Write a program
a.Write a program to determine if a positive integer,N ,is a.Write a program to determine if a positive integer,N ,is prime.prime.
b.In terms of N,what is the worst-case running time time b.In terms of N,what is the worst-case running time time of your program?(you should be able to do this in of your program?(you should be able to do this in O(sqrt(N)) .O(sqrt(N)) .
c.Let B equal the number of bits in the binary c.Let B equal the number of bits in the binary representation of N ,what is the value of B?representation of N ,what is the value of B?
d.In terms of B,what is the worst-case running times if a d.In terms of B,what is the worst-case running times if a 20-bit number and a 40-bit number are prime.20-bit number and a 40-bit number are prime.
e.Is it more reasonable times to give the running time in e.Is it more reasonable times to give the running time in terms of N or B?Whyterms of N or B?Why
Execises
Data Structure Software College Northeastern University 153
3.Show that X3.Show that X6262 can be computed with only eight can be computed with only eight multiplication.multiplication.
4.Program A and B are analyzed and found to have 4.Program A and B are analyzed and found to have worst-caserunning times no greater than 150Nlog2N,and worst-caserunning times no greater than 150Nlog2N,and N2,respectively. Answer the following questions,if N2,respectively. Answer the following questions,if possible:possible:
a.Which program has the better guarantee on running a.Which program has the better guarantee on running time,for large values of N(N >10,0000)time,for large values of N(N >10,0000)
b.Which program has the better guarantee on running b.Which program has the better guarantee on running times,for small values of N (N<100)?times,for small values of N (N<100)?
c.Which program will run faster on average for N = c.Which program will run faster on average for N = 1,000?1,000?
d.Is it possible that program B will run faster than d.Is it possible that program B will run faster than program A on all possible input?program A on all possible input?
Execises