loop-invariant

When to use low < high or low + 1 < high for loop invariant

帅比萌擦擦* 提交于 2021-02-18 07:34:33
问题 I've read multiple articles including Jon Bentleys chapter on binary search. This is what I understand about CORRECT binary search logic and it works in the simple tests I did: binarysearch (arr, low, high, k) 1. while (low < high) 2. mid = low + (high - low)/2 3. if (arr[mid] == k) return mid 4. if (arr[mid] < k ) high = mid -1 5. else low = mid + 1 Now to find the 1st occurence with sorted duplicates, you'd chance line 3 if condition to continue instead of returning mid as binarysearch_get

GCC not performing loop invariant code motion

爷,独闯天下 提交于 2021-02-09 07:12:33
问题 I decided to check the result of loop invariant code motion optimization using g++. However, when I compiled the following code with -fmove-loop-invariants and analysed its assembly, I saw that k + 17 calculation is still performed in the loop body. What could prevent the compiler from optimizing it? May be the compiler concludes that it is more efficient to recalculate k + 17 ? int main() { int k = 0; std::cin >> k; for (int i = 0; i < 10000; ++i) { int n = k + 17; // not moved out of the

Can we prove correctness of algorithms using loop invariants in which we prove that it is true after the first iteration rather than before?

梦想的初衷 提交于 2020-02-21 14:40:49
问题 CLRS says that We must show three things about a loop invariant: Initialization: It is true prior to the first iteration of the loop. Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration. Termination: When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct. My question is can I edit the steps and make them these instead: Initialization: It is true after the first iteration of the loop.

Can we prove correctness of algorithms using loop invariants in which we prove that it is true after the first iteration rather than before?

左心房为你撑大大i 提交于 2020-02-21 14:34:26
问题 CLRS says that We must show three things about a loop invariant: Initialization: It is true prior to the first iteration of the loop. Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration. Termination: When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct. My question is can I edit the steps and make them these instead: Initialization: It is true after the first iteration of the loop.

Loop invariant of linear search

我与影子孤独终老i 提交于 2019-12-20 08:57:54
问题 As seen on Introduction to Algorithms (http://mitpress.mit.edu/algorithms), the exercise states the following: Input: Array A[1...n] Output: i, where A[i]=v or NIL when not found Write a pseudocode for LINEAR-SEARCH, which scans through the sequence, looking for v. Using a loop invariant, prove that your algorithm is correct. (Make sure that your loop invariant fulfills the three necessary properties – initialization, maintenance, termination.) I have no problem creating the algorithm, but

Loop invariant not strong enough when manipulating (array) fields of this

两盒软妹~` 提交于 2019-12-13 01:54:19
问题 UPDATED Problems on solving some dafny problems, described below with the given class and respective methods. If you need something else please tell me, thank you in advance. Also the link is updated with all this code in rise4fun. class TextEdit { var text:array<char>; //conteúdo var scrap: array<char>; //conteúdo temporariamente guardado para copy/paste var tlen:int; //length di text var slen:int; //length di scrap var at:int; //current position var sellen:int; //length of the selection

Loop invariant of linear search

折月煮酒 提交于 2019-12-02 18:05:37
As seen on Introduction to Algorithms ( http://mitpress.mit.edu/algorithms ), the exercise states the following: Input: Array A[1...n] Output: i, where A[i]=v or NIL when not found Write a pseudocode for LINEAR-SEARCH, which scans through the sequence, looking for v. Using a loop invariant, prove that your algorithm is correct. (Make sure that your loop invariant fulfills the three necessary properties – initialization, maintenance, termination.) I have no problem creating the algorithm, but what I don't get is how can I decide what's my loop invariant. I think I understood the concept of loop

What is the best way of determining a loop invariant?

老子叫甜甜 提交于 2019-11-28 19:38:47
When using formal aspects to create some code is there a generic method of determining a loop invariant or will it be completely different depending on the problem? It has already been pointed out that one same loop can have several invariants, and that Calculability is against you. It doesn't mean that you cannot try. You are, in fact, looking for an inductive invariant : the word invariant may also be used for a property that is true at each iteration but for which is it not enough to know that it hold at one iteration to deduce that it holds at the next. If I is an inductive invariant, then

What is the best way of determining a loop invariant?

北慕城南 提交于 2019-11-27 12:26:05
问题 When using formal aspects to create some code is there a generic method of determining a loop invariant or will it be completely different depending on the problem? 回答1: It has already been pointed out that one same loop can have several invariants, and that Calculability is against you. It doesn't mean that you cannot try. You are, in fact, looking for an inductive invariant : the word invariant may also be used for a property that is true at each iteration but for which is it not enough to

What is a loop invariant?

隐身守侯 提交于 2019-11-27 02:28:47
I'm reading "Introduction to Algorithm" by CLRS. In chapter 2, the authors mention "loop invariants". What is a loop invariant? Tomas Petricek 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 for loop that looks like this: int j = 9; for(int i=0; i<10; i++) j--; In this example it is true (for every iteration) that i + j == 9 . A weaker invariant that is also true is that i >= 0 && i <= 10 . TNi I like this very simple definition: ( source ) A loop invariant is a condition [among program variables]