How do I calculate Second Neighborhood?
Show older comments
Hello All,
I was wondering how I can calculate the second neighborhood given an adjacency matrix of size "n".
The second neighborhood has to account for any repeated node counts.
1 Comment
Ben Frankel
on 27 Jul 2018
What do you mean by second neighborhood?
Accepted Answer
More Answers (2)
Keviv Agnav
on 27 Jul 2018
1 vote
For each node, I would start with a 1xn column vector initialized with 0s. Then, access row i in the adjacency matrix and find each index j that is valued 1. For each vertex in the first neighborhood, iterate through row j of the adjacency matrix. When you encounter a 1 in row j at index k, change the value at index k of the earlier column vector to a 1. Index k of the column vector should represent whether vertex k is in the second neighborhood, so then just count the number of 1s in the column vector.
Steven Lord
on 28 Jul 2018
1 vote
I'm not familiar with the term "second neighborhood" in the context of graph theory. My guess is that what you're looking for is all nodes that are distance at most two from the node whose neighborhood you're trying to find. If that's the case, build a graph object and call the distances function or the nearest function on it.
Categories
Find more on Matrices and Arrays 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!