how can i give coloring for 4 nodes with using 4 colors with optimizing backtracking algorithm.please help me to do this.

1 view (last 30 days)
clc;close all;clear all;
v=[1 3 9 10 2 4 5 6 7 8 6 2 8 2 6 8 8 5 5 6 5 7 6 7 9 10 1 3 3 3 9 7 4 6];
e=[1 3 9 10 2 4 5 6 7 8 2 6 2 8 8 6 5 8 6 5 7 5 7 6 3 3 3 9 10 1 7 9 6 4];
S=graph(v,e);
plot(S);
A=adjacency(S);
G=full(A);
G2=G;G1=G;
c2=[1 3 9 10];
G1(:,c2)=[];G1(c2,:)=[];
c1=[6 2 4 5 7 8];
G2(:,c1)=[];G2(c1,:)=[];
k=1;n=4;m=4;x1=zeros(1,4);
y=Graphcolor(k,G2,x1,1);
function [x]=Graphcolor(k,G,y,c)
while k<5
if(colorSafe(k,c,G,y))
y(k)=c;
k=k+1;
if(k<5 && c~=4)
y=Graphcolor(k,G,y,c+1);
else
if k<5
c=1;
y = Graphcolor(k,G,y,c+1);
end
end
end
if k==5
break;
end
end
x=y;
end
function [y]=colorSafe(k,c,G,x)
i=1;
while i<5
if(G(k,i)==1 && c==x(i))
i=i+1;
y=false;
else
i=i+1;
y=true;
if i==4
break;
end
end
end
end

Answers (0)

Categories

Find more on Problem-Based Optimization Setup in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!