1 of 31

COL100 �Introduction to Computer Science

Lecture 24 – Correctness and Termination

April, 2024

Semester II

2024-2025

Vireshwar Kumar, Aaditeshwar Seth

CSE@IITD

2 of 31

Agenda

  • Need for Correctness and Termination Proofs

​

  • Example: GCD

​

  • Example: Fibonacci Sequence

​

  • Take Home Message

2

Disclaimer: The contents of these slides are taken from various publicly available resources such as books, talks and lectures. They are being used for the purpose of classroom teaching and academic dissemination only. The sources are acknowledged wherever possible.

3 of 31

Algorithm

  • A finite specification of the solution to a given problem using the primitives of the computing tool.

​

  • It specifies a definite input and output

​

  • It is unambiguous

​

  • It specifies a solution as a finite process, i.e., the number of steps in the computation is finite

3

4 of 31

Recall Long Multiplication Algorithm

4

9854

67

 

9854

67

XXXX

XXXX0

Can you prove its correctness?

5 of 31

Correctness Proof

5

 

 

9854

67

XXXX

XXXX0

6 of 31

Algorithm Validity

  • Are all valid mathematical function definitions lead to valid algorithm also?

6

 

7 of 31

Algorithm Validity

  • Are all valid mathematical function definitions lead to valid algorithm also?

​

  • Not necessary!

7

 

8 of 31

Algorithm Validity

  • Are all valid mathematical function definitions lead to valid algorithm also?

​

  • How about the following definition?

8

 

9 of 31

Algorithm Validity

  • Are all valid mathematical function definitions lead to valid algorithm also?

​

  • Why the following definition not a valid algorithm?

9

 

10 of 31

Algorithm Validity

  • Are all valid mathematical function definitions lead to valid algorithm also?

​

  • Why the following definition not a valid algorithm? Non-Terminating

10

 

11 of 31

Another Example

  • Not a valid algorithm, since the definition does not tell us how to evaluate the function.

11

 

12 of 31

Algorithms

  • Thus, for all our algorithms, we need to prove

​

    • Correctness

​

    • Termination

12

13 of 31

Example: GCD

13

 

 

14 of 31

Proving Correctness

  •  

14

 

15 of 31

Proving Correctness

  •  

15

 

16 of 31

Proving Correctness

  •  

16

 

17 of 31

Proving Correctness

  •  

17

 

Both the cases are proved.

Hence, the mathematical definition is a correct algorithm

18 of 31

Proving Termination

  •  

18

 

19 of 31

Example: Fibonacci Sequence

19

 

 

20 of 31

More Efficient Fibonacci Computation

20

 

 

21 of 31

Comparison

21

def fib(n):

global ops

global calls

calls += 1

if n <= 1:

return n

else:

ops += 3

return fib(n - 1) + fib(n - 2)

​

def fiba(n, a, b):

global ops

global calls

calls += 1

if n == 0:

return a

elif n == 1:

return b

else:

ops += 2

return fiba(n - 1, b, a + b)

for n in range(4, 25):

calls = 0

ops = 0

print(fib(n), calls, ops)

​

calls = 0

ops = 0

print(fiba(n, 0, 1), calls, ops)

print()

22 of 31

Comparison

22

def fib_avoid_duplication(n):

global fib_vals

global ops

global calls

calls += 1

if n <= 1:

return n

else:

if (n - 1) not in fib_vals.keys():

fib_vals[n - 1] = fib_avoid_duplication(n - 1)

if (n - 2) not in fib_vals.keys():

fib_vals[n - 2] = fib_avoid_duplication(n - 2)

ops += 3

fib_vals[n] = fib_vals[n - 1] + fib_vals[n - 2]

return fib_vals[n]

for n in range(4, 25):

calls = 0

ops = 0

fib_vals = {}

print(fib_avoid_duplication(n), calls, ops)

print()

23 of 31

Proving Correctness of Efficient Version

  •  

23

24 of 31

Proving Correctness of Efficient Version

  •  

24

25 of 31

Proof Attempt by Induction

  •  

25

 

 

26 of 31

Intuitive Understanding

  •  

26

 

 

27 of 31

Intuitive Understanding

  •  

27

 

 

 

28 of 31

Claim

  •  

28

29 of 31

Proving the Claim by Induction

  •  

29

 

 

Using Induction Hypothesis

Rearranging terms

 

30 of 31

Take Home Message

  • We tried to prove correctness of non-trivial GCD and Fibonacci algorithms.

​

  • Our attempts to prove the correctness show that
    • Certain obvious and direct approaches may not always work.

​

    • An algorithm may have subtle generalizations (or even properties) which may need to be established before we attempt to prove correctness.

​

    • Coming up with such generalized claims is crucial to justify the correctness of the algorithm.

30

31 of 31

Take Home Message

  • Getting the right generalizations requires hard work and intuition about the algorithm.

​

  • In certain cases, designing a new algorithm often requires studying the properties and proving fresh claims based on properties.

​

  • The only way to convince ourselves about the correctness of our algorithm is to prove it as we do in mathematics.

​

  • The process of coming up with alternative algorithms and proving their correctness requires a degree of creativity.

​

  • Herein lies one of the major contributions in the field of computer science.

31