BERLIN – After a month of searching and testing thousands of vegetables, simple detective work trumped science in the hunt for the source of the world’s deadliest E. coli outbreak. The culprit: German-grown sprouts.

Health officials announced Friday that sprouts from a farm in northern Germany caused the outbreak that has killed 31 people, sickened nearly 3,100 and prompted much of Europe to shun vegetables.

“It was like a crime thriller where you have to find the bad guy,” said Helmut Tschiersky-Schoeneburg, head of Germany’s consumer protection agency.

Health officials said they tracked the bacteria’s path from hospital patients struggling with diarrhea and kidney failure, to the restaurants where they had dined, to specific meals and ingredients they ate, and finally back to a single farm.

There are more questions to answer, including what contaminated the sprouts in the first place: Was it tainted seeds or water, or nearby animals?

Still, it was little surprise that sprouts were the culprit. They have been blamed in least 30 food poisoning cases over the past 15 years in the U.S. and a large outbreak in Japan in 1996 that killed 11 people and sickened more than 9,000.

While sprouts are full of protein and vitamins, their growing conditions and the fact that they are mostly eaten raw make them ideal transmitters of disease. Cultivated in water, they require heat and humidity — precisely the same conditions E. coli needs to thrive. Sprouts have abundant surface area for bacteria to cling to — and washing won’t help if the seeds themselves are contaminated.

Interviews with thousands of patients — mostly women ages 20 to 50 with healthy lifestyles — led investigators to conclude initially that salads could be the problem.

Health officials immediately warned consumers to avoid cucumbers, tomatoes and lettuce — causing huge losses to European farmers as demand plummeted for their produce. But the seemingly ubiquitous alfalfa, radish and other sprouts weren’t yet on anyone’s radar.

“You get this stuff in every cafeteria,” said Gert Hahne, spokesman for the Agriculture Ministry in Lower Saxony, the state where the contaminated sprouts were found.

“But after two weeks of diarrhea, most people don’t remember if they had a few sprouts on top of a ham sandwich or mixed into a salad.”

Inspectors visited more than 400 farms in Lower Saxony alone, and the state put 1,000 people on the case, including health authorities, food inspectors and veterinarians.

Experts conducted microbiologic tests — a total of 4,645 nationwide. They visited farms and checked their hygienic conditions, especially whether manure was used and could have contaminated produce.

Then May 26, some pieces began to fall into place: Patients mentioned they had eaten sprouts and inspectors visited a small organic farm near the village of Bienenbuettel that grows many different types, including alfalfa, radish, onion, broccoli, garlic, linseed and sunflower.

Although tests turned up negative — a common result in E. coli investigations, when the offending food is usually consumed before the probe begins — authorities started looking into the farm’s delivery records.

That took them to a golf club in Lueneburg, a restaurant in Luebeck, another in Rothenburg/Wuemme and cafeterias in Frankfurt, Darmstadt and Bochum — all places where customers had fallen ill.

On Friday, authorities lifted the warning against eating cucumbers, tomatoes and lettuce, and Russia agreed to remove its ban on European vegetable imports.