Roy-Floyd algorithm