Implementation and examples for backtracking and optimization