An algebraic approach to model-based diagnosis