Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

reordernodes

对图节点重新排序

说明

示例

H = reordernodes(G,order) 根据 order 对图 G 中的节点重新排序。此重新排序对应于 G 的邻接矩阵的对称置换。

示例

[H,idx] = reordernodes(G,order) 还会返回边索引 idx 的置换向量。例如,如果 G.Edges 具有变量 Weight,则 H.Edges.Weight == G.Edges.Weight(idx)

示例

全部折叠

创建并绘制一个图。

s = [1 1 1 2 5 3 6 4 7 8 8 8];
t = [2 3 4 5 3 6 4 7 2 6 7 5];
G = graph(s,t);
plot(G)

Figure contains an axes object. The axes object contains an object of type graphplot.

使用置换向量对图节点重新排列。

order = [7 2 3 4 8 1 5 6];
G = reordernodes(G,order);
plot(G)

Figure contains an axes object. The axes object contains an object of type graphplot.

创建并绘制一个加权图。

s = [1 1 1 2 2 2 2 3 4];
t = [3 4 2 3 4 5 6 5 6];
weights = [6 7 6 3 2 8 7 1 1]; 
G = digraph(s,t,weights);
plot(G,'EdgeLabel',G.Edges.Weight)

Figure contains an axes object. The axes object contains an object of type graphplot.

根据出度对图节点重新排列,例如节点 1 具有最大出度。

[~,order] = sort(outdegree(G),'descend')
order = 6×1

     2
     1
     3
     4
     5
     6

[H,idx] = reordernodes(G,order);
plot(H,'EdgeLabel',H.Edges.Weight)

Figure contains an axes object. The axes object contains an object of type graphplot.

idx 值描述 G.Edges 中行的置换。使用 Weight 变量确认此对应关系。

isequal(H.Edges.Weight, G.Edges.Weight(idx))
ans = logical
   1

输入参数

全部折叠

输入图,指定为 graphdigraph 对象。可使用 graph 创建一个无向图,或使用 digraph 创建一个有向图。

示例: G = graph(1,2)

示例: G = digraph([1 2],[2 3])

节点顺序,指定为节点索引或节点名称。order 指定 G 的邻接矩阵的对称置换。如果 A = adjacency(G),则 A(order,order) 生成 adjacency(H)

order 可以是以下项之一:

  • 节点索引的数值向量,例如 [1 3 2]

  • 节点名称的字符向量元胞数组或字符串数组,例如 {'A' 'C' 'B'}["A" "C" "B"]

示例: H = reordernodes(G,[3 1 2])

输出参数

全部折叠

输出图,以 graphdigraph 对象形式返回。H 包含的 NodesEdges 属性与 G 相同,但具有置换后的行 H.NodesH.Edges

  • H.NodesG.Nodes(order,:) 相同。

  • H.Edges 类似于 G.Edges(idx,:),不同的是节点经过重新编号。

边索引的置换向量,以向量形式返回。idx 中的值描述 G.Edges 中行的置换。

扩展功能

基于线程的环境
使用 MATLAB® backgroundPool 在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool 加快代码运行速度。

版本历史记录

在 R2015b 中推出