Professional Documents
Culture Documents
Design and Analysis of Algorithms: Unit - V
Design and Analysis of Algorithms: Unit - V
Algorithms
Unit – V
Periyar Govt. Arts College
Cuddalore
Dr. R. Bhuvaneswari
Assistant Professor
Department of Computer Science
Periyar Govt. Arts College, Cuddalore.
1 Dr. R. Bhuvaneswari
Syllabus
UNIT - V: TRAVERSAL, SEARCHING & BACKTRACKING
Techniques for Binary Trees- Techniques for Graphs - The General
Method - The 8-Queens Problem – Sum of Subsets- Graph
Colouring- Hamiltonian Cycles.
TEXT BOOK
Fundamentals of Computer Algorithms, Ellis Horowitz, Sartaj Sahni,
Sanguthevar Rajasekaran, Galgotia Publications, 2015.
Algorithm Inorder(t)
{
if t 0 then
{
Inorder(tlchild);
visit(t);
Inorder(trchild);
}
}
Algorithm Preorder(t)
{
if t 0 then
{
visit(t);
Preorder(tlchild);
Preorder(trchild);
}
}
Periyar Govt. Arts College
6 Dr. R. Bhuvaneswari Cuddalore
Post-order Traversal
Algorithm Postorder(t)
{
if t 0 then
{
Postorder(tlchild);
Postorder(trchild);
visit(t);
}
}
Periyar Govt. Arts College
7 Dr. R. Bhuvaneswari Cuddalore
Techniques for Graphs
• The graph is a non-linear data structure. It consists of some nodes and their
connected edges. The edges may be directed or undirected.
• A Graph G is a pair (V, E), where V is a finite set of elements called vertices
or nodes and E is a set of pairs of elements of V called edges or arcs.
• A graph in which every edge is directed is called directed graph or
digraph.
• A graph in which edges are undirected are called undirected graph.
• A graph which contains parallel edges is called multi-graph.
• A graph which does not contain parallel edges are called simple graph.
• Graph traversal is the problem of visiting all the nodes in a graph in a
particular manner, updating and/or checking their values along the way.
• The graph has two types of traversal algorithms.
Depth First Search or Traversal
Breadth First Search or Traversal
Periyar Govt. Arts College
8 Dr. R. Bhuvaneswari Cuddalore
Depth First Search (DFS)
DFS(v)
{
visited[v] = 1;
for each vertex w adjacent from v do
{
if (visited[w] = 0) then DFS(w);
}
}
1, 2, 4, 8, 5, 6, 3, 7
Q Q Q
Q Q Q
Q Q
Q
Q
Q
Q
Q
𝑩𝒌 𝒙𝟏 , … , 𝒙𝒌 = 𝒕𝒓𝒖𝒆 𝒊𝒇𝒇 𝒘𝒊 𝒙𝒊 + 𝒘𝒊 ≥ 𝒎
𝒌 𝒊=𝟏 𝒊=𝒌+𝟏
𝒂𝒏𝒅 𝒘𝒊 𝒙𝒊 + 𝒘𝒌+𝟏 ≤ 𝒎
𝒊=𝟏
Example:
n = 3, m = 6, w = {2, 4, 6}
Algorithm SumOfSubsets(s, k, r)
// S = w[j]*x[j] and r = w[j]. w[j]’s are in non decreasing order. It is
//assumed that w[1] m and w[j] m.
{
x[k] = 1;
if(s+w[k] = m) then write(x[1:k]);
else if(s+w[k]+w[k+1] m) then
SumOfSubset(s+w[k],k+1,r-w[k]);
if((s+r-w[k] m) and (s+w[k+1] m) then
{
x[k]=0;
SumOfSubset(s,k+1,r-w[k]);
}
}
• A special case is the 4 - colors problem for planar graphs. The problem
is to color the region in a map in such a way that no two adjacent regions
have the same color.
• A map can be easily transformed into a graph.
• Each region of the map becomes the node, and if two regions are
adjacent, they are joined by an edge.
Periyar Govt. Arts College
25 Dr. R. Bhuvaneswari Cuddalore
Graph Coloring
• For solving the graph coloring problem, we represent the graph by its
adjacency matrix G[1:n, 1:n], where, G[i, j]= 1 if (i, j) is an edge of G,
and G[i, j] = 0 otherwise.
• The colors are represented by the integers 1, 2, ..., m and the solutions
are given by the n-tuple (x1, x2, x3, ..., xn), where xi is the color of node i.
• The total computing time of mcoloring is O(nmn).
Adjacency matrix
1, 2, 8, 7,6, 5, 4, 3, 1
A, B, C, E, D, A
A, D, E, C, B, A
1 2 3 4 5
1 0 1 1 0 1
2 1 0 1 1 1
3 1 1 0 1 0
4 0 1 1 0 1
5 1 1 0 1 0
State space tree
Boolean adjacency matrix Periyar Govt. Arts College
30 Dr. R. Bhuvaneswari Cuddalore
Hamiltonian cycles
Algorithm NextValue(k)
Algorithm Hamiltonian(k) {
//The graph is stored as an repeat
//adjacency matrix G[1:n,1:n]. {
{ x[k] = (x[k]+1)mod(n+1);
repeat if(x[k]=0) then return;
{ if(G[x[k-1],x[k]]0) then
NextValue(k); {
if(x[k]=0) then return; for j =1 to k-1 do
if(k=n) then if(x[j]=x[k]) then break;
write(x[1:n]); if(j=k) then
else Hamiltonian(k+1); if((k<n) or ((k=n) and G[x[n],x[1]]0)) then
}until false; return;
} }
}until(false);
}
Periyar Govt. Arts College
31 Dr. R. Bhuvaneswari Cuddalore