Average properties of combinatorial problems and the thermodynamics of spin models on graphs