CS1102 Tut 4 Recursion and Big Oh - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

CS1102 Tut 4 Recursion and Big Oh

Description:

Hence h levels has O(hn) computations. h = log n, therefore. Time complexity is O(nlog n) ... swap exists and simply swaps the contents of the two arguments. ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 33
Provided by: dcsa
Category:

less

Transcript and Presenter's Notes

Title: CS1102 Tut 4 Recursion and Big Oh


1
CS1102 Tut 4 Recursion and Big Oh
  • Max Tan
  • tanhuiyi_at_comp.nus.edu.sg
  • S15-03-07 Tel65164364http//www.comp.nus.edu.sg
    /tanhuiyi

2
Groups Assignment
  • Question 1 Group 1
  • Question 2 Group 2
  • Question 3 Group 3
  • Question 4 Group 4
  • Question 5 Group 1
  • Question 6 Group 2
  • Question 7 Group 3

3
(No Transcript)
4
First 15 minutes
  • Recap on MergeSort and QuickSort
  • Insertion sort, selection sort, bubble sort are
    very simple O(n2) algorithms.

5
First 15 minutes
  • How does mergeSort work?
  • MergeSort(Ai..j) Precondition
    NilPostcondition Ai..j is sorted

6
First 15 minutes
  • MergeSort(Ai..j) Precondition
    NilPostcondition Ai..j is sorted
  • MergeSort on leftHalf
  • MergeSort on rightHalf
  • Merge 2 sorted halves.

7
First 15 minutes
mergeSort Sorts an imput array
Unsorted array
divide
mergeSort
mergeSort
Unsorted array
After mergesort is done, array is sorted!
8
First 15 minutes
mergeSort Sorts an imput array
Unsorted array
divide
mergeSort
mergeSort
Sorted arrays
9
First 15 minutes
mergeSort Sorts an imput array
Unsorted array
divide
mergeSort
mergeSort
Sorted arrays
Merge
10
First 15 minutes
mergeSort Sorts an imput array
Unsorted array
divide
mergeSort
mergeSort
Sorted arrays
Merge
Output
11
First 15 minutes
  • Time complexity analysis

Assume the height of this tree is h Number of
elements in each array in first level n Number
of elements in each array in 2nd level n/2 in
kth level n/2k-1 In the hth level, n/2h-1 1 n
2h-1 lg n h-1 h lg n 1
h
12
First 15 minutes
  • Time complexity analysis

h lg n 1 Each level has O(n)
computations Hence h levels has O(hn)
computations h log n, therefore Time complexity
is O(nlog n)
h
13
First 15 minutes
  • QuickSort(Ai..j) Precondition
    NilPostcondition Ai..j is sorted
  • Find a pivot and position the elements
    accordingly
  • QuickSort on left
  • QuickSort on right

14
First 15 minutes
quickSort Sorts an imput array
Unsorted array
pivot
Find pivot
quickSort
quickSort
15
First 15 minutes
quickSort Sorts an imput array
Unsorted array
pivot
Find pivot
quickSort
quickSort
16
First 15 minutes
  • QuickSort
  • QuickSort(Ai..j) Precondition
    NilPostcondition Ai..j is sorted

17
Question 1
  • Given the following recursive function
  • int f(int n, int m)
  • if (n 0) return m
  • else return f(n-1, 1-m)
  • What is the result of f(3,7)?
  • (a) 0
  • (b) 3
  • (c) 7
  • (d) -7
  • (e) None of the above

18
Question 1Solution
  • (e)
  • f(3,7)
  • f(2,-6)
  • f(1, 7)
  • f(0, -6)
  • -6

19
Question 2
  • Given the following recursive function
  • int g(int n)
  • if (n 0) return 1
  • else return g(n-1) g(n-2)
  • Which of the following statement is true?
  • (a) It does not have a base case
  • (b) It is not recursive
  • (c) The function does not terminate for all n
  • (d) None of the above

20
Question 2Solution
  • (d)
  • g(n) where n is 0 terminates
  • As g(0) 1
  • All other values do not terminate.
  • g(-1) does not terminate, so g(1) also does not
    terminate

21
Question 3
  • Consider the following function where the input n
    is assumed to be positive integer.
  • f(n) n2 (n-1)2 22 12
  • Write a recursive function to implement it and a
    main() function to display the first 10 function
    values. What if n is not positive?

22
Question 3Solution
  • int f(int n)
  • if (n lt0) return 0 // To be defensive
  • return nn f(n-1)
  • void main(String args)
  • System.out.println(n\tf(n))
  • for (int i 1 i lt10 i)
  • System.out.println(i \t f(i))

23
Question 4
  • int h(int n)
  • if (n lt 3) return 1
  • else return 1 h(n-1) h(n-3)
  • (i) Compute the value of h(10)
  • (ii) Explain why the given (original) definition
    of h is inefficient. Based on the above
    observation (about how to compute a h call
    efficiently), show how you can provide a more
    efficient h implementation.

24
Question 4Solution (1)
  • h(10) 1 h(9) h(7)
  • h(9) 1 h(8) h(6)
  • h(8) 1 h(7) h(5)
  • h(7) 1 h(6) h(4)
  • h(6) 1 h(5) h(3)
  • h(5) 1 h(4) h(2)
  • h(4) 1 h(3) h(1)
  • h(3) 1
  • h(2) 1
  • h(1) 1
  • The same computations are repeatedly executed
    when it is called with the same value and hence
    the recursive solution is inefficient

25
Question 4Solution (1)
  • To improve its performance, use iterative
    solution.
  • int h(int n)
  • if (n lt3) return 1
  • int p11 int p21 int p31
  • int p
  • for (int i 4 i lt n i)
  • p 1 p1 p3
  • p3 p2
  • p2 p1
  • p1 p
  • return p

26
Question 4Solution (3)
  • compute h(0), h(1), , until h(10) will be easier
    to get the answer
  • h(0) 1
  • h(1) 1
  • h(2) 1
  • h(3) 1
  • h(4) 3
  • h(5) 5
  • h(6) 7
  • h(7) 11
  • h(8) 17
  • h(9) 25
  • h(10) 37

27
Question 5
  • int method( int n)
  • if (n lt 1) return 1
  • return method(n/2) method(1)
  • The tightest time complexity of this method is
  • (a) O(log n)
  • (b) O(n)
  • (c) O(n2)
  • (d) O(2n)
  • (e) O(n2n)

28
Question 5Solution
  • (a)
  • Domain of interest is reduced by half for each
    recursive call.

29
Question 6
  • What is the time complexity of each of the
    following tasks in the worst case?
  • a. Computing the sum of the first n even integers
    by using a for loop
  • b. Displaying all n integers in an array
  • c. Displaying all n integers in a sorted linked
    list
  • d. Displaying all n names in a circular linked
    list
  • e. Displaying one array element
  • f. Displaying the last integer in a linked list
  • g. Searching a sorted array of n integers for a
    particular value by using a binary search
  • h. Adding an item to a stack of n items
  • i. Adding an item to a queue of n items

30
Question 6Solution
  • O(n)
  • O(n)
  • O(n)
  • O(n)
  • O(1)
  • O(n)
  • O( log n)
  • O(1)
  • O(1)
  • a. Computing the sum of the first n even integers
    by using a for loop
  • b. Displaying all n integers in an array
  • c. Displaying all n integers in a sorted linked
    list
  • d. Displaying all n names in a circular linked
    list
  • e. Displaying one array element
  • f. Displaying the last integer in a linked list
  • g. Searching an array of n integers for a
    particular value by using a binary search
  • h. Adding an item to a stack of n items
  • i. Adding an item to a queue of n items

31
Question 7
  • Consider the following method f, which calls the
    method swap. Assume that swap exists and simply
    swaps the contents of the two arguments. Do not
    be concerned with fs purpose.
  • void f(int theArray, int n)
  • for (int j 0 j lt n j)
  • int i 0
  • while (i lt 1)
  • if (theArrayi lt theArrayj)
  • swap(theArrayi, theArrayj)
  • i
  • // end while
  • // end for
  • // end f
  • How many comparisons does f perform?

32
Question 7Solution
  • O(n)
  • as the statements in the while loop only execute
    once for each j because of the condition ilt1.
Write a Comment
User Comments (0)
About PowerShow.com