Graph theory- Recursive formula