Data Structures with C++ using STL Chapter 15.ppt_第1页
Data Structures with C++ using STL Chapter 15.ppt_第2页
Data Structures with C++ using STL Chapter 15.ppt_第3页
Data Structures with C++ using STL Chapter 15.ppt_第4页
Data Structures with C++ using STL Chapter 15.ppt_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1,Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Partitioning and Merging of Sublists in mergeSort() Function calls in mergeSort() Quicksort Example (8 slides) Finding kth Largest Element powerSet() Example,Chapter 15 Advanced Recursive Algorithms,Recursive calls for fib(5) Effect

2、of fib(5) Using Dynamic Programming The 8-Queens Example (3 slides) Summary Slides (6 slides),2,Building a Ruler: drawRuler(),Problem: create a program that draws marks at regular intervals on a line. The sizes of the marks differ, depending on the specific interval. - The recursive function drawRul

3、er() assumes the existence of the function drawMark(), which takes a point x and an integer value h as arguments and draws a mark at point x with size proportional to h.,3,The Merge Algorithm Example,The merge algorithm takes a sequence of elements in a vector v having index range first, last). The

4、sequence consists of two ordered sublists separated by an intermediate index, called mid.,4,The Merge Algorithm (Cont),5,The Merge Algorithm (Cont),6,The Merge Algorithm (Cont),7,Partitioning and Merging of Sublists in mergeSort(),8,Function calls in mergeSort(),9,Quicksort Example,The quicksort alg

5、orithm uses a series of recursive calls to partition a list into smaller and smaller sublists about a value called the pivot. Example: Let v be a vector containing 10 integer values: v = 800, 150, 300, 650, 550, 500, 400, 350, 450, 900,10,Quicksort Example (Cont),11,Quicksort Example (Cont),12,Quick

6、sort Example (Cont),13,Quicksort Example (Cont),14,Quicksort Example (Cont),15,Quicksort Example (Cont),16,Quicksort Example (Cont),17,Finding Kth Largest Element,To locate the position of the kth-largest value (kLargest) in the list, partition the elements into two disjoint sublists. The lower subl

7、ist must contain k elements that are less than or equal to kLargest and the upper sublist must contain elements that are greater than or equal to kLargest. The elements in the lower sublist do not need to be ordered but only to have values that are less than or equal to kLargest. The opposite condit

8、ion applies to the upper sublist.,18,powerSet() Example,19,Recursive calls for fib(5),20,Affect of fib(5) Using Dynamic Programming,21,The 8-Queens Example,22,The 8-Queens Example (Cont),23,The 8-Queens Example (Cont),24,Summary Slide 1,- Divide-and-Conquer Algorithms - splits a problem into subprob

9、lems and works on each part separately - Two type of divide-and-conquer strategy: 1) mergesort algorithm - Split the range of elements to be sorted in half, sort each half, and then merge the sorted sublists together. - running time: O(n log2n), requires the use of an auxiliary vector to perform the

10、 merge steps.,25,Summary Slide 2,2) quicksort algorithm - uses a partitioning strategy that finds the final location of a pivot element within an interval first,last). - The pivot splits the interval into two parts, first, pivotIndex), pivotIndex,last). All elements in the lower interval have values

11、 pivot and all elements in the upper interval have values pivot. - running time: O(n log2n) - worst case: of O(n2), highly unlikely to occur,26,Summary Slide 3,- Recursion in Combinatorics - A set of n elements has 2n subsets, and the set of those subsets is called the power set. By using a divide a

12、nd conquer strategy that finds the power set after removing an element from the set and then adds the element back into each subset, we implement a function that computes the power set. The section also uses recursion to list all the n! permutations of the integers from 1 through n. The success of t

13、his algorithm depends on the passing of a vector by value to the recursive function.,27,Summary Slide 4,- Dynamic Programming - Two type of dynamic programming: 1) top-down dynamic programming - uses a vector to store Fibonacci numbers as a recursive function computes them - avoids costly redundant

14、recursive calls and leads to an O(n) algorithm that computes the nth Fibonacci number. - recursive function that does not apply dynamic programming has exponential running time. - improve the recursive computation for C(n,k), the combinations of n things taken k at a time.,28,Summary Slide 5,2) bott

15、om-up dynamic programming - evaluates a function by computing all the function values in order, starting at the lowest level and using previously computed values at each step to compute the current value. - 0/1 knapsack problem,29,Summary Slide 6,-Backtracking Algorithm - finds a consistent partial solution to a problem and then tries to extend the partial solution to a complete solution by executing a recursive step. If the recursive step fails to find a solution, it returns to the previous state and the algorithm tries a

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论