数据结构与算法分析c++版课件_2.pdf
《数据结构与算法分析c++版课件_2.pdf》由会员分享,可在线阅读,更多相关《数据结构与算法分析c++版课件_2.pdf(27页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、数据结构与算法分析数据结构与算法分析数据结构与算法分析数据结构与算法分析数据结构与算法分析数据结构与算法分析数据结构与算法分析数据结构与算法分析A Practical Introduction toData Structures and Algorithm Analysis2 Mathematical Preliminaries2 Mathematical PreliminariesSet concepts and notationLogarithmsLogarithmsSummations and RecurrencesRecursionM thtil Pf Th iMathematical
2、 Proof TechniquesEstimation2Sets and RelationsSets and Relations?Set:A collection of distinguishable members or elements.2,3,5 xP?Bag:A collection of elements with no order(like a set),but with duplicate-valued elements.3,4,5,4?Sequence:A collection of elements with an order,and which may contain du
3、plicate-valued elements.3 4 5 4?A relation R over set S is a set of ordered pairs from S S.?Ex:if S is a,b,c,then,is a relation.Sets and RelationsSets and Relations?If tuple is in relation R,we may use the infix notation xRy.D fithtifl tif llith R?Define the properties of relations as follows,with R
4、 a binary relation over set S.?R is reflexive if aRa for all a S?R is reflexive if aRa for all a S.?R is symmetric if whenever aRb,then bRa,for all a,b S.?R is antisymmetric if whenever aRb and bRa,then a=b,for all a,b S.?R is transitive if whenever aRb and bRc,then aRc,for all a,b,c SS.antisymmetri
5、c 2Fib(1)=Fib(2)=1A Closed-Form SolutionA Closed Form Solution?The recurrence?T(n)=T(n 1)+1 for n 1?T(0)=T(1)=0?Replace the recurrence relation with a closed-?Replace the recurrence relation with a closedform solution?T(n)=T(n 1)+1?T(n)T(n 1)+1=(T(n 2)+1)+1=T(n 2)+2=T(n 2)+2?T(n)=T(n (n 1)+(n 1)=T(1
6、)+n 1=T(1)+n 1=n-1RiRecursionAlithiiif itll itlf td?An algorithm is recursive if it calls itself to do part of its work.?the base case?the recursive partp?The factorial of n.long fact(int n)/Compute n!recursivelyg()py/To fit n!into a long variable,we require n=0)&(n=12),Input out of range);if(n 1ret
7、urn n fact(n-1);/Recursive call for n 1RecursionRecursionint Factorial(int n)参数0Factorial(0)1int Factorial(int n)参数11*Factorial(0)()1if(n=0)return 1;参数22*Factorial(1)参数11 Factorial(0)12return 1;elsereturn n*参数33*Factorial(2)参数()6return n Factorial(n-1);参数44*Fti l(3)参数33 Factorial(2)624主程序参数44*Factor
8、ial(3)24主程序汉诺塔问题ABCABC以C柱为中转,将盘从A柱移动到B柱上,一次以 柱为中转将盘从 柱移动到 柱次只能移动一个盘,而且大盘不能压在小盘上面汉诺塔问题ABC将 n 个盘子从 a 柱移动到b柱,用c柱做中转,可以分以下3步实现可以分以下3步实现:1)先将n-1个盘子,以b为中转,从a柱移动到c柱,2)将个盘子从 移动到b2)将一个盘子从a移动到b3)将c柱上的n-1个盘子,以a为中转,移动到b柱终止条件?终止条件?n=1时,直接移动盘子即可,不用递归#il dit汉诺塔问题#include using namespace std;/将 n 个盘子从 a 柱移动到b柱,用c柱做
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 算法 分析 c+ 课件 _2
限制150内