RECURSION Flashcards

1
Q

What is recursion

A

Process of solving a problem by reducing it to smaller versions of itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a recursive algorithm

A

– Algorithm that finds the solution to a given problem by

reducing the problem to smaller versions of itself– Has one or more base cases – Implemented using recursive methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a recursive method

A

Method that calls itself
– Every recursive call has its own• Code
• Set of parameters
• Set of local variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a base case

A

– Case in recursive definition in which the solution is

obtained directly – Stops the recursion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a general solution

A

– Breaks problem into smaller versions of itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a general case

A

– Case in recursive definition in which a smaller

version of itself is called – Must eventually be reduced to a base case

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between a directly recursive method and an indirectly recursive method

A

• Directly recursive: a method that calls itself • Indirectly recursive: a method that calls
another method and eventually results in the
original method call. Method Acalls method B, which in turn calls method A.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is infinite recursion

A

– Recursive calls are continuously made until memory has been exhausted – Caused by either omitting base case or writing recursion step that
does not converge on base case – This error is analogous to the problem of an infinite loop in an
iterative (nonrecursive) solution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe the progression after a recursive call is completed

A

• After completing a recursive call – Control goes back to the calling environment – Recursive call must execute completely before
control goes back to previous call – Execution in previous call begins from point
immediately following recursive call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the differences between recursion and iteration

A
  1. Control statement:
    Recursion: selection; Iteration: repetition(loops)
    2.Termination test:
    – Iteration terminates when the loop-continuation condition fails– Recursion terminates when a base case is reached
    3.recursion is a process, always applied to a function and iteration is applied to the set of instructions which we want to get repeatedly executed.

*See notes for code that can be executed both recursively and iteratively

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why is a recursive approach preferred over an iterative approach

A

• A recursive approach is normally preferred over an iterative
approach when the recursive approach more naturally
mirrors the problem and results in a program that is easier
to understand and debug. • A recursive approach can often be implemented with fewer
lines of code. • Another reason to choose a recursive approach is that an
iterative one might not be apparent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

One of the main disadvantages of recursive solutions is that they may take more memory space as compared to iterative solutions. Using relevant examples, identify and explain scenarios / situations, where it is prudent to use a recursive solution.

A

1.If time complexity is the point of focus, and number of recursive calls would be large, it is better to use iteration. However, if time complexity is not an issue and shortness of code is, recursion would be the way to go.

2) Faster to code.
3) Easy to handle all the edge cases. Because edge cases are the base cases in recursion.
4) Easier to read and understand.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give two reasons why we need the if statement in recursive code

A

To prevent infinite recursion

Identifies general cases

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the disadvantages of recursion

A

Recursion uses more memory. Because the function has to add to the stack with each recursive call and keep the values there until the call is finished, the memory allocation is greater than that of an iterative function.

Recursion can be slow. If not implemented correctly (as stated above with memoization) it can be much slower than iteration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly