Adjacency matrix of a network to Distance matrix (Two -Hop)

22 ビュー (過去 30 日間)
Christy Jackson
Christy Jackson 2016 年 3 月 17 日
コメント済み: Muhammad fawad 2021 年 9 月 9 日
I have the following code for generating an adjacency matrix for a network. How do I go about creating a distance matrix (Probably a two hop matrix) from this output.
function adj = AdjMatrixLattice4( N, M )
% Size of adjacency matrix
MN = M*N;
adj = zeros(MN,MN);
for i=1:N
for j=1:N
A = M*(i-1)+j; %Node # for (i,j) node
if(j<N)
B = M*(i-1)+j+1; %Node # for node to the right
C = M*(i-1)+j+2;
D = M*(i-1)+j+2;
adj(A,B) = 1;
adj(B,A) = 1;
adj(A,C) = 1;
adj(C,A) = 1;
adj(A,D) = 1;
adj(D,A) = 1;
end
if(i<M)
B = M*i+j;
C = M*i+j+1; %Node # for node below
D = M*i+j;
adj(A,B) = 1;
adj(B,A) = 1;
adj(A,C) = 1;
adj(C,A) = 1;
adj(A,D) = 1;
adj(D,A) = 1;
end
end
end
end
ans =
0 1 1 1 1 0 0 0 0 0
1 0 1 1 1 1 0 0 0 0
1 1 0 0 0 1 1 0 0 0
1 1 0 0 1 1 1 1 0 0
1 1 0 1 0 1 1 1 1 0
0 1 1 1 1 0 0 0 1 1
0 0 1 1 1 0 0 1 1 0
0 0 0 1 1 0 1 0 1 1
0 0 0 0 1 1 1 1 0 0
0 0 0 0 0 1 0 1 0 0
How do i convert this to a two hop distance matrix.?
Can someone help?
  5 件のコメント
Christy Jackson
Christy Jackson 2016 年 3 月 19 日
Thank you very much for your support. Appreciated. I have a certain condition in the network which allows the diagonals only to be zero. Thanks again, i ll come back with few more questions to you though.!
Ced
Ced 2016 年 3 月 19 日
I see, thanks! I copied one of the solutions to the answer section so you can close the topic. Cheers

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

採用された回答

Ced
Ced 2016 年 3 月 19 日
Possible solution, see comment section for details:
A = [ 0 1 1 0 0 0 0 0
1 0 1 0 0 0 0 0
1 1 0 1 0 0 0 0
0 0 1 0 1 1 0 0
0 0 0 1 0 0 1 0
0 0 0 1 0 0 1 0
0 0 0 0 1 1 0 1
0 0 0 0 0 0 1 0 ];
MN = size(A,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Looking at each node once
A = logical(A);
TwoHopMat_2 = zeros(MN,MN);
for i = 1:MN
% This is everyone Node i can reach in a single hop
single_hops = A(i,:);
% now combine it with every node it can reach in two hops
TwoHopMat_2(i,:) = any([ single_hops ; A(single_hops,:) ],1);
end
% we want the diagonal to be false
TwoHopMat_2 = xor(TwoHopMat_2,diag(true(MN,1)));
  1 件のコメント
Muhammad fawad
Muhammad fawad 2021 年 9 月 9 日
How we find 3 and 4 hops. please help

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

その他の回答 (1 件)

Christine Tobler
Christine Tobler 2016 年 4 月 25 日
An easier way to compute the two-hop matrix is through matrix multiplication, I think.
The adjacency matrix A is the one-hop matrix. The matrix A2 = A*A has a non-zero in A(i, j), if it is possible to go from node i to node j in exactly two steps. Use A + A*A to get non-zeros in A(i, j) if you can go from node i to node j in 2 or less steps.
  2 件のコメント
Rogier Noldus
Rogier Noldus 2021 年 8 月 19 日
When we use A + A*A, then (A + A*A)_ij is the mathematical sum of A_ij and A*A_ij. Shouldn't we rather use A OR A*A? (A OR A*A)_ij will have a value 1 in position (i,j) when node j can be reached from node i in 1 hop or in 2 hops.
Muhammad fawad
Muhammad fawad 2021 年 9 月 9 日
how to find 4 or less than 4 hops using this method

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

カテゴリ

Help Center および File ExchangeInertias and Loads についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by