Function to calculate degree of separation