DOC PREVIEW
GSU CSC 2510 - ch02s4

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Proofs, Recursion and Analysis of AlgorithmsRecursive SequencesRecursive functionRecursively defined operationsRecursively defined algorithmsRecursive algorithm for selection sortSelection Sort ExampleRecursive algorithm for binary searchBinary Search ExampleClass exerciseProofs, Recursion and Analysis of AlgorithmsMathematical Structures for Computer ScienceChapter 2Copyright © 2006 W.H. Freeman & Co. MSCS Slides Proofs, Recursion and Analysis of AlgorithmsSection 2.4 Recursive Definitions 2Recursive SequencesDefinition: A sequence is defined recursively by explicitly naming the first value (or the first few values) in the sequence and then defining later values in the sequence in terms of earlier values.Examples: S(1) = 2S(n) = 2S(n-1) for n  2•Sequence S  2, 4, 8, 16, 32,….T(1) = 1T(n) = T(n-1) + 3 for n  2•Sequence T  1, 4, 7, 10, 13,….Fibonaci SequenceF(1) = 1F(2) = 1 F(n) = F(n-1) + F(n-2) for n > 2•Sequence F  1, 1, 2, 3, 5, 8, 13,….Section 2.4 Recursive Definitions 3Recursive functionAckermann functionn+1 m = 0A(m,n) = A(m-1, 1) for m > 0 and n = 0A(m-1, A(m,n-1)) for m > 0 and n > 0Find the terms A(1,1), A(2,1), A(1,2)A(1,1) = A(0, A(1,0)) = A(0, A(0,1)) = A(0,2) = 3A(1,2) = A(0, A(1,1)) = A(0, 3) = 4A(2,1) = A(1, A(2,0)) = A(1, A(1,1)) = A(1, 3) = A(0, A(1,2)) = A(0, 4) = 5Using induction it can be shown that A(1,n) = n + 2 for n = 0,1,2…A(2,n) = 2n + 3 for n = 0,1,2…Section 2.4 Recursive Definitions 4Recursively defined operationsExponential operationa0 = 1an = aan-1 for n  1Multiplication operationm(1) = m m(n) = m(n-1) + m for n  2Factorial OperationF(0) = 1F(n) = nF(n-1) for n  1Section 2.4 Recursive Definitions 5Recursively defined algorithmsIf a recurrence relation exists for an operation, the algorithm for such a relation can be written either iteratively or recursivelyExample: Factorial, multiplication etc.S(1) = 1S(n) = 2S(n-1) for n  2S(integer n) //function that iteratively computes the value S(n)Local variables: integer i ;//loop index CurrentValue ;if n =1 thenoutput 2j = 2CurrentValue = 2while j  n CurrentValue = CurrentValue *2j = j+1end whilereturn CurrentValueend ifend function SS(integer n) //recursively calculates S(n) if n =1 then //base caseoutput 2 elsereturn (2*S(n-1)) end ifend function SSection 2.4 Recursive Definitions 6Recursive algorithm for selection sort Algorithm to sort recursively a sequence of numbers in increasing or decreasing orderFunction sort(List s,Integer n) //This function sorts in increasing order if n =1 thenoutput “sequence is sorted” //base case end if max_index = 1 //assume s1 is the largest for j = 2 to n doif s[j] > s[max_index] thenmax_index = j //found larger, so updateend if end for exchange/swap s[n] and s[max_index] //move largest to the end return(sort(s,n-1)) end function sortSection 2.4 Recursive Definitions 7Selection Sort ExampleSequence S to be sorted:S: 23 12 9 –3 89 54After 1st recursive call, the sequence is: Swap -3 and 23 S: -3 12 9 23 89 54After 2nd recursive call, the sequence is:Swap 12 and 9S: -3 9 12 23 89 54After 3rd and 4th recursive call, the sequence is:Nothing is swapped for the next two recursive calls as elements happen to be in increasing order.S: -3 9 12 23 89 54After 5th recursive call, the sequence is:S: -3 9 12 23 54 89Final sorted array S: -3 9 12 23 54 89Section 2.4 Recursive Definitions 8Recursive algorithm for binary searchLooks for a value in an increasing sequence and returns the index of the value if it is found or 0 otherwise.Function binary_search(s, j, k, key)if j > k then //not foundreturn 0 end if m = (j+k)/2 if key = sk then // base case return (m) end if if key < sk then k = m-1 else j = m+1 end if return(binary_search(s,j,k,key)) end binary_searchSection 2.4 Recursive Definitions 9Binary Search ExampleSearch for 81 in the sequence 3 6 18 37 76 81 92Sequence has 7 elements.Calculate middle point: (1 + 7)/2 = 4.Compares 81 to 37 (4th sequence element): no match.Search in the lower half since 81 > 37New sequence for search: 76 81 92Calculate midpoint: (5 + 7)/2 = 66th Element: 81 Compares 81 to 81: perfect match Element 81 found as the 6th element of the sequenceSection 2.4 Recursive Definitions 10Class exerciseWhat does the following function calculate?Function mystery(integer n) if n = 1 thenreturn 1 elsereturn (mystery(n-1)+1) end if end function mysteryWrite the algorithm for the recursive definition of the following sequencea, b, a+b, a+2b, 2a+3b,


View Full Document

GSU CSC 2510 - ch02s4

Documents in this Course
ch02s2

ch02s2

5 pages

ch04s2

ch04s2

8 pages

ch01s6

ch01s6

10 pages

ch01s1

ch01s1

21 pages

ch08s2

ch08s2

21 pages

ch08s3

ch08s3

15 pages

ch07s3

ch07s3

21 pages

ch04s3

ch04s3

23 pages

ch06s3

ch06s3

16 pages

ch01s3

ch01s3

15 pages

ch03s4

ch03s4

11 pages

ch03s6

ch03s6

6 pages

ch03s5

ch03s5

9 pages

ch06s2

ch06s2

9 pages

ch03s1

ch03s1

19 pages

ch07s1

ch07s1

11 pages

ch02s1

ch02s1

14 pages

ch08s1

ch08s1

15 pages

ch02s5

ch02s5

9 pages

ch01s4

ch01s4

13 pages

Load more
Download ch02s4
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view ch02s4 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view ch02s4 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?