Graphical Models: Queries, Complexity, Algorithms