0% found this document useful (0 votes)
7 views22 pages

Backtracking

Dsa

Uploaded by

raisinglegends38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views22 pages

Backtracking

Dsa

Uploaded by

raisinglegends38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Program to reverse a linked list using Stack

Given a linked list. The task is to reverse the order of the elements of
the Linked List using an auxiliary Stack.

Examples:
Input : List = 3 -> 2 -> 1 Output : 1 -> 2 -> 3

Input : 9 -> 7 -> 4 -> 2 Output : 2 -> 4 -> 7 -> 9


• Algorithm:
1.Traverse the list and push all of its nodes onto a stack.
2.Traverse the list from the head node again and pop a
value from the stack top and connect them in reverse
order.
• How to Reverse a Linked List using Stack
• A linked list is a data structure that consists of a sequence of nodes, where
each node contains data and a reference (or link) to the next node in the
sequence. The last node in the list points to None, indicating the end of
the list.
• Reversing a linked list means changing the order of the nodes so that the
last node becomes the first node, the second-to-last node becomes the
second node, and so on, resulting in a completely reversed sequence.
• One approach to reversing a linked list is by using a stack. A stack is a
Last-In-First-Out (LIFO) data structure, meaning that the last element
added to the stack is the first one to be removed.
Backtracking Algorithm
• A backtracking algorithm is a problem-solving algorithm that uses
a brute force approach for finding the desired output.
• The Brute force approach tries out all the possible solutions and
chooses the desired/best solutions.
• The term backtracking suggests that if the current solution is not
suitable, then backtrack and try other solutions. Thus, recursion is used
in this approach.
• This approach is used to solve problems that have multiple solutions.
If you want an optimal solution, you must go for
dynamic programming.
State Space Tree
• A space state tree is a tree representing all the possible states (solution
or nonsolution) of the problem from the root as an initial state to the
leaf as a terminal state
• The 4 Queens Problem consists in placing four queens on a 4 x
4 chessboard so that no two queens attack each other. That is, no two
queens are allowed to be placed on the same row, the same column or
the same diagonal.
• We are going to look for the solution for n=4 on a 4 x 4 chessboard in
this article.
Step 0: Initialize a 4×4 board.
• Step 1:
• Put our first Queen (Q1) in the (0,0) cell .
• ‘x‘ represents the cells which is not safe i.e. they are under attack by the
Queen (Q1).
• After this move to the next row [ 0 -> 1 ].
Step 2:
Put our next Queen (Q2) in the (1,2) cell .
After this move to the next row [ 1 -> 2 ].
• Step 3:
• At row 2 there is no cell which are safe to place Queen (Q3) .
• So, backtrack and remove queen Q2 queen from cell ( 1, 2 ) .
• Step 4:
• There is still a safe cell in the row 1 i.e. cell ( 1, 3 ).
• Put Queen ( Q2 ) at cell ( 1, 3).
Step 5:
Put queen ( Q3 ) at cell ( 2, 1 ).
• Step 6:
• There is no any cell to place Queen ( Q4 ) at row 3.
• Backtrack and remove Queen ( Q3 ) from row 2.
• Again there is no other safe cell in row 2, So backtrack again and
remove queen ( Q2 ) from row 1.
• Queen ( Q1 ) will be remove from cell (0,0) and move to next safe
cell i.e. (0 , 1).
• Step 7:
• Place Queen Q1 at cell (0 , 1), and move to next row.
Step 8:
Place Queen Q2 at cell (1 , 3), and move to next row.
Step 9:
Place Queen Q3 at cell (2 , 0), and move to next row.
Step 10:
Place Queen Q4 at cell (3 , 2), and move to next row.
This is one possible configuration of solution
• Follow the steps below to implement the idea:
• Make a recursive function that takes the state of the board and the current row number as its
parameter.
• Start in the topmost row.
• If all queens are placed, return true
• For every row.
• Do the following for each column in current row.
• If the queen can be placed safely in this column
• Then mark this [row, column] as part of the solution and recursively check if placing
queen here leads to a solution.
• If placing the queen in [row, column] leads to a solution, return true.
• If placing queen doesn’t lead to a solution then unmark this [row, column] and track back
and try other columns.
• If all columns have been tried and nothing worked, return false to trigger backtracking.
• Below is the implementation of the above Backtracking solution:

You might also like