how to do forward neighbor discovery in un-directed graph?

1 visualización (últimos 30 días)
Farah Mahmood
Farah Mahmood el 14 de Feb. de 2020
Comentada: Christine Tobler el 17 de Feb. de 2020
I have an undirected graph G with 11 nodes and 15 edges. There is a source and destination node and i find neighbors of source node using
n1=neighbors(G,'Source');
next i want to find neighbors of n1 excluding 'Source'. Want to repeat it for neighbors of n1 as well. I only want to select the forward nodes and not the previous ones.

Respuestas (1)

Christine Tobler
Christine Tobler el 14 de Feb. de 2020
If I understand correctly, you want to find all nodes that are direct neighbors of n1, then all nodes that connect to n1 through one other node, but aren't n1 or a direct neighbor of n1. In other words, you need all nodes at distance 1 from n1, at distance 2 from n1, and so on.
Simplest is to compute the distances of every node from n1, and to use that vector:
d = distances(G, 'Source', 'all', 'Method', 'unweighted');
directNeighbors = G.Nodes.Name(d == 1); % this is equivalent to neighbors(G, 'Source')
neighborsDistance2 = G.Nodes.Name(d == 2);
neighborsDistance3 = G.Nodes.Name(d == 3);
Note: If G does not have any edge weights (G.Edges has no variable named 'Weight'), you can also use
d = distances(G, 'Source', 'all');
as the 'Method', 'unweighted' option will have no effect in that case.
  2 comentarios
Farah Mahmood
Farah Mahmood el 14 de Feb. de 2020
I was looking for something like successors that is used for directed graphs.
Let me rephrase the problem, there is a source node and i use neighbors to find its direct neighbors. then i choose one of those neighbors (lets say n1) and find its direct neighbors other than the source node. I only want to see/display successors of n1.
Christine Tobler
Christine Tobler el 17 de Feb. de 2020
You can use setdiff(successors(G, n1), {'Source'}), which will remove the original source node from the list of successors of n1.

Iniciar sesión para comentar.

Categorías

Más información sobre Directed Graphs en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by