Creating a function that outputs adjacency matrix
3 views (last 30 days)
Show older comments
I was asked to write a Matlab function Mytransitive that receives as an input the adjacency matrix
of a graph G and outputs the adjacency matrix of Gt. Your function should check whether
the input is a square binary matrix and then use the function Mysumadjacency.
Hint: you can use the predefined function min that accepts two matrices of the same size as
input and outputs their minimum computed component by component.
I have two working functions (issqbinary and mysumadjacency), but I don't know how to ouput a transitive matrix of 0's and 1's after using mysumadjacency
Function issqbinary(x)
function [result] = issqbinary(x)
[row, cols] = size(x);
if row ~= cols
[result] = false;
fprintf('error - matrix is not sq')
return
else
[result] = true
for
ii = 1:length(x)
if x(ii) ~=0 && x(ii) ~= 1
[result] = false
fprintf('error - matrix is not binary')
return
else
[result] = true;
end
end
end
end
Function mysumadjacency
function [result] = mysumadjacency(x)
if issqbinary(x) == true
g = graph(x,'upper');
A = adjacency(g);
f = full(A);
result = conv2(f, ones(length(x)), 'same');
else
result = false;
end
end
1 Comment
Answers (1)
Ayush
on 2 Dec 2024
Why not compute Transitive closure of a graph using Floyd Warshall Algorithm?
function [Gt] = Mytransitive(G)
% Check if the input is a square binary matrix
if ~issqbinary(G)
error('Input must be a square binary matrix');
end
% Initialize the transitive closure matrix Gt as a copy of G
Gt = G;
% Floyd-Warshall algorithm to compute the transitive closure
n = size(G, 1);
for k = 1:n
for i = 1:n
for j = 1:n
Gt(i, j) = Gt(i, j) || (Gt(i, k) && Gt(k, j));
end
end
end
end
0 Comments
See Also
Categories
Find more on Sparse Matrices 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!