DAA Unit 5
DAA Unit 5
lgorithms
(Unit 5: Question Bank)
1. Explain traversal of tree and write an algorithm to generate a tree, give its inorder, preorder and
postorder sequences.
2. Explain BFS algorithm with example.
3. Explain DFS algorithm with example. Explain in which case DFS is better than BFS.
4. Explain Backtracking strategy for 8 Queen Problems. And find out two solutions for it.
Explain Backtracking strategy for N Queen Problems. Give at least 3 solutions of 5 Queen Problem.
5. Explain Backtracking framework in brief.
6. What are the advantages of Back Tracking? Explain with proper example.
7. Explain m coloring problem with an example.
8. Explain Hamiltonian cycle with example.
9. List and explain the different state spaces.
10. Explain some typical spaces where backtracking is used.
11. Explain Combinatorial Search with example.
12. Explain BFS and DFS traversal algorithm with adjacency list and Adjacency matrix.
13. Consider the graph G = (V, E) shown in fig, Find the Hamiltonian circuit using back tracking
method.
Design & Analysis of
Algorithms
(Unit 5)
Traversal of Tree
Traversal is a process to visit all the nodes of a tree and may print their values too.
Because, all nodes are connected via edges (links) we always start from the root (head) node.
That is, we cannot randomly access a node in a tree.
There are three ways which we use to traverse a tree,
1. In-order Traversal
2. Pre-order Traversal
3. Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the
values it contains.
1. In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right sub-tree.
We should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order.
Algorithm
Inorder (T)
1. If T != Null then
2. Call Inorder(LeftChild(T));
3. Print(Data(T));
4. Call Inorder(RightChild(T));
5. End Inorder
For Example,
2. Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally the right
subtree.
Algorithm
Preorder (T)
6. If T != Null then
7. Print(Data(T));
8. Call Preorder(LeftChild(T));
9. Call Preorder(RightChild(T));
10. End Preorder
For Example,
3. Post-order Traversal
In this traversal method, the root node is visited last, hence the name.
First we traverse the left subtree, then the right subtree and finally the root node.
Algorithm
Postorder (T)
11. If T != Null then
12. Call Postorder(LeftChild(T));
13. Call Postorder(RightChild(T));
14. Print(Data(T));
15. End Postorder
For Example,
Graph Traversal
Traversal is a process to visit all the nodes of a graph and may print their values too.
There are two graph traversal techniques,
1. Depth First Search (DFS)
Depth First Search (DFS) traversing method uses the stack for storing the visited vertices.
DFS is the edge based method and works in the recursive fashion where the vertices are explored
along a path (edge).
The exploration of a node is suspended as soon as another unexplored node is found and the deepest
unexplored nodes are traversed at foremost.
DFS traverse/visit each vertex exactly once and each edge is inspected exactly twice.
Algorithm
For Example,
Consider a Graph,
For Example,
Consider a Graph,
BFS and DFS, both of the graph searching techniques have similar running time but different
space consumption, DFS takes linear space because we have to remember single path with
unexplored nodes, while BFS keeps every node in memory.
DFS yields deeper solutions and is not optimal, but it works well when the solution is dense
whereas BFS is optimal which searches the optimal goal at first.
Design & Analysis of
Algorithms
(Unit 5)
The expected output is a binary matrix which has 1s for the blocks where queens are placed.
For example, following is the output matrix for above 4 queen solution.
Backtracking Framework
Backtracking is an algorithmic-technique for solving problems recursively by trying to build a
solution incrementally, one piece at a time, removing those solutions that fail to satisfy the
constraints of the problem at any point of time.
Backtracking can be defined as a general algorithmic technique that considers searching every
possible combination in order to solve a computational problem.
There are three types of problems in backtracking –
a. Decision Problem: In this, we search for a feasible solution.
b. Optimization Problem: In this, we search for the best solution.
c. Enumeration Problem: In this, we find all feasible solutions.
The method follows the common man’s adage “count again from the point where we made a
mistake”.
Algorithm
M Colouring Problem
Given an undirected graph and a number m, determine if the graph can be colored with at most m
colors such that no two adjacent vertices of the graph are colored with the same color.
Here coloring of a graph means the assignment of colors to all vertices.
Input:
1) A 2D array graph[V][V] where V is the number of vertices in graph and graph[V][V] is adjacency
matrix representation of the graph. A value graph[i][j] is 1 if there is a direct edge from i to j, otherwise
graph[i][j] is 0.
2) An integer m which is the maximum number of colors that can be used.
Output:
An array color[V] that should have numbers from 1 to m. color[i] should represent the color assigned to
the ith vertex. The code should also return false if the graph cannot be colored with m colors.
Hamiltonian Cycle
Hamiltonian Path in an undirected graph is a path that visits each vertex exactly once.
A Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian Path such that there is an edge (in the
graph) from the last vertex to the first vertex of the Hamiltonian Path.
To Determine whether a given graph contains Hamiltonian Cycle or not.
If it contains, then prints the path.
Following are the input and output of the required function.
Input:
A 2D array graph[V][V] where V is the number of vertices in graph and graph[V][V] is adjacency
matrix representation of the graph.
A value graph[i][j] is 1 if there is a direct edge from i to j, otherwise graph[i][j] is 0.
Output:
An array path[V] that should contain the Hamiltonian Path. path[i] should represent the ith vertex in
the Hamiltonian Path.
The code should also return false if there is no Hamiltonian Cycle in the graph.
Backtracking Algorithm
1. Create an empty path array and add vertex 0 to it.
2. Add other vertices, starting from the vertex 1.
3. Before adding a vertex, check for whether it is adjacent to the previously added vertex and not
already added.
4. If we find such a vertex, we add the vertex as part of the solution.
5. If we do not find a vertex then we return false.
For Example,
For example, a Hamiltonian Cycle in the following graph is {0, 1, 2, 4, 3, 0}.
4. Bandwidth Minimization
To have a clear understanding of the power of pruning and symmetry detection, let us apply these
ideas to producing a search program that solves the bandwidth minimization problem.
The bandwidth problem takes as input a graph G, with n vertices and m edges.
The goal is to find a permutation of the vertices on a line that minimizes the maximum length of any
edge.
Below figure gives two distinct layouts of a complete binary tree with 15 vertices.
The clean, neat layout on the top has the longest edge of length 4, but the seemingly cramped layout
on the bottom realizes the optimal bandwidth of 3.
Convex Hull
Design & Analysis of
Algorithms
(Unit 5)
Combinatorial Search
In computer science and artificial intelligence, combinatorial search studies search algorithms for
solving instances of problems that are believed to be hard in general, by efficiently exploring the
usually large solution space of these instances.
Combinatorial search algorithms achieve this efficiency by reducing the effective size of the search
space or employing heuristics. Some algorithms are guaranteed to find the optimal solution, while
others may only return the best solution found in the part of the state space that was explored.
Classic combinatorial search problems include solving the eight queens puzzle or evaluating moves
in games with a large game tree, such as chess.
The benefits that can accrue from analyzing and improving algorithms for problems which exhibit
combinatorial explosion, the time of which grows exponentially in the size of the problem, can be
very high.
By using exhaustive search techniques, we can solve small problems optimally, although the time
complexity may be enormous.
For certain applications, it may well pay to spend some extra time, to get an optimal solution.
A good example occurs in testing a circuit or a program on all possible inputs.
We can prove the correctness of the device by trying all possible inputs and verifying that they give
the correct answer.
Proving such correctness is a property to be proud of.
However, claiming that it works correctly on all the inputs we have tried is worth much less.
Backtracking as a technique for listing all configurations representing possible solutions for a
combinatorial algorithm problem.
Techniques for pruning the search, such that, it significantly improves efficiency by eliminating
irrelevant configurations from consideration.
We can illustrate the power of some pruning techniques to speed up real-life search applications.
For problems that are too large to be solved using brute-force combinatorial search, we introduce
heuristic methods such as simulated annealing, and genetic algorithms, such heuristic methods are an
important weapon in the practical algorithm development arena.