使用Matlab在网络内广播信息

时间:2018-10-16 03:25:05

标签: matlab graph-theory

我有一个复杂的网络(图),我想随机选择一个节点,并让该节点根据网络的平均距离通过网络广播信息。可以帮我在matlab中实现这个吗。...我不知道该如何实现。 可以从http://www-personal.umich.edu/~mejn/netdata/下载图形数据(电网)集。 下面是我用于读取和绘制图形的代码。

%Extracting edges from gml file graph
fileName = 'power.gml';
inputfile = fopen(fileName);
Edges=[];
l=0;
k=1;
while 1
      % Get a line from the input file
      tline = fgetl(inputfile);
      % Quit if end of file
      if ~ischar(tline)
          break
      end
      nums = regexp(tline,'\d+','match');
      if length(nums)
          if l==1
              l=3;
              Edges(k,2)=str2num(nums{1});  
              k=k+1;
              continue;
          end
          Edges(k,1)=str2num(nums{1});
          l=1;
      else
          l=0;
          continue;
      end 
end



Edges = Edges+1;
NewEdge = Edges;
%Edges(:,1:2) = sort(Edges(:,1:2)); 
Edges = unique(Edges(:,1:2),'rows','stable');
G = graph(Edges(:,1), Edges(:,2));      % create a graph from A
deg = degree(G);
figure                                  % visualize the graph
plot(G);
title('Power Grid, Before Movement')
NewEdge = NewEdge(randperm(size(NewEdge,1)),:);  % <-- Optional --

nodesize = numnodes(G);
size = nodesize;
not = [];
x = [];
for c = 1:size
[P,d] = shortestpath(G,c,nodesize);
if d == Inf
    not = [not, d];
else
x = [x, d];
end
end

LongestShortestPath = max(x)
disp('Before Change');
AverageDistance = mean(x)
AverageDegree = mean(deg)

0 个答案:

没有答案