Algorithms and Data Structures for Exact Computation of Marginals