r/datastructures • u/Makstar05 • Nov 27 '25
Is it technically fine if I prove a recursive alogrithm using a loop invariant
So basically I had to prove the correctness of a recursive algorithm on an exam, but I was confused which method is used for proving recursive algorithms. I knew that iterative algorithms could be proven using loop invariants, so I tried to draw parallels between the iterative and recursive case by considering each recursvive call as a loop and then using the initialization, maintenance and termination steps.
The problem is that the teacher didn't accept my method and said that I should've used an inductive hypothesis instead of a loop invariant even though the induction underlying my own method was entirely correct, just under the name of a loop invariant. He further said that you cannot call a recusive step/call a loop, but I tried arguing that it wouldn't make a difference from the perspective of the proof.
What do you guys think? Is the teacher being too harsh or do I deserve credit?
3
u/EntrepreneurHuge5008 Nov 27 '25 edited Nov 27 '25
I'm siding with your teacher on this one.
If you're calling it a "loop step" and using a "loop" invariant, then by definition, you're formulating a proof for an iterative algorithm, not a recursive one. Yes, word choice matters.
Iterative problems can be solved with recursion; recursive issues can be solved with iteration. You do need to be very specific with your proofs, and "it wouldn't make a difference from the perspective of the proof" isn't specific enough.
2
u/authorinthesunset Nov 28 '25
Does it matter what we think? Your teacher, who decides your score, says ya wrong!
You'd be doing yourself a favor by being a little more open to being wrong. Spend more time understanding what s/he is telling and less trying to figure out how your going to convince them you were actually right.
This is true, regardless of how correct you think you are.
It is true, regardless how correct you actually are.
1
u/umop_aplsdn Nov 27 '25
Loops are a special case of recursion, loop invariants are a special case of induction hypotheses. All loops can be converted into recursive algorithms via tail recursion. I think you are technically right and your teacher is wrong, but it depends on your specific argument / at what level you understand the equivalence.
1
u/Ronin-s_Spirit Nov 28 '25
I know one thing - you can definitely write any kind of recursion as loop+stack (because that's what it technically is). I don't know anything about proofs though.
1
u/zhivago Nov 29 '25
Iteration is recursive, but analyzing each recursive step as a loop seems wrong.
You need to prove that the size of the problem is reduced at each step in order to prove that the recursion converges on a solution and terminates.
It's possible that you did this, but it's unclear from your description. :)
1
u/Beregolas Nov 29 '25
No, I think your teacher is correct. While a loop is often working analogous to a recursive function, they are still two different things, with different properties. So technically, you haven't proven anything about the recursive function, if your proof didn't include the step, that the recursive function is equivalent to the loop you used in the rest of the proof.
If you don't prove equivalence, you are basically inventing your own algorithm(loop= and proving that instead of what you were assigned(recursion).
Proofs are all about exactness and proper use of words and names. Everyone (with the proper knowledge) is supposed to be able to pick it up and understand it from beginning to end without any further input.
1
u/No-Contract7853 Nov 29 '25
He's correct
1
u/Trending_Boss_333 Dec 20 '25
You can't always generalize a recursive call to a loop, they are kind of different under the hood so your proof is wrong in that sense. But idk, listen to your teacher they know best.