ImageVerifierCode 换一换
格式:PPT , 页数:44 ,大小:1.23MB ,
资源ID:11729230      下载积分:12 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/11729230.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(算法设计与分析基础第三版PPTch06.ppt)为本站上传会员【精***】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

算法设计与分析基础第三版PPTch06.ppt

1、Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,Click to edit Master title style,1,Transform and Conquer,This group of techniques solves a problem by a,transformation,to,a simpler/more convenient instance of the same problem(,instance simplification,),a differen

2、t representation of the same instance(,representation change,),a different problem for which an algorithm is already available(,problem reduction,),2,Instance simplification-Presorting,Solve a problems instance by transforming it into,another simpler/easier instance of the same problem,Presorting,Ma

3、ny problems involving lists are easier when list is sorted,e.g.,searching,computing the median(selection problem),checking if all elements are distinct(element uniqueness),Also:,Topological sorting helps solving some problems for dags.,Presorting is used in many geometric algorithms.,3,How fast can

4、we sort?,Efficiency of algorithms involving sorting depends on,efficiency of sorting.,Theorem,(see Sec.11.2):,log,2,n,!,n,log,2,n,comparisons are necessary in the worst case to sort a list of size,n,by,any,comparison-based algorithm.,Note:About,n,log,2,n,comparisons are also sufficient to sort array

5、 of size,n,(by mergesort).,4,Searching with presorting,Problem:Search for a given,K,in A0.,n,-1,Presorting-based algorithm:,Stage 1 Sort the array by an efficient sorting algorithm,Stage 2 Apply binary search,Efficiency:,(,n,log,n,)+O(log,n,)=,(,n,log,n,),Good or bad?,Why do we have our dictionaries

6、telephone directories,etc.sorted?,5,Element Uniqueness with presorting,Presorting-based,algorithm,Stage 1:sort by efficient sorting algorithm(e.g.mergesort),Stage 2:scan array to check pairs of,adjacent,elements,Efficiency,:,(,n,log,n,)+O(,n,)=,(,n,log,n,),Brute force algorithm Compare all pairs of

7、 elements Efficiency:O(,n,2,),Another algorithm?Hashing,Instance simplification,Gaussian Elimination,Given:A system of,n,linear equations in,n,unknowns with an arbitrary coefficient matrix.Transform to:An equivalent system of,n,linear equations in,n,unknowns with an upper triangular coefficient matr

8、ix.Solve the latter by substitutions starting with the last equation and moving up to the first one.,a,11,x,1,+,a,12,x,2,+,a,1,n,x,n,=,b,1,a,1,1,x,1,+,a,12,x,2,+,a,1,n,x,n,=,b,1,a,21,x,1,+,a,22,x,2,+,a,2,n,x,n,=,b,2,a,22,x,2,+,a,2,n,x,n,=,b,2,a,n,1,x,1,+,a,n,2,x,2,+,a,nn,x,n,=,b,n,a,nn,x,n,=,b,n,6,G

9、aussian Elimination(cont.),The transformation is accomplished by a sequence of elementary operations on the systems coefficient matrix (which dont change the systems solution):,for,i,1 to,n-,1,do,replace each of the subsequent rows(i.e.,rows,i,+1,n,)by a difference between that row and an appropriat

10、e multiple of the,i-,th row to make the new coefficient in the,i-,th,column of that row 0,7,Example of Gaussian Elimination,Solve 2,x,1,-4,x,2,+,x,3,=6 3,x,1,-,x,2,+,x,3,=11,x,1,+,x,2,-,x,3,=-3,Gaussian elimination,2,-4,1 6 2,-4,1,6,3 -1 1 11 row2 (3/2)*row1 0 5 -1/2 2,1 1 -1,-3 row3 (1/2)*row1 0 3

11、3/2 -6 row3(3/5)*row2,2,-4,1,6,0 5 -1/2 2,0 0 -6/5-36/5,Backward substitution,x,3,=(-36/5)/(-6/5)=6,x,2,=(2+(1/2)*6)/5=1,x,1,=(6 6+4*1)/2=2,8,Pseudocode and Efficiency of Gaussian Elimination,Stage 1:Reduction to an upper-triangular matrix,for,i,1,to,n-,1,do,for,j,i,+1,to,n,do for,k,i,to,n+,1,do,A,

12、j,k,A,j,k,-,A,i,k,*A,j,i,/,A,i,i,/improve!,Stage 2:Back substitutions,for,j,n,downto,1,do,t,0,for,k,j,+1,to,n,do,t,t,+,A,j,k,*,x,k,x,j,(,A,j,n,+1-,t,)/,A,j,j,Efficiency:,(,n,3,),+,(,n,2,),=,(,n,3,),9,10,Searching Problem,Problem,:Given a(multi)set,S,of keys and a search key,K,find an occurrence of,K

13、in,S,if any,Searching must be considered in the context of:,file size(internal vs.external),dynamics of data(static vs.dynamic),Dictionary operations(dynamic data):,find(search),insert,delete,11,Taxonomy of Searching Algorithms,List searching,sequential search,binary search,interpolation search,Tre

14、e searching,binary search tree,binary balanced trees:AVL trees,red-black trees,multiway balanced trees:2-3 trees,2-3-4 trees,B trees,Hashing,open hashing(separate chaining),closed hashing(open addressing),12,Binary Search Tree,Arrange keys in a binary tree with the,binary search tree property,:,K,K,

15、Example:5,3,1,10,12,7,9,13,Dictionary Operations on Binary Search Trees,Searching straightforward,Insertion search for key,insert at leaf where search terminated,Deletion 3 cases:,deleting key at a leaf,deleting key at node with single child,deleting key at node with two children,Efficiency depends

16、of the trees height:,log,2,n,h,n,-1,with height average(random files)be about 3,log,2,n,Thus all three operations have,worst case efficiency:,(,n,),average case efficiency:,(log,n,),Bonus,:inorder traversal produces sorted list,14,Balanced Search Trees,Attractiveness of,binary search tree,is marred

17、by the bad(linear)worst-case efficiency.Two ideas to overcome it are:,to rebalance binary search tree when a new insertion makes the tree“too unbalanced”,AVL trees,red-black trees,to allow more than one key per node of a search tree,2-3 trees,2-3-4 trees,B-trees,15,Balanced trees:AVL trees,Definitio

18、n,An,AVL tree,is a binary search tree in which,for every node,the difference between the heights of its left and right subtrees,called the,balance factor,is at most 1(with the height of an empty tree defined as-1),Tree(a)is an AVL tree;tree(b)is not an AVL tree,16,Rotations,If a key insertion violat

19、es the balance requirement at some node,the subtree rooted at that node is transformed via one of the four,rotations.,(The rotation is always performed for a subtree rooted at an“unbalanced”node closest to the new leaf.),Single,R-,rotation,Double,LR-,rotation,17,General case:Single R-rotation,18,Gen

20、eral case:Double LR-rotation,19,AVL tree construction-an example,Construct an AVL tree for the list 5,6,8,3,2,4,7,20,AVL tree construction-an example(cont.),21,Analysis of AVL trees,h,1.4404,log,2,(,n,+2)-1.3277,average height:1.01,log,2,n,+,0.1 for large,n,(found empirically),Search and insertion a

21、re O(,log,n,),Deletion is more complicated but is also O(,log,n,),Disadvantages:,frequent rotations,complexity,A similar idea:,red-black trees,(height of subtrees is allowed to differ by up to a factor of 2),22,Multiway Search Trees,Definition,A,multiway search tree,is a search tree that allowsmore

22、than one key in the same node of the tree.,Definition,A node of a search tree is called an,n,-,node,if it contains,n-,1 ordered keys(which divide the entire key range into,n,intervals pointed to by the nodes,n,links to its children):,Note:Every node in a classical binary search tree is a 2-node,k,1,

23、k,2,k,n-1,k,1,k,1,k,2,),k,n-,1,23,2-3 Tree,Definition,A,2-3 tree,is a search tree that,may have 2-nodes and 3-nodes,height-balanced(all leaves are on the same level),A 2-3 tree is constructed by successive insertions of keys given,with a new key always inserted into a leaf of the tree.If the leaf is

24、 a 3-node,its split into two with the middle key promoted to the parent.,24,2-3 tree construction an example,Construct a 2-3 tree the list 9,5,8,3,2,4,7,25,Analysis of 2-3 trees,log,3,(,n,+1)-1,h,log,2,(,n,+1)-1,Search,insertion,and deletion are in,(,log,n,),The idea of 2-3 tree can be generalized b

25、y allowing more keys per node,2-3-4 trees,B-trees,26,Heaps and Heapsort,Definition,A,heap,is a binary tree with keys at its nodes(one key per node)such that:,It is essentially complete,i.e.,all its levels are full except possibly the last level,where only some rightmost keys may be missing,The key a

26、t each node is,keys at its children,27,Illustration of the heaps definition,a heap,not a heap,not a heap,Note:Heaps elements are ordered top down(along any path down from its root),but they are not ordered left to right,28,Some Important Properties of a Heap,Given,n,there exists a unique binary tree

27、 with,n,nodes that,is essentially complete,with,h,=,log,2,n,The root contains the largest key,The subtree rooted at any node of a heap is also a heap,A heap can be represented as an array,29,Heaps Array Representation,Store heaps elements in an array(whose elements indexed,for convenience,1 to,n,)in

28、 top-down left-to-right order,Example:,Left child of node,j,is at 2,j,Right child of node,j,is at 2,j,+1,Parent of node,j,is at,j,/2,Parental nodes are represented in the first,n,/2,locations,9,1,5,3,4,2,1 2 3 4 5 6,9 5 3 1 4 2,30,Step 0:Initialize the structure with keys in the order given,Step 1:S

29、tarting with the last(rightmost)parental node,fix the heap rooted at it,if it doesnt satisfy the heap condition:keep exchanging it with its largest child until the heap,condition holds,Step 2:Repeat Step 1 for the preceding parental node,Heap Construction(bottom-up),31,Example of Heap Construction,C

30、onstruct a heap for the list 2,9,7,6,5,8,32,Pseudopodia of bottom-up heap construction,33,Stage 1:Construct a heap for a given list of,n,keys,Stage 2:Repeat operation of root removal,n,-1 times:,Exchange keys in the root and in the last(rightmost)leaf,Decrease heap size by 1,If necessary,swap new ro

31、ot with larger child until the heap condition holds,Heapsort,34,Sort the list 2,9,7,6,5,8 by heapsort,Stage 1(heap construction)Stage 2(root/max removal),1 9,7,6 5 8,9,6 8 2 5 7,2,9,8 6 5 7 7 6 8 2 5|9,2,9 8 6 5 7,8,6 7 2 5|9,9,2,8 6 5 7 5 6 7 2|8 9,9 6 8 2 5 7,7,6 5 2|8 9,2 6 5|7 8 9,6,2 5|7 8 9,5,

32、2|6 7 8 9,5,2|6 7 8 9,2|5 6 7 8 9,Example of Sorting by Heapsort,35,Stage 1:Build heap for a given list of,n,keys,worst-case,C,(,n,)=,Stage 2:Repeat operation of root removal,n,-1 times(fix heap),worst-case,C,(,n,)=Both worst-case and average-case efficiency:,(,n,log,n,)In-place:yesStability:no(e.g.

33、1 1),2(,h-i,)2,i,=,2(,n,log,2,(,n,+1),(,n,),i,=0,h,-1,#nodes at level,i,i=,1,n,-1,2log,2,i,(,n,log,n,),Analysis of Heapsort,36,A,priority queue,is the ADT of a set of elements with,numerical priorities with the following operations:,find element with highest priority,delete element with highest pri

34、ority,insert element with assigned priority(see below),Heap is a very efficient way for implementing priority queues,Two ways to handle priority queue in which highest priority=smallest number,Priority Queue,37,Insertion of a New Element into a Heap,Insert the new element at last position in heap.,C

35、ompare it with its parent and,if it violates heap condition,exchange them,Continue comparing the new element with nodes up the tree until the heap condition is satisfied,Example:,Insert key 10,Efficiency:O(log,n,),38,Horners Rule For Polynomial Evaluation,Given a polynomial of degree,n,p,(,x,)=,a,n,

36、x,n,+a,n,-1,x,n,-1,+,a,1,x,+,a,0,and a specific value of,x,find the value of,p,at that point.,Two brute-force algorithms:,p,0,p,a,0,;,power,1,for,i,n,downto 0,do for,i,1 to,n,do,power,1,power,power,*,x,for,j,1 to,i,do,p,p,+,a,i,*,power,power,power,*,x,return,p,p,p+a,i,*,power,return,p,39,Horners Rul

37、e,Example:,p,(x)=2,x,4,-,x,3,+3,x,2,+,x,-5=,=,x,(2,x,3,-,x,2,+3,x,+1)-5=,=,x,(,x,(2,x,2,-,x,+3)+1)-5=,=,x,(,x,(,x,(2,x,-1)+3)+1)-5,Substitution into the last formula leads to a faster algorithm,Same sequence of computations are obtained by simply arranging the coefficient in a table and proceeding a

38、s follows:,coefficients2-1 3 1-5,x,=3,40,Horners Rule pseudocode,Efficiency of Horners Rule:#multiplications=#additions=,n,S,ynthetic division,of of,p,(,x,)by(,x-x,0,),Example:Let,p,(,x,)=2,x,4,-,x,3,+3,x,2,+,x,-5.Find,p,(,x,):(,x,-3),41,Computing,a,n,(revisited),Left-to-right binary exponentiation,

39、Initialize product accumulator,by 1.,Scan,n,s binary expansion from left to right and do the following:,If the current binary digit is 0,square the accumulator(S);if the binary digit is 1,square the accumulator and multiply it by,a,(SM).,Example:Compute a,13,.Here,n,=13=1101,2,binary rep.of 13:1 1 0

40、 1 SM SM S SM accumulator:1 1,2,*,a=a,a,2,*,a,=,a,3,(,a,3,),2,=,a,6,(,a,6,),2,*,a,=,a,13,(computed left-to-right)Efficiency:,b,M(,n,)2,b,where,b=,log,2,n,+1,42,Computing,a,n,(cont.),Right-to-left binary exponentiation,Scan,n,s binary expansion from right to left and compute,a,n,as the product of ter

41、ms,a,2,i,corresponding to 1s in this expansion.,Example,Compute,a,13,by the right-to-left binary exponentiation.Here,n,=13=1101,2,.,1 1 0 1,a,8,a,4,a,2,a,:,a,2,i,terms,a,8,*a,4,*a,:product (computed right-to-left),Efficiency:same as that of left-to-right binary exponentiation,43,Problem Reduction,Th

42、is variation of transform-and-conquer solves a problem by a transforming it into different problem for which an algorithm is already available.,To be of practical value,the combined time of the transformation and solving the other problem should be smaller than solving the problem as given by anothe

43、r method.,44,Examples of Solving Problems by Reduction,computing lcm(,m,n,)via computing gcd(,m,n,),counting number of paths of length,n,in a graph by raising the graphs adjacency matrix to the,n-,th power,transforming a maximization problem to a minimization problem and vice versa(also,min-heap construction),linear programming,reduction to graph problems(e.g.,solving puzzles via state-space graphs),

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服