- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
【Definition】 T (N) = O( f (N) ) if there are positive constants c 】
and n0 such that T (N) ≤ c f (N) for all N ≥ n0.
【Definition】 T (N) = ( g(N) ) if there are positive constants c 】
But it takes more time to compute each step.
float rsum ( float list[ ], int n ) { /* add a list of numbers */ if ( n ) /* count ++ */ return rsum(list, n1) + list[n 1];
Note: A program is written in some programming language, and does not have to be finite (e.g. an operation system). An algorithm can be described by human languages, flow charts, some programming languages, or pseudocode. 〖Example〗 Selection Sort: Sort a set of n ≥ 1 integers in 〗 increasing order.
logk N = O(N) for any constant k. This tells us that
Sort = Find the smallest integer + Interchange it with list[i].
§1 What to Analyze
Machine & compiler-dependent run times. Time & space complexities : machine & compilerindependent. Assumptions:
≥ 2N + 3 = O( N ) = O( Nk≥1 ) = O( 2N ) = We shall always take the smallest f (N). 2N + N2 = ( 2N ) = ( N2 ) = ( N ) = ( 1 ) = We shall always take the largest g(N).
数据结构
Data Structures
主讲人: 主讲人: 李晓红
教材 (Text Book)
Data Structures and Algorithm Analysis in C
(2nd Edition)
Mark Allen Weiss
参考书目 (Reference)
数据结构 (C语言版)严蔚敏等编著 清华大学出版社 数据结构 (C语言版)严蔚敏等编著,清华大学出版社 (C语言版 数据结构 (用面向对象方法与C++描述) 数据结构 (用面向对象方法与C++描述) C++描述 殷人昆等编著, 殷人昆等编著,清华大学出版社
Rules of Asymptotic Notation
If T1 (N) = O( f (N) ) and T2 (N) = O( g(N) ), then (a) T1 (N) + T2 (N) = max( O( f (N)), O( g(N)) ), (b) T1 (N) * T2 (N) = O( f (N) * g(N) ). If T (N) is a polynomial of degree k, then T (N) = Θ( N k ).
From those integers that are currently unsorted, find the smallest and place it next in the sorted list. for ( i = 0; i < n; i++) { list[n Examine list[i] to list[n1] and suppose that the smallest integer is at list[min]; Interchange list[i] and list[min]; }
instructions are executed sequentially each instruction is simple, and takes exactly one time unit integer size is fixed and we have infinite memory
Typically the following two functions are analyzed:
/* count ++ */
Tsum ( n ) = 2n + 3
}
tempsum += list [ i ] ; /* count ++ */
/* count ++ for last execution of for */ return tempsum; /* count ++ */
〖Example〗Recursive 〗 function for summing a list of numbers Trsum ( n ) = 2n + 2
课程评分方法 (Grading Policies)
Lecture Grade (80) = Final Exam (80)
1 5 Laboratory Grade (15) = ∑ Lab ( i ) × 0.15 5 i =1
Other Grade(5)=Attendance + Homework
实验 (Laboratory Projects)
/day;抄袭者0 共 5 次;迟交罚扣为 10% /day;抄袭者0分; 3人一组,各组自行分工(例如:按功能模块,按开 人一组,各组自行分工(例如:按功能模块, 人一组 发过程等),各自工作量应相当,试验报告中必须 发过程等),各自工作量应相当, ),各自工作量应相当 写明各自工作量; 写明各自工作量; 通过e-mail提交,请在邮件标题写明班级学号及试验 提交,请在邮件标题写明班级学号及 通过 提交 邮件标题写明班级学号 ID,并在文档末尾写明分工. 文档末尾写明分工. ,并在文档末尾写明分工
助教( 助教(Teaching Assistant): ): 杜洪伟( 杜洪伟(dhw198426@) 潘东 (pandongshilige@)
CHAPTER 2
ALGORITHM ANALYSIS 【Definition】An algorithm is a finite set of instructions 】 that, if followed, accomplishes a particular task. In addition, all algorithms must satisfy the following criteria:
作业&出勤 作业 出勤
布置作业的下周(Tuesday)交,迟交不交 布置作业的下周( ) 扣分.做则有分,不计对错, 扣分.做则有分,不计对错, 一周后公布 参考答案. 参考答案. 必须按照软件学院规范做作业, 必须按照软件学院规范做作业,否则不计 作业未交者不能参加期末考试. 分,1/3作业未交者不能参加期末考试. 作业未交者不能参加期末考试 点名三次不到者不能参加期末考试. 点名三次不到者不能参加期末考试.
and n0 such that T (N) ≥ c g(N) for all N ≥ n0. T (N) = ( h(N) ) . Θ( p(N) ) .
【Definition】 T (N) = Θ( h(N) ) if and only if T (N) = O( h(N) ) and 】 【Definition】 T (N) = o( p(N) ) if T (N) = O( p(N) ) and T (N) ≠ 】 Note:
(1) Input There are zero or more quantities that are externally supplied. (2) Output At least one quantity is produced. (3) Definiteness Each instruction is clear and unambiguous. (4) Finiteness If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after finite number of steps. (5) Effectiveness Every instruction must be basic enough to be carried out, in principle, by a person using only pencil and paper. It is not enough that each operation be definite as in(3); it also must be feasible.
Tavg(N) & Tworst(N) -- the average and worst case time complexities, respectively, as functions of input size N.
〖Example〗 Matrix addition 〗
void add ( int int int int { int i, j ; for ( i = 0; i < rows; i++ ) /* rows + 1 */ for ( j = 0; j < cols; j++ ) /* rows(cols+1) */ c[ i ][ j ] = a[ i ][ j ] + b[ i ][ j ]; } T(rows, cols ) = 2 rows cols + 2rows + 1 /* rows cols */ a[ ][ MAX_SIZE ], b[ ][ MAX_SIZE ], c[ ][ MAX_SIZE ], rows, int cols )