Upload
quentin-small
View
227
Download
0
Embed Size (px)
Citation preview
1 Main IndexMain Index ContentsContents1 Main IndexMain Index ContentsContents
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 Chapter 15 – – Advanced Recursive AlgorithmsAdvanced Recursive Algorithms
Recursive calls for fib(5)
EEffect of fib(5) Using Dynamic Programming
The 8-Queens Example (3 slides)
Summary Slides (6 slides)
2 Main IndexMain Index ContentsContents2 Main IndexMain Index ContentsContents2 Main IndexMain Index ContentsContents
Building a Ruler: drawRuler()Building a Ruler: drawRuler()
1 /20 1
(a)
1 /40 1
3 /41 /2 1 /40 1
3 /41 /21 /8 3 /8 7 /85 /8
d raw R u ler(0 .0 , 1 .0 , 3 )
(b )
d raw R u ler(0 .0 , 0 .5 , 2 )d raw R u ler(0 .5 , 1 .0 , 2 )
(c)
d raw R u ler(0 .0 , 0 .2 5 , 1 )
d raw R u ler(0 .2 5 , 0 .5 , 1 ) d raw R u ler(0 .7 5 , 1 .0 , 1 )
d raw R u ler(0 .5 , 0 .7 5 , 1 )
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 IndexMain Index ContentsContents
The Merge Algorithm ExampleThe Merge Algorithm Example
7 1 0 1 9 2 5 1 2 1 7 2 1 3 0 4 8
s u b lis t Bs u b lis t A
firs t las tm id
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 IndexMain Index ContentsContents4 Main IndexMain Index ContentsContents
The Merge Algorithm… (Cont…) The Merge Algorithm… (Cont…)
7 1 0 1 9 2 5 1 2 1 7 2 1 3 0 4 8
7
s u b lis t Bs u b lis t A
in d exA in d exB
t em p Vect o r
7 1 0
t em p Vect o r
7 1 0 1 9 2 5 1 2 1 7 2 1 3 0 4 8
s u b lis t Bs u b lis t A
in d exA in d exrB
5 Main IndexMain Index ContentsContents5 Main IndexMain Index ContentsContents
The Merge Algorithm… (Cont…)The Merge Algorithm… (Cont…)
7 1 0 1 9 2 5 1 2 1 7 2 1 3 0 4 8
7 1 0 1 2
s u b lis t Bs u b lis t A
in d exA in d exB
t em p Vect o r
7 1 0 1 9 2 5 1 2 1 7 2 1 3 0 4 8
7 1 0 1 2 1 7 1 9 2 1 2 5
s u b lis t Bs u b lis t A
in d exA in d exB
t em p Vect o r
6 Main IndexMain Index ContentsContents
The Merge Algorithm… (Cont…)The Merge Algorithm… (Cont…)
7 1 0 1 9 2 5 1 2 1 7 2 1 3 0 4 8
7 1 0 1 2 1 7 1 9 2 1 2 5 3 0 4 8
s u b lis t Bs u b lis t A
in d exA in d exB
t em p Vect o r
las t
7 10 12 17 19 21 25 30 48
t e m p Ve c t o r
7 10 12 17 19 21 25 30 48
firs t la s t
7 Main IndexMain Index ContentsContents7 Main IndexMain Index ContentsContents
Partitioning and Merging of Partitioning and Merging of Sublists in mergeSort()Sublists in mergeSort()
(2 5 1 0 7 1 9 3 4 8 1 2 1 7 5 6 3 0 2 1 )
(2 5 1 0 7 1 9 3 )
(7 1 9 3 )(2 5 1 0 )
[3 7 1 0 1 9 2 5 ]
(4 8 1 2 1 7 5 6 3 0 2 1 )
(5 6 3 0 2 1 )(4 8 1 2 1 7 )
[1 2 1 7 2 1 3 0 4 8 5 6 ]
[3 7 1 0 1 2 1 7 1 9 2 1 2 5 3 0 4 8 5 6 ]
(1 0 )
[1 0 2 5 ]
(2 5 ) (1 9 3 )(7 )
[3 7 1 9 ]
(5 6 )
[2 1 3 0 5 6 ]
(1 2 1 7 )(4 8 )
[1 2 1 7 4 8 ]
(2 1 )(3 )
[3 1 9 ]
(1 9 )
(3 0 2 1 )
[2 1 3 0 ]
(3 0 )(1 7 )
[1 2 1 7 ]
(1 2 )
8 Main IndexMain Index ContentsContents
Function calls in mergeSort()Function calls in mergeSort()
C all m s o rt () - recu s iv e call1 t o m s o rt () - recu s iv e call2 t o m s o rt () - call1 m erge()
m s o rt () : n /2 m s o rt (): n /2
m s o rt (): n /4
m s o rt (): n /8 m s o rt (): n /8
m s o rt (): n /4
m s o rt (): n /8 m s o rt (): n /8
m s o rt (): n /4
m s o rt (): n /8 m s o rt (): n /8
m so r t (): n /4
m s o rt (): n /8 m s o rt (): n /8
L ev el 0 :
L ev el 3 :
L ev el 2 :
L ev el 1 :
L ev el i:
. . .
9 Main IndexMain Index ContentsContents
Quicksort ExampleQuicksort Example
1 5 0 3 0 0 6 5 0 5 5 0 8 0 0 4 0 0 3 5 0 4 5 0
s can U p s can D o w n
v [0 ] v [9 ]v [8 ]v [7 ]v [6 ]v [5 ]v [4 ]v [3 ]v [2 ]v [1 ]
p iv o t
5 0 0 9 0 0
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 IndexMain Index ContentsContents
Quicksort Example (Cont…)Quicksort Example (Cont…)B efo re t h e exch an ge
A ft er t h e exch an ge an d u p d at es t o s can U p an d s can D o w n
1 5 0 3 0 0 6 5 0 5 5 0 8 0 0 4 0 0 3 5 0 4 5 0
s can U p s can D o w n
v [0 ] v [9 ]v [8 ]v [7 ]v [6 ]v [5 ]v [4 ]v [3 ]v [2 ]v [1 ]
p iv o t
5 0 0 9 0 0
1 5 0 3 0 0 4 5 0 5 5 0 8 0 0 4 0 0 3 5 0 6 5 0
s can U p s can D o w n
v [0 ] v [9 ]v [8 ]v [7 ]v [6 ]v [5 ]v [4 ]v [3 ]v [2 ]v [1 ]
p iv o t
5 0 0 9 0 0
11 Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents
Quicksort Example (Cont…)Quicksort Example (Cont…)B efo re t h e exch an ge
A ft er t h e exch an ge an d u p d at es t o s can U p an d s can D o w n
1 5 0 3 0 0 4 5 0 5 5 0 8 0 0 4 0 0 3 5 0 6 5 0
s can U p s can D o w n
v [0 ] v [9 ]v [8 ]v [7 ]v [6 ]v [5 ]v [4 ]v [3 ]v [2 ]v [1 ]
p iv o t
5 0 0 9 0 0
1 5 0 3 0 0 4 5 0 3 5 0 8 0 0 4 0 0 5 5 0 6 5 0
s can U p s can D o w n
v [0 ] v [9 ]v [8 ]v [7 ]v [6 ]v [5 ]v [4 ]v [3 ]v [2 ]v [1 ]
p iv o t
5 0 0 9 0 0
12 Main IndexMain Index ContentsContents
Quicksort Example (Cont…)Quicksort Example (Cont…)
B efo re t h e exch an ge
A ft er t h e exch an ge an d u p d at es t o s can U p an d s can D o w n
1 5 0 3 0 0 4 5 0 3 5 0 8 0 0 4 0 0 5 5 0 6 5 0
s can U p s can D o w n
v [0 ] v [9 ]v [8 ]v [7 ]v [6 ]v [5 ]v [4 ]v [3 ]v [2 ]v [1 ]
p iv o t
5 0 0 9 0 0
1 5 0 3 0 0 4 5 0 3 5 0 4 0 0 8 0 0 5 5 0 6 5 0
s can U ps can D o w n
v [0 ] v [9 ]v [8 ]v [7 ]v [6 ]v [5 ]v [4 ]v [3 ]v [2 ]v [1 ]
p iv o t
5 0 0 9 0 0
13 Main IndexMain Index ContentsContents
Quicksort Example (Cont…)Quicksort Example (Cont…)
4 0 0 1 5 0 3 0 0 4 5 0 3 5 0 5 0 0 8 0 0 5 5 0 6 5 0
v [0 ] v [9 ]v [8 ]v [7 ]v [6 ]v [5 ]v [4 ]v [3 ]v [2 ]v [1 ]
9 0 0
P iv o t in it s fin al p o s it io n
4 0 0 1 5 0 3 0 0 4 5 0 3 5 0 5 0 0 8 0 0 5 5 0 6 5 0
v [0 ] v [9 ]v [8 ]v [7 ]v [6 ]v [5 ]v [4 ]v [3 ]v [2 ]v [1 ]
v [0 ] - v [4 ] v [6 ] - v [9 ]
9 0 0
14 Main IndexMain Index ContentsContents
Quicksort Example (Cont…)Quicksort Example (Cont…)p iv o t
3 0 0 1 5 0 4 0 0 4 5 0 3 5 0
s can U p
v [0 ] v [4 ]v [3 ]v [2 ]v [1 ]
In it ial Valu es
s can D o w n
p iv o t
3 0 0 1 5 0 4 0 0 4 5 0 3 5 0
v [0 ] v [4 ]v [3 ]v [2 ]v [1 ]
s can U p
A ft er Scan s St o p
s can D o w n
1 5 0 3 0 0 4 0 0 4 5 0 3 5 0
v [0 ] v [4 ]v [3 ]v [2 ]v [1 ]
15 Main IndexMain Index ContentsContents
Quicksort Example (Cont…)Quicksort Example (Cont…)p iv o t
6 5 0 5 5 0 8 0 0 9 0 0
s can U p
v [6 ] v [9 ]v [8 ]v [7 ]
In it ial Valu es
s can D o w n
p iv o t
6 5 0 5 5 0 8 0 0 9 0 0
v [6 ] v [9 ]v [8 ]v [7 ]
s can U p
A ft er St o p s
s can D o w n
5 5 0 6 5 0 8 0 0 9 0 0
v [6 ] v [9 ]v [8 ]v [7 ]
16 Main IndexMain Index ContentsContents16 Main IndexMain Index ContentsContents
Quicksort Example (Cont…)Quicksort Example (Cont…)
v [0 ] v [4 ]v [3 ]v [2 ]v [1 ] v [6 ] v [9 ]v [8 ]v [7 ]v [5 ]
1 5 0 9 0 08 0 06 5 05 5 05 0 03 5 04 5 04 0 03 0 0
4 0 0 4 5 0 3 5 0
v [4 ]v [3 ]v [2 ]
B efo re P art it io n in g
3 5 0 4 0 0 4 5 0
v [4 ]v [3 ]v [2 ]
A ft er P art it io n in g
1 5 0 3 0 0 3 5 0 4 0 0 4 5 0 5 0 0
v [0 ] v [4 ]v [3 ]v [2 ]v [1 ]
5 5 0 6 5 0 8 0 0 9 0 0
v [6 ] v [9 ]v [8 ]v [7 ]v [5 ]
17 Main IndexMain Index ContentsContents
Finding KFinding Kthth – Largest Element – Largest Element
v alu es < k L arges t k L arges t v alu es k L arges t
k0 ... k -1 k + 1 ... n -1
To locate the position of the kth-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 IndexMain Index ContentsContents18 Main IndexMain Index ContentsContents
powerSet() ExamplepowerSet() Example
12
3 { 1 }2
3
S tar t w ithS = { 1 , 2 , 3 }
R em o v e 1 f r o m SS ' = { 2 , 3 }
P o w er S e t f o r S '( c o n ta in s 2 2 = 4 e lem en ts )
2
3 2 3
Ad d 1 to eac h s e t( c o n ta in s 2 2 = 4 e lem en ts )
1
2 31 3
12
1
2
3 2 3
1
2 31 3
12
1
P o w er S e t f o r S( c o n ta in s 2 2 + 2 2 = 2 3 = 8 e lem en ts )
19 Main IndexMain Index ContentsContents
Recursive calls for fib(5)Recursive calls for fib(5)
fib (5 )
fib (4 ) fib (3 )
fib (3 ) fib (2 ) fib (2 ) fib (1 )
fib (2 ) fib (1 ) fib (1 ) fib (0 ) fib (1 ) fib (0 )
fib (1 ) fib (0 )
20 Main IndexMain Index ContentsContents20 Main IndexMain Index ContentsContents
Affect of fib(5) Using Dynamic Affect of fib(5) Using Dynamic ProgrammingProgramming
fib (5 )
fib (4 ) fib (3 )
fib (3 ) fib (2 ) fib (2 ) fib (1 )
fib (2 ) fib (1 ) fib (1 ) fib (0 ) fib (1 ) fib (0 )
fib (1 ) fib (0 )
6
5
21
3
4
21 Main IndexMain Index ContentsContents
The 8-Queens ExampleThe 8-Queens Example
22 Main IndexMain Index ContentsContents22 Main IndexMain Index ContentsContents
The 8-Queens Example (Cont…)The 8-Queens Example (Cont…)
0
1
2
3
4
5
6
7
0 1 2 3 4 5 6 7
23 Main IndexMain Index ContentsContents23 Main IndexMain Index ContentsContents
The 8-Queens Example (Cont…)The 8-Queens Example (Cont…)
A t (4 ,4 ) at t ack fro m (0 ,0 ) A t (5 ,4 ) at t ack fro m (2 ,1 ) A t (6 ,4 ) at t ack fro m (4 ,2 ) A t (7 ,4 ) s u cces s
0
1
2
3
4
5
6
7
0 1 2 3 4 5 6 7
24 Main IndexMain Index ContentsContents24 Main IndexMain Index ContentsContents
Summary Slide 1Summary 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 log2n), requires the use of an auxiliary vector to perform the merge steps.
25 Main IndexMain Index ContentsContents25 Main IndexMain Index ContentsContents
Summary Slide 2Summary 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 log2n)
- worst case: of O(n2), highly unlikely to occur
26 Main IndexMain Index ContentsContents26 Main IndexMain Index ContentsContents
Summary Slide 3Summary 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 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 IndexMain Index ContentsContents27 Main IndexMain Index ContentsContents
Summary Slide 4Summary 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 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 Main IndexMain Index ContentsContents28 Main IndexMain Index ContentsContents
Summary Slide 5Summary 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 IndexMain Index ContentsContents29 Main IndexMain Index ContentsContents
Summary Slide 6Summary 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.