Graph how to get every connected nodes from this specific graph

Hi, I have graph that is described in this way
U = {[1 3],[1 4],[1 5],[5 2]};
and it looks like this
How can i get two arrays, s array would show first node and t array would show accordingly the next node connected to it.
Example below.
s[1 1 1 2 3 4 5 5]
t[3 4 5 5 1 1 1 2]
Thanks in advance.

 采纳的回答

Stephan
Stephan 2018-12-3
编辑:Stephan 2018-12-3
Hi,
you find s and t by using:
U = {[1 3],[1 4],[1 5],[5 2]};
A = cat(1,U{:});
s = A(:,1);
t = A(:,2);
Best regards
Stephan

5 个评论

prints out
s =
1
1
1
5
t =
3
4
5
2
i needed it to be
s[1 1 1 2 3 4 5 5]
t[3 4 5 5 1 1 1 2]
The resulting graph built from my answer is:
graph_orig.PNG
which corresponds to the graph in your question. If you want the result like you showed in your comment use:
s = [A(:,1); A(:,2)]'
t = [A(:,2); A(:,1)]'
>> s
s =
1 1 1 5 3 4 5 2
>> t
t =
3 4 5 2 1 1 1 5
Note that this corresponds to such a graph:
This is really what you want?
Yup, i needed those arrays, but for some reason i cant use
G = graph(s,t);
my code:
A = cat(1,U{:});
s = [A(:,1); A(:,2)];
t = [A(:,2); A(:,1)];
disp(s);
disp(t);
G = graph(s,t);
d = distances(G)
error:
Error using matlab.internal.graph.MLGraph
Duplicate edges not supported.
Error in matlab.internal.graph.constructFromEdgeList (line 125)
G = underlyingCtor(double(s), double(t), totalNodes);
Error in graph (line 264)
matlab.internal.graph.constructFromEdgeList(...
Error in TrumpKel (line 43)
G = graph(s,t);
maybe you know why there is error?
See my edited comment - use:
U = {[1 3],[1 4],[1 5],[5 2]};
A = cat(1,U{:});
s = A(:,1);
t = A(:,2);
G = graph(s,t);
d = distances(G)
d =
0 2 1 1 1
2 0 3 3 1
1 3 0 2 2
1 3 2 0 2
1 1 2 2 0

请先登录,再进行评论。

更多回答(2 个)

Well, that's a bit silly. Your desired output is more or less the same as your input, just with duplicated information.
U = {[1 3],[1 4],[1 5],[5 2]}; %why is it in a cell array?
edgetable = vertcat(U{:}); %make a matrix out ot it
s = [edgetable(:, 1); edgetable(:, 2)]'
t = [edgetable(:, 2); edgetable(:, 1)]'
Note that since R2015b, matlab has functions dedicated to graphs. For example to visualise the above graph:
g = graph(edgetable(:, 1), edgetable(:, 2));
plot(g)

2 个评论

S is great but t shows up weird
s =
1
1
1
5
3
4
5
2
t =
3
4
5
2
1
1
1
5
1
1
1
5
3
4
5
2
Well, I don't know what you've done. You certainly didn't just copy paste the code I wrote:
>> U = {[1 3],[1 4],[1 5],[5 2]}; %why is it in a cell array?
edgetable = vertcat(U{:}); %make a matrix out ot it
s = [edgetable(:, 1); edgetable(:, 2)]'
t = [edgetable(:, 2); edgetable(:, 1)]'
s =
1 1 1 5 3 4 5 2
t =
3 4 5 2 1 1 1 5

请先登录,再进行评论。

The s and t vectors in the original question double-count each edge. Since what your picture shows is an undirected graph (created by the MATLAB function graph), the edge (1, 3) is the same as the edge (3, 1). If you need those two edges to be different, use a directed graph (the digraph function.) See this documentation page for an illustration of the difference. There is an edge from A to B in both the graph and digraph shown in the first two pictures on that page, but there is an edge from B to A only in the undirected graph.
If you want a list of edges, you can either get the Edges table from the graph object or you can use findedge.
U = [[1 3];[1 4];[1 5];[5 2]];
G = graph(U(:, 1), U(:, 2));
plot(G)
E = G.Edges
[S, T] = findedge(G)
But I'm guessing you're not getting the edges just to get the edges. If you tell us what analysis or manipulation you want to perform on this graph we may be able to tell you what functions available for working on graphs may be of use to you.

类别

在 帮助中心 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by