# How can I prove algorithm correctness? [closed]

How can I prove algorithm correctness ? when i face a problem and come up with a solution the only way to know if this a valid solution or not is by trying some test cases. if they pass through the algorithm and produce the expected output then my algorithm most properly true. but obviously this is not hold all the time because i may forget some corner cases or it is hard to figure out all the test cases. So how can I prove mathematically if my algorithm produce the expected output or not ?

For example, consider the program below.

You’re given a read only array of $n$ integers. Find out if any integer occurs more than $n/3$ times in the array in linear time and constant additional space.

Algorithm: We will use an array of size 3 to count occurrence of numbers let it be count adding numbers to our count array with its proper count. If we reach the size of count array we decrement one from count of each number. If number count becomes zero it can be safely eliminated from the count array.

Here is an example:

• Input: 4 3 3 7 2 3 4 5
• count arr (4,1) 4 as first element and 1 is its count till now
• count arr (4,1)(3,1)
• count arr (4,1)(3,2)
• count arr (4,1)(3,2)(7,1). Here we reach the max allowed size for count then we need to decrement count by one and if it reaches zero its item will be removed from our count array so count arr becomes count arr (3,1). We will proceed with next element in the array which is 2.
• count arr (3,1)(2,1)
• count arr (3,2)(2,1)
• and so on. At the end count arr will be (3,1)(5,1).

We will make another loop to the input array to count occurrence of 3 and 5 and if any one exceeds $n/3$ it will be printed out.

• You will need to tell us what algorithm you have in mind, otherwise your question is way too general. Jun 20, 2015 at 3:40
• You use mathematical proof. Jun 20, 2015 at 4:47
• I believe that Yuval meant that the way to prove depends much on the specific algorithm. There are plenty of methods: by induction, case analysis, etc. etc. Try to search this site within the algorithm tag questions that have "correctness" in their title, you will find several different examples Jun 20, 2015 at 4:48
• This is far too broad to answer. It's at essentially the level of "How do I prove a mathematical theorem?" Jun 20, 2015 at 7:54
• @RenatoSanhueza When someone tries to understand a concept, it is more important to try help his intuition rather than telling him first that it is hopelessly useless. Can we prove things about programs: Yes we can, and we do have methods for doing it in many cases. And we find new ones. The cosmological fact that there is no unique method to do it is second order for most people What is the point of telling it is undecidable when he does not really know what proving correctness means to begin with. And how many people bother to define what it means before saying it is undecidable. Jun 20, 2015 at 17:06

## 1 Answer

In practice, to prove an algorithm you should search a good invariant property for each loop. For example, if you compute in a given order the sum of $n$ integers with a for loop (indexed by $i$), the invariant could be : "At the end of an iteration, the $sum$ variable contains the sum of the $i$ first values". It's invariant over iterations number and always true, it's easy to prove this by recurrence on the iterations number. Afterwards you can easily conclude that, at the end of the loop, $i=n$ and thus that $sum$ is the expected sum.

Then, there is several levels of strictness, but for simple algorithms we can generally conclude promptly since the correction of the algorithm becomes almost always trivial with all the loops invariants. A very classical approach is to prove before that the algorithm finishes and after that the algorithm is correct when it ends. For complete examples you can look here. For more subtil algorithms, you can also need some mathematical theorems which provide some links beetween objects.

Your example is very close to Misra-Gries algorithm. You can look for the proof of the Misra-Gries algorithm and try to adapt it or you can try to bound the number of times the count of the most frequent number is decreased.

In general, there isn't a systematic way to find if an algorithm produces the expected output or not. Indeed, this systematic way would be an algorithm and such an algorithm can't exist (Rice's theorem).

• i found this paper that describe Misra-Gries algorithm is very helpful and i get a clue of why proposed algorithm is correct. Jun 20, 2015 at 14:49