The 'sheepdog mystery' that has niggled mathematical minds for years has been solved.
The puzzle is how a single dog manages to get so many selfish sheep to move so efficiently in the same direction.
The answer, revealed in a journal published by Britain's Royal Society, is that sheepdogs cleverly follow a simple rule book.
Researchers fitted highly accurate GPS tracking devices into backpacks that were then placed on a trained Australian Kelpie sheepdog and on a flock of 46 female merino sheep in a five-hectare field.
They then used the GPS data to build a computer model of what prompted the dog to move, and how it responded.
Sheep cohesiveness is the big clue.
The dog's first rule is to bind the sheep together by weaving around side-to-side at their backs, and once this has been achieved, it drives the group forward.
"It basically sees white, fluffy things in front of it," said Andrew King of Swansea University in Wales.
"If the dog sees gaps between the sheep, or the gaps are getting bigger, the dog needs to bring them together."
Daniel Stroembom of Uppsala University in Sweden explained: "At every step in the model, the dog decides if the herd is cohesive enough or not.
"If not cohesive, it will make it cohesive, but if it is already cohesive, the dog will push the herd towards the target."
A single sheepdog can herd flocks of 80 or more sheep in its everyday work and in competitive herding trials.
The model suggests that, in theory, a dog could herd more than 100 by following the two simple rules.
The study, published in the Journal of the Royal Society Interface, is graced with an intimidatingly geeky, but hardly snappy, headline: "Solving the Shepherding Problem: Heuristics for Herding Autonomous, Interacting Agents."
But the work went beyond scientific curiosity, said the authors.
"There are numerous applications for this knowledge, such as crowd control, cleaning up the environment, herding of livestock, keeping animals away from sensitive areas and collective or guiding groups of exploring robots," said King.