-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_First_Second_Neighbors.m
More file actions
32 lines (28 loc) · 993 Bytes
/
get_First_Second_Neighbors.m
File metadata and controls
32 lines (28 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function [N1, N2] = get_First_Second_Neighbors(node, G)
% This function takes a graph and a selector node and outputs its first-
% and second- hop neighbors
%
% N1: direct (1-hop) neighbors of "node" (direct connection)
% N2: nodes reachable in 2 hops via any node in N1, excluding:
% - the node itself
% - all nodes in N1
%
% Outputs are row vectors, unique and sorted.
% 1-hop neighbors
N1 = sort(neighbors(G, node))'; % row, sorted
% If node is isolated, it has no 2-hop neighbors
if isempty(N1)
N2 = [];
return;
end
% Collect neighbors of all 1-hop neighbors
n2_all = [];
for k = 1:numel(N1)
n2_all = [n2_all; neighbors(G, N1(k))];
end
% Keep only true 2-hop nodes (remove self and 1-hop nodes)
n2_all = unique(n2_all(:));
n2_all(n2_all == node) = [];
N2 = setdiff(n2_all, N1(:), 'stable');
N2 = sort(N2(:))'; % row, sorted (in ascending order)
end