Download presentation
Presentation is loading. Please wait.
Published byQuentin Small Modified over 9 years ago
1
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm Example (4 slides) Partitioning and Merging of Sublists in mergeSort() Partitioning and Merging of Sublists in mergeSort() Function calls in mergeSort() Function calls in mergeSort() Quicksort Example (8 slides) Quicksort Example (8 slides) Finding k th – Largest Element Finding k th – Largest Element powerSet() Example powerSet() Example Chapter 15 – Advanced Recursive Algorithms Recursive calls for fib(5) Recursive calls for fib(5) Effect of fib(5) Using Dynamic Programming ffect of fib(5) Using Dynamic Programmingffect of fib(5) Using Dynamic Programming The 8-Queens Example (3 slides) The 8-Queens Example (3 slides) Summary Slides (6 slides) Summary Slides (6 slides)
2
Main Index Contents 22 Main Index Contents 2 Main Index Contents 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 drawRuler() 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
Main Index Contents 3 The Merge Algorithm Example The merge algorithm takes a sequence of elements in a vector v having index range [first, last). The sequence consists of two ordered sublists separated by an intermediate index, called mid.
4
Main Index Contents 44 Main Index Contents The Merge Algorithm… (Cont…)
5
Main Index Contents 55 Main Index Contents The Merge Algorithm… (Cont…)
6
Main Index Contents 6 The Merge Algorithm… (Cont…)
7
Main Index Contents 77 Main Index Contents Partitioning and Merging of Sublists in mergeSort()
8
Main Index Contents 8 Function calls in mergeSort()
9
Main Index Contents 9 Quicksort Example The quicksort algorithm 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
Main Index Contents 10 Quicksort Example (Cont…)
11
Main Index Contents 11 Main Index Contents Quicksort Example (Cont…)
12
Main Index Contents 12 Quicksort Example (Cont…)
13
Main Index Contents 13 Quicksort Example (Cont…)
14
Main Index Contents 14 Quicksort Example (Cont…)
15
Main Index Contents 15 Quicksort Example (Cont…)
16
Main Index Contents 16 Main Index Contents Quicksort Example (Cont…)
17
Main Index Contents 17 Finding K th – Largest Element To locate the position of the k th -largest value (kLargest) in the list, partition the elements into two disjoint sublists. The lower sublist 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 condition applies to the upper sublist.
18
Main Index Contents 18 Main Index Contents powerSet() Example
19
Main Index Contents 19 Recursive calls for fib(5)
20
Main Index Contents 20 Main Index Contents Affect of fib(5) Using Dynamic Programming
21
Main Index Contents 21 The 8-Queens Example
22
Main Index Contents 22 Main Index Contents The 8-Queens Example (Cont…)
23
Main Index Contents 23 Main Index Contents The 8-Queens Example (Cont…)
24
Main Index Contents 24 Main Index Contents Summary Slide 1 §- Divide-and-Conquer Algorithms - splits a problem into subproblems 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 log 2 n), requires the use of an auxiliary vector to perform the merge steps.
25
Main Index Contents 25 Main Index Contents 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 pivot and all elements in the upper interval have values pivot. - running time: O(n log 2 n) - worst case: of O(n 2 ), highly unlikely to occur
26
Main Index Contents 26 Main Index Contents Summary Slide 3 §- Recursion in Combinatorics - A set of n elements has 2 n subsets, and the set of those subsets is called the power set. By using a divide and 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 this algorithm depends on the passing of a vector by value to the recursive function.
27
Main Index Contents 27 Main Index Contents 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 recursive calls and leads to an O(n) algorithm that computes the n th 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
Main Index Contents 28 Main Index Contents Summary Slide 5 2) bottom-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
Main Index Contents 29 Main Index Contents 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 again from a new consistent partial solution. - takes "1 step forward and n steps backward". - solving advanced problems in graph theory and operations research. - Example: The 8-Queens, developing a series of free functions and the class chessboard.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.