From d0e9b353f7aaaeecf87096aae77c6a560755ddd0 Mon Sep 17 00:00:00 2001 From: srilekha279 <158455553+srilekha279@users.noreply.github.com> Date: Sat, 25 May 2024 21:17:23 +0530 Subject: [PATCH 1/2] Updated recursion.md with example to explain the working of recursion with simple python program This commit updates the recursion explanation in the recursion.md file to be more beginner-friendly and comprehensive. The factorial example has been revised with n = 4 and stack variables have been included for each recursive call to demonstrate how functions are called and how values are returned. These changes aim to provide a clearer understanding of recursion, especially for beginners, by illustrating the step-by-step process of function calls and return values. --- contrib/ds-algorithms/recursion.md | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/contrib/ds-algorithms/recursion.md b/contrib/ds-algorithms/recursion.md index 7ab3136..67c630b 100644 --- a/contrib/ds-algorithms/recursion.md +++ b/contrib/ds-algorithms/recursion.md @@ -17,6 +17,49 @@ When a recursive function is called, the following sequence of events occurs: - Stack Management: Each recursive call is placed on the call stack. The stack keeps track of each function call, its argument, and the point to return to once the call completes. - Unwinding the Stack: When the base case is eventually met, the function returns a value, and the stack starts unwinding, returning values to previous function calls until the initial call is resolved. +# Python Code: Factorial using Recursion + +```python +def fact(n): + if n == 0 or n == 1: + return 1 + return n * fact(n - 1) + +if __name__ == "__main__": + n = int(input("Enter a positive number: ")) + print("Factorial of", n, "is", fact(n)) +``` + +## Explanation + +This Python script calculates the factorial of a given number using recursion. + +- **Function `fact(n)`:** + - The function takes an integer `n` as input and calculates its factorial. + - It checks if `n` is 0 or 1. If so, it returns 1 (since the factorial of 0 and 1 is 1). + - Otherwise, it returns `n * fact(n - 1)`, which means it recursively calls itself with `n - 1` until it reaches either 0 or 1. + +- **Main Section:** + - The main section prompts the user to enter a positive number. + - It then calls the `fact` function with the input number and prints the result. + +### Example : Let n = 4 + + +The recursion unfolds as follows: +1. When `fact(4)` is called, it computes `4 * fact(3)`. +2. Inside `fact(3)`, it computes `3 * fact(2)`. +3. Inside `fact(2)`, it computes `2 * fact(1)`. +4. `fact(1)` returns 1 (`if` statement executes), which is received by `fact(2)`, resulting in `2 * 1` i.e. `2`. +5. Back to `fact(3)`, it receives the value from `fact(2)`, giving `3 * 2` i.e. `6`. +6. `fact(4)` receives the value from `fact(3)`, resulting in `4 * 6` i.e. `24`. +7. Finally, `fact(4)` returns 24 to the main function. + + +#### So, the result is 24. + + + # What is Stack Overflow in Recursion Stack overflow is an error that occurs when the call stack memory limit is exceeded. During execution of recursion calls they are simultaneously stored in a recursion stack waiting for the recursive function to be completed. Without a base case, the function would call itself indefinitely, leading to a stack overflow. From 1646a8c0330695ef4750a2ca533062a7c1c61599 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Fri, 31 May 2024 06:32:34 +0530 Subject: [PATCH 2/2] Update recursion.md --- contrib/ds-algorithms/recursion.md | 41 +++++++----------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/contrib/ds-algorithms/recursion.md b/contrib/ds-algorithms/recursion.md index 67c630b..4233242 100644 --- a/contrib/ds-algorithms/recursion.md +++ b/contrib/ds-algorithms/recursion.md @@ -2,7 +2,7 @@ When a function calls itself to solve smaller instances of the same problem until a specified condition is fulfilled is called recursion. It is used for tasks that can be divided into smaller sub-tasks. -# How Recursion Works +## How Recursion Works To solve a problem using recursion we must define: - Base condition :- The condition under which recursion ends. @@ -17,7 +17,7 @@ When a recursive function is called, the following sequence of events occurs: - Stack Management: Each recursive call is placed on the call stack. The stack keeps track of each function call, its argument, and the point to return to once the call completes. - Unwinding the Stack: When the base case is eventually met, the function returns a value, and the stack starts unwinding, returning values to previous function calls until the initial call is resolved. -# Python Code: Factorial using Recursion +## Python Code: Factorial using Recursion ```python def fact(n): @@ -30,7 +30,7 @@ if __name__ == "__main__": print("Factorial of", n, "is", fact(n)) ``` -## Explanation +### Explanation This Python script calculates the factorial of a given number using recursion. @@ -43,8 +43,7 @@ This Python script calculates the factorial of a given number using recursion. - The main section prompts the user to enter a positive number. - It then calls the `fact` function with the input number and prints the result. -### Example : Let n = 4 - +#### Example : Let n = 4 The recursion unfolds as follows: 1. When `fact(4)` is called, it computes `4 * fact(3)`. @@ -55,48 +54,26 @@ The recursion unfolds as follows: 6. `fact(4)` receives the value from `fact(3)`, resulting in `4 * 6` i.e. `24`. 7. Finally, `fact(4)` returns 24 to the main function. - #### So, the result is 24. - - -# What is Stack Overflow in Recursion +#### What is Stack Overflow in Recursion? Stack overflow is an error that occurs when the call stack memory limit is exceeded. During execution of recursion calls they are simultaneously stored in a recursion stack waiting for the recursive function to be completed. Without a base case, the function would call itself indefinitely, leading to a stack overflow. -# Example - -- Factorial of a Number - - The factorial of i natural numbers is nth integer multiplied by factorial of (i-1) numbers. The base case is if i=0 we return 1 as factorial of 0 is 1. - -```python -def factorial(i): - #base case - if i==0 : - return 1 - #recursive case - else : - return i * factorial(i-1) -i = 6 -print("Factorial of i is :", factorial(i)) # Output- Factorial of i is :720 -``` -# What is Backtracking +## What is Backtracking Backtracking is a recursive algorithmic technique used to solve problems by exploring all possible solutions and discarding those that do not meet the problem's constraints. It is particularly useful for problems involving combinations, permutations, and finding paths in a grid. -# How Backtracking Works +## How Backtracking Works - Incremental Solution Building: Solutions are built one step at a time. - Feasibility Check: At each step, a check is made to see if the current partial solution is valid. - Backtracking: If a partial solution is found to be invalid, the algorithm backtracks by removing the last added part of the solution and trying the next possibility. - Exploration of All Possibilities: The process continues recursively, exploring all possible paths, until a solution is found or all possibilities are exhausted. -# Example +## Example: Word Search -- Word Search - - Given a 2D grid of characters and a word, determine if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where "adjacent" cells are horizontally or vertically neighboring. The same letter cell may not be used more than once. +Given a 2D grid of characters and a word, determine if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where "adjacent" cells are horizontally or vertically neighboring. The same letter cell may not be used more than once. Algorithm for Solving the Word Search Problem with Backtracking: - Start at each cell: Attempt to find the word starting from each cell.