Backtracking 1
Backtracking 1
Flow-chart:
Function
Anchor Step
Recursion
Infinite Loop
Backtracking
Optimisation
Decision Problem
Enumeration
Take a snap at :
• Returning values
• Modifying values
In programming:
C++:
Recursion:
o Calling my function;
So:
Backtracking.
Using the concept of recursion, we try to do lot of tasks example
1. Enumeration
2. Decision problem
3. Optimisation
Enumeration:
Trying to find all possible solutions.
Decision problem:
Trying to find all the feasible solutions.
Optimisation:
Trying to find the best solutions.
Example:
Below there is a maze..,Imagine there are more paths to exit not one like in haibujji.
If you are asked to find solutions, you start with the pen and start drawing lines till you reach the
exit part.
➔ Enumerations:
You try to do draw all lines that directs to exit ,no matter if you reach exit in the middle.
This is enumeration, trying to find all possible solutions, not solutions lets say paths.
➔ Decision problems:
As said earlier there are many paths in maze but only some solutions to reach exit, these are
solutions called feasible solutions that give us some output.
➔ Optimisation
In the feasible solutions, we can sort the solutions which we got by little strain, that is
optimisation solution.
BACKTRACKING EXAMPLE:
You took a paper to draw a path from shop A to B to C as you are not sure from where you can
find that.
But unfortunately shop B asked to go to shop F , so you changed plan and went to F shop rather
than going to C ,D shops.
1. returning a value.
Backtracking helps in processing a piece of code when you need to complete the whole solution.
Example: factorial
In the example,
If you observe, in the above picture, you need factorial(n-1) to compute factorial of n, so we
again call the same function for the value of n-1.
Here function is returning some value which is used to compute the remaining part i.e fact(n-1)
as n is already there with us in n*(fact(n-1));
2. modifying a value
1. first type does not have the capacity to change the values of a,b
Backtracking is also used in changing the values.Refer to second example in the below section.
Code in c++:
THIS WAIT WILL BE OVER WHEN I GOES OUT OF S LENGTH, THEN SLOWLY THE RESULTS
ARE COMING BACK GETS ATTACHED TO S[I] AT THE BEGINNING.
CAN YOU SAY FROM WHICH FUNCTION WE MIGHT GET REVERSED STRING BACK
if(i<s.size())
reverse(s,i+1,s[i]+temp+"",ans);
else
ans=temp;
Q:DOES THERE WOULD BE ANY EFFECT IF WE GIVE ANS AS PARAMETER INSTEAD OF &ANS,
Q:IN WHICH FUNCTION AS A NUMBER LIKE SPECIFIED ABOVE. DOES ANS IS GETTING ASSIGNED
THE REVERSE STRING.
Q:CAN YOU DO SOME CHANGES AT PARAMETER LEVEL AND ASSIGN THE SAME STRING AGAIN IN
ANS
STANDARD PROBLEMS.
1. https://leetcode.com/problems/sum-of-all-subset-xor-totals
2. https://leetcode.com/problems/power-of-three
3. https://leetcode.com/problems/power-of-four
4. https://www.geeksforgeeks.org/given-a-string-print-all-possible-palindromic-partition/amp/
5. https://www.geeksforgeeks.org/recursive-solution-count-substrings-first-last-characters/
6. https://www.geeksforgeeks.org/sort-a-stack-using-recursion/amp/
7. https://www.geeksforgeeks.org/find-middle-singly-linked-list-recursively/amp/
8. https://leetcode.com/problems/n-queens/
9. https://leetcode.com/problems/combinations/
10. https://leetcode.com/problems/maximum-score-words-formed-by-letters/
11. https://leetcode.com/problems/subsets/
12. Rat in a Maze Problem - I
13. Restore IP Addresses
See Backtracking is a high degree concept. So the level of problems will be quite high. Spendtime on
each problem.
For other problems : Striver Sheet