CS1102 Tut 5 Recursion and Complexity Analysis - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

CS1102 Tut 5 Recursion and Complexity Analysis

Description:

... past year papers, email me the exam paper before the ... Just call merge recursively on the remaining elements! What should be next element of node 3? ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 32
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
First 15 minutes
  • Help session on Friday 630 830
  • If you are coming, I expect you to contribute to
    the discussion by preparing some questions
  • If you are practicing on past year papers, email
    me the exam paper before the meeting (so that I
    am prepared)

3
Midterm tips
  • Midterm tips!
  • I dont know the questions, so I cant reveal
    anything accidentally, which also means I can
    discuss anything I like.

4
Midterm tips
  • In my opinion
  • Exam tips (lazy) Lecturers like to reuse exam
    questions especially MCQs.
  • Dont memorize code.
  • Know how it works, if you memorize how it works,
    you wont be able to solve questions.

5
Midterm tips
  • For example
  • How to delete a listnode pointed to by current ?
  • Get the previous node pointed by current, and
    set the next pointer of this previous node to the
    next node of current

6
Midterm tips
  • Oh-nos, my programming sucks, I wont be able to
    do the last long question!
  • If you CANT write code, then DONT write code,
    write your ALGORITHM in ENGLISH (that I can
    understand).
  • But of course if you cant write code you get
    lesser marks

7
Midterm tips
  • For long and short answer questions if you leave
    them empty, you get ZERO!!! marks.
  • Hence, dont leave them blank. Even if you dont
    know how to answer the question at all TOTALLY,
    write something related you might get lucky

8
Midterm tips
  • Topics which ARE important
  • Programming styles, ADTs, modularity
  • Linear search/binary search
  • LinkedList, likes to pose questions on given a
    function that does a traversal, or deletion, or
    insertion, what does curr point to?
  • Ordering of Stack push and pop operations as well
    as Queue
  • Complexity analysis, how to compute?
  • Postfix, infix, prefix expressions do YOU know?
  • Shouldnt be straightforward questions like you
    have in your tutorials! (Tutorials are to let you
    UNDERSTAND the basic concepts, so you better
    understand them before your midterms)

9
  • Tutorials

10
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

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

12
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

13
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

14
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
15
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

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

17
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
18
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
19
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
20
Question 2
  • When do we terminate (base case)
  • When either list is empty!

21
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
22
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

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

4
3
2
4
1
3
2
1
24
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!
25
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!
26
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!

27
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
28
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

29
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

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

31
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