matlab读图生邻接矩阵
时间: 2023-10-18 22:27:55 浏览: 148
在Matlab中,可以使用输入参数size和edges来构建图的邻接矩阵。其中,size表示图中点的个数,edges是一个n*2的矩阵,每一行代表图中的一条边。例如,如果点1和点2有一条边,那么edges的一行是(1,2)。
要生成邻接矩阵,可以使用combntns函数从给定的集合A中选取指定数量的元素。在这个问题里,我们使用combntns(A,size)来选取size个元素。然后,我们可以根据选取的边来构造图的邻接矩阵。
具体的代码如下:
```matlab
function adjacencyMatrix = readGraph(size, edges)
number = length(edges);
testEdges = zeros(size-1,2);
test = combntns(1:number,size-1);
su = 1;
adjacencyMatrix = [];
for i=1:length(test)
for j = 1:size-1
testEdges(j,1) = edges(test(i,j),1);
testEdges(j,2) = edges(test(i,j),2);
end
graph = zeros(size,size);
for k = 1:size-1
graph(testEdges(k,1),testEdges(k,2)) = 1;
graph(testEdges(k,2),testEdges(k,1)) = 1;
end
current = zeros(1,size);
current(1) = 1;
mark = zeros(1,size);
mark(1) = 1;
while sum(current) > 0
next = zeros(1,size);
for j = 1:size
if current(j)>0
for k = 1:size
if k ~= j && graph(j,k) > 0 && mark(k) == 0
mark(k) = 1;
next(k) = 1;
end
end
end
end
current = next;
end
if sum(mark) == size
adjacencyMatrix(:,:,su) = graph;
su = su + 1;
end
end
end
```
这段代码会返回一个邻接矩阵adjacencyMatrix,其中每个矩阵都代表一个生成树。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [找到图中所有生成树的matlab实现](https://blog.csdn.net/cuiods/article/details/52502441)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文