Uploaded by Sunil Kumar B

DAA-UNIT-III

advertisement
UNIT-3
Part – I : Basic Traversal and Search Techniques
By
Y. Indira Priyadarshini
Assistant Professor
Department of Computer Science and Engineering
Contents:
1.
2.
3.
4.
Techniques for binary trees.
Techniques for Graphs.
Connected components and Spanning trees.
Bi-connected components and DFS.
1.Techniques for binary trees:
Binary tree traversal: There are many operations that we
want to perform on binary trees- “traversing a tree or
visiting each node in the tree exactly once”.
Three possible traversals are defined as1. Inorder traversal
2. Preorder traversal
3. Postorder traversal
2.Techniques for Graphs:
(ii) Adjacency list: One linked list per vertex, each storing directly reachable vertices
.
(iii) Linked List or Edge list:
Graph traversal techniques:
“The process of traversing all the nodes or vertices on a graph is called graph
traversal”.
We have two traversal techniques on graphs
DFS
BFS
Depth First Search
The DFS explore each possible path to its conclusion before another path is tried.
In other words go as a far as you can (if u don’t have a node to visit), otherwise,
go back and try another way. Simply it can be called as “backtracking”.
Breadth First Search
It is one of the simplest algorithms for searching or visiting each vertex in a graph.
In this method each node on the same level is checked before the search
proceeds to the next level. BFS makes use of a queue to store visited vertices,
expanding the path from the earliest visited vertices
Example: Implement BFS,DFS traversals for the following graph
3.Connected components and Spanning trees:
Connected component: If G is connected undirected graph, then we can
visit all the vertices of the graph in the first call to BFS. The sub graph
which we obtain after traversing the graph using BFS represents the
connected component of the graph.
Spanning tree of a graph: Consider the set of all edges (u, w) where all
vertices w are adjacent to u and are not visited. According to BFS
algorithm it is established that this set of edges give the spanning tree
of G, if G is connected. We obtain depth first search spanning tree
similarly.
These are the BFS and DFS spanning trees of the graph G
4.Bi-connected components and DFS:
•A connected undirected graph is said to be bi-connected if it
remains connected after removal of any one vertex and the
edges that are incident upon that vertex.
•In this we have two components.
i. Articulation point: Let G= (V, E) be a connected undirected
graph. Then an articulation point of graph ‘G’ is a vertex
whose articulation point of graph is a vertex whose removal
disconnects the graph ‘G’. It is also known as “cut point”.
ii. Bi-connected graph: A graph ‘G’ is said to be bi-connected
if it contains no-articulation point.
UNIT-3
Part – II : Back tracking
By
Y. Indira Priyadarshini
Assistant Professor
Department of Computer Science and Engineering
Contents:
1. General Method.
2. 8 – queens problem.
3. Sum of subsets problem.
4. Graph coloring and Hamiltonian cycles.
5. Knapsack Problem.
1. General Method:
•Backtracking is the most general technique to solve problem searching for a
set of solutions or find an optimal solution by satisfying some constraints.
•In Backtracking the solution is expressed as n-tuple (x1,x2….xn) , where xi
are chosen from some finite set Si.
•Some of the problems whose solutions are best done using backtracking are
1. N-queens problem
2. Sum of Subsets
3. Graph coloring
4. Hamiltonian cycle
5. 0/1 Knapsack problem.
Principle of backtracking:
Suppose mi is the size of set Si. Then there are
m=m1m2m3…mn n-tuples that are possible for satisfying
the function P. The brute force approach would form all
these n-tuple evaluate each one with P, and save those
which yield the optimum. The backtrack algorithm has tha
ability to yield the same answer with far fewer than m
trials.
BACKTRACKING STEPS
1.
2.
3.
4.
5.
6.
Build the initial node as the active node (E-node)
With the E-node, track the next node with Depth First Search
method
Check constraint, if the constraint is satisfied then state nodes as
active nodes (E-node)
If constraint is not satisfied, declare the node as a dead node (Dnode)
If the obtained D-node, backtrack to the E-node on it, then track to
another branch
and so on until the desired solution is obtained.
2. 8 – queens problem:
3.Sum of subsets problem:
•We are given ‘n’ positive numbers called weights and we
have to find all combinations of these numbers whose sum
is M. this is called sum of subsets problem.
•If we consider backtracking procedure using fixed tuple
strategy , the elements X(i) of the solution vector is either 1
or 0 depending on if the weight W(i) is included or not.
•If the state space tree of the solution, for a node at level I,
the left child corresponds to X(i)=1 and right to X(i)=0.
Example: n=6, w[1:6]={5,10,12,13,15,18}, m=30
4.Graph coloring and Hamiltonian cycles:
5. Knapsack Problem:
Download