Bridging OOD Detection and Generalization: A Graph-Theoretic View