算法设计与分析基础第三版PPTch.ppt
《算法设计与分析基础第三版PPTch.ppt》由会员分享,可在线阅读,更多相关《算法设计与分析基础第三版PPTch.ppt(44页珍藏版)》请在三一办公上搜索。
1、Transform and Conquer,This group of techniques solves a problem by a transformation toa simpler/more convenient instance of the same problem(instance simplification)a different representation of the same instance(representation change)a different problem for which an algorithm is already available(p
2、roblem reduction),Instance simplification-Presorting,Solve a problems instance by transforming it intoanother simpler/easier instance of the same problemPresortingMany problems involving lists are easier when list is sorted,e.g.searching computing the median(selection problem)checking if all element
3、s are distinct(element uniqueness)Also:Topological sorting helps solving some problems for dags.Presorting is used in many geometric algorithms.,How fast can we sort?,Efficiency of algorithms involving sorting depends onefficiency of sorting.Theorem(see Sec.11.2):log2 n!n log2 n comparisons are nece
4、ssary in the worst case to sort a list of size n by any comparison-based algorithm.Note:About nlog2 n comparisons are also sufficient to sort array of size n(by mergesort).,Searching with presorting,Problem:Search for a given K in A0.n-1 Presorting-based algorithm:Stage 1 Sort the array by an effici
5、ent sorting algorithm Stage 2 Apply binary search Efficiency:(nlog n)+O(log n)=(nlog n)Good or bad?Why do we have our dictionaries,telephone directories,etc.sorted?,Element Uniqueness with presorting,Presorting-based algorithm Stage 1:sort by efficient sorting algorithm(e.g.mergesort)Stage 2:scan ar
6、ray to check pairs of adjacent elements Efficiency:(nlog n)+O(n)=(nlog n)Brute force algorithm Compare all pairs of elements Efficiency:O(n2)Another algorithm?Hashing,Instance simplification Gaussian Elimination,Given:A system of n linear equations in n unknowns with an arbitrary coefficient matrix.
7、Transform to:An equivalent system of n linear equations in n unknowns with an upper triangular coefficient matrix.Solve the latter by substitutions starting with the last equation and moving up to the first one.a11x1+a12x2+a1nxn=b1 a1,1x1+a12x2+a1nxn=b1 a21x1+a22x2+a2nxn=b2 a22x2+a2nxn=b2 an1x1+an2x
8、2+annxn=bn annxn=bn,Gaussian 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 a
9、nd an appropriate multiple of the i-th row to make the new coefficient in the i-th column of that row 0,Example of Gaussian Elimination,Solve 2x1-4x2+x3=6 3x1-x2+x3=11 x1+x2-x3=-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-3/2-6 row3(3/5)*row2 2
10、-4 1 6 0 5-1/2 2 0 0-6/5-36/5 Backward substitution x3=(-36/5)/(-6/5)=6 x2=(2+(1/2)*6)/5=1 x1=(6 6+4*1)/2=2,Pseudocode and Efficiency of Gaussian Elimination,Stage 1:Reduction to an upper-triangular matrixfor i 1 to n-1 do for j i+1 to n do for k i to n+1 do Aj,k Aj,k-Ai,k*Aj,i/Ai,i/improve!Stage 2:
11、Back substitutionsfor j n downto 1 do t 0 for k j+1 to n do t t+Aj,k*xk xj(Aj,n+1-t)/Aj,j Efficiency:(n3)+(n2)=(n3),Searching Problem,Problem:Given a(multi)set S of keys and a search key K,find an occurrence of K in S,if anySearching must be considered in the context of:file size(internal vs.externa
12、l)dynamics of data(static vs.dynamic)Dictionary operations(dynamic data):find(search)insertdelete,Taxonomy of Searching Algorithms,List searchingsequential searchbinary searchinterpolation searchTree searching binary search treebinary balanced trees:AVL trees,red-black treesmultiway balanced trees:2
13、-3 trees,2-3-4 trees,B treesHashingopen hashing(separate chaining)closed hashing(open addressing),Binary Search Tree,Arrange keys in a binary tree with the binary search tree property:,K,K,K,Example:5,3,1,10,12,7,9,Dictionary Operations on Binary Search Trees,Searching straightforwardInsertion searc
14、h for key,insert at leaf where search terminatedDeletion 3 cases:deleting key at a leafdeleting key at node with single childdeleting key at node with two children Efficiency depends of the trees height:log2 n h n-1,with height average(random files)be about 3log2 n Thus all three operations have wor
15、st case efficiency:(n)average case efficiency:(log n)Bonus:inorder traversal produces sorted list,Balanced Search Trees,Attractiveness of binary search tree is marred by the bad(linear)worst-case efficiency.Two ideas to overcome it are:to rebalance binary search tree when a new insertion makes the t
16、ree“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,Balanced trees:AVL trees,Definition 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
17、 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,Rotations,If a key insertion violates the balance requirement at some node,the subtree rooted at that node is transformed via one of the four rotations.(The rotation is always
18、performed for a subtree rooted at an“unbalanced”node closest to the new leaf.),Single R-rotation,Double LR-rotation,General case:Single R-rotation,General case:Double LR-rotation,AVL tree construction-an example,Construct an AVL tree for the list 5,6,8,3,2,4,7,AVL tree construction-an example(cont.)
19、,Analysis of AVL trees,h 1.4404 log2(n+2)-1.3277 average height:1.01 log2n+0.1 for large n(found empirically)Search and insertion are O(log n)Deletion is more complicated but is also O(log n)Disadvantages:frequent rotationscomplexityA similar idea:red-black trees(height of subtrees is allowed to dif
20、fer by up to a factor of 2),Multiway Search Trees,Definition A multiway search tree is a search tree that allowsmore 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 poi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 算法 设计 分析 基础 第三 PPTch
链接地址:https://www.31ppt.com/p-6329411.html