An optimal information gathering algorithm