Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | erick-mckenzie |
View: | 222 times |
Download: | 3 times |
Data Structure and Algorithm Analysis
2015
About the Course
• Objective• Organize a large amount of data• Algorithm: the operation on the organized data• Analysis the running time of the algorithms
• Prerequisite courses:• Programming Fundamentals (C/C++ or Java)• Discrete Math
About the Course
• At the end of the course• Familiar with the common data structures
• Internal and external• Fundamental algorithms
• Traverse, sorting, merge etc• Time and space complexity of above algorithms
• Evaluation• Final exam• Assignment
• Textbook• Mark Allen Weiss, Data Structure and Algorithm Analysis in C
Examination
• Algorithm complexity analysis• Linear data structure• Tree and graph• Search, sorting
• [optional] Hash, priority queue
Self Introduction
• Name: Zhu Huiquan (Nate)• Education in China and Singapore• Research areas: formal method, data mining and
graph algorithm• Mobile:+86 131 8909 7204• Email: [email protected]
Programming Fundamentals• Variables and data types (struct)• Control flow: sequential, conditional and loop• Function• Array and collection• Input/output, exceptions
Math
• Exponents
Logarithms
• Definition:• if and only if
• In computer science, we usually use represent
• for all
Series
• ;
• ;
Recursion
• Base cases• Making progress• Ensure all recursive calls work• Avoid duplicate work on the same instance
Recursion programming
• An example
• Questions:• Does it follow the four rules in previous slide?
int Fibonacci(int n){ if ( n == 0 ) return 0; else if ( n == 1 ) return 1; else return ( Fibonacci(n-1) + Fibonacci(n-2) );}
Algorithm Analysis
• How to describe the complexity of the algorithms• Typical complexity levels• Analysis with an example
Notation
• Definitions:• if there are constants and such that when
• if there are constants and such that when
• if and only if and
• if and
• Why we need the above notations?• Computer deals with large data set, i.e. is very big
• When grows large, grows much faster than
Complexity level
Function Name
Constant
Logarithmic
Log-squared
Linear
Quadratic
Cubic
Exponential
• Rule 1:If and , thena) ,
• Rule 2:If is a polynomial of degree , then
• Rule 3: for any constant .
Examples
int A(int n){ if(n%2==0)
return B(n); else
return C(n); }
int A(int n) { int i = 0, s = 0; while(i<n) {
s = s + B(n);i++;
} return s;}int B(n) { int s = 0; for(int j = 0; j < n; j++)
s = s + j; return s;}
Examples
void print_out(int n){ if(n<10) { printf(“%d\n”, n); } else { print_out(n/10); printf(“%d\n”, n%10); }//else}
Examples
Examplesint bsearch(int a[], int x, int n){ int low, mid, high; low = 0; high = n - 1; whle(low <= high) { mid = (low + high) / 2; if(a[mid] < x) low = mid + 1; else if (a[mid] < x) high = mid - 1; else return(mid); } //while return(NOT_FOOUND);}
low highmid
low highmid
high'
Assumption
• Assumption• The same time cost for all basic operation• No complex operation, like hardware acceleration• No memory constraint• Random access on external storage
• To measure the complexity• Based on the input size n• Average running time and worse-case running time, in
O(f(n))
Abstract Dada Types
• An abstract data type (ADT) is a set of operations.• Abstract, no implementation defined• Modular design• Template concept in C++
• Example:• List: sequential• Set/Collection:
List
• Typical Implementation• ArrayList• LinkList
• Typical operations:• Insert• Append• Find• Delete• Concatenate/join• Reverse• Sum
ArrayList
• Allocated N units of space for the list of size n, n <N• An integer to store the current size “n”
• Typical operations:• Insert• Append• Find• Delete• Concatenate/join• Reverse• Sum
ArrayList
After append 3,7,4,3
3 7 4 3
Insert (5, 2)
3 5 7 4 3
ArrayList
Insert (2, 5)
3 5 7 4 3
3 5 7 4 3
delete (3)
3 5 4 3