平摊分析 (amortized analysis) -算法导论学习笔记

http://freelet.blogspot.com/2008/12/amortized-analysis.html


在平摊分析中,执行一系列数据结构操作所需要的时间是通过执行的所有操作求平均而得出的。平摊分析可以用来证明在一系列操作中,通过对所有操作求平均之后,即使其中单一的操作具有较大的代价,平均代价还是很小的。

平摊分析不牵涉到概率, 平摊分析保证在最坏情况下,每个操作具有的平均性能。

三中最常用的技术:

1.聚集分析(aggregate analysis)

 证明对所有的n n个操作所构成的序列的总时间在最坏情况下为T(n)

例子:

栈操作

PUSH(S, x):x压入S

POPS):弹出栈顶

MULTIPOPS, k):弹出栈顶k个对象

nPUSH POPMULTIPOP操作序列,其作用于一个初始为空的栈。

分析:每个操作的最坏情况时间是On. 因此n个操作的序列的代价是O (n2)

虽然这一分析正确,但这个界不够紧确,利用聚集分析,可以获得更好的上界。

一个对象在每次被压入栈后,至多被弹出一次。所以,调用POP(包括MULTIPOP)的次数至多等于PUSH的次数,即至多为n.。对任意的n,包含nPUSH, POP, MULTIPOP操作的序列的总时间为O(n). 每个操作的平均代价为O(n) / n = O(1). 聚集分析中,将每个操作的平摊代价指派为平均代价。所以三个栈操作的平摊代价都是O(1)

 

2.记帐方法

在平摊分析的记帐方法中,对不同的操作赋予不同的费用,某些操作的费用比它们的实际代价或多或少。

我们对一个操作的收费的数量称为平摊代价。当一个操作的平摊代价超过了它的实际代价时,两者的差值就被当作存款(credit),并赋予数据结构中的一些特定对象,可以用来补偿那些平摊代价低于其实际代价的操作。这种方法与聚集分析不同的是,对后者,所有操作都具有相同的平摊代价。

数据结构中存储的总存款等于总的平摊代价和总的实际代价之差。注意:总存款不能是负的。

 

例子:栈操作

实际代价:

PUSH             1

POP               1

MULTIPOP    min(k, s)

现在对它们赋予以下的平摊代价:

PUSH:           2

POP:            0

MULTIPOP    0

假设用1元钱来表示代价的单位。开始时栈时空的。

栈数据结构与餐厅中一堆叠放的盘子类似。当把一个盘子压入栈时,用1元钱来支付该动作的实际代价,还有1元的存款,将其放在盘子的顶上。盘子上面的钱时用来预付将盘子从栈中弹出所需代价的。当执行一个POP操作时,对该操作不收取任何费用,只需用盘中存放的存款来支付其实际代价即可。同理,也无需对MULTIPOP收取任何费用。

因为栈上的每个盘子上面都有1元钱,而且栈中总有非负个数的盘子。这就保证了存款的总量总是非负的。这样,对任意的包含nPUSH, POP, MULTIPOP操作的序列,总的平摊代价就是总的实际代价的一个上界。总的平摊代价为O(n), 故总的实际代价也为O(n).

 

3.势能方法

在平摊分析中,势能方法(potential  method)不是将已预付的工作作为存在数据结构特定对象中存款来表示,而是表示成一 种“势能”或“势”,它在需要时可以释放出来,以支付后面的操作。势是与整个数据结构而不是其中的个别对象发生联系的。

对一个初始数据结构D0 执行n个操作。对每个i,设ci为每个操作的实际代价,

 Di 为对数据结构Di-1执行第i个操作的结果. 势函数Φ 将每个数据结构Di 映射为一个实数Φ(Di), 即与Di相联系的势. i个操作的平摊代价ai根据势函数Φ 定义为

ai    ci   Φ(Di) Φ(Di-1)

每个操作的平摊代价为其实际代价ci加上由于该操作所增加的势。

n个操作的总的平摊代价为

ai ci Φ(Dn) Φ(D0)

如果我们能定义一个势函数Φ使得对所有n,有Φ(Dn) Φ(D0), 则总的平摊代价ai就是总的实际代价ci的一个上界。通常为了方便起见定义Φ(D0) 0

直观上看,如果第i个操作的势差Φ(Di) - Φ(Di-1)是正的,则平摊代价ai表示对第i个操作多收了费,同时数据结构的势也随之增加了。如果势差是负值,则平摊代价就表示对第i个操作的不足收费,这是通过减少势来支付该操作的实际代价。

平摊代价依赖于所选择的势函数Φ。不同的势函数可能会产生不同的平摊代价,但它们都是实际代价的上界。最佳势函数的选择取决于所需的时间界。

 

例子:栈操作

定义栈上的势函数Φ为栈中对象的个数。开始时要处理的空栈D0,且Φ(D0)0。因为栈中的对象数始终时非负的,故在第i个操作后,栈Di就具有非负的势。

Φ表示的n个操作的平摊代价的总和就表示了总的实际代价的一个上界。

现在计算各个操作的平摊代价。

如果作用于一个包含s个对象的栈上的第i个操作是PUSH

则势差为Φ(Di) Φ(Di-1) = (s+1) – s = 1

这个PUSH操作的平摊代价为ai    ci   Φ(Di) Φ(Di-1) = 1+1=2

假设栈上的第i个操作是MULTIPOP(S, k). 该操作的实际代价为min(s, k).

势差为Φ(Di) Φ(Di-1) = - min(s, k)

因此MULTIPOP操作的平摊代价为

ai    ci   Φ(Di) Φ(Di-1) min(s, k) - min(s, k) = 0

类似POP操作的平摊代价也是0

三中栈操作的每一种的平摊代价都是O(1) 这样包含n个操作的序列的总平摊代价就是O(n)。已经证明n个操作的平摊代价的总和是总的实际代价的一个上界。所以n个操作的最坏情况代价为O(n).

 

例子:

动态表,当向满的表插入一项时,将表扩大一倍,但当删除一项而引起表不足1/4满时(不是1/2),就将表缩写为原来的一半

可以利用平摊分析,证明插入和删除操作的平摊代价均为O(1).

 



  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
注:下载后,评价时给5星,还你11分 Table of Contents Introduction to Algorithms, Second Edition Preface Part I - Foundations Chapter 1 - The Role of Algorithms in Computing Chapter 2 - Getting Started Chapter 3 - Growth of Functions Chapter 4 - Recurrences Chapter 5 - Probabilistic Analysis and Randomized Algorithms Part II - Sorting and Order Statistics Chapter 6 - Heapsort Chapter 7 - Quicksort Chapter 8 - Sorting in Linear Time Chapter 9 - Medians and Order Statistics Part III - Data Structures Chapter 10 - Elementary Data Structures Chapter 11 - Hash Tables Chapter 12 - Binary Search Trees Chapter 13 - Red-Black Trees Chapter 14 - Augmenting Data Structures Part IV - Advanced Design and Analysis Techniques Chapter 15 - Dynamic Programming Chapter 16 - Greedy Algorithms Chapter 17 - Amortized Analysis Part V - Advanced Data Structures Chapter 18 - B-Trees Chapter 19 - Binomial Heaps Chapter 20 - Fibonacci Heaps Chapter 21 - Data Structures for Disjoint Sets Part VI - Graph Algorithms Chapter 22 - Elementary Graph Algorithms Chapter 23 - Minimum Spanning Trees Chapter 24 - Single-Source Shortest Paths Chapter 25 - All-Pairs Shortest Paths Chapter 26 - Maximum Flow Part VII - Selected Topics Chapter 27 - Sorting Networks Chapter 28 - Matrix Operations Chapter 29 - Linear Programming Chapter 30 - Polynomials and the FFT Chapter 31 - Number-Theoretic Algorithms Chapter 32 - String Matching Chapter 33 - Computational Geometry Chapter 34 - NP-Completeness Chapter 35 - Approximation Algorithms Part VIII - Appendix: Mathematical Background Appendix A - Summations Appendix B - Sets, Etc. Appendix C - Counting and Probability Bibliography Index List of Figures List of Corollaries List of Problems List of Exercises
Introduction to Algorithms, Second Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein ISBN:0262032937 The MIT Press © 2001 (1180 pages) A course in computer algorithms, suitable for use as a field reference for working software developers. Table of Contents Introduction to Algorithms, Second Edition Preface Part I - Foundations Chapter 1 - The Role of Algorithms in Computing Chapter 2 - Getting Started Chapter 3 - Growth of Functions Chapter 4 - Recurrences Chapter 5 - Probabilistic Analysis and Randomized Algorithms Part II - Sorting and Order Statistics Chapter 6 - Heapsort Chapter 7 - Quicksort Chapter 8 - Sorting in Linear Time Chapter 9 - Medians and Order Statistics Part III - Data Structures Chapter 10 - Elementary Data Structures Chapter 11 - Hash Tables Chapter 12 - Binary Search Trees Chapter 13 - Red-Black Trees Chapter 14 - Augmenting Data Structures Part IV - Advanced Design and Analysis Techniques Chapter 15 - Dynamic Programming Chapter 16 - Greedy Algorithms Chapter 17 - Amortized Analysis Part V - Advanced Data Structures Chapter 18 - B-Trees Chapter 19 - Binomial Heaps Chapter 20 - Fibonacci Heaps Chapter 21 - Data Structures for Disjoint Sets Part VI - Graph Algorithms Chapter 22 - Elementary Graph Algorithms Chapter 23 - Minimum Spanning Trees Chapter 24 - Single-Source Shortest Paths Chapter 25 - All-Pairs Shortest Paths Chapter 26 - Maximum Flow Part VII - Selected Topics Chapter 27 - Sorting Networks Chapter 28 - Matrix Operations Chapter 29 - Linear Programming Chapter 30 - Polynomials and the FFT Chapter 31 - Number-Theoretic Algorithms Chapter 32 - String Matching Chapter 33 - Computational Geometry Chapter 34 - NP-Completeness Chapter 35 - Approximation Algorithms Part VIII - Appendix: Mathematical Background Appendix A - Summations Appendix B - Sets, Etc. Appendix C - Counting and Probabili
中文名: 算法导论 原名: Introduction to Algorithms 作者: Thomas H. Cormen Ronald L. Rivest Charles E. Leiserson Clifford Stein 资源格式: PDF 版本: 文字版 出版社: The MIT Press书号: 978-0262033848发行时间: 2009年09月30日 地区: 美国 语言: 英文 简介: 内容介绍: Editorial Reviews Review "In light of the explosive growth in the amount of data and the diversity of computing applications, efficient algorithms are needed now more than ever. This beautifully written, thoughtfully organized book is the definitive introductory book on the design and analysis of algorithms. The first half offers an effective method to teach and study algorithms; the second half then engages more advanced readers and curious students with compelling material on both the possibilities and the challenges in this fascinating field." —Shang-Hua Teng, University of Southern California "Introduction to Algorithms, the 'bible' of the field, is a comprehensive textbook covering the full spectrum of modern algorithms: from the fastest algorithms and data structures to polynomial-time algorithms for seemingly intractable problems, from classical algorithms in graph theory to special algorithms for string matching, computational geometry, and number theory. The revised third edition notably adds a chapter on van Emde Boas trees, one of the most useful data structures, and on multithreaded algorithms, a topic of increasing importance." —Daniel Spielman, Department of Computer Science, Yale University "As an educator and researcher in the field of algorithms for over two decades, I can unequivocally say that the Cormen book is the best textbook that I have ever seen on this subject. It offers an incisive, encyclopedic, and modern treatment of algorithms, and our department will continue to use it for teaching at both the graduate and undergraduate levels, as well as a reliable research reference." —Gabriel Robins, Department of Computer Science, University of Virginia Product Description Some books on algorithms are rigorous but incomplete; others cover masses of material but lack rigor. Introduction to Algorithms uniquely combines rigor and comprehensiveness. The book covers a broad range of algorithms in depth, yet makes their design and analysis accessible to all levels of readers. Each chapter is relatively self-contained and can be used as a unit of study. The algorithms are described in English and in a pseudocode designed to be readable by anyone who has done a little programming. The explanations have been kept elementary without sacrificing depth of coverage or mathematical rigor. The first edition became a widely used text in universities worldwide as well as the standard reference for professionals. The second edition featured new chapters on the role of algorithms, probabilistic analysis and randomized algorithms, and linear programming. The third edition has been revised and updated throughout. It includes two completely new chapters, on van Emde Boas trees and multithreaded algorithms, and substantial additions to the chapter on recurrences (now called "Divide-and-Conquer"). It features improved treatment of dynamic programming and greedy algorithms and a new notion of edge-based flow in the material on flow networks. Many new exercises and problems have been added for this edition. As of the third edition, this textbook is published exclusively by the MIT Press. About the Author Thomas H. Cormen is Professor of Computer Science and former Director of the Institute for Writing and Rhetoric at Dartmouth College. Charles E. Leiserson is Professor of Computer Science and Engineering at the Massachusetts Institute of Technology. Ronald L. Rivest is Andrew and Erna Viterbi Professor of Electrical Engineering and Computer Science at the Massachusetts Institute of Technology. Clifford Stein is Professor of Industrial Engineering and Operations Research at Columbia University. 目录: Introduction 3 1 The Role of Algorithms in Computing 5 1.1 Algorithms 5 1.2 Algorithms as a technology 11 2 Getting Started 16 2.1 Insertion sort 16 2.2 Analyzing algorithms 23 2.3 Designing algorithms 29 3 Growth of Functions 43 3.1 Asymptotic notation 43 3.2 Standard notations and common functions 53 4 Divide-and-Conquer 65 4.1 The maximum-subarray problem 68 4.2 Strassen's algorithm for matrix multiplication 75 4.3 The substitution method for solving recurrences 83 4.4 The recursion-tree method for solving recurrences 88 4.5 The master method for solving recurrences 93 4.6 Proof of the master theorem 97 5 Probabilistic Analysis and Randomized Algorithms 114 5.1 The hiring problem 114 5.2 Indicator random variables 118 5.3 Randomized algorithms 122 5.4 Probabilistic analysis and further uses of indicator random variables 130 II Sorting and Order Statistics Introduction 147 6 Heapsort 151 6.1 Heaps 151 6.2 Maintaining the heap property 154 6.3 Building a heap 156 6.4 The heapsort algorithm 159 6.5 Priority queues 162 7 Quicksort 170 7.1 Description of quicksort 170 7.2 Performance of quicksort 174 7.3 A randomized version of quicksort 179 7.4 Analysis of quicksort 180 8 Sorting in Linear Time 191 8.1 Lower bounds for sorting 191 8.2 Counting sort 194 8.3 Radix sort 197 8.4 Bucket sort 200 9 Medians and Order Statistics 213 9.1 Minimum and maximum 214 9.2 Selection in expected linear time 215 9.3 Selection in worst-case linear time 220 III Data Structures Introduction 229 10 Elementary Data Structures 232 10.1 Stacks and queues 232 10.2 Linked lists 236 10.3 Implementing pointers and objects 241 10.4 Representing rooted trees 246 11 Hash Tables 253 11.1 Direct-address tables 254 11.2 Hash tables 256 11.3 Hash functions 262 11.4 Open addressing 269 11.5 Perfect hashing 277 12 Binary Search Trees 286 12.1 What is a binary search tree? 286 12.2 Querying a binary search tree 289 12.3 Insertion and deletion 294 12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 13.3 Insertion 315 13.4 Deletion 323 14 Augmenting Data Structures 339 14.1 Dynamic order statistics 339 14.2 How to augment a data structure 345 14.3 Interval trees 348 IV Advanced Design and Analysis Techniques Introduction 357 15 Dynamic Programming 359 15.1 Rod cutting 360 15.2 Matrix-chain multiplication 370 15.3 Elements of dynamic programming 378 15.4 Longest common subsequence 390 15.5 Optimal binary search trees 397 16 Greedy Algorithms 414 16.1 An activity-selection problem 415 16.2 Elements of the greedy strategy 423 16.3 Huffman codes 428 16.4 Matroids and greedy methods 437 16.5 A task-scheduling problem as a matroid 443 17 Amortized Analysis 451 17.1 Aggregate analysis 452 17.2 The accounting method 456 17.3 The potential method 459 17.4 Dynamic tables 463 V Advanced Data Structures Introduction 481 18 B-Trees 484 18.1 Definition of B-trees 488 18.2 Basic operations on B-trees 491 18.3 Deleting a key from a B-tree 499 19 Fibonacci Heaps 505 19.1 Structure of Fibonacci heaps 507 19.2 Mergeable-heap operations 510 19.3 Decreasing a key and deleting a node 518 19.4 Bounding the maximum degree 523 20 van Emde Boas Trees 531 20.1 Preliminary approaches 532 20.2 A recursive structure 536 20.3 The van Emde Boas tree 545 21 Data Structures for Disjoint Sets 561 21.1 Disjoint-set operations 561 21.2 Linked-list representation of disjoint sets 564 21.3 Disjoint-set forests 568 21.4 Analysis of union by rank with path compression 573 VI Graph Algorithms Introduction 587 22 Elementary Graph Algorithms 589 22.1 Representations of graphs 589 22.2 Breadth-first search 594 22.3 Depth-first search 603 22.4 Topological sort 612 22.5 Strongly connected components 615 23 Minimum Spanning Trees 624 23.1 Growing a minimum spanning tree 625 23.2 The algorithms of Kruskal and Prim 631 24 Single-Source Shortest Paths 643 24.1 The Bellman-Ford algorithm 651 24.2 Single-source shortest paths in directed acyclic graphs 655 24.3 Dijkstra's algorithm 658 24.4 Difference constraints and shortest paths 664 24.5 Proofs of shortest-paths properties 671 25 All-Pairs Shortest Paths 684 25.1 Shortest paths and matrix multiplication 686 25.2 The Floyd-Warshall algorithm 693 25.3 Johnson's algorithm for sparse graphs 700 26 Maximum Flow 708 26.1 Flow networks 709 26.2 The Ford-Fulkerson method 714 26.3 Maximum bipartite matching 732 26.4 Push-relabel algorithms 736 26.5 The relabel-to-front algorithm 748 VII Selected Topics Introduction 769 27 Multithreaded Algorithms Sample Chapter - Download PDF (317 KB) 772 27.1 The basics of dynamic multithreading 774 27.2 Multithreaded matrix multiplication 792 27.3 Multithreaded merge sort 797 28 Matrix Operations 813 28.1 Solving systems of linear equations 813 28.2 Inverting matrices 827 28.3 Symmetric positive-definite matrices and least-squares approximation 832 29 Linear Programming 843 29.1 Standard and slack forms 850 29.2 Formulating problems as linear programs 859 29.3 The simplex algorithm 864 29.4 Duality 879 29.5 The initial basic feasible solution 886 30 Polynomials and the FFT 898 30.1 Representing polynomials 900 30.2 The DFT and FFT 906 30.3 Efficient FFT implementations 915 31 Number-Theoretic Algorithms 926 31.1 Elementary number-theoretic notions 927 31.2 Greatest common divisor 933 31.3 Modular arithmetic 939 31.4 Solving modular linear equations 946 31.5 The Chinese remainder theorem 950 31.6 Powers of an element 954 31.7 The RSA public-key cryptosystem 958 31.8 Primality testing 965 31.9 Integer factorization 975 32 String Matching 985 32.1 The naive string-matching algorithm 988 32.2 The Rabin-Karp algorithm 990 32.3 String matching with finite automata 995 32.4 The Knuth-Morris-Pratt algorithm 1002 33 Computational Geometry 1014 33.1 Line-segment properties 1015 33.2 Determining whether any pair of segments intersects 1021 33.3 Finding the convex hull 1029 33.4 Finding the closest pair of points 1039 34 NP-Completeness 1048 34.1 Polynomial time 1053 34.2 Polynomial-time verification 1061 34.3 NP-completeness and reducibility 1067 34.4 NP-completeness proofs 1078 34.5 NP-complete problems 1086 35 Approximation Algorithms 1106 35.1 The vertex-cover problem 1108 35.2 The traveling-salesman problem 1111 35.3 The set-covering problem 1117 35.4 Randomization and linear programming 1123 35.5 The subset-sum problem 1128 VIII Appendix: Mathematical Background Introduction 1143 A Summations 1145 A.1 Summation formulas and properties 1145 A.2 Bounding summations 1149 B Sets, Etc. 1158 B.1 Sets 1158 B.2 Relations 1163 B.3 Functions 1166 B.4 Graphs 1168 B.5 Trees 1173 C Counting and Probability 1183 C.1 Counting 1183 C.2 Probability 1189 C.3 Discrete random variables 1196 C.4 The geometric and binomial distributions 1201 C.5 The tails of the binomial distribution 1208 D Matrices 1217 D.1 Matrices and matrix operations 1217 D.2 Basic matrix properties 122

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值