FWIW: It's not too off topic because it's about an algorithm

The Guardian asked in a recent Monday puzzle for an optimal strategy to find the smallest and the biggest in a deck of 100 cards.

https://www.theguardian.com/science/2025/may/12/can-you-solve-it-are-you-craftier-than-a-cat-burglar

(It's not the rope riddle)

While my strategy was optimal like the proposed solution, they claimed the formal proof that there can't be any better solution to be "too technical" to be published there. (Pierre de Fermat giggles in his tomb).

The attempts in the comment section didn't impress me either.

Anyway I spent the last hours figuring out an elegant proof for a general formula of any numbers of cards.

But the comment section there is long closed and I thought the local crowd here would like to try their best too ...:)

So here the task again: (copied from above link)

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

¹) It's quite rich to ask for an optimal strategy without presenting one...

  • Comment on 1. Go compare! Guardian's algortithm riddle and mathematical proof

Replies are listed 'Best First'.
Re: 1. Go compare! Guardian's algortithm riddle and mathematical proof
by jo37 (Curate) on Jun 08, 2025 at 15:45 UTC

    Before revealing your spoiler or following the link in the original post, here is my attempt to solve the puzzle.

    I cannot prove this being optimal and I don't even have an idea about how to prove it. Tough stuff.

    I'm curious to look at the hidden/linked resources.

    Update: I had expected more information from following the link. Waiting for lanx's proof 😀.

    Greetings,
    🐻

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
      My spoiler has only the task, I didn't want to show it verbatim because of the authors copyright.

      If you follow the links you'll see that your solution is optimal.

      But I would hide your number behind spoiler tags too. :)

      I'll wait some days before showing my proof.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

      > Update: I had expected more information from following the link

      Behind the link is another link to the "solution", with another comment section. With various so called proofs, which I didn't find understandable or convincing.

      Ignore the comments about "less vs fewer"

      Except the one saying there should be a special hell for people nagging about grammar in a math blog.

      > Waiting for lanx's proof

      Please remind me next weekend. :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        For those involved with computers, just remember:

        "less" is analog,
        "fewer" is digital.

        Hey

        > > Waiting for lanx's proof

        > Please remind me next weekend. :)

        I got messages asking me when I'm posting the proof.

        I wanted everybody interested to have a chance to see the task and attempt to find a(nother) proof.

        Please /msg [lanx] if you want me to wait till the weekend. Otherwise I'll post it tomorrow.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery

Re: 1. Go compare! Guardian's algortithm riddle and mathematical proof (PROOF)
by LanX (Saint) on Jun 10, 2025 at 23:18 UTC
    Here the promised proof.

    An algorithm A was shown to calculate the min_max of a set of 100 sortable elements in a set C in o(A,C) = 148 comparisons. It was maintained that this solution is optimal.

    I'm shying away from a full formal proof, because of

    • lack of mathematical notation possible.
    • avoiding Tl;dR
    A sketch should be sufficient, feel free to ask if something is unclear

    The basic idea

    We'll is to show that any algorithm A applicable at n elements can be transformed into an algorithm A' solving the problem for n-2 elements. But in 3 steps less.

    This leads to a contradiction if 148 wasn't minimal, because one needs at least 1 comparison for n=2 and (100-2)/2*3 = 147.

    I.o.W repeated transformation of the algorithm would lead to a magical solution which works without comparisons. (one could also make this argument cleaner by induction, see below)

    Construction of A'

    Given a set C' of n elements, and an Algorithm A for n+2 elements.

    We apply A on a expanded set with two new pseudo elements I : = {Inf, -Inf} which are extreme by definition.

    C := C' + {Inf,-Inf}

    Inf and -Inf have the property that they are always bigger resp. smaller with every comparison.

    Since the result is always obvious we don't need explicit comparisons. We can short-circuit the step and feed the result immediately into the algorithm A.

    • cmp( c , Inf) = smaller
    • cmp( c ,-Inf) = bigger
    • cmp(-Inf,Inf) = smaller (edge case of former)

    But we keep records which elements in C' were compared with them.

    Furthermore - without loss of generality - we also chose cmp(-Inf,Inf) for the first (pseudo) comparison in A - since we are free to chose were we start. (1)

    After running thru the Algorithm A we have recorded C+ the subset of C' with n+ elements which were compared with Inf, C+ = { c in C' | cmp(c,Inf) }

    It's easy to see that the second biggest element in C - which is max(C') - must have been compared directly with Inf and is in C+.

    (Anyway, here the argument: In order to succeed, the algorithm A must have assured that

    for all c in C: c < c1 < ... < cm < max(C).

    But for the second biggest element in C there can't be any elements in between => m = 0 => a direct comparison must have happened.)

    Hence we get the desired max(C') by continuing the algorithm to calculate max(C+) and this is trivially known to only need n+-1 comparisons. (2)

    Analog with min(C') := min(C-) in n--1 comparisons (3)

    In short the new algorithm is

    A' := A + max(C+) + min(C-)

    With (1),(2) and (3) we avoided 1 + n+ + n- pseudo-comparisons and needed n+ -1 and n- -1 extra comparisons.

    => o(A',C') = o(A,C) - 3

    q.e.d

    Generalization

    this can be expanded to a general formula d(n) for the optimal number of steps needed for a set of size in

    It's obvious that

    • d(1) = 0
    • d(2) = 1
    We have just shown that d(n+2) >= d(n) + 3 (4)

    OTOH we can always explicitly construct a new Algorithm A* for a set C* := C + {a,b} with two new elements a,b by adding 3 new comparisons to A (5)

    • cmp(a,b) # determine the min and max of the new elements
    • cmp( max{a,b}, max C ) # compare the max's
    • cmp( min{a,b}, min C ) # compare the mins

    With (4) and (5) we get an exact result d(n+2) = d(n) + 3 for all n or more explicitly:

    • d(n) = 3/2 n -2 ; for n even
    • d(n) = 3/2 (n-1) ; for n odd
    Meta

    In a more formal proof I would have stated various lemmas and used induction and better notation. I would also have a better separation between statements and proofs.

    But I hope it's somehow clear.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      Maybe I don't understand your proof correctly or it has a flaw.

      In the three comparisons you propose between the solutions for C and C' you assume there must be a comparison between min and max. This is rarely the case in the implementations regarded as optimal. Thus your proof excludes the relevant algorithms.

      Disproving the existence of a certain algorithm is indeed hard. You must not make any assumptions about its function, otherwise you may exclude some algorithms and the proof becomes worthless.

      First of all, there is a strong need do define what that algorithm shall be. In this specific case I'd define a solving algorithm as follows:

      • It is an nondeterministic state machine.
      • The initial input is the number of items.
      • The state machine then outputs pairs (i, k) of indices and expects the result of m(i) <=> m(k) as the next input.
      • The machine stops after S steps and produces a directed graph G = (V, E) as output with these properties:
        • The vertices V are the indices 1 .. N.
        • There is an edge i -> k in E if and only if m(i) < m(k) has been directly proven in the running phase of the machine.
        • The graph G has one source in min = min m and one sink in max = max m.

      There should be a proof that there is no algorithm that produces a valid output with S < 2 N/3 - 2 comparisons. Such a proof is impossible, as the defined algorithms include those that are cheating. An algorithm may just call for N - 1 comparisons producing a straight sequence of subsequent numbers producing the trivial graph min -> p(2) -> ... ->p(n-1) -> max.

      Therefore we need to modify the setup. We do not preallocate the numbers, but arrange them in a consistent way while requested from the algorithm. E.g. if we are first asked for comparing a < b, then for c < d and afterwards for b < c, we are free to arrange these such that b > c.

      Then we need to prove that an algorithm under such dynamic conditions is not capable of:

      • Finding the solution with S < 3 N/2 - 2 comparisons in every attempt (strict). Or
      • Finding the solution with an average number of S < 3 N/2 - 2 comparisons (relaxed).

      Maybe my expectations are too high, but that is the kind of proof I had expected and I don't feel capable to provide.

      Greetings,
      🐻

      $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
        > In the three comparisons you propose between the solutions for C and C' you assume there must be a comparison between min and max.

        You mean statement (1) ?

        All algorithms must start somewhere, you just pick (Inf,-Inf) for the first pair in algorithm A.¹ And since you already know the result, it's a pseudo comparison - you skip the step it in the constructed algorithm A'.

        Just imagine playing the game with C' 100 covered cards plus two extra dummy cards in your head and applying an algorithm A for C 102 cards.

        The dealer doesn't know what's in your head, he'll never hear a question about the dummy cards, because you already know the necessary results to continue your algorithm.

        Two things are relevant:

        • you got the min&max of C' in the end
        • you asked the dealer 3 questions less than you would have needed for 102 covered cards.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery

        ¹) it doesn't matter if it was unlikely. What matters is that A is guaranteed to work with any starting pair.

Re: 1. Go compare! Guardian's algortithm riddle and mathematical proof
by jo37 (Curate) on Jun 10, 2025 at 13:35 UTC

    There is a subtlety in this puzzle. While it is possible to find min and max in the given number of operations, it can be done with one operation less, with a chance of about 0.163. In the other cases this causes on more operation. Compared to a lottery, there is a very high chance of winning the game :-)

    This makes a proof more difficult IMHO.

    Greetings,
    🐻

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
      No probabilities, no subtlety.

      Algorithms and complexities are by default calculated for the worst case.

      FWIW: I just saw a probabilistic approach to prime factorization which is very successful but also very slow in worst case.

      But this kind of algorithms play in a different league and are off topic for the task at hand.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

      Update

      > This makes a proof more difficult IMHO.

      My proof isn't difficult, and once you've seen it it feels "natural"