DEV Community

Cover image for Recursion and Space Complexity

Recursion and Space Complexity

El Marshall (she/they) on June 05, 2020

When I was first reading up on recursive solutions to algorithms, I kept hearing about space complexity and the call stack, and at first had no ide...
Collapse
 
hudson-td profile image
Tyler Hudson

I'm also from "The Odin Project" and feel like I lost a lot of wind in my sail after hitting recursion. Your analogy really helps with understanding the double edge nature of recursion. Thank you for the quick read!

Collapse
 
acekuria profile image
Alvin Kuria

From the Odin

Collapse
 
autotelico profile image
Henrique Magalhães

Son of Odin here as well. Loved your article, especially the analogy provided at the end. Makes recursion really stick.

Collapse
 
itskokeh profile image
Kokeh

From TOP, I love how you simplified your article and not going all too technical on it.

Here's another way to make it more relatable

The Shopping Cart Analogy for Recursion
Imagine you’re at a mall, pushing a shopping cart. As you walk through the aisles, you pick up items one by one and place them in your cart. The first item you pick up goes at the bottom of the cart, the second item goes on top of it, and so on. You know the price of each item individually, but to find out the total cost, you need to go to the checkout counter.

Here’s where the magic of recursion comes in:

First In, Last Out (FILO):
At the checkout counter, the cashier starts by scanning the last item you added to the cart (the one on top). Then they move to the second-to-last item, and so on, until they finally reach the first item you placed in the cart (the one at the bottom).

Breaking It Down:
Each item is like a step in a recursive function. The cashier doesn’t calculate the total all at once—they process one item at a time, adding its price to the running total. This is similar to how a recursive function breaks a problem into smaller, manageable pieces.

The Base Case:
The checkout process ends when the cashier reaches the first item in the cart. In recursion, this is called the base case—the point where the function stops calling itself and starts returning results.

The Total:
Once all the items have been scanned, you get the total cost. In recursion, this is the final result after all the smaller problems have been solved.

Thanks for your analogy. And if you are from the Odin Project, this is my message to tell you that you're doing great and should not give up.

Collapse
 
shahidharith profile image
Shahid

an Odinite too

Collapse
 
0xkev profile image
Kev

Hello from TOP ^_^

Collapse
 
guillermo_martn_68cb8009 profile image
Guillermo Martín

Hola desde TOP. Excelente articulo, ,uy comprensible.

Collapse
 
syartey007 profile image
S-yartey007

Hello from Odin also and thanks for the excellent explanation

Collapse
 
batetdev profile image
Fran Batet

From TOP - JS Course. Thanks for the clear and concise article.

Collapse
 
subhash_r_828f712b9e2c3c6 profile image
Subhash R

Hello, I'm Odinite too!!

Collapse
 
ivanrsalcedo profile image
Ivan R. Salcedo

Hello from Odin project!

Collapse
 
buturum_26c988a5dcacf3fe1 profile image
BUTURUM

Any article used by Odin Project is doomed to be recognised only for being in Odin Project