You are on page 1of 1

Q2.

Write an algorithm and then code for solving the Depth First Search
Algorithm.

1. First, create a stack with the total number of vertices in the graph.
2. Now, choose any vertex as the starting point of traversal, and push that vertex
into the stack.
3. After that, push a non-visited vertex (adjacent to the vertex on the top of the
stack) to the top of the stack.
4. Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex on the
stack's top.
5. If no vertex is left, go back and pop a vertex from the stack.
6. Repeat steps 2, 3, and 4 until the stack is empty.

Q3 Write an algorithm and then code to solve computations in postfix


expression.
STEP 1: Create a stack to store operands (or values).

STEP 2: Scan the given expression and do the following for every scanned element.

a) If the element is a number, push it into the stack.

b) If the element is an operator, pop operands for the operator from the stack. Evaluate the
operator and push the result back to the stack.

STEP 3: When the expression is ended, the number in the stack is the final answer.

Q5. Write backtracking application with algorithm and code to show how stacks
has been implemented.
N QUEENS PROBLEM

You might also like