r/programming • • May 08 '15

Five programming problems every Software Engineer should be able to solve in less than 1 hour

https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
2.5k Upvotes

2.1k comments sorted by

View all comments

Show parent comments

1

u/iqtestsmeannothing May 12 '15

Well, it took me 10 minutes to show that it is an equivalence relation on non-empty strings, but I found it easier to prove (by induction) that it is a total order on equivalence classes. How does this help us show that the algorithm is correct?

(Basic idea of the proof is to show that if compare(A, B) = compare (A * n, B), then compare(A, B) = compare(A * (n + 1), B), and induct on n, where * is the string repeat operator.)

1

u/myxie May 12 '15 edited May 12 '15

It tells us that if we sort the numbers using that ordering then we will have a well-defined total order, unique up to runs of equivalent items. Equivalent items p and q satisfy pq = qp, so reordering them (any permutation decomposing into a combination of swaps) makes no difference to the resulting number.

Suppose we have a maximal (and clearly at least one exists) ordering of numbers x1, x2, ..., xn, then it follows that xi x(i+1) >= x(i+1) xi. Ignoring equivalent items, the sorting method finds the unique such sequence, therefore it is the maximal one.

1

u/iqtestsmeannothing May 12 '15

At long last I have a proof of transitivity, from which it follows that this algorithm works. Let < be the order defined by 'compare' and let <L be the restriction of lexicographic order to pairs of strings, neither of which is a proper prefix of the other. (We write = for ordinary string equality.) We wish to show that < is a total order on non-empty strings. We've already shown that < defines an equivalence relation and is consistent over equivalent strings, so it suffices to show that < is transitive on inequivalent strings. It can be shown with some work that <L is transitive.

The definition of < is: (A < B) = (AB <L BA). Note that if A <L B then A < B (so <L is a restriction of <). If A and B can't be lexicographically compared, say B = AC, then A < B iff A < C (similarly for >).

To show that < is transitive on inequivalent strings, it suffices to show that for any pairwise inequivalent A, B, C that {A, B, C} has a maximum (or has a minimum) under <. We do this by induction on the length of ABC, and some casework.

Suppose A is the shortest string of A, B, C. We will consider the case that A is a prefix of both B and C later; for now, suppose otherwise, that A is not a prefix of both B and C. If A is a prefix of neither, then A can be lexicographically compared to both of them. If A is a prefix of B but not C, then C can be lexicographically compared to both A and B. In either case, one of the strings can be lexicographically compared to both of the others, so therefore {A, B, C} has a maximum (or minimum) under <L which therefore is a maximum (or minimum) under <, so we are done.

Now suppose A is a prefix of both B and C, and write B = AD and C = AE. We need two lemmas:

Lemma 1. If X < Z and Y < Z, then XY < Z.

Proof. XYZ <L XZY <L ZXY.

Lemma 2. If Z < X and Z < Y, then Z < XY. (proof similar)

Now we return to proving that {A, B, C} = {A, AD, AE} has a maximum (or minimum). By induction < is transitive on {A, D, E}. Suppose WLOG that D < E. There are three cases.

Case 1. A < D < E and A < E. Then A < AD and A < AE so A is a minimum of {A, B, C}.

Case 2. D < E < A and D < A. Then AD < A and AE < A so A is a maximum of {A, B, C}.

Case 3. D < A < E and D < E. We claim that B is a minimum of {A, B, C}. Certainly B = AD < A, so it suffices to show that AD < AE.

If D <L E then AD <L AE so AD < AE. Therefore we can suppose that D and E cannot be lexicographically compared, so one is a prefix of the other.

Case 3.1. Suppose E = DF. Since D < A < DF, by Lemma 1, A < F. By induction D < F, so by Lemma 1, AD < F, so AD < ADF = AE.

Case 3.1. Suppose D = EF. Since EF < A < E, by Lemma 2, F < A. By induction F < E, so by Lemma 2, F < AE, so AD = AEF < AE.

Therefore < is transitive.

1

u/myxie May 13 '15

There may be a simpler proof of transitivity.

Lemma (not proved). Suppose xy = yx where x and y are non-empty, then there exists w, m, n s.t. x = wm and y = wn (and len(w) = gcd(len(x), len(y)).

If you can prove this, transitivity is easy as you end up with y = vn = wp and so y = ur for some u and r = gcd(n, p) (by similar reasoning to the lemma, I think) and this implies that x, y and z are all of the form ui.

→ More replies (0)