CS1102 Tut 5 Recursion and Complexity Analysis - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

CS1102 Tut 5 Recursion and Complexity Analysis

Description:

4n2, log3(n) 20n, 3lg n, 2, lg(n), nn, 3n, nlg(n), 100n2/3, 2n, ... 2, lg(n) = log3(n), 100n2/3, 20n, nlg(n), 3lg n, 4n2, 2n, 2n 1, 3n, nn, Why is 3lg n 4n2 ? ... – PowerPoint PPT presentation

Number of Views:147
Avg rating:3.0/5.0
Slides: 24
Provided by: soc128
Category:

less

Transcript and Presenter's Notes

Title: CS1102 Tut 5 Recursion and Complexity Analysis


1
CS1102 Tut 5 Recursion and Complexity Analysis
  • Max Tan
  • tanhuiyi_at_comp.nus.edu.sg
  • S15-03-07 Tel65164364http//www.comp.nus.edu.sg
    /tanhuiyi

2
Question 1
  • Revisit the MineSweeper ADT in Tutorial 2
  • Uncover() uncovers a single square in MineSweeper
  • Write a UncoverRecursive() to uncover squares
    which meets the condition

3
Question 1
  • Where is the recursion?
  • When we uncover a tile and its neighbours have no
    mines, we want to open its neighbours too.

4
Question 1
  • What is the base case?
  • A tile is not within the confines of a grid
  • A tile is already uncovered
  • A tile has more then 0 neighbouring mines (it has
    a number on it)
  • What is the recursive case?
  • A tile has 0 neighbouring mines

5
Question 1
  • public void uncoverRecursive(int x, int
    y) //Base case, if the tile we want to open is
    at //the edge, then return
  • if(x lt 0 x gt grid0.length y lt 0 y
    gt grid.length)
  • return
  • //Base case, if tile is uncovered, do not
  • //perform any action
  • if(isUncovered(x,y))
  • return

6
Question 1
  • //Another base case, if tile has more then
  • //zero mines, then just uncover it
  • if(getNeighborMines(x,y) gt 0)
  • uncover(x,y)
  • return
  • //Recursive case
  • else //getNeighbourMines(x,y) 0
  • //Uncover itself first
  • uncover(x,y)
  • //Uncover its neighbours
  • for(int i x-1 i lt x1 i)
  • for(int j y-1 j lt y1 j)
  • uncoverRecursive(i, j)

Notice that I do not check here if (i,j) is
valid! Thats ok because the previous base case
checks if (i,j) is out of the board
7
Question 2
  • Merging two lists recursively
  • What are the base cases?
  • List1 is empty, List2 has more nodes
  • List2 is empty, List1 has more nodes
  • Both lists are empty
  • What is the recursive case?
  • Both lists are not empty

8
Question 2
  • Consider this -
  • What is the head element in the two list?
  • What is the next element of the head?

9
Question 2
  • Visually look at the algorithm

3
What is the head of this two list ? What is the
next element of this head?
head2
sortedHead
head1
10
Question 2
  • Visually look at the algorithm

merge(head1, head2)
Can we look at the remaining elements in another
way? Just call merge recursively on the remaining
elements! What should be next element of node
3??? It should just be the head element of the
remaining elements!
sortedHead
11
Question 2
merge(head1, head2)
Find the head in these list Recursive call merge
on the remainder of the two lists! Return the
head of this list
head1
head2
12
Question 2
  • When do we terminate (base case)
  • When either list is empty!

13
Question 2
  • Visually look at the algorithm

merge(head1, head2)
We want to write the code in such a way that this
blue box gives us a single sorted list!
sortedHead
14
Question 2
  • static ListNode merge(ListNode head1, ListNode
    head2)
  • //Base caseif(head1 null) return
    head2else if(head2 null) return
    head1//Recursive caseelse ListNode
    sortedHead
  • //Find the correct head if(head1.info lt
    head2.info) sortedHead head1 head1
    head1.next else sortedHead
    head2 head2 head2.next sortedHead.next
    merge(head1, head2) return sortedHead

15
Question 3
  • Pushing a stack onto another stack while
    maintaining its ordering
  • Needs a little creativity!

4
3
2
4
1
3
2
1
16
Question 3
  • Attempt one
  • public E pushRecursive(StackltEgt s)
  • E element //base case if(s.empty() return
    null else element s.pop()
  • push(element) pushRecursive(s) retur
    n element

What happens here if we push onto the stack first
before recursive calling pushRecursive? The stack
does not maintain its order!
17
Question 3
  • Attempt two
  • public E pushRecursive(StackltEgt s)
  • E element //base case if(s.empty() return
    null else element s.pop()
  • pushRecursive(s)
  • push(element) return element

What we actually want is to push the LAST element
of the stack first before pushing the FIRST
element so that we can maintain order!
18
Question 4
  • Mathematical formula is given to you!
  • Just write it out in code!
  • Recursion does not only mean calling itself, when
    a function has to call another function, which in
    turns calls itself, this is called mutual
    recursion!

19
Question 4
  • public static int f(int n)
  • if(nlt1)
  • return 1
  • else if(n 2 0) //Is even
  • return n f(n / 2)
  • else
  • return g(n - 1) - n
  • public static int g(int n)
  • if(nlt1)
  • return 1
  • else if(n 2 0) //Is even
  • return g(n-1) f(n - 1)
  • else
  • return f(n-3)

Ans -487
20
Question 5
  • Put the following expression by Big-Oh bound.
  • 4n2, log3(n) 20n, 3lg n, 2, lg(n), nn, 3n,
    nlg(n), 100n2/3, 2n, 2n1

21
Question 5
  • Answer
  • 2, lg(n) log3(n), 100n2/3, 20n, nlg(n), 3lg n,
    4n2, 2n, 2n1, 3n, nn,
  • Why is 3lg n lt 4n2 ? Let x 3 lg nTake lg on
    both sides,
  • lg(x) lg(3 lg n ) , bring lg(n) downlg(x)
    lg(n)lg(3)
  • lg(x) lg(3)lg(n), bring log3 uplg(x) lg(n
    lg 3 )x n lg 3

22
Question 5
  • Can you prove it?
  • Prove that 4n 3n3 is bounded by O(2n)
  • Its not possible! So disprove it!

23
Question 5
  • To prove, there exist a constant c gt 0 and a
    positive integer n0 such that f(n) lt cg(n) for
    all n gt n0
  • Suppose there exist a constant c k and a
    positive integer n0 z such that the above
    statement holds true.
  • cg(z) k 2z f(z) 4z 3z3
  • 2z2z 3z3
  • But we can see that unless k gt 2z, cg(z) will
    never be smaller then f(z). And even if k gt 2z ,
    there will be a value of n such that it will no
    longer be true as 2n grows while k does not grow.
  • Contradicts our assumption!
Write a Comment
User Comments (0)
About PowerShow.com