27 August 2014 Last updated at 01:33
By Claire MarshallBBC environment correspondent
The relationship between a shepherd and his sheepdog has always seemed almost magical, but scientists now say it can be explained by two simple rules.
Researchers have used GPS data to reveal the mathematical secrets of how sheepdogs do their job.
The new model helps to explain why one shepherd and a single dog can herd an unruly flock of more than 100 sheep.
It could be used to help develop "shepherd robots", for controlling crowds or cleaning up an oil spill.
The first rule: The sheepdog learns how to make the sheep come together in a flock. The second rule: Whenever the sheep are in a tightly knit group, the dog pushes them forwards.