0% found this document useful (0 votes)
6 views15 pages

DAA Unit 5

The document discusses various algorithms and techniques in the design and analysis of algorithms, focusing on tree and graph traversals, including BFS and DFS, as well as backtracking strategies for solving problems like the N Queen and M Coloring problems. It also covers Hamiltonian cycles, combinatorial search, and typical state spaces where backtracking is applied. The content includes algorithms, examples, and advantages of backtracking in solving complex computational problems.

Uploaded by

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

DAA Unit 5

The document discusses various algorithms and techniques in the design and analysis of algorithms, focusing on tree and graph traversals, including BFS and DFS, as well as backtracking strategies for solving problems like the N Queen and M Coloring problems. It also covers Hamiltonian cycles, combinatorial search, and typical state spaces where backtracking is applied. The content includes algorithms, examples, and advantages of backtracking in solving complex computational problems.

Uploaded by

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

Design & Analysis of

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,

 The inorder traversal of the above tree is : D,B,E,A,F,C,G


Design & Analysis of
Algorithms
(Unit 5)

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,

 The preorder traversal of the above tree is : A,B,D,E,C,F,G.

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,

 The postorder traversal of the above tree is : D,E,B,F,G,C,A


Design & Analysis of
Algorithms
(Unit 5)

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,

 The DFS Traversal Sequence of the given graph is,


DFS(1)
DFS(2)
DFS(4)
DFS(3)
Design & Analysis of
Algorithms
(Unit 5)

2. Breadth First Search (BFS)


 Breadth First Search (BFS) is the traversing method used in graphs.
 It uses a queue for storing the visited vertices.
 In this method the emphasize is on the vertices of the graph, one vertex is selected at first then it is
visited and marked.
 The vertices adjacent to the visited vertex are then visited and stored in the queue sequentially.
 Similarly, the stored vertices are then treated one by one, and their adjacent vertices are visited.
 A node is fully explored before visiting any other node in the graph, in other words, it traverses
shallowest unexplored nodes first.
Algorithm
Design & Analysis of
Algorithms
(Unit 5)

 For Example,
 Consider a Graph,

 The BFS Traversal Sequence of the given graph is,


BFS(1)
BFS(2)
BFS(3)
BFS(4)

 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)

N Queen Problem using Backtracking


 The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens
attack each other.
 For example, following is a solution for 4 Queen problem.

 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 Algorithm for N Queens Problem


 The idea is to place queens one by one in different columns, starting from the leftmost column.
 When we place a queen in a column, we check for clashes with already placed queens.
 In the current column, if we find a row for which there is no clash, we mark this row and column as
part of the solution.
 If we do not find such a row due to clashes then we backtrack and return false.
Design & Analysis of
Algorithms
(Unit 5)

Few Solutions of 5 Queen Problem

Few Solutions of 8 Queen Problem


Design & Analysis of
Algorithms
(Unit 5)

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

Advantages of Backtracking Algorithm


 Backtracking Algorithm is the best option for solving tactical problem.
 Also Backtracking is effective for constraint satisfaction problem.
 In greedy Algorithm, getting the Global Optimal Solution is a long procedure and depends on user
statements but in Backtracking it can easily getable.
 Backtracking technique is simple to implement and easy to code.
 Different states are stored into stack so that the data or info can be usable anytime.
 The accuracy is granted.
 The main advantage of backtracking is to take the step back if the solution is wrong. Backtracking
allows taking the decision properly.
 For Example, In N Queen problem, if the movement of queen is wrong then by backtracking, it
allows us to make a right movement.
The solution of 4 Queen Problem is,
Design & Analysis of
Algorithms
(Unit 5)

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.

 Following is an example of graph that can be colored with 3 different colors.

Backtracking Algorithm for M Colouring Problem


 The idea is to assign colors one by one to different vertices, starting from the vertex 0.
 Before assigning a color, we check for safety by considering already assigned colors to the adjacent
vertices.
 If we find a color assignment which is safe, we mark the color assignment as part of solution.
 If we do not a find color due to clashes then we backtrack and return false.
Design & Analysis of
Algorithms
(Unit 5)

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}.

And the following graph doesn’t contain any Hamiltonian Cycle.


Design & Analysis of
Algorithms
(Unit 5)

Typical State Spaces where backtracking is used


 To understand how backtracking works, we must see how objects such as permutations and subsets
can be constructed by defining the right state spaces.
 Examples of several state spaces are given below,

1. Constructing All Subsets


 To design a suitable state space for representing a collection of combinatorial objects, it is important
to know the number of objects we will need to represent.
 How many subsets of an n-element set, say the integers (1, 2, ...,n}, exist?
 There are exactly two subsets for n = 1 namely Ø and (1), four subsets for n = 2, and eight subsets
for n = 3.
 Since each new element doubles the number of possibilities, there are 2" subsets of n elements.
 Each subset is described by stating which elements are in it.
 Therefore, to construct all 2n subsets, we can set up an array/vector of n cells, where the value of a is
either true or false, signifying whether the ith item is or is not in the given subset.
 To use the notion of the general backtrack algorithm, S = {true, false}, and A is a solution whenever
k >=n.
 Using this state space representation, the backtracking algorithm constructs the following sequence
of partial solutions in finding the subsets of (1, 2, 3).
 In the below figure, final solutions, that is, complete subsets, are in solid boxes.
 Dashed boxes denote the partial solution with un-instantiated positions shown by dashes.
 An accepted position i is denoted by i itself.
 The top-most state U indicates that all variables are un-instantiated.
 The arrow marked with "Done" indicates that backtracking would not yield any further state.

Generating Power Set by Backtracking


Design & Analysis of
Algorithms
(Unit 5)

2. Constructing All Permutations


 To design a suitable state space for representing permutations, we start by counting them.
 There are n distinct choices for the value of the first element of a permutation of {1, 2,...,n).
 Once we have fixed this value of a, there are n-1 candidates remaining for the second position, since
we can have any value except a1, as repetitions are forbidden.
 If we take this argument to its logical conclusion we get a total of n! distinct permutations.

Generating Permutations by Backtracking


3. Constructing All Paths in a Graph
 The enumeration of all the simple paths from s to t through a given graph is complicated compared
to listing permutations or subsets.
 Unlike the earlier problems, there is no explicit formula that maps the number of solutions as a
function of the number of edges or vertices, because the number of paths depends on the structure of
the graph.
 Backtracking provides a necessary condition for correctness by enumerating all possibilities.
 For example, a correct algorithm to find the optimal traveling salesman tour could enumerate all n!
permutations of n vertices of the graph and selecting the best one.
 For each permutation, we could check whether each of the n edges implied in the tour really exists in
the graph G, and if so, sum the weights of these edges together.
Design & Analysis of
Algorithms
(Unit 5)

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.

5. Covering Chess Boards


 Chess is a game that has fascinated mankind for thousands of years.
 In addition, inspired a number of combinatorial problems of independent interest.
 The combinatorial explosion was first recognized in the legend that the inventor of chess demanded
as payment, one grain of rice for the first square of the board, and twice the amount of the ith square
for the (i+1)st square, for a total of 64 the ith square for the (i + 1)st square, for a total of ,

6. Convex Hull (Graham's Scan)


 The problem is to construct the smallest polygon which encloses a given set of points on a plane.
 Intuitively, the polygon can be determined by stretching a rubber band around the set of points.

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.

You might also like