@@ -24,14 +24,78 @@ It stops evaluating a path as soon as some of the conditions are broken and move
2424However, it an only be applied if a quick test can be run to tell if a candidate will contribute to a valid solution.
2525****
2626
27+ == How to develop Backtracking algorithms?
2728
28- Let's do an example to explain better how backtracking works.
29-
30- // https://leetcode.com/problems/combination-sum/description/
29+ Backtracking algorithms can be tricky to get right or reason about but we are going to follow this recipe to make it easier.
3130
31+ .Steps to create backtracking algorithms
32+ . Iterate through all the elements in the input
33+ . Make a change
34+ . Call recursive function
35+ . Test if the current change is a solution
36+ . Revert back the change (backtracking)
3237
38+ Let's do an exercise to explain better how backtracking works.
3339
40+ // https://leetcode.com/problems/combination-sum/description/
3441
42+ == Permutations
43+
44+ > Return all the permutations (without repetitions) of a given word.
45+
46+ For instace, if are given the word `art` we can have the following permutions
47+
48+ ----
49+ [ [ 'art' ],
50+ [ 'atr' ],
51+ [ 'rat' ],
52+ [ 'rta' ],
53+ [ 'tra' ],
54+ [ 'tar' ] ]
55+ ----
56+
57+
58+ We already solved this problem using an <<Getting all permutations of a word, iterative program>>, now let's do it using backtracking.
59+
60+
61+ [graphviz, Recursive Fibonacci call tree with dp, svg]
62+ ....
63+ digraph g {
64+ node [shape = record,height=.1];
65+
66+ art[label = "<f0> A|<f1> R|<f2> T"];
67+ art1[label = "<f0> A|<f1> R|<f2> T"];
68+ art2[label = "<f0> A|<f1> R|<f2> T", color="red"];
69+ atr[label = "<f0> A|<f1> T|<f2> R", color="red"];
70+ rat[label = "<f0> R|<f1> A|<f2> T"];
71+ rat1[label = "<f0> R|<f1> A|<f2> T", color="red"];
72+ rta[label = "<f0> R|<f1> T|<f2> A", color="red"];
73+ tra[label = "<f0> T|<f1> R|<f2> A"];
74+ tra1[label = "<f0> T|<f1> R|<f2> A", color="red"];
75+ tar[label = "<f0> T|<f1> A|<f2> R", color="red"];
76+
77+ art:f0 -> art1:f0 [ label = "1. swap A/A"];
78+ art1:f0 -> art2:f0 [ label = "2. swap R/R"];
79+ art2:f2 -> art1:f1 [ label = "3.", color="grey", fontcolor="grey"];
80+ art1:f2 -> atr:f0 [ label = "4. swap R/T"];
81+ atr:f2 -> art1:f2 [ label = "5.", color="grey", fontcolor="grey"];
82+ art1:f1 -> art:f0 [ label = "6.", color="grey", fontcolor="grey"];
83+
84+ art:f1 -> rat:f0 [ label = "7. swap A/R"];
85+ rat:f0 -> rat1:f0 [ label = "8. swap A/A"];
86+ rat1:f2 -> rat:f1 [ label = "9.", color="grey", fontcolor="grey"];
87+ rat:f2 -> rta:f0 [ label = "10. swap A/T"];
88+ rta:f2 -> rat:f2 [ label = "11.", color="grey", fontcolor="grey"];
89+ rat:f2 -> art:f2 [ label = "12.", color="grey", fontcolor="grey"];
90+
91+ art:f2 -> tra:f0 [ label = "13. swap A/T"];
92+ tra:f0 -> tra1:f0 [ label = "14. swap R/R"];
93+ tra1:f2 -> tra:f2 [ label = "15.", color="grey", fontcolor="grey"];
94+ tra:f2 -> tar:f0 [ label = "16. swap R/A"];
95+ tar:f2 -> tra:f2 [ label = "17.", color="grey", fontcolor="grey"];
96+ tra:f2 -> art:f2 [ label = "18.", color="grey", fontcolor="grey"];
97+ }
98+ ....
3599
36100
37101
0 commit comments