Creating a function that outputs adjacency matrix

3 ビュー (過去 30 日間)
Emily Newton
Emily Newton 2022 年 6 月 6 日
回答済み: Ayush 2024 年 12 月 2 日
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 件のコメント
Matt J
Matt J 2022 年 6 月 6 日
Much easier:
function tf=issqbinary(x)
tf=isequal(x,logical(x)');
end

サインインしてコメントする。

回答 (1 件)

Ayush
Ayush 2024 年 12 月 2 日
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

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by