Algorithms Lec 2
Algorithms Lec 2
Loop Invariant
with Examples
Fall 2024
Mr. Ahsan Shah
What is an Algorithm?
Algorithm Correctness
Loop Invariant
• Definition:
• A loop invariant is a condition [among program variables] that is
necessarily true immediately before and immediately after each
iteration of a loop. (Note that this says nothing about its truth or
falsity part way through an iteration.)
• When the first two properties hold, the loop invariant is true prior to every iteration of
the loop.
• Here, showing that the invariant holds before the first iteration is like
the base case, and showing that the invariant holds from iteration to
iteration is like the inductive step.
• The third property is perhaps the most important one, since we are using the loop
invariant to show correctness. It also differs from the usual use of mathematical
induction, in which the inductive step is used infinitely; here, we stop the “induction”
when the loop terminates.
Example
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.
One may get confused between the loop invariant, and the loop conditional ( the condition which
controls termination of the loop ).
Usage
• Loop invariants capture key facts that explain why code works.
• A loop Invariant can help in the design of iterative algorithms
when considered an assertion that expresses important
relationships among the variables that must be true at the start
of every iteration and when the loop terminates. If this holds, the
computation is on the road to effectiveness. If false, then the
algorithm has failed.
• Loop invariants are used to reason about the correctness of
computer programs. Intuition or trial and error can be used to
write easy algorithms however when the complexity of the
problem increases, it is better to use formal methods such as
loop invariants.
• In the above example after the 3rd iteration of the loop max
value is 7, which holds true for the first 3 elements of array A.
Here, the loop invariant condition is that max is always
maximum among the first i elements of array A.
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
CS 478: Design and Analysis of Algorithms
for (i = 1 to n-1)
{
key = arr[i];
j = i-1;
while (j >= 0 and arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
2.7 7.4 0.5 1.1 1.1 0.3 6.2 4.4 3.1 7.7
Value 8 2 6 2 7 2 1 2 4 1
Iteration 0: step 0.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
2.7 7.4 0.5 1.1 1.1 0.3 6.2 4.4 3.1 7.7
Value 8 2 6 2 7 2 1 2 4 1
Iteration 1: step 0.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
2.7 0.5
7.4 7.4
0.5 1.1 1.1 0.3 6.2 4.4 3.1 7.7
Value 8 2
6 6
2 2 7 2 1 2 4 1
Iteration 2: step 0.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.5
2.7 2.7
0.5 7.4 1.1 1.1 0.3 6.2 4.4 3.1 7.7
Value 8
6 6
8 2 2 7 2 1 2 4 1
Iteration 2: step 1.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.5 2.7 7.4 1.1 1.1 0.3 6.2 4.4 3.1 7.7
Value 6 8 2 2 7 2 1 2 4 1
Iteration 2: step 2.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.5 2.7 1.1
7.4 7.4
1.1 1.1 0.3 6.2 4.4 3.1 7.7
Value 6 8 2 2 7 2 1 2 4 1
Iteration 3: step 0.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.5 1.1
2.7 2.7
1.1 7.4 1.1 0.3 6.2 4.4 3.1 7.7
Value 6 8
2 2
8 2 7 2 1 2 4 1
Iteration 3: step 1.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.5 1.1 2.7 7.4 1.1 0.3 6.2 4.4 3.1 7.7
Value 6 2 8 2 7 2 1 2 4 1
Iteration 3: step 2.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.5 1.1 2.7 1.1
7.4 7.4
1.1 0.3 6.2 4.4 3.1 7.7
Value 6 2 8 2
7 7
2 2 1 2 4 1
Iteration 4: step 0.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.5 1.1 1.1
2.7 2.7
1.1 7.4 0.3 6.2 4.4 3.1 7.7
Value 6 2 8
7 7
8 2 2 1 2 4 1
Iteration 4: step 1.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.5 1.1 1.1 2.7 7.4 0.3 6.2 4.4 3.1 7.7
Value 6 2 7 8 2 2 1 2 4 1
Iteration 4: step 2.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.5 1.1 1.1 2.7 0.3
7.4 7.4
0.3 6.2 4.4 3.1 7.7
Value 6 2 7 8 2 2 1 2 4 1
Iteration 5: step 0.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.5 1.1 1.1 0.3
2.7 2.7
0.3 7.4 6.2 4.4 3.1 7.7
Value 6 2 7 8
2 2
8 2 1 2 4 1
Iteration 5: step 1.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.5 1.1 0.3
1.1 1.1
0.3 2.7 7.4 6.2 4.4 3.1 7.7
Value 6 2 7
2 2
7 8 2 1 2 4 1
Iteration 5: step 2.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.5 0.3
1.1 1.1
0.3 1.1 2.7 7.4 6.2 4.4 3.1 7.7
Value 6 2 2 7 8 2 1 2 4 1
Iteration 5: step 3.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.3
0.5 0.5
0.3 1.1 1.1 2.7 7.4 6.2 4.4 3.1 7.7
Value 6
2 2
6 2 7 8 2 1 2 4 1
Iteration 5: step 4.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.3 0.5 1.1 1.1 2.7 7.4 6.2 4.4 3.1 7.7
Value 2 6 2 7 8 2 1 2 4 1
Iteration 5: step 5.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.3 0.5 1.1 1.1 2.7 6.2
7.4 7.4
6.2 4.4 3.1 7.7
Value 2 6 2 7 8 2
1 1
2 2 4 1
Iteration 6: step 0.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.3 0.5 1.1 1.1 2.7 6.2 7.4 4.4 3.1 7.7
Value 2 6 2 7 8 1 2 2 4 1
Iteration 6: step 1.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.3 0.5 1.1 1.1 2.7 6.2 4.4
7.4 7.4
4.4 3.1 7.7
Value 2 6 2 7 8 1 2 2 4 1
Iteration 7: step 0.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.3 0.5 1.1 1.1 2.7 4.4
6.2 6.2
4.4 7.4 3.1 7.7
Value 2 6 2 7 8 1
2 2
1 2 4 1
Iteration 7: step 1.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.3 0.5 1.1 1.1 2.7 4.4 6.2 7.4 3.1 7.7
Value 2 6 2 7 8 2 1 2 4 1
Iteration 7: step 2.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.3 0.5 1.1 1.1 2.7 4.4 6.2 3.1
7.4 7.4
3.1 7.7
Value 2 6 2 7 8 2 1 2
4 4
2 1
Iteration 8: step 0.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.3 0.5 1.1 1.1 2.7 4.4 3.1
6.2 6.2
3.1 7.4 7.7
Value 2 6 2 7 8 2 1
4 4
1 2 1
Iteration 8: step 1.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.3 0.5 1.1 1.1 2.7 3.1
4.4 4.4
3.1 6.2 7.4 7.7
Value 2 6 2 7 8 2
4 4
2 1 2 1
Iteration 8: step 2.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.3 0.5 1.1 1.1 2.7 3.1 4.4 6.2 7.4 7.7
Value 2 6 2 7 8 4 2 1 2 1
Iteration 8: step 3.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.3 0.5 1.1 1.1 2.7 3.1 4.4 6.2 7.4 7.7
Value 2 6 2 7 8 4 2 1 2 1
Iteration 9: step 0.
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in
ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
0.3 0.5 1.1 1.1 2.7 3.1 4.4 6.2 7.4 7.7
Value 2 6 2 7 8 4 2 1 2 1