Recursion

1.How to approach?

  • Recursive solutions are built of solutions to subproblem

  • Three most common approach:

Bottom-Up Approach

  • Most intuitive

  • We start with how to solve the problem for a simple case then we figure out how to solve the problem for more components and so on. (先解決一個, 再處理下一個)

Top-Down Approach

  • Divid the problem for case N into subproblems (將問題分為許多子問題)

Half-and-Half Approach

  • We first figure out which half of the array contains the value

  • e.g., Binary search

Dynamic Programming & Memoization

  • Taking a recursive algorithm and finding the overlapping subproblems

  • Cache the result for future recursive calls

  • e.g., Fibonacci numbers

Typical solution

  • 複雜度: O(N^2) (O(branches ^ depth))

Top-Down Dynamic Programming (or Memorization)

Bottom-Up Programming

2. What is the best case?

  1. There is no part of the list to search and the element has not been found

  2. The search element has been found st the mid point of the portion we're searching

3. What is the recursive case?

  • Binary search smaller portions of the list where the element might be found

4. Examples

2.Find all subsets of a set (take Θ (2^n) time )

  • 1. What is the best case?

    • When the original set is empty, only the empty set is it's subset

  • 2. What is the recursive case?

    • Add the elements to the existing subsets to create new subsets

4.Same binary tree (take Θ (n) time )

binary tree

  • A binary tree is one where every node can have a maximum of two children - a left child and right child

  • Two binary trees are the same if :

    • Every corresponding node has the same value

    • The structure of the tree at every corresponding node is the same

Algorithm

  • 1. What is the best case?

    • 1.A null current node on one tree should correspond to a null in the other tree

    • 2.Node values should be the same at every one

  • 2. What is the recursive case?

    • Check that the subtree rooted at every node is the same

Code

  • Node class

  • same tree

5.Implement the paintfill feature from MS paint

  • Start with one pixel which has some original color

  • Move outwards, if the neighboring pixels have the same original color, color them as well

  • Repeat until the borders are reached

  • Complexity: O(N)

Base case

  • 當目前的pixel沒有相同的顏色時

  • 當目前的pixel位置超出螢幕範圍時

Recursive case

  • Move outward from the start pixel coloring individual pixels

6.Build a car given all tasks and each task's dependencies

  • Say that all the tasks needed to build the car: A, B, C, D, E, F, G, H

    • B depends on A

    • D depends on E

    • C depends on A, B, D

    • F depends on C

  • An acceptable order of performing the tasks are:

    • E -> D -> A -> B -> C -> F

    • A -> E -> B -> D -> C -> F

  • The tasks and it's dependencies form a directed acyclic graph

  • The graph may not be fully connected

Base case

  • The current task has been executed, it's marked done.

Recursive case

  • Execute dependencies before coming to the current task

Complexity

  • O(N)

7.Find all anagrams of a given word

8.Find a path a rat can travel

Base case

  • The rat has reached the end cell.

Recursive case

  • Keep visiting cells by passing through doors when they exist

  • Do not visit cells if they are present in the current path, we don't want the rat to move in circles

9.Place 8 queens on a chessboard so they don't kill each other

Rule

  • Remember the queens can move along their rows, columns and along diagonals

  • Place one queen at a time, one on each row or one on each column

Tips

  • Place a queen in each row or column in a safe position

  • See if the remaining queens can be placed safely with the current position of the queen

  • Recursively do this till the right positions for all queens have been found

Base case

  • The queens have been all placed and we're beyond the boundary of the chessboard

Recursive case

  • Keep placing queens in different positions of the same row or column

  • Check whether the remaining queens can be successfully placed

Implementation

Place the queens

Check if current queen position is safe

Check the row and the column

Check the left diagonal

Check the right diagonal

Complexity

  • O(N!)

Last updated