Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Graph coloring

Apply color to all vertices in a


graph in such a way that
adjacent vertices do not have
same color.
Like the above graph coloring
method we need to find all
the possibilities of coloring in
each vertices in a graph using
state space tree.
This problem is also called
m coloring problem
Draw the root node, starting from
the first vertex A, we can color with
1, 2, 3(1=red,2= green,3= blue)
Assume that A is colored with
1 then explore A.
Now adjacent vertex of A is B
and D.
B only two possible color
remain is color 2 or 3.

Now assume that B vertex


colored with color 2. Then
explore B, the two adjacent
vertex of B is A and C, here A is
over so take C. B is already
colored with 2, so we can color
C with 1 or 3 color.
Let us assume C is
colored with 1 color.
Now we have to color D,
the adjacent vertex of D
is A and C.
A and C are already
colored with 1. So we can
color with 2 or 3.

So this is over we can


now backtrack
Lets move back to 1 then move
back to 2 then explore 3.

Here 3 means C, let us assume


that C is colored with 3, then
what are the possibilities for D,
Here A is colored with 1 and C is
colored with 3 so only possible
for D is 2. So here all the
combinations are over. So again
backtrack. Back track to 1 explore
3
Here 3 is B, let us assume B is colored
with 3. the adjacent vertices of B are A
and C. A is already over so C. Let us
explore C.

for C we have only two color options


either 1 or 2
Assume C is colored with first color 1.
Now need to color D, here A and C is
colored with 1 so we can color D with
either 2 or 3.
Backtrack again assume C with color 2

Here for D the adjacent vertex are A and


C colored with 1 and 2 so only possible
color for D is 3.
Now backtrack to 2 then 3 then 1 then
root node. Now go to 2 that is A is
colored with color 2.
2
If A is colored with 2 explore the
adjacent vertex B and D.
So for B we can have two color
combinations 1,3.
Now let us assume that B is colored
with 1.
Then we can colored C with 2 or 3.

Let us assume that C is colored with


2 then for D the color combinations
are 1,3 . Now backtrack to C.
Let us Assume C is colored with 3rd color. Then
for D only one color remains that is 1.
Backtrack again, now B, assume B is colored
with 3.

Then color combinations for C is 1 and 2.

Let us assume that C is colored with 1, then for


vertex D only one color is possible that is 3,
because the adjacent vertex have 1 and 2 colur.
Backtrack to C and try color with 2
Let us assume C is colored with 2
then possible color combinations
for D is 1 and 3.
Now back track to root node and
start A with color 3.
Hamiltonian cycles.

For this case it is (0, 1, 2, 4, 3, 0). This graph has some other Hamiltonian paths.
If one graph has no Hamiltonian path, the algorithm should return false.
Apply state space tree
method.set x values to
0.
Here we apply
backtracking with Depth
first search.
We make the first
starting vertex 1.
After placing 1 draw the state
space tree.
Next from one we need to try all
other possibilities. Hamiltonian
algorithm starting from 2 position
onwards. It will try to put 1 in 2
position.so already one there, if
there any vertex repeating again it
will avoid.
Then try with 2, two is already not
present, now check any edge from
1 to 2, yes. So this is valid and
draw the state space tree.
Now it go to next position.it is
already 0 now it make it as one
and check, you can see that 1 is
already there,

so change it 2 and check,


already there.

So change it to 3 and check, 3 is


not there and check is there any
edge from 2 and 3, yes there is
an edge, so it is valid draw the
state space tree.
Now move to position 4 check
with 1, 2, 3,4, you can see 1,2,3
already present. Now check
with 4, possible, check is there
any edge from 3 to 4,yes.so
draw the state space tree.
Now move to 5th
position and check with
1,2,3,4,5, only 5 is
possible and check is
there any edge from 4
to 5 yes draw the tree.

Once you reach the last


position check is there
any edge from last
1 position to first position,
yes.5 to 1 there is a
edge, it means there is a
2
cycle we found, hence it
is one solution.
3

You might also like