Writing min function, part 5: Stabilizing the algorithm
Oct 05, 2014
This is the fifth article of the series called “Writing min function”.
Finally I will address the last pending issue.
To see the importance of the point I want to show to you, we have to write another function of our API, the max function.
This issue is not related to C++, you could be see it in the implementation of the min/max functions of any programming language. Because of that, I want to go back to pseudo-code.
So, let’s take min written in pseudo-code and adapt it to write max.
Now, it easy to write the max function:
Min and max are almost the same, the only difference resides at the two return clauses.
Now, to see the problem, let’s write some usage code:
According to the code above, what will be printed by the lines marked as #1, #2, #3 and #4?
Let’s analyze it:
-
min_e1 is the employee with lower salary between e1 and e2. Because 5000 (John salary) is less than 6000 (Peter salary) then e1 is the min employee, so in #1 “John” will be printed. Trivial case.
-
max_e1 is the employee with higher salary between e1 and e2. Doing a similar analysis, in #2 “Peter” will be printed. Another trivial case.
-
min_e2 is the employee with lower salary between e1 and e4. But, in this case both salaries are equal, so, what will be printed? Well, according to the implementation of our min function, in case of equality (or equivalence) the second return clause will executed, so, in this case “Frank” will be printed for #3.
-
max_e2 is the employee with higher salary between e1 and e4. Doing a similar analysis, in #2 “John” will be printed.
Let’s focus on the last two cases, where we have the equal or equivalent objects.
Are our min and max functions correct?
What should they return in that case?
You might wonder:
“Why worry about returning one or another object if both are equal?”
In mathematics, this question makes sense, because for example, in the set of Natural numbers, 14 is equal to 14, indeed they are identical, so in:
returning the first 14 or the second 14 does not matter, because they are the same number.
But, in computer science we deal with things that reside in memory (objects). In a computer I can represent, for example, the chair where I’m sitting now. My chair maybe is equal to the chair next to me, but for sure they are not the same chair, they are not identical.
So, in cases like this, it is important to distinguish exactly what to return.
Let’s look at what options we have for returning in case of equality of the objects:
- min returns a and max returns a
- min returns a and max returns b
- min returns b and max returns a
- min returns b and max returns b
Our min/max functions fall in the case number 3. But… why? Have we consciously designed in that way?
Which of these options is more correct?
The 1st case looks good, both functions returns the same object in case of equality, the left-hand object. Sounds consistent.
The 4th function too, both functions returns the same object, but… they are returning the right-hand object… hmm, at least to me, it seems a bit odd.
The 3rd case sounds more illogical still.
But, what about the case 2?
The answer here lies in the concept of stability.
Surely you know the property of stability from algorithms such as sort.
An stable algorithm preserves the relative order of equivalent objects.
But, what have to do stability and sorting with min and max?
“To see it, let us implement another function that could be done with two objects and a strict weak ordering on them, namely, a sorting function” [1]
sort_2_wrong has two problems:
- it does more work than necessary, because equivalent objects are swapped
- it is not stable, because the relative order of equivalent objects is not preserved.
“Stability is an important property, and we should not abandon it without necessity. As a matter of fact, it is trivial to fix the problem by performing the swap only when the second object is strictly less than the first” [1]
Now, we can see that there should be a relationship between min, max and sort_2: after we sort two elements, the first one should be the minimum and the second one should be the maximum.
In the code above, note the specification of the postcondition written as a comment:
But this postcondition never holds. Our min function returns the second object and our max function returns the first one when both objects are equivalent.
We need to make min and max stable [2]:
You could argue, “Stability is not a property of the min (or max) function, according to its definition. So, if we add stability to min, it will become something else, other than min”
Thoughts like this are wrong.
The first thing you have to think is: What is the definition of min?
Remember, this is Computer Science, we deal non-identical things.
Think!. What are our options?
There are three ways a library can implement min:
- Random
- Anti-Stable
- Stable
The random way is obviously crazy and expensive.
The stable way is more useful and as efficient as the anti-stable one.
So, what would you choose?
Whatever your decision, you must specify (document) in your library/API the behavior of min (and max) in case of equality (or equivalence) of its arguments.
Do not make me guess or have to look at the implementation details of your API to discover the behavior!
This would be wrong, for sure.
We will see in the following articles some languages/libraries whose specification is definitely wrong.
Conclusion: Make min and max functions stable, and so consistent with the other functions of your API.
Specify their behavior in the library documentation.
The Series
Part 1: The rise of Concepts
Part 2: Understanding Concepts
Part 3: Weakening the ordering
Part 4: Const-Correctness
Part 5: Stabilizing the algorithm
References
[1] Notes on Programming by Alexander Stepanov [2007, Pages 61-62]
[2] Note the use of the Conditional operator