Polynomial-time derivation of optimal k-tree topology from Markov networks