필터 지우기
필터 지우기

Labelling nodes for undirected graph

조회 수: 1 (최근 30일)
Njud Alotaibi
Njud Alotaibi 2022년 2월 3일
답변: ag 2023년 9월 29일
I have the following issue in my code for L(2,1)-labelling problem. L(2,1)-labelling is to label positive integers to the nodes of graph such that
1) adjacent nodes receive value which differ by at least 2.
2) If the distance between the two nodes is 2 the difference between these nodes are 1.
my issue in the second loop
%this codes related to Njud Alotaibi (copy right)
function labellingcycles
%-----creating a graph-------
s=[1 1 2 3 4];
t=[2 3 4 5 5];
G=graph(s,t);
plot(G);
%-----Sets--------
S=[0,randperm(4)];
Ver=[];
x=[];
% the first for loop here for first random labelling for the nodes
for i=1:5
Ver(i)=S(i);
Ver(:, i) = Ver(i);
end
Ver = Ver(:, 1:5)
% the second for loop to test the values in Ver set does it meet the L(2,1)-labelling problem or not and
% store it in x set
for j=1:5
% If conditions on the values should differen from their neighbors values and %
% the difference between values should be 2 or more
if (Ver(j)~= Ver(neighbors(G,j))) & (abs(Ver(j)-Ver(neighbors(G,j)))>=2)
x(j)=Ver(j);
x(:, j) = x(j);
% if does not meet the condition i need to change the value to
% meet the condition but how
else
break % break becuse i just need to change the value that does not meet the condition
end
end
x = x(:, 1:5)
end

답변 (1개)

ag
ag 2023년 9월 29일
Hi Njud,
I understand that you need to label your graph as per the below stated conditions:
  1. Value for the adjacent nodes should differ by at least 2.
  2. Value for nodes with distance 2, should differ by 1.
To do so, you will have to use recursion or stack for implementing a graph traversal algorithm(BFS should be of help).
Please refer to the following documentation of “bfsearch, which is an in-built function for BFS,
Hope this helps!
Best Regards,
Aryan Gupta

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by