Did you solve it? The science of streaming

8 months ago 76

Earlier today I acceptable the pursuing puzzle, repeated present with its solution.

The inspiration for the occupation is 1 of the earliest, and astir important streaming algorithms, the predecessor of the tech utilized by streaming services specified arsenic Netflix and Spotify.

The large ballot brainteaser

An predetermination is held betwixt candidates A, B, C and D. You are fixed a acceptable of 100 completed ballot papers and your occupation is to enactment retired whether immoderate of the candidates has an wide majority, i.e. won 51 oregon much votes. If a campaigner has done so, you indispensable accidental who it is.

But there’s a catch. You are not allowed to number the ballot papers. That is, you are not allowed to usage numbers successful immoderate way. You can’t constitute thing down, nor are you allowed to support way of the tally successful your memory. Instead you volition person to devise a clever strategy that involves making instant decisions astir each ballot insubstantial erstwhile you spot it.

All you are allowed to bash is to determination the papers betwixt 3 piles connected a table. The starting position, arsenic illustrated below, has each the papers successful a stack connected the archetypal pile. The ballot papers are look up, truthful you tin ever spot which campaigner has received a ballot connected the ballot insubstantial astatine the apical of the pile. There are 2 different positions for piles, but arsenic yet they are empty.

At the commencement  Pile 1 has 100 ballot papers and the different   2  piles person  zero papers
At the commencement Pile 1 has 100 ballot papers and the different 2 piles person zero papers

There are lone two options to start. You determination the apical ballot connected Pile 1 to either Pile 2 oregon Pile 3. For your adjacent determination you tin bash the aforesaid again, oregon you tin determination the ballot that is connected either Pile 2 oregon Pile 3 to different pile. And truthful on. At immoderate signifier each you tin bash is to determination the ballot insubstantial that is astatine the apical of 1 heap to the apical of different pile.

Your task is to find a strategy that tells you if 1 campaigner has an wide bulk (i.e 51 oregon much votes) simply by shifting ballot papers betwixt piles. Can you bash it?

The hint

As is often the case, it is adjuvant to simplify the occupation to spot if it gives america immoderate insights. Let’s see the lawsuit erstwhile determination are lone 2 candidates, A and B.

A elemental strategy is arsenic follows: if we spot A atop the archetypal pile, we enactment it connected the 2nd pile, and if we spot B atop the archetypal pile, we enactment it connected the 3rd pile. After 100 moves, the archetypal heap volition beryllium empty, the 2nd heap volition person each the As and the 3rd heap each the Bs. We tin present enactment retired which heap has the astir ballots by placing them alternately backmost connected the archetypal pile. If determination are immoderate ballots near connected the 2nd heap astatine the end, past A has astatine slightest 51 votes, and if determination are immoderate ballots near connected the 3rd pile, past B does. Call this the “two-group strategy”.

Now effort scaling up to 4 candidates, A, B, C and D. One mode to bash it would beryllium to usage the 2 radical strategy with the candidates divided into 2 coalitions, accidental A&B vs C&D. If 1 of the coalitions has much than 51 votes, we past bash the 2 radical strategy doubly much – erstwhile for each subordinate of the winning coalition. This strategy works, but it is not precise efficient: it relies connected moving papers betwixt piles astatine slightest 500 times, arsenic good arsenic keeping tracks of coalitions. There is simply a overmuch quicker way. Can you find it?

Solution

I’m going to explicate the algorithm first, and afterwards explicate the reasoning.

STEP 1. Move the apical ballot connected Pile 1 to Pile 2

STEP 2: Look astatine the candidates chosen connected the apical ballots of Pile 1 and Pile 2. If they are the same, spot the apical ballot of Pile 1 onto the apical of Pile 2, arsenic below.

majority ballot  puzzle

If the candidates chosen connected the apical ballot of Pile 1 and the apical ballot of heap 2 are different, spot some of these ballots connected Pile 3, arsenic below.

Alex bells bulk  vote

STEP 3: If Pile 2 is empty, use STEP 1. If Pile 2 has ballots, use STEP 2. Carry connected until determination are nary much ballots near successful Pile 1. At the extremity of the process, either determination volition beryllium ballots successful Pile 2 oregon it volition beryllium empty. If it is empty, nary campaigner has an wide majority. If it is not empty, past each the ballots successful the heap 2 volition beryllium for the aforesaid candidate, and it whitethorn oregon whitethorn not beryllium the lawsuit that this campaigner has an wide majority. The last measurement is to cheque whether this campaigner does person an wide majority. We tin bash this utilizing the two-group strategy, investigating the campaigner against a conjugation of the different three, oregon somewhat faster, usage this 2nd strategy again, utilizing Pile 1 to beryllium the ‘Pile 3’ dump.

Why this works:

What the algorithm is doing is letting Pile 2 beryllium a ‘clicker’ for the campaigner successful Pile 2. When we adhd a ballot to this pile, the full fig clicks up by one, and erstwhile we region a ballot from this heap it clicks down by one.

It is intolerable astatine immoderate signifier for Pile 2 to person ballots for much than 1 candidate. It is imaginable for Pile 2 to commencement disconnected stacking ballots for, accidental A, and decorativeness disconnected with a stack of ballots for, accidental D. But this power tin lone hap erstwhile the process goes done a constituent erstwhile Pile 2 is empty.

At the extremity of the process, Pile 3 indispensable incorporate an adjacent fig of ballots, due to the fact that ballot papers are moved determination successful pairs. Each brace has votes for 2 antithetic candidates, and truthful nary azygous campaigner successful Pile 3 has much than 50 per cent of the votes successful Pile 3. (The champion they tin anticipation for is 50 per cent, i.e a ballot successful each pair.)

Thus the lone campaigner with a accidental of having an wide bulk is the campaigner successful Pile 2. If we cheque this by applying the strategy again, utilizing Pile 1 to beryllium the ‘Pile 3’ dump, past Pile 1 volition person an adjacent fig of campaigner and non-candidate ballots, and the presumption of Pile 2 volition archer america if the campaigner has a bulk oregon not.

Neat!

This solution is the ground for the Boyer-Moore bulk ballot algorithm, published by Robert S Boyer and J Strother Moore successful 1981. This algorithm and ones similar it are utilized wide contiguous successful telecommunications and the investigation of web queries.

Just to marque the analogy betwixt the puzzle and streaming algorithms clear: ideate that each ballot insubstantial is simply a portion of incoming data. You are not allowed to count, which is analogous to not being capable to usage a representation to store information. Instead, you indispensable marque an instant determination astir what to bash arsenic soon arsenic each portion of information arrives, with the anticipation that precise rapidly you tin larn immoderate wide spot of the full stream.

I anticipation you went with the flow! Please gush with your comments below.

Today’s puzzle was devised by Pierre Chardaire, a retired machine scientist.

I acceptable a puzzle present each 2 weeks connected a Monday. I’m ever connected the look-out for large puzzles. If you would similar to suggest one, email me.

I springiness schoolhouse talks astir maths and puzzles (online and successful person). If your schoolhouse is funny delight get successful touch.

Read Entire Article