Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e8cd69d

Browse files
add permutations backtracking
1 parent bd588c2 commit e8cd69d

File tree

3 files changed

+111
-9
lines changed

3 files changed

+111
-9
lines changed

‎book/chapters/backtracking.adoc‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,78 @@ It stops evaluating a path as soon as some of the conditions are broken and move
2424
However, 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

‎book/chapters/sample.adoc‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,41 @@ graph G {
256256
}
257257
....
258258

259+
[graphviz, Recursive Fibonacci call tree with dp, svg]
260+
....
261+
digraph g {
262+
node [shape = record,height=.1];
263+
264+
art[label = "<f0> A|<f1> R|<f2> T"];
265+
art1[label = "<f0> A|<f1> R|<f2> T"];
266+
art2[label = "<f0> A|<f1> R|<f2> T", color="red"];
267+
atr[label = "<f0> A|<f1> T|<f2> R", color="red"];
268+
rat[label = "<f0> R|<f1> A|<f2> T"];
269+
rat1[label = "<f0> R|<f1> A|<f2> T", color="red"];
270+
rta[label = "<f0> R|<f1> T|<f2> A", color="red"];
271+
tra[label = "<f0> T|<f1> R|<f2> A"];
272+
tra1[label = "<f0> T|<f1> R|<f2> A", color="red"];
273+
tar[label = "<f0> T|<f1> A|<f2> R", color="red"];
274+
275+
"art":f0 -> "art1":f0 [ label = "1. swap A/A"];
276+
"art1":f0 -> "art2":f0 [ label = "2. swap R/R"];
277+
"art2":f2 -> "art1":f1 [ label = "3", color="grey"];
278+
"art1":f2 -> "atr":f0 [ label = "4. swap R/T"];
279+
"atr":f2 -> "art1":f2 [ label = "5", color="grey"];
280+
"art1":f1 -> "art":f0 [ label = "6", color="grey"];
281+
282+
"art":f1 -> "rat":f0 [ label = "7. swap A/R"];
283+
"rat":f0 -> "rat1":f0 [ label = "8. swap A/A"];
284+
"rat1":f2 -> "rat":f1 [ label = "9", color="grey"];
285+
"rat":f2 -> "rta":f0 [ label = "10. swap A/T"];
286+
287+
"art":f2 -> "tra":f0 [ label = "swap A/T"];
288+
"tra":f0 -> "tra1":f0 [ label = "swap R/R"];
289+
"tra":f2 -> "tar":f0 [ label = "swap R/A"];
290+
291+
}
292+
....
293+
259294

260295

261296
// ------------------

‎lab/exercises/medium/permute.js‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ var permute = function (nums) {
1313
return result;
1414
};
1515

16-
function backtrack(nums, result, first = 0) {
16+
function backtrack(nums, result, index = 0) {
1717
// console.log(Array(first).fill(" ").join(""), JSON.stringify({nums, first}));
18-
if (first === nums.length - 1) {
18+
if (index === nums.length - 1) {
1919
// console.log(nums);
2020
result.push(nums.slice());
2121
} else {
22-
for (let i = first;i < nums.length; i++) {
23-
swap(nums, first,i);
24-
backtrack(nums, result, first + 1);
25-
swap(nums, first,i);
22+
for (let current = index;current < nums.length; current++) {
23+
swap(nums, index,current);
24+
backtrack(nums, result, index + 1);
25+
swap(nums, index,current);
2626
}
2727
}
2828
};
@@ -51,5 +51,8 @@ function test() {
5151
[3, 2, 1],
5252
[3, 1, 2]
5353
]);
54+
55+
console.log(permute(Array.from('art')));
56+
5457
}
5558
test();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /