extracted Floyds algorithm