0% found this document useful (0 votes)
34 views

Backtracking 1

Backtracking is an algorithmic technique for solving problems recursively by trying to build a solution incrementally, abandoning a partial solution ("backtracking") as soon as it is determined that the solution cannot possibly be completed and returning back to the previous step to try other options. It is commonly used to find all (or some) solutions to some computational problem that may involve finding possible combinations or permutations, such as chess game moves, Hamiltonian paths, solutions to Sudoku puzzles, and more. The document provides examples of how backtracking can be used to solve problems involving enumeration, decision problems, and optimization through recursive functions that modify values or return values.

Uploaded by

venuupuli
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Backtracking 1

Backtracking is an algorithmic technique for solving problems recursively by trying to build a solution incrementally, abandoning a partial solution ("backtracking") as soon as it is determined that the solution cannot possibly be completed and returning back to the previous step to try other options. It is commonly used to find all (or some) solutions to some computational problem that may involve finding possible combinations or permutations, such as chess game moves, Hamiltonian paths, solutions to Sudoku puzzles, and more. The document provides examples of how backtracking can be used to solve problems involving enumeration, decision problems, and optimization through recursive functions that modify values or return values.

Uploaded by

venuupuli
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Backtracking...

Flow-chart:

Function

Anchor Step

Recursion
Infinite Loop

Backtracking

Optimisation
Decision Problem
Enumeration

Mini flow chart


Enumeration decision problem optimisation

Take a snap at :
• Returning values
• Modifying values

LET US GO BY FLOW CHART


Function:
A block of code that takes parameters(not mandatory) and does some processing(mandatory) and
returns a value(not mandatory).

➔ Mostly used when lot of objects need same functionality.


Example:
In a car ,lights, sound system , indicators all need battery power, fixing a battery for each
component is tiresome, rather keep a big battery that does the same function(discharges
battery )when any component asks.

In programming:

C++:

Recursion:
o Calling my function;

• Its basically calling the function by itself.


• Can you guess how many times the sum function is called?
• Infinite times.
• So the function is lost in infinite loop.
• We give a anchor condition to stop the function lost in infinite loop.

So:

Above function exists till b>0, stops when b<=0.


Fun fact: stops in above sentence is backtracking.

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.

We don’t try, we do...

Example:

Let us get them by an 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.

So at the end, backtracking helps to do all these three.

Dynamic programming helps us to give optimised solution for problem.

BACKTRACKING EXAMPLE:

You are asked to bring maaza from general store ,

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.

No maaza at F shop, what you do?

• You backtrack to shop B , then continue till D shop. Then after?


• You backtrack till you reach home.

Take a snap at:

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

Strictly speaking in c++, you give parameter in two different types

• Function(a,b) – first type


• Function(&a,&b) – second type

1. first type does not have the capacity to change the values of a,b

2.second type 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.

Lets finish by discussing a example.


Question: reverse the string using backtracking;

Code in c++:

1. string reverse(string s,inti)


2. {
3. if(i<s.size())
4. {
5. return reverse(s,i+1) + s[i] + "";
6. }
7. else return "";
8. }
HERE , WE ARE WAITING FOR THE STRING THAT NEEDS TO RETURNED BY
REVERSE(S,I+1) THAT WILL INTURN WAIT FOR THE STRING NEEDS TO BE RETURNED BY
REVERSE(S,I+2)

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.

Q: CAN YOU THINK WHAT WOULD HAPPEN IF WE WRITE S[I]+REVERSE(S,I+1) INSTEAD


AT LINE NO:5

DO WE GET THE SAME STRING AGAIN , HOW?

AT THE END, WE RETURN THE REVERSE OF STRING.

Q: IF YOU NUMBER EACH CALLED FUNCTION, LIKE

REVERSE(S,I) AS 1, REVERSE(S,I+1) AS 2, REVERSE(S,I+3) AS 3 ....... TILL THE END

CAN YOU SAY FROM WHICH FUNCTION WE MIGHT GET REVERSED STRING BACK

IS IT FROM THE LAST FUNCTION OR FIRST FUNCTION?

2. MODIFY THE VALUE,

➔ I AM LEAVING TO YOU HOW THE REVERSE STRING WORKS IN THIS SCENARIO.

void reverse(string s,inti,stringtemp,string&ans)

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

You might also like