Recursion(not finished) & Recursive Data Structures(deffo not finished) & Recurrence Relation (finished)

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/15

encourage image

There's no tags or description

Looks like no tags are added yet.

16 Terms

1
New cards

What is recursion?

defining a function (or problem) in terms of itself

(solving a problem by solving a smaller version of the same problem)

2
New cards

What 2 things does every recursive definition need?

  • Base case

    • simplest input, answered directly, so the recursion stops

  • Recursive case

    • reduces the problem to a smaller version of itself


3
New cards

What is “divide and conquer”?

  • divide problem into smaller problems that are easier to solve

  • combine solutions of small problems = solution to big problem


4
New cards
5
New cards
6
New cards
7
New cards
8
New cards

Towers of Hanoi

3 stages

  1. move top n-1 disks from start peg to spare peg

  2. move largest disk froms start peg to destination peg

  3. move n-1 disks from spare peg to destination peg


Recurrence relation:

  • two recursive calls: T(n-1)

  • one disk movement O(1)

  • therefore reccurrence relation: T(n) = 2T(n-1) + O(1)

  • recurrence produces exponential growth therefore time complexity is O(2^n)


9
New cards

What is the general structure of a recursive function

knowt flashcard image
10
New cards

How would a binary tree search be written as a recursive function?

knowt flashcard image
11
New cards

What is recurrence relation?

  • mathemetical equation

  • describes running time of a recursive algorithm in terms of the running time of its smaller recursive calls

    • tells us how much time an algorithm takes by looking at:

      • time taken by recursive calls

      • additional work performed outside those calls



<ul><li><p>mathemetical equation</p></li><li><p>describes<strong> running time</strong> of a recursive algorithm in terms of the running time of its <strong>smaller recursive calls</strong></p><ul><li><p>tells us how much time an algorithm takes by looking at:</p><ul><li><p>time taken by recursive calls</p></li><li><p>additional work performed outside those calls</p></li></ul></li></ul></li></ul><p></p><p></p>
12
New cards

In recurrence relation what is the additional work represented as in big O notation?

O(1) - constant time

13
New cards

How would you calculate the recurrence relation for working out a factorial?

  • factorial algorithm performs:

    1. one recursive call : factorial(n-1)

      • takes T(n-1)

    2. one multiplication

      • takes O(1)

    3. contstant amount of additional work

      1. takes O(1)

  • therefore the running time is T(n) = O(n)


<ul><li><p>factorial algorithm performs:</p><ol><li><p>one recursive call : factorial(n-1)</p><ul><li><p>takes T(n-1)</p></li></ul></li><li><p>one multiplication</p><ul><li><p>takes O(1)</p></li></ul></li><li><p>contstant amount of additional work</p><ol><li><p>takes O(1)</p></li></ol></li></ol></li><li><p>therefore the running time is T(n) = O(n)</p></li></ul><p></p>
14
New cards

When looking at normal and efficicent exponentiation why is the efficient version better?

dividing the input by 2 reduces number of recursive calls mcuh faster than subtracting one each time

15
New cards

How do you derive the recurrence relation in an exam?

  1. Count the recursive calls

    • how many times does the function call itself?

  2. Identify size of each recursive input

    • look at how the input changes

    • does it decrease by 1 (n-1)? decrease by half (n/2)?

  3. Calculate additional work

    • what happens outside recursive calls

    • e.g. O(1) when multiplication/comparison/loop through all n elements

  4. Write recurrence

    • put recursive calls & additional work together


16
New cards