I’m reading “Introduction to Algorithm” by CLRS. In chapter 2, the authors mention “loop invariants”. What is a loop invariant?
I’m reading Introduction to Algorithm by CLRS. In chapter 2, the authors mention loop
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In simple words, a loop invariant is some predicate (condition) that holds for every iteration of the loop. For example, let’s look at a simple
forloop that looks like this:In this example it is true (for every iteration) that
i + j == 9. A weaker invariant that is also true is thati >= 0 && i <= 10.