1.56k likes | 1.89k Views
Data Structure in C ++. February 2009 software college Northeastern University. Why Study Data structure. Using Computer Want it to go faster ? Process more data? Want it to do something that would otherwise be impossible Technology improves things by a constant factor
E N D
Data Structure in C ++ February 2009 software college Northeastern University Software College Northeastern University
Why Study Data structure • Using Computer • Want it to go faster ? Process more data? • Want it to do something that would otherwise be impossible • Technology 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 Software College Northeastern University
Why Study Data Structure • Present commonly used data structure • Introduce 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. Software College Northeastern University
What will you learn? • What are some of the common data structures • What are some ways to implement them • How to analyze their efficiency • How to use them to solve practical problems Software College Northeastern University
Prerequisites • four important ideas • Iteration - Do, While, Repeat • Data Representation - variables and pointers -class definition • Subprograms and Recursion - modular design and abstraction • Inheritance • discrete math Software College Northeastern University
Approach • 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 ++) Software College Northeastern University
Course Organization Chapter 1 Introduction Chapter 2 Array Chapter 3 Lists Chapter 4 Stacks, and Queues Chapter 5 Recursion Chapter 6 Trees Chapter 7 Priority Queues Chapter 8 Searching Chapter 9 Sorting Chapter 10 Graph Algorithms Software College Northeastern University
Course work and Grading • Programming assignments 20% • Written exercises 10% • Exams: • Closed book • Final 70% Software College Northeastern University
Course Materials • TextBook - Data Structures and Algorithm Analysis in C++ (second Edition) Mark Allen Weiss POSTS & TELECOM PRESS -《数据结构与算法分析》(C语言描述) Mark Allen Weiss 冯舜玺译 China Machine Press Software College Northeastern University
Course Materials • Recommended readings: • Herbert Schildt, C++: The Complete Reference, Fourth Edition,周志荣等译,电子工业出版社出版 • 殷人昆,陶永雷等编著:《数据结构》(用面向对象方法与C++描述),清华大学出版社,2005.11 • 殷人昆,徐孝凯编著:《数据结构习题解析》(用面向对象方法与C++描述) 清华大学出版社,2005.7 Software College Northeastern University
Course Materials • Useful link: • Dictionary of Algorithms and Data Structures http://www.nist.gov/dads/ • Download from http://www.163.com Email:datastructure_neu@163.com Password:datastructure From:网易网盘 Software College Northeastern University
Chapter 1 Introduction Software College Northeastern University
Overview • What’s the course about • The goal to study Data structure • Concepts about data structure • Review of C++ • Algorithm • Performance and analysis of algorithm Software College Northeastern University
What’s the course about Before we go into details, what is a data structure exactly? What is a computer program? Some mysterious processing Input Output Program = Data structure + Algorithm Software College Northeastern University
V0 V2 V3 V5 V4 V7 V6 V1 An Example of program • Network connectivity • Nodes • Add Connections between pairs of nodes • Is there a path from Node A to Node B? Software College Northeastern University
An 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 Software College Northeastern University
Union–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 Software College Northeastern University
Another Example-Image Processing • Find connected component • Read in a 2D color image and find regions of connected pixels that have the same color Software College Northeastern University
Another Example-Image Processing • 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 Software College Northeastern University
Another Example-Image Processing Software College Northeastern University
Objects • 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 Software College Northeastern University
Quick-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 Software College Northeastern University
Another Example: Selection Problem • 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. Software College Northeastern University
Another Example:Selection Problem • 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 Software College Northeastern University
Another Example:Selection Problem • Which solution is better when (1) N ~ k (2) N » k ? why? Software College Northeastern University
What do these examples illustrate? • more than one solutions to a given problem • Each solution has it’s advantages and disadvantages - time - space • Performance of Algorithms Always ask: How can we do better? Software College Northeastern University
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 andthe efficiency of the algorithm. The algorithm chosen to solve a particular programming problemhelps to determine which data structure should be used. Software College Northeastern University
array Linked list queue tree stack Software College Northeastern University
student table Software College Northeastern University
Course table Linear structure Software College Northeastern University
UNIX file system / (root) user bin etc lib sw yin tao xie math ds Queue.cpp Stack.cpp Tree.cpp Software College Northeastern University
J I A C B D H G F E • Tree • Graph set Software College Northeastern University
Concepts about Data Structure Data: everything can store in computers data object data element data item Software College Northeastern University
Concepts about Data Structure • 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 Software College Northeastern University
Concepts about Data Structure • representations of data structure in memory • representations of data object • representations of relations • representation of relations • static • dynamic • physical structure Software College Northeastern University
Concepts about Data Structure • 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; } . Software College Northeastern University
Concepts about Data Structure • 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) Software College Northeastern University
Concepts about Data Structure • 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. . Software College Northeastern University
select insertdelete update ADT Records of student Software College Northeastern University
Something about C++ • Dynamic Memory Management • Shallow copy and deep copy • Template • Inheritance • Exception Handling • Design pattern • STL Software College Northeastern University
Dynamic Memory Management • C: malloc() and free() • C++: newand delete Software College Northeastern University
Dynamic Memory Management • Memory Allocation • The new operator works for all data types // Dynamically allocate an array of size 100 float* ptr1 = new float[100]; // Prompt the user for the size of the second array int size = 0; cin >> size; float* ptr2 = new float[size]; 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; Software College Northeastern University
Dynamic 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. Software College Northeastern University
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; } #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;} }; Software College Northeastern University
Dynamic Memory Management • Memory Allocation • The new operator always returns a memory address. // Allocate a single integer int* ptr = new int; *ptr = 10; cout << "Address: " << ptr << endl; cout << "Value: " << *ptr << endl; Software College Northeastern University
Dynamic 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]; …… delete [ ] p; Software College Northeastern University
Shallow copy and deep copy • Internal data and external data struct Teacher { string *FirstName; string *LastName; int employeeNum; }; • Shallow copy ---- copy the pointer • Deep copy ----- copy the resource that the pointer pointing to Software College Northeastern University