DESIGN AND ANALYSIS OLUFEMI OLOLADE
OLAEWE
OF ALGORITHM
LECTURE 4
SENIOR SOFTWARE
DEVELOPER
[BSc. , M.PHIL]
WHAT IS A LOOP
A loop is a control structure that allows a
block of code to be executed repeatedly,
either a fixed number of times or until a
certain condition is met.
Loops are an essential part of programming,
as they allow you to automate repetitive tasks
and process large amounts of data efficiently.
WRITING A LOOP
Class discussion
DEFINITION OF LOOP
INVARIANT
A loop invariant is a property or condition that holds true before,
during, and after each iteration of a loop in a computer program. It
is an assertion that remains unchanged throughout the execution of
the loop, helping to ensure that the loop is correct and that the
program meets its intended purpose.
DEFINITION OF LOOP
INVARIANT
A loop invariant must satisfy the following properties:
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.
WHY STUDY LOOP
INVARIANT
The core purpose of a loop invariant is to provide a
clear understanding of the loop's behavior, help
identify errors, and aid in proving its correctness.
IMPORTANCE OF LOOP
INVARIANT IN
PROGRAMMING
Understanding the loop behavior
Error detection
Proving correctness
Debugging
Collaboration
EXAMPLES OF LOOP
INVARIANT
Arr = [1,2,9,3,1]
sum = 0
for (i =0;i<arr.size;i++)
sum += i
return sum
EXPLANATION
Initialization: Before the loop starts, the loop invariant holds true
because sum is initialized to 0, and there are no positive integers to
sum up.
Maintenance: After this iteration, sum will contain the sum of the
first i positive integers, which satisfies the loop invariant.
Termination: When the loop terminates, the loop invariant still holds
true. At this point, sum contains the sum of the first n positive
integers, as required by the loop invariant.
EXAMPLE 2
for (i =0;i<arr.size;i++)
if arr[i] == target:
return i
return -1
EXPLANATION
At the start of each iteration of the loop, the subarray arr[0:i] does
not contain the target value.
Initialization: Before the loop starts (i = 0), the loop invariant holds
true trivially because there are no elements in the subarray
arr[0:0].
Maintenance:
Assume that before the i-th iteration (0 <= i < len(arr)), the loop
invariant holds true. That is, the subarray arr[0:i] does not contain
the target value. In the i-th iteration, we check if arr[i] is equal to
the target value. If it is, we return i, which means the loop
terminates. If arr[i] is not equal to the target, the loop continues to
the next iteration. Regardless, the loop invariant still holds true
because the contents of arr[0:i] does not contain the target value.
EXPLANATION CONTD.
Termination: When the loop terminates, one of two things happens:
either we find the target value and return its index, or we iterate
through the entire array without finding the target value. In either
case, the loop invariant still holds true because we the target value
is not in arr[0:i].
STEPS IN CREATING A LOOP
INVARANT
Understand the loop
Identify a property that is true before and after each iteration
Prove that the property is true: This can be done using any of the
proof techniques
Test the loop: After creating and proving the loop invariant, it's
important to test the loop to ensure that it is functioning correctly.
This may involve testing with a variety of inputs and edge cases.
Update the invariant as needed: If you find that the loop is not
behaving as expected, you may need to revise the loop invariant
and start the process again from step 2.
COMMON PITFALLS WHILES
CREATING LOOP INVARIANT
Incorrect or incomplete invariants
Loop variables not updated properly
Using mutable variables in the invariant: use immutable variables
Invariants that are too complex