You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<4> Test if the current change is a solution: reached the end of the string.
71
+
<5> Revert back the change (backtracking): Undo swap from step 2
72
+
73
+
As you can see, we iterate through each letter and swap with the following letters until we reach the end of the string. Then, we rollback the change and try another path.
74
+
75
+
In the following tree, you can visualize how the backtracking algorithm is swaping the letters.
60
76
61
77
[graphviz, Recursive Fibonacci call tree with dp, svg]
62
78
....
63
79
digraph g {
64
80
node [shape = record,height=.1];
65
81
66
-
art[label = " A| R| T"];
67
-
art1[label = " A| R| T"];
82
+
art[label = " A*| R| T"];
83
+
art1[label = " A| R*| T"];
68
84
art2[label = " A| R| T", color="red"];
69
85
atr[label = " A| T| R", color="red"];
70
-
rat[label = " R| A| T"];
86
+
rat[label = " R| A*| T"];
71
87
rat1[label = " R| A| T", color="red"];
72
88
rta[label = " R| T| A", color="red"];
73
-
tra[label = " T| R| A"];
89
+
tra[label = " T| R*| A"];
74
90
tra1[label = " T| R| A", color="red"];
75
91
tar[label = " T| A| R", color="red"];
76
92
@@ -97,18 +113,13 @@ digraph g {
97
113
}
98
114
....
99
115
116
+
.Legend:
117
+
- The [red]#red# words are the iterations added to the solution array.
118
+
- *Black* arrows indicate the `swap` operations.
119
+
- *Grey* arrows indicate the _backtracking_ operation (undo swap).
120
+
- The asterisk (`*`) indicates `start` index.
100
121
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
122
+
Most of backtracking algorihtms do something similar. What changes is the test function to determine if a current iteration is a solution or not.
0 commit comments