DAA Unit-3 Course Material
DAA Unit-3 Course Material
COURSE MATERIAL
UNIT 3
COURSE B.TECH
SEMESTER 3-2
Version V-1
31|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
32|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
1. Course Objectives
1. To demonstrate the importance of algorithms in computing.
2. To explain the analysis of algorithms
3. To illustrate the method of finding the complexity of algorithms
4. To explain the advanced algorithm design and analysis techniques.
5. To introduce special classes of algorithms NP – completeness and the classes P
and NP.
2. Prerequisites
Students should have knowledge on
1. Students should have basic knowledge on graphs
2. They should have basic data structure’s knowledge.
3. Syllabus
UNIT III
Basic Traversal and Search Techniques: Techniques for binary trees, Techniques for Graphs,
Connected components and Spanning trees, Bi-connected components and DFS
Back tracking: General Method, 8 – queens problem, Sum of subsets problem, Graph coloring and
Hamiltonian cycles, Knapsack Problem.
4. Course outcomes
DAA PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 P10 PO11 PO12 PSO1 PSO2
CO1 3 3 2 2
CO2 3 3 2 2
CO3 3 3 2 2
CO4 3 3 2 2
CO5 3 3 2 2
33|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
5. Lesson Plan
Lecture No. Weeks Topics to be covered References
34|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
• In order
• Post order
• Pre order
Examples
In order: A-B-C-D-E-F-G-H-I
`
Algorithm postorder(x)
Input: x is the root of a subtree
If x ≠ NULL
Then postorder(left(x));;
Postorder(right(x));
Outputkey(x);
Algorithm inorder(x)
Input: x is the root of a subtree
If x≠ null
Then inorder(left(x));
Outputkey(x);
Inorder(right(x));
Fig: c) inorder
36|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
Exercise
37|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
3.1 TECHNIQUES FOR GRAPHS
• Graph: The sequence of edges that connected two vertices. A graph is a pair (V,
E), where
• V is a set of nodes, called vertices
• E is a collection (can be duplicated) of pairs of vertices, called edges Vertices and
edges are data structures and store elements.
• Types of graphs: Graphs are of three types.
• Directed/Undirected: In a directed graph the direction of the edges must be
considered
38|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
Cyclic/Acyclic: A cycle is a path that begins and ends at same vertex and A graph with
no cycles is acyclic.
39|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
2. Adjacency list: One linked list per vertex, each storing directly reachable vertices .
310|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
3.1.2.1 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”.
Steps for DFS
1. Select an unvisited node ‘v’ visits it and treats it as the current node.
2. Find an unvisited neighbor of current node, visit it and make it new current node
If the current node has no unvisited neighbors, backtrack to its parent and make it
As a new current node
3. Repeat steps 2 and 3 until no more nodes can be visited
4. Repeat from step 1 for remaining nodes also.
Implementation of DFS DFS (Vertex)
{
Mark u as visiting
For each vertex V directly reachable from u
If v is unvisited DFS (v)
}
311|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
• Unexplored vertex: The node or vertex which is not yet visited.
• Visited vertex: The node or vertex which is visited is called ‘visited vertex’ i.e. can be
called as “current node”.
• Unexplored edge: The edge or path which is not yet traversed.
• Discovery edge: It is opposite to unexplored edge, the path which is already
traversed is known as discovery edge.
• Back edge: If the current node has no unvisited neighbors we need to backtrack to
its parent node. The path used in back tracking is called back edge.
For the following graph the steps for tracing are as follows:
312|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
PROPERTIES OF DFS
• DFS (G, v) visits all the vertices and edges in the connected component of v.
• The discovery edges labeled by DFS (G, v) form a spanning tree of the connected
component of v.
• Tracing of graph using Depth First Search
313|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
IMPLEMENTATION OF BFS
315|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
316|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
PROPERTIES OF BFS
Notation: Gs (connected component of s)
• BFS (G, s) visits all the vertices and edges of Gs
• The discovery edges labeled by BFS (G, s) form a spanning tree Ts of G
• For each vertex v in Li
• The path of Ts from s to v has i edges
• Every path from s to v in Gs has at least i edges.
317|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
COMPLEXITY OF BFS
• Step1: read a node from the queue O (v) times.
• Step2: examine all neighbors, i.e. we examine all edges of the currently read node.
Not oriented graph: 2*E edges to examine
• Hence the complexity of BFS is O (V + 2*E)
• Tracing of graph using Breadth first search
318|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
Thus BFS can be used to determine whether G is connected. All the newly visited vertices
on call to BFS represent the vertices in connected component of graph G. The sub
graph formed by theses vertices make the connected component.
• 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
319|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
i) After deleting vertex B and incident edges of B, the given graph is divided into two
components
320|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
ii) After deleting the vertex E and incident edges of E, the resulting components are
iii) After deleting vertex F and incident edges of F, the given graph is divided into teo
components.
321|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
322|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
The solution is based on finding one or more vectors that maximize, minimize, or satisfy a
criterion function P (x1, ......... , xn).
Form a solution and check at every step if this has any chance of success. If the solution
at any point seems not promising, ignore it. All solutions requires a set of constraints
divided into two categories: explicit and implicit constraints.
• Definition 1: Explicit constraints are rules that restrict each xi to take on values only
from a given set. Explicit constraints depend on the particular instance I of
problem being solved. All tuples that satisfy the explicit constraints define a possible
solution space for I.
• Definition 2: Implicit constraints are rules that determine which of the tuples in the
solution space of I satisfy the criterion function. Thus, implicit constraints describe
the way in which the xi‟s must relate to each other.
Explicit constraints using 8-tuple formation, for this problem are S= {1, 2, 3, 4, 5, 6, 7, 8}.
The implicit constraints for this problem are that no two queens can be the same (i.e., all
queens must be on different columns) and no two queens can be on the same diagonal.
Backtracking is the procedure where by, after determining that a node can lead to
nothing but dead end, we go back (backtrack) to the nodes parent and proceed with
the search on the next child.
A backtracking algorithm need not actually create a tree. Rather, it only needs to keep
track of the values in the current branch being investigated. This is the way we
implement backtracking algorithm. We say that the state space tree exists implicitly in
the algorithm because it is not actually constructed.
1.7.1. TERMINOLOGY
• State space is the set of paths from root node to other nodes. State space tree is
the tree organization of the solution space. The state space trees are called static
323|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
trees. This terminology follows from the observation that the tree organizations are
independent of the problem instance being solved. For some problems it is
advantageous to use different tree organizations for different problem instance.
• In this case the tree organization is determined dynamically as the solution space is
being searched. Tree organizations that are problem instance dependent are
called dynamic trees.
• Live node is a node that has been generated but whose children have not yet
been generated.
• E-node is a live node whose children are currently being explored. In other words,
an E-node is a node currently being expanded.
• Dead node is a generated node that is not to be expanded or explored any
further. All children of a dead node have already been expanded
• Branch and Bound refers to all state space search methods in which all children of
an E-node are generated before any other live node can become the E-node.
• Depth first node generation with bounding functions is called backtracking. State
generation methods in which the E-node remains the E-node until it is dead, lead
to branch and bound methods.
1.8 SUM OF SUBSETS
• Given positive numbers wi, 1 ≤ i ≤ n, and m, this problem requires finding all subsets
of wi whose sums are „m‟.
• All solutions are k-tuples, 1 ≤ k ≤ n. Explicit constraints:
• xi Є {j | j is an integer and 1 ≤ j ≤ n}.
• Implicit constraints:
• No two xi can be the same.
• The sum of the corresponding wi‟s be m.
• xi < xi+1 , 1 ≤ i < k (total order in indices) to avoid generating multiple instances of
the same subset (for example, (1, 2, 4) and (1, 4, 2) represent the same subset).
• A better formulation of the problem is where the solution subset is represented by
an n- tuple (x1,......... , xn) such that xi Є {0, 1}.
• The above solutions are then represented by (1, 1, 0, 1) and (0, 0, 1, 1). For both the
above formulations, the solution space is 2n distinct tuples.
• For example, n = 4, w = (11, 13, 24, 7) and m =
• 31, the desired subsets are (11,
• 13, 7) and (24, 7).
324|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
325|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
• The graph is
• // represented by its Boolean adjacency matrix G [1: n, 1: n]. All assignments of
• // 1, 2, . . . . . , m to the vertices of the graph such that adjacent vertices are
assigned
• // distinct integers are printed. k is the index of the next vertex to color.
• {
• repeat
• { // Generate all legal assignments for x[k].
• NextValue (k); // Assign to x [k] a legal color. If (x [k] = 0) then return; // No new
color possible If (k = n) then // at most m colors have been
• // used to color the n vertices.
• write (x [1: n]);
• else mcoloring (k+1);
• } until (false);
• }
// x [1] , . . . . x [k-1] have been assigned integer values in the range [1, m] such that
// adjacent vertices have distinct integers. A value for x [k] is determined in the range
// [0, m].x[k] is assigned the next highest numbered color while maintaining distinctness
// from the adjacent vertices of vertex k. If no such color exists, then x [k] is 0.
{
repeat
{
x [k]: = (x [k] +1) mod (m+1) // Next highest color.
If (x [k] = 0) then return; // All colors have been used for j := 1 to n do
{ // check if this color is distinct from adjacent colors if ((G [k, j] 0) and (x [k] = x
[j]))
// If (k, j) is and edge and if adj. vertices have the same color. then break;
}
if (j = n+1) then return; // New color found
} until (false); // Otherwise try to find another color.
}
326|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
• . . . , vn+1, then the edges (vi, vi+1) are in E, 1 < i < n, and the vi are distinct expect
for v1 and vn+1, which are equal.
327|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
• The backtracking solution vector (x1, . . . . . xn) is defined so that xi represents the ith
visited vertex of the proposed cycle. If k = 1, then x1 can be any of the n vertices.
• To avoid printing the same cycle n times, we require that x1 = 1. If 1 < k < n, then xk
can be any vertex v that is distinct from x1, x2, . . . , xk–1 and v is connected by an
edge to kx-1. The vertex xn can only be one remaining vertex and it must be
connected to both xn-1 and x1.
• Using Next Value algorithm we can recursive particularize the backtracking schema
to find all Hamiltonina cycles.This algorithm is started by first initializing the
adjacency matrix G 1[1:n,1:n], then setting x[2:n] to zero and x[1] to 1, and then
executing Hamiltonian (2).
The traveling salesperson problem using dynamic programming asked for a tour that
has minimum cost. This tour is a Hamiltonian cycles. For the simple case of a graph all of
whose edge costs are identical, Hamiltonian will find a minimum-cost tour if a tour exists.
// x [1: k-1] is a path of k – 1 distinct vertices . If x[k] = 0, then no vertex has as yet been
// assigned to x [k]. After execution, x[k] is assigned to the next highest numbered vertex
// which does not already appear in x [1 : k – 1] and is connected by an edge to x [k –
1].
// Otherwise x [k] = 0. If k = n, then in addition x [k] is connected to x [1].
{
repeat
{
x [k] := (x [k] +1) mod (n+1); // Next vertex. If (x [k] = 0) then return;
If (G [x [k – 1], x [k]] 0) then
{ // Is there an edge?
for j := 1 to k – 1 do if (x [j] = x [k]) then break;
// check for distinctness.
If (j = k) then // If true, then the vertex is distinct. If ((k < n) or ((k = n) and G [x [n], x [1]]
0))
then return;
}
} until (false);
}
{ // Is there an edge?
for j := 1 to k – 1 do if (x [j] = x [k]) then break;
// check for distinctness.
If (j = k) then // If true, then the vertex is distinct. If ((k < n) or ((k = n) and G [x [n], x [1]]
0))
then return;
}
328|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
} until (false);
}
Solution
After assigning the profit and weights ,we have to take the first object weights
and check if the first weight is less than or equal to the capacity, if so then we
include that object (i.e.) the unit is 1.(i.e.) K 1.
Then We are going to the next object, if the object weight is exceeded that
object does not fit. So unit of that object is ‘0’.(i.e.) K=0.
329|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
Then We are going to the bounding function ,this function determines an upper
bound on the best solution obtainable at level K+1.
Repeat the process until we reach the optimal solution.
Algorithm Bknap(k,cp,cw
// ‘m’ is the size of the knapsack; ‘n’ no.of weights & profits. W[]&P[] are the
//weights & weights. P[I]/W[I] P[I+1]/W[I+1].
m) then
Y[k] =1;
fp = cp + P[k]; fw = Cw+W[k];
y[k] = 0;
fp = cp; fw = cw;
}
330|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
}
}
9.Practice Quiz
1. In a binary tree, the number of terminal nodes (leaf nodes)are 10, then the number of
nodes with two children are
a)9
b)10
c)11
d)20
2. If the post order and preorder traversal of a binary tree are M,N,L,P,R,O,K and
M,N,L,P,R,O,K respectively then, the in order traversal of that tree is
a) M,L,N,K,P,O,R
b)L,N,M,K,P,R,O
c)P,R,O,K,L,N,M
d)M,N,O,P,R,L,K
4. When comparing the binary tree with the general tree, which statement is true?
a) O(n+e)
b) O(ne)
c) O(n-e)
331|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
d) O(2n )
6. If S(n,e) represents the maximum additional space taken by DFS for an n-vertex, e-edge
graph, then what is the value of S(n,e) if adjacency lists are used.
a) O(n)
b) O(n+e)
c) O(n-e)
d) O(2n )
7. If T(n,e) represents the time complexity of DFS for an n-vertex, e-edge graph, then what
is the value of T(n,e) if adjacency matrix is used.
a) O(n+e)
b) O(ne)
c) O(1)
d) O(n2)
8. If adjacency matrix is used, then what is the time complexity of BFS algorithm?
a) O(n)
b) O(e)
c) O(n2)
d) O(n+e)
9. The broken edges of the depth first spanning tree of the graph are called _ _ _
b) it contains no loops
c) it contains no cycle
b) it contains no loops
c) it contains no cycle
d) it contains no parallel edges
10. Assignments
332|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
`
S.No Question BL CO
1 Explain Hamiltonian cycles with example? 2 1
2 Explain 8-queens problem? 2 1
3 Explain sum of subset problem? 2 1
4 Describe Graph colouring method? 2 1
5 Discuss Knapsack problem? 3 1
6 Describe General method with examples? 3 1
S.No Question BL CO
1 Write an algorithm to estimate the efficiency of backtracking? 1 1
333|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2
SVCE KADAPA
1. Isomorphic Graphs
Cocktail sort is the variation of Bubble Sort which traverses the list in both directions
alternatively. It is different from bubble sort in the sense that, bubble sort traverses the list in
forward direction only, while this algorithm traverses in forward as well as backward
direction in one iteration.
`
Text Book
1. Introduction to Design Analysis of Algorithms - K. Raghava Rao 2nd Edition
References:
1. Design And Analysis Of Algorithms-A.Puntambekar Technical Publications 2010.
17. Mini Project Suggestion
1.Chromatic polynomial
The chromatic polynomial is a graph polynomial studied in algebraic graph theory, a
branch of mathematics. It counts the number of graph colorings as a function of the
number of colors and was originally defined by George David Birkhoff to study the four
color problem.
2. A Harmonious Coloring
It is a recent growing topic in the last three decades. Harmonious colouring number is
used in the different families of graph such as trees, cycles, complete bipartite graphs etc.
3. Topological Sorting
Topological Sorting is mainly used for scheduling jobs from the given dependencies
among jobs. In computer science, applications of this type arise in instruction scheduling,
ordering of formula cell evaluation when re-computing formula values in spreadsheets,
logic synthesis, determining the order of compilation tasks to perform in make files, data
serialization, and resolving symbol dependencies in linkers.
335|D A A - U N I T - I I I
B.TECH_CSE_SEM 3 2