Backtracking Flashcards

1
Q

What is backtracking ??

A

In a given problem, if all possible solutions is to be found, then we can apply backtracking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is bounded function or constraint ?

A

when finding the solution, there will be constraints can be enforced. Solution which satisfies the constraint will be considered as one of the solution. If the constraint is not met, the path/track will not be explored further. will move onto next possible solution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which data structure used ?

A

State-space tree can be used to track the path traveled to find the solution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which algorithm most commonly used in backtracking ?

A

DFS with Recursive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Backtracking problems

A

N-Queens problem
Maze
sudoku

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Backtracking Vs. Branch-and-Bound

A

Backtracking uses DFS to construct the solution

Branch and Bound used BFS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

N Queens problem - Backtracking

A
  • Problem
    Place ā€˜Nā€™ queens in a N*N matrix.
  • Constraints/ Bounded Functions
    • Queens cannot be placed in same row, column, diagonally.
  • Start with Qi and place it in a matrix and place other queens by applying constraint for every move, if it satisfies move on to next queen, if not move to next place for the queen.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Sum of subsets - Backtracking

A
  • Problem
    In a given array of numbers and target sum, find all possible subset of numbers which adds upto exactly the target sum.
  • Constraints
    • added weight cannot exceed target sum
    • remaining weight has to be greater than zero before exploring the path downwards.
  • Approach.
    • maintain currSum , remainingSum
    • Add numbers from array by adding the weight to currSum and subtracting it from remaining sum. Before moving further down, apply boundary conditions.
      if remaining sum < targetSum, continue adding numbers.
      if remainingSum is > targetSum , try by not adding the number.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly