[This paper][1] proposes an algorithm for a slight generalisation [1]: http://www.dcc.ufla.br/infocomp/artigos/v8.4/art03.pdf