UPI Study › Courses › Discrete Mathematics › Free lessons › Lesson 8.8 Free with a free account
Recursive Functions and Self-Referential Definitions
Discrete Mathematics · Chapter 8: Binomial Probability, Recurrence Relations, and Generating FunctionsSection 8.8 · lesson 53 of 879 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Discrete Mathematics.
Lesson complete
Earn the 4 college credits for Discrete Mathematics
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: First-Order Linear Recurrence Relations →← Applied Binomial Experiment ProblemsNext lesson: First-Order Linear Recurrence Relations →
In this lesson
Defining something in terms of smaller copies of itself - safely.
- Identify base cases and recursive cases
- Explain what makes a recursive definition well-founded
- Trace factorial and Fibonacci recursively
- Define structures, not just numbers, recursively
Transcript
Everything said in the video, section by section (1,247 words). Read along, or search it.
Binomial Probability, Recurrence Relations, and Generating Functions
Recursive Functions and Self-Referential Definitions
Chapter eight now turns from binomial probability to recurrence relations. It starts with the idea underneath them. Recursion. This section covers what makes a definition in terms of itself legitimate, traces two classic examples, factorial and Fibonacci, and shows that recursion describes structures, not just numbers.
The Opening Problem
Replies to Replies to Replies
When someone opens a busy post, the interface renders the post, and potentially thousands of nested replies. A comment can have replies, each reply can have its own replies, and so on, to whatever depth the conversation has reached. So a comment thread is a comment, together with a list of reply-threads. And each of those is exactly the same kind of object. No separate vocabulary is needed for a reply three levels deep. The definition of a comment thread refers to itself. A function that counts a thread's total replies naturally mirrors that. It computes the answer for each smaller reply-thread, then combines the results. That pattern is called recursion.
Definition 8.8.1
Base Cases and Recursive Cases
A recursive definition defines something partly in terms of itself. Left unrestricted, that's pure circularity. Like a dictionary where every word is defined using other words, and none is ever tied to anything outside. It defines nothing at all. A recursive definition avoids that with two ingredients. Base cases, whose values are stated directly, with no self-reference. And recursive cases, which express the value at an input in terms of the value at strictly smaller inputs. For the definition to work, every recursive case has to reach a base case after finitely many steps. Usually some quantity strictly decreases, and can't decrease forever. Such a definition is called well-founded. That's the same shape as induction, from chapter four. An anchor, plus a step toward it. A well-founded recursive definition is induction used to build a function, instead of proving a statement.
Definition 8.8.2 and Worked Example 8.8.1
Factorial, Down and Back Up
The factorial function is one of the simplest examples. Zero factorial is one, the base case. And n factorial is n times n minus one factorial, for n at least one. It's well-founded, since each application replaces n with n minus one. After exactly n steps, the base case is reached. Compute four factorial. Unwinding down, four factorial is four times three factorial. Three factorial is three times two factorial, and so on, down to zero factorial, which is one. Then unwind back up. One factorial is one. Two factorial is two. Three factorial is six. And four factorial is four times six, which is twenty-four. Four recursive calls going down, matched by four multiplications coming back up.
Recursion as Code
1 Branch of Code per Case
A recursive definition already specifies, for every input that isn't a base case, which smaller subproblem to solve and how to combine the result. So it translates directly into a recursive function in code, typically one branch per case. If n is zero, return one. That's the base case, with no further call. Otherwise, return n times the factorial of n minus one. That's the recursive case.
Definition 8.8.3
Fibonacci: 2 Base Cases, 2 Calls
The Fibonacci sequence is the standard next example, because it departs from factorial in an instructive way. It has two base cases. F of zero is zero, and F of one is one. And for n at least two, F of n is F of n minus one, plus F of n minus two. It's still well-founded. Both n minus one and n minus two are strictly smaller, and stay non-negative, so repeated application always reaches zero or one. What's new is that a single recursive case makes two recursive calls. The code mirrors that directly.
Figure 8.8.1
The Recursion Tree for F(5)
Because each non-base call branches into two, the calls form a recursion tree, not a single chain. Here's the full tree for F of five. Every box is one call. The gold boxes are base cases, returned immediately as zero or one. Reading upward, each box adds its two children. F of two is one plus zero, which is one. F of three is one plus one, which is two. F of four is two plus one, three. And F of five is three plus two, which is five.
Worked Example 8.8.2
15 Calls to Get F(5) = 5
Count the calls. There are fifteen, to compute a value of five. That's because several subproblems are solved more than once. F of three is computed twice. F of two, three times. F of one, five times, and F of zero, three times. Nine of the fifteen calls recompute a value already found somewhere else in the tree. And the redundancy gets worse for larger n, since each extra level roughly doubles the calls. But the recursive definition itself is unambiguous, and entirely correct. The redundancy belongs to this particular way of carrying it out.
8.8.3 Beyond Numbers
Recursive Structures
Nothing requires a recursive definition to produce numbers. Sets and data structures can be defined recursively too, with the same two ingredients. A binary tree is either empty, or a node together with two binary trees, its left and right subtrees. The definition refers to itself, just like the comment thread. Now make the thread precise. A comment thread is either a single comment with no replies, the base case. Or a comment with a nonempty list of comment threads, its replies. The total reply count is defined on that same structure. A thread with no replies has total zero. Otherwise, for each reply, count one for the reply, plus its own total, and add those up.
Worked Example 8.8.3
Counting Replies in a Small Thread
The code mirrors that definition. If a thread has no replies, return zero. Otherwise, sum one plus the total for each reply. Take a thread rooted at comment A. A has two replies, B and C. B has none. C has one reply, D, and D has none. Start with the base cases, the leaves. B and D each have total zero. C has one reply, D, so its total is one plus zero, which is one. And A has two replies. Its total is one plus zero for B, plus one plus one for C. That's three. Directly inspecting the thread confirms it. Besides A, there are exactly three comments, B, C, and D.
Table 8.8.1
The Same Anatomy, 3 Times
This table lays the three definitions side by side. Factorial has one base case, makes one recursive call per step, and decreases n toward zero. Fibonacci has two base cases, makes two calls per step, and also decreases n toward zero. And the reply count has a base case of a thread with no replies. It makes one call per direct reply, and the nesting depth decreases. Recursive sequences like Fibonacci are called recurrence relations. The next two sections develop systematic ways to find a closed formula for the nth term, without unwinding the recursion by hand.
Takeaways
Anchor, Then Shrink
To summarize. A recursive definition has base cases, stated with no self-reference, and recursive cases that reduce every other input to a strictly smaller one. It's well-founded only if a base case is always reached after finitely many steps. A recursive case can make one smaller call, like factorial, or several, like Fibonacci. And structures such as binary trees and comment threads can be defined recursively too. Here's something to think about. Computing F of five by direct recursion takes fifteen calls. Yet the Fibonacci definition is exactly as correct as factorial's. What does that say about the difference between a recursive definition, and a recursive procedure for computing it? Suppose you had to compute F of thirty. Would you follow the recursion by hand? Or would you fill in a table, F of zero, F of one, F of two, in order? Which is less work, and why doesn't the definition force either choice?
About Discrete Mathematics at UPI Study
Discrete Mathematics (Math 230) is a self-paced online course of 87 video lessons in 13 chapters, from logic and proof to counting, probability, graph theory and trees. It carries a NCCRS credit recommendation for 4 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus Every course with PRO →
Why take it here
Why students take Discrete Mathematics with UPI Study
Where the credit goes
Universities that work with NCCRS
Discrete Mathematics carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Recursive Functions and Self-Referential Definitions
What are base cases and recursive cases in a recursive definition?
What makes a recursive definition well-founded?
How do you trace the factorial and Fibonacci functions recursively?
Can you define things other than numbers recursively?
About the Discrete Mathematics course
How many college credits is Discrete Mathematics?
How much does Discrete Mathematics cost and what is included?
Which universities accept UPI Study credits?
How long does Discrete Mathematics take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 4 credits in Discrete Mathematics.
87 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
4 credits · NCCRSEnroll →