赞
踩
在贝叶斯网推理中,计算loop-cutset时,会需要找出经过指定顶点的所有环。
这时候先将贝叶斯网络结构转换成隐含的无向图,即保留所有顶点与边,去掉所有边的方向。
然后使用matlab中自带的深度搜索函数,确定深度搜索的序列;基于序列,判断找出经过指定顶点的环。
程序如下:
function [circles, n_circle]=findallcircles(G,startid) %Input:undirected graph G , startid %Output: n = size(G.Nodes,1); A = adjacency(G); strstart = num2str(startid); gid_start = findnode(G,strstart); id_v = dfsearch(G,gid_start); str_v = dfsearch(G,strstart); circles = cell(n); n_circle = 0; for j = 1:n circles{j,1} = strstart; end lencircle = 1; for i = 1:length(id_v)-1 if A(id_v(i),id_v(i+1))==1 circles(n_circle+1,lencircle+1) = str_v(i+1); lencircle = lencircle+1; if A(gid_start,id_v(i+1))==1 && lencircle>2 n_circle = n_circle+1; circles(n_circle+1,:) = circles(n_circle,:); % lencircle = 1; end elseif A(gid_start,id_v(i+1))==1 circles(n_circle+1,:) = []; circles{n_circle+1,1} = strstart; circles(n_circle+1,2) = str_v(i+1); lencircle = 2; else 1; % error('circle finding unexpected error!') end end
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。