Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an invariant?

People also ask

What do you mean by invariant?

Definition of invariant : constant, unchanging specifically : unchanged by specified mathematical or physical operations or transformations invariant factor.

What is an invariant C++?

CLASS. Description. The INVARIANT statement describes a condition that should be always true in an object life, that is, whenever one of its method can be called. It appears in a CLASS block. <native expression> is a C++ Boolean expression (or an expression that can be converted to a Boolean).

What is an example of an invariant?

Examples. A simple example of invariance is expressed in our ability to count. For a finite set of objects of any kind, there is a number to which we always arrive, regardless of the order in which we count the objects in the set.

What is an invariant class 11?

Solution. An expression involving variables, which remains unchanged by an assignment to one of these variables is called an invariant of the assignment.


An invariant is more "conceptual" than a variable. In general, it's a property of the program state that is always true. A function or method that ensures that the invariant holds is said to maintain the invariant.

For instance, a binary search tree might have the invariant that for every node, the key of the node's left child is less than the node's own key. A correctly written insertion function for this tree will maintain that invariant.

As you can tell, that's not the sort of thing you can store in a variable: it's more a statement about the program. By figuring out what sort of invariants your program should maintain, then reviewing your code to make sure that it actually maintains those invariants, you can avoid logical errors in your code.


It is a condition you know to always be true at a particular place in your logic and can check for when debugging to work out what has gone wrong.


The magic of wikipedia: Invariant (computer science)

In computer science, a predicate that, if true, will remain true throughout a specific sequence of operations, is called (an) invariant to that sequence.


I usually view them more in terms of algorithms or structures.

For example, you could have a loop invariant that could be asserted--always true at the beginning or end of each iteration. That is, if your loop was supposed to process a collection of objects from one stack to another, you could say that |stack1|+|stack2|=c, at the top or bottom of the loop.

If the invariant check failed, it would indicate something went wrong. In this example, it could mean that you forgot to push the processed element onto the final stack, etc.


As this line states:

In computer science, a predicate that, if true, will remain true throughout a specific sequence of operations, is called (an) invariant to that sequence.

To better understand this hope this example in C++ helps.

Consider a scenario where you have to get some values and get the total count of them in a variable called as count and add them in a variable called as sum

The invariant (again it's more like a concept):

// invariant:
// we have read count grades so far, and
// sum is the sum of the first count grades

The code for the above would be something like this,

int count=0;
double sum=0,x=0;
while (cin >> x) {
++count;
sum+=x;
}

What the above code does?

1) Reads the input from cin and puts them in x

2) After one successful read, increment count and sum = sum + x

3) Repeat 1-2 until read stops ( i.e ctrl+D)

Loop invariant:

The invariant must be True ALWAYS. So initially you start out your code with just this

while(cin>>x){
  }

This loop reads data from standard input and stores in x. Well and good. But the invariant becomes false because the first part of our invariant wasn't followed (or kept true).

// we have read count grades so far, and

How to keep the invariant true?

Simple! increment count.

So ++count; would do good!. Now our code becomes something like this,

while(cin>>x){
 ++count; 
 }

But

Even now our invariant (a concept which must be TRUE) is False because now we didn't satisfy the second part of our invariant.

// sum is the sum of the first count grades

So what to do now?

Add x to sum and store it in sum ( sum+=x) and the next time cin>>x will read a new value into x.

Now our code becomes something like this,

while(cin>>x){
 ++count; 
 sum+=x;
 }

Let's check

Whether code matches our invariant

// invariant:
// we have read count grades so far, and
// sum is the sum of the first count grades

code:

while(cin>>x){
 ++count; 
 sum+=x;
 }

Ah!. Now the loop invariant is True always and code works fine.

The above example was taken and modified from the book Accelerated C++ by Andrew-koening and Barbara-E


This answer is for my 5 year old kid. Do not think of an invariant as a constant or fixed numerical value. But it can be. However, it is more than that.

Rather, an invariant is something like of a fixed relationship between varying entities. For example, your age will always be less than that compared to your biological parents. Both your age, and your parent's age changes in the passage of time, but the relationship that i mentioned above is an invariant.

An invariant can also be a numerical constant. For example, the value of pi is an invariant ratio between the circle's circumference over its diameter. No matter how big or small the circle is, that ratio will always be pi.