Added all pairs algorithm