Implement the friend of friends algorithm to find mutual connections within a social graph. Describe your solution's time and space complexity.