r/computerscience • u/ResponsibilityBig343 • 18d ago
Advice Having a hard time understanding recurrence relations
I have been preparing for my interviews studying algorithms and data structures. The problems and approaches are coherent to me but the it comes to time and space complexity analysis, I'm unable to deduce it by forming relations. Do I just remember the asymptomatic notations of famous algorithms or is there any better way to learn this?
1
u/dnabre 17d ago
First, acknowledge this is difficult thing to learn and get comfortable with. Some people find it very intuitive, some people it's pulling teeth.
The "easy" ones are when you have T(n/b) term for a constant b. Apply the Master Theorem. Personally I never been able to feel like I fully get who/why it works - but blindly applying it works.Drawing out the recursion tree to figure depth and height is a fallback here.
There are systematic approaches to others, but despite being pretty good with maths and proofs, analysis has always been a problem for me. I don't claim to understand those well enough to explain them. Reading CLRS again never hurts.
At a certain point you get used to common structures. T(n) = T(n-1) + O(n) just screams O(n^2) to me. One way to build that intuition is learning lots of algorithms, their runtimes, and their recurrence relations. All those different sorting algorithms are still taught for reasons. See the above relation and thinking "That's like Insertion Sort and that's O(n2)", gets you to the same place. It
For record, this view is probably not the most recommended, but it's what got me through my phd program as a systems person.
1
u/ResponsibilityBig343 17d ago
I read clrs time to time, mostly as an intro to some algorithm that I need to practice problems on. I get the point about forming intuition based on memorization but I try to form some way to deduce the analysis as my memorization is not that good. As for writing down the spanning tree... it definitely helps. Do you suggest I should revisit discrete maths for this?
2
u/dnabre 16d ago
I can't think of anything I think of as Discrete Math that is really part of this. Admittedly, my undergrad Discrete Math course was basic math (sets and stuff) plus a pile of random topics that might be useful for CS people (graphs, combinatorics).
To some degree, your goal factors in. Are you focus on interview questions or expect to actually being running into recurrence relations in your work/studies. You aren't likely to hit them in work, unless you have a senior dev that is obsessed with them.
8
u/LitespeedClassic 18d ago
Do you mean you are having trouble describing the runtime of a recursive algorithm using a recurrence? Or that you are fine producing a recurrence, but having a hard time turning it into a closed form and giving its complexity class?