Mental Models204 cards · 12 fields

Mental Models/Computing & Information

Divide and Conquer

分而治之

Split a big problem into smaller copies of itself, solve each, then put them back together.

On July 14, 2019, on Centre Court at the All England Club in London, Novak Djokovic beat Roger Federer in a five-set epic to win the Wimbledon men's singles title. That title didn't come from all 128 players playing each other. The draw is first split into a top half and a bottom half, each half into smaller sections, and so on down to the smallest groups of just two people, where one match settles it. Each round's winners move up to meet, the number of players halves, and after seven rounds only one of the 128 names is left.

You need to sort a shuffled deck of 52 cards by rank. If you insert the cards one at a time into the sorted pile, the bigger the sorted pile gets, the longer it takes to find the spot for each new card. Divide and conquer does it differently. You first split the cards into two piles of 26 and sort each one. Then you compare the top card of the two piles and put the smaller one on the result pile, until both piles are empty. Each of the two piles is sorted the same way: split it in half again, and keep going until each pile has just one card, and one card doesn't need sorting.

Halve down to single cards, then merge in pairs
HalveAgainMergeCompare52 shuffled26 cards26 cards1 per pileNothing to sort26 sorted26 sorted52 sorted
HalveAgainMergeCompare52 shuffled26 cards26 cards1 per pileNothing to sort26 sorted26 sorted52 sorted

Every layer is still "sort a pile of cards," just with fewer cards. When merging, compare only the top card of the two piles; the smaller goes first.

After you cut it, what's left is still the same problem

Cut a list of numbers in half, and each half still needs sorting, just with fewer numbers. Cut again, until each piece holds only one number, and one number is sorted by nature. From there, merge back up: two sorted lists combine into one longer one, layer by layer, until you're back to the full list.

Small answers fit into a big one only if the seams follow a rule

To merge two sorted lists, look only at the number at the front of each list, take the smaller, and repeat. What you take is always the smallest of the numbers left, because in each list no later number is smaller than an earlier one. Some problems have no seam like this. Cut a list of numbers in half and find the median of each half: those two numbers can't tell you how the numbers in the two halves interleave, so they can't be combined into the median of the whole list. Each piece would have to keep more information.

The more data, the more you save

Go from 10,000 records to 1 million, 100 times as many. Inserting records one by one into a sorted line, the work grows roughly with the square of the number of records, about 10,000 times. When you split and merge, records cut in half go at most layers deep, and each layer's merging passes over all records once, so the work grows roughly with the number of records times the number of layers. 10,000 records is about 13 layers and 1 million about 20, so the work grows only about 150 times. These savings don't come from having more hands. Even one person doing it all from start to finish saves just as much.

Divide and conquer works best when each piece, once done, can be joined directly to the others without going back to change anything, as in sorting, counting votes or counting log entries. It fails when the pieces change each other's conditions. Cut a map in half and find the shortest route around each half, and putting them together may not give the shortest route on the whole map, because how you go through one half depends on where you come in from the other. If a job is divided among several departments but nobody writes down how the results join up, what the departments hand in won't fit together.

Predict
Estimate how many days it takes to total up a national census: count how many levels there are, from province to city to county to township, and how long totaling takes at each level, then add them up.
Explore
When you face a hard problem and don't know where to start, first ask what the smallest piece looks like. Solve that, then work out how two pieces combine.
Act
Split check-in for twenty people across several desks by the first letter of their surname. Each desk's list overlaps with no other, so all the desks can let people in at once when the doors open.
  1. How small does this problem have to get before I can just say the answer?
  2. Once the two pieces are each done, how do I join them? How much work does that joining step take?

Technology

A thousand machines counting logs

You want to count how many times each web address was opened in 100 million access log entries, and one machine would take a long time. You cut the log into a thousand parts and give them to a thousand machines. Each machine counts only its own part and returns a table of "address: count." Then you merge the tables two at a time, adding up the counts for the same address, until only one total table is left. It is exactly the same as what one machine would have counted on its own.

Investing

Valuing a group layer by layer

You need to value a listed group that makes food and household goods. First you split it into its two businesses, then split each business by product line. For each product line, you use a discount rate that fits its risk to turn future cash flows into today's money. One layer up, you add up the values of the product lines. Head-office costs are subtracted only once, at the very top, and finally you subtract net debt to get the value of the equity.

Society

Counting votes on election night

You work in a county election office and have to report the county's result that night. Each polling station counts its own votes and reports just one table: each candidate's votes and the number of spoiled ballots. Each township adds up the tables of its stations, and the county then adds up the townships' tables. The county total is out.

Divide and ConquerTight Coupling and Cascading FailureWhen every link waits on the last with no slack, one failure runs all the way down.A piece of software is split among three small teams, each writing one module. Divide and conquer says to write each part to an agreed interface and put them together at the end, with all three teams working at once, which is fastest. Tight coupling sees that the three modules share one database and have to be released on the same night, so changing one place pulls on the other two, and it suggests leaving these parts to one team and not splitting them yet. If the modules talk to each other only through a few clear interfaces, split; if every change needs all three teams to meet, keep them together for now.Divide and ConquerAbstraction and ModularityWrap up the complicated part behind a simple handle, so users needn't know what's inside.When the teams' work doesn't fit together after splitting, it's often because nobody first decided what each piece hands to the others. Abstraction is what settles that: each piece shows only a few inputs and outputs, and how it works inside doesn't matter. Only on that basis can divide and conquer join the pieces' answers into a whole.Divide and ConquerCombinatorial ExplosionEach new choice multiplies the options, and soon there are too many to count.You're fitting 30 courses into a weekly timetable, with clashes between courses over teachers and classrooms, and combinatorial explosion sees more possible timetables than you could ever try. Divide and conquer wants to split by year group and schedule each separately, but some teachers teach across year groups, so the two separately scheduled timetables won't fit together. If the clashes fall on only a few teachers, schedule their classes first, then split; if there are clashes everywhere, don't split.

An operations engineer wants to find, from a week of records, how many minutes the longest unbroken outage lasted. For each minute the record says only "online" or "down." He keeps cutting the records in half until each piece is a single minute, then merges summaries of neighboring pieces layer by layer. An unbroken outage may run across the place where the records were cut.

Which of these summaries lets the merge work out the right answer?

A researcher wants to find the highest yield to maturity among 512 bond quotes. Each value is already worked out, and no two are the same. Scanning straight through from the start takes 511 comparisons. The new method keeps splitting the quotes in half, keeps the highest value in each group, then compares the groups' winners layer by layer. He still does every step himself, one after another.

How many comparisons does the new method take in all?

You need to sort 1,024 orders by amount. Both plans "cut until each piece has one order, then merge layer by layer." Plan A cuts the orders still to be sorted in half each time; Plan B cuts off just one order each time and keeps cutting the rest. Both plans merge the same way: compare the fronts of the two lists and take the smaller first.

Why is Plan B's total work so much more than Plan A's?