Efficient Algorithms For Structured Inference And Collaborative Learning