Sheepdogs could find their job taken over by robots after scientists learned the secret of their herding ability.
Rounding up sheep successfully is a deceptively simple process involving just two basic mathematical rules, a study found.
One causes a sheepdog to close any gaps it sees between dispersing sheep. The other results in sheep being driven forward once the gaps are sufficiently closed.
A computer simulation showed that obeying these two rules alone allowed a single shepherd - or sheepdog - to control a flock of more than 100 individuals.
The discovery has implications for human crowd control as well as the development of robots that can gather and herd livestock, say the scientists.
Lead researcher Dr Andrew King, from Swansea University, said: "If you watch sheepdogs rounding up sheep, the dog weaves back and forth behind the flock in exactly the way that we see in the model.
"We had to think about what the dog could see to develop our model. It basically sees white, fluffy things in front of it. If the dog sees gaps between the sheep, or the gaps are getting bigger, the dog needs to bring them together."
Colleague Daniel Strombom, a mathematician from Uppsala University in Sweden, added: "At every time 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's already cohesive the dog will push the herd towards the target.
"Other models don't appear to be able to herd really big groups - as soon as the number of individuals gets above 50 you start needing multiple shepherds or sheepdogs."
To conduct the study, the researchers fitted a flock of sheep and a sheepdog with backpacks containing highly accurate GPS "satnavs".
Movement tracking data from the devices was programmed into computer simulations to develop the mathematical shepherding "model".
Writing in the Journal of the Royal Society Interface, the researchers conclude: "Our approach should support efficient designs for herding autonomous, interacting agents in a variety of contexts.
"Obvious cases are robot-assisted herding of livestock, and keeping animals away from sensitive areas, but applications range from control of flocking robots, cleaning up of environments and human crowd control."
Previous strategies for guiding large numbers of robots have involved either the whole group or a leader "homing in" on a target heading.
"A simpler alternative is to shepherd such groups, using the algorithm which we have described here," said the scientists.
"This would be particularly useful for guiding robots back to a base after completion of some task."