Applications of Stack
Applications of Stack
Stack is an abstract data type and a data structure that follows LIFO (last in first out)
strategy. It means the element added last will be removed first. Stack allows two operations push
and pop. Push adds an element at the top of the stack and pop removes an element from top of
the stack.
Below I have mentioned few applications of stack data structure.
Applications of Stack
Expression Evaluation
Stack is used to evaluate prefix, postfix and infix expressions.
Expression Conversion
An expression can be represented in prefix, postfix or infix notation. Stack can be used to
convert one form of expression to another.
Syntax Parsing
Many compilers use a stack for parsing the syntax of expressions, program blocks etc. before
translating into low level code.
Backtracking
Suppose we are finding a path for solving maze problem. We choose a path and after following it
we realize that it is wrong. Now we need to go back to the beginning of the path to start with new
path. This can be done with the help of stack.
Parenthesis Checking
Stack is used to check the proper opening and closing of parenthesis.
String Reversal
Stack is used to reverse a string. We push the characters of string one by one into stack and then
pop character from stack.
Function Call
Stack is used to keep information about the active functions or subroutines.
2.APPLICATIONS OF BACKTRACKING
Backtracking is a technique based on algorithm to solve problem. It uses recursive
calling to find the solution by building a solution step by step increasing values with time. It
removes the solutions that doesn't give rise to the solution of the problem based on the
constraints given to solve the problem.
Backtracking algorithm is applied to some specific types of problems,
Decision problem used to find a feasible solution of the problem.
Optimisation problem used to find the best solution that can be applied.
Enumeration problem used to find the set of all feasible solutions of the problem.
In backtracking problem, the algorithm tries to find a sequence path to the solution which has
some small checkpoints from where the problem can backtrack if no feasible solution is found
for the problem.
Example,
Here,
Green is the start point, blue is the intermediate point, red are points with no feasible solution,
dark green is end solution.
Here, when the algorithm propagates to an end to check if it is a solution or not, if it is then
returns the solution otherwise backtracks to the point one step behind it to find track to the next
point to find solution.
ALGORITHM
Step 1 − if current_position is goal, return success
Step 2 − else,
Step 3− if current_position is an end point, return failed.
Step 4− else, if current_position is not end point, explore and repeat above steps.
Let’s use this backtracking problem to find the solution to N-Queen Problem.
In N-Queen problem, we are given an NxN chessboard and we have to place n queens on the
board in such a way that no two queens attack each other. A queen will attack another queen if it
is placed in horizontal, vertical or diagonal points in its way. Here, we will do 4-Queen problem.
Here, the solution is −
Here, the binary output for n queen problem with 1’s as queens to the positions are placed.
{0 , 1 , 0 , 0}
{0 , 0 , 0 , 1}
{1 , 0 , 0 , 0}
{0 , 0 , 1 , 0}
For solving n queens problem, we will try placing queen into different positions of one row. And
checks if it clashes with other queens. If current positioning of queens if there are any two
queens attacking each other. If they are attacking, we will backtrack to previous location of the
queen and change its positions. And check clash of queen again.
ALGORITHM
Step 1 − Start from 1st position in the array.
Step 2 − Place queens in the board and check. Do,
o After placing the queen, mark the position as a part of the solution and then
recursively check if this will lead to a solution.
o Now, if placing the queen doesn’t lead to a solution and trackback and go to step
(a) and place queens to other rows.
o If placing queen returns a lead to solution return TRUE.
Step 3 − If all queens are placed return TRUE.
Step 4 − If all rows are tried and no solution is found, return FALSE.
A BST supports operations like search, insert, delete, floor, ceil, greater, smaller, etc in O(h) time
where h is height of the BST. To keep height less, self balancing BSTs (like AVL and Red Black
Trees) are used in practice. These Self-Balancing BSTs maintain the height as O(Log n).
Therefore all of the above mentioned operations become O(Log n). Together with these, BST
also allows sorted order traversal of data in O(n) time.
1. A Self-Balancing Binary Search Tree is used to maintain sorted stream of data. For
example, suppose we are getting online orders placed and we want to maintain the live data
(in RAM) in sorted order of prices. For example, we wish to know number of items
purchased at cost below a given cost at any moment. Or we wish to know number of items
purchased at higher cost than given cost.
2. A Self-Balancing Binary Search Tree is used to implement doubly ended priority queue.
With a Binary Heap, we can either implement a priority queue with support of extractMin()
or with extractMax(). If we wish to support both the operations, we use a Self-Balancing
Binary Search Tree to do both in O(Log n)
3. There are many more algorithm problems where a Self-Balancing BST is the best suited
data structure, like count smaller elements on right, Smallest Greater Element on Right
Side, etc.
4.Branch and bound
Branch and bound is an algorithm design paradigm which is generally used for solving
combinatorial optimization problems. These problems are typically exponential in terms of time
complexity and may require exploring all possible permutations in worst case. The Branch and
Bound Algorithm technique solves these problems relatively quickly.
0/1 Knapsack using Branch and Bound
Implementation of 0/1 Knapsack using Branch and Bound
8 puzzle Problem using Branch And Bound
Job Assignment Problem using Branch And Bound
N Queen Problem using Branch And Bound
Traveling Salesman Problem using Branch And Bound