Scattering theory on graphs: II. The Friedel sum rule