CS603PC Daa Unit-2
CS603PC Daa Unit-2
So we go on traversing all left node. as we visit the node. we will put that node into
stack remember need to visit parent after the child and as We will encounter parent first
when start from root. it's case for LIFO :) and hence the stack). Once we reach NULL
node. we will take the node at the top of the stack. last node which we visited. Print it.
Check if there is right child to that node. If yes, move right child to stack and again start
traversing left child node and put them on to stack. Once we have traversed all node. our
stack will be empty.
3
1 5
7 8 9 2 10 4 6
S1 S2 S3
DISJOINT SET UNION: Means Combination of two disjoint sets elements. Form above
example S1 U S2 ={1,7,8,9,5,2,10 }
For S1 U S2 tree representation, simply make one of the tree is a
subtree of the other.
1 1
5
7 8 9 5 7 8 9
S1U
S2 2 10 2 10
S1US2 S2US1
Find(1) S1
Find(10) S2
1 1 1
1 1 1
1
1
1 1 1
Tress can be accomplished easily if, with each set name, we keep a pointer to the
root of the tree representing that set.
For presenting the union and find algorithms, we ignore the set names and identify sets
just by the roots of the trees representing them.
For example: if we determine that element ‘i’ is in a tree with root ‘j’ has a pointer to
entry ‘k’ in the set name table, then the set name is just name[k]
For unite (adding or combine) to a particular set we use FindPointer function. Example:
If you wish to unite to Si and Sj then we wish to unite the tree with roots
FindPointer (Si) and FindPointer (Sj)
FindPointer is a function that takes a set name and determines the root of the tree that
represents it.
For determining operations:
Find(i) 1St determine the root of the tree and find its pointer to entry in setname
table.
Union(i, j) Means union of two trees whose roots are i and j.
If set contains numbers 1 through n, we represents tree node P[1:n].
n Maximum number of elements.
Each node represent in array
i 1 2 3 4 5 6 7 8 9 10
P -1 5 -1 3 -1 3 1 1 1 5
If n numbers of roots are there then the above algorithms are not useful for union and find.
For union of n trees Union(1,2), Union(2,3), Union(3,4),…..Union(n-1,n).
For Find i in n trees Find(1), Find(2),….Find(n).
Time taken for the union (simple union) is O(1) (constant).
For the n-1 unions O(n).
Time taken for the find for an element at level i of a tree → O(i).
is For n finds → O(n2).
To improve the performance of our union and find algorithms by avoiding the creation of
degenerate trees. For this we use a weighting rule for union(i, j)
For implementing the weighting rule, we need to know how many nodes there are
in every tree.
For this we maintain a count field in the root of every tree.
i root node
count[i] number of nodes in the tree.
Time required for this above algorithm is O(1) + time for remaining unchanged is
determined by using Lemma.
Collapsing rule: If ‘j’ is a node on the path from ‘i’ to its root and p[i]≠root[i], then
set p[j] to root[i].
Algorithm for Collapsing find.
Algorithm CollapsingFind(i)
//Find the root of the tree containing element i.
//collapsing rule to collapse all nodes form i to the root.
{
r;=i;
while(p[r]>0) do r := p[r]; //Find the root.
While(i ≠ r) do // Collapse nodes from i to root r.
{
s:=p[i];
p[i]:=r;
i:=s;
}
return r;
}
Collapsing find algorithm is used to perform find operation on the tree created by
WeightedUnion.
GENERAL METHOD:
Many problems are difficult to solve algorithmically. Backtracking makes it possible
to solve at least some large instances of difficult combinatorial problems.
Suppose you have to make a series of decisions among various choices, where
You don’t have enough information to know what to choose
Each decision leads to a new set of choices.
Some sequence of choices ( more than one choices) may be a solution to your
problem.
Explicit constraints.
Implicit constraints.
Explicit constraints: Explicit constraints are rules that restrict each xi to take on values only
from a given set.
Example: xi ≥ 0 or si={all non negative real numbers}
Xi=0 or 1 or Si={0, 1}
li ≤ xi ≤ ui or si={a: li ≤ a ≤ ui }
The explicit constraint depends on the particular instance I of the problem being
solved. All tuples that satisfy the explicit constraints define a possible solution space for I.
Implicit Constraints:
N-QUEENS PROBLEM:
It is a classic combinatorial problem. The eight queen’s puzzle is the problem of
placing eight queens puzzle is the problem of placing eight queens on an 8×8 chessboard so
that no two queens attack each other. That is so that no two of them are on the same row,
column, or diagonal.
The 8-queens puzzle is an example of the more general n-queens problem of placing n
queens on an n×n chessboard.
The above equation specify that x1, x2, x3, --- xk cannot lead to an answer node if this
condition is not satisfied.
X[k]=1
If(S+w[k]=m) then write(x[1: ]); // subset found.
Else if (S+w[k] + w{k+1] ≤ M)
Then SumOfSub(S+w[k], k+1, r-w[k]);
if ((S+r - w{k] ≥ M) and (S+w[k+1] ≤M) ) then
{
X[k]=0;
SumOfSub(S, k+1, r-w[k]);
}
}
Let G be a undirected graph and ‘m’ be a given +ve integer. The graph coloring
problem is assigning colors to the vertices of an undirected graph with the restriction that
no two adjacent vertices are assigned the same color yet only ‘m’ colors are used.
The optimization version calls for coloring a graph using the minimum number of
coloring. The decision version, known as K-coloring asks whether a graph is colourable
using at most k-colors.
Note that, if ‘d’ is the degree of the given graph then it can be colored with ‘d+1’
colors.
The m- colorability optimization problem asks for the smallest integer ‘m’ for which the
graph G can be colored. This integer is referred as “Chromatic number” of the graph.
Example
o Example:
o The
above map requires 4 colors.
Many years, it was known that 5-colors were required to color this map.
Ex:
Adjacency matrix is
k~0
~ ~