-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprim.m
executable file
·68 lines (57 loc) · 2.11 KB
/
prim.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function [E] = prim(Aadj)
%PRIM Prim's algorithm for calculating a minimal spanning tree.
% [E] = PRIM(AADJ)
%
% Implementation of Prim's algorithm for calculating a minimal spanning
% tree from a graph adjacency matrix. This implementation can incorporate
% negative edge weights. Create a maximal spanning tree by specifying
% the negative of the graph adjacency matrix.
%
% Inputs:
% AADJ : A graph adjacency matrix, including the possibility of
% negative edge weights.
%
% Outputs:
% E : Matrix with two columns describing the resulting minimal weight
% spanning tree. Each row gives the maximal cliques for a branch
% of the tree.
% MATPOWER
% Copyright (c) 2013-2019, Power Systems Engineering Research Center (PSERC)
% by Daniel Molzahn, PSERC U of Wisc, Madison
%
% This file is part of MATPOWER/mx-sdp_pf.
% Covered by the 3-clause BSD License (see LICENSE file for details).
% See https://github.com/MATPOWER/mx-sdp_pf/ for more info.
Vnew = 1;
E = [];
nnode = size(Aadj,1);
cols(1).c = sparse(Aadj(Vnew(1),:)~=0);
cols(1).c(Vnew) = 0;
while length(Vnew) < nnode
% Choose an edge (u,v) with minimal weight so that u is in Vnew and
% v is not
% Find the best available edge
bestedge.value = inf;
bestedge.row = [];
bestedge.col = [];
for i=1:length(Vnew)
[tempMaxVal,tempMaxCol] = min(Aadj(Vnew(i),cols(i).c));
if tempMaxVal < bestedge.value
bestedge.value = tempMaxVal;
bestedge.row = Vnew(i);
tempc = find(cols(i).c,tempMaxCol);
bestedge.col = tempc(tempMaxCol);
end
end
Vnew = [Vnew; bestedge.col];
cols(length(Vnew)).c = sparse(Aadj(Vnew(end),:) ~= 0);
cols(length(Vnew)).c(Vnew) = 0;
for i=1:length(Vnew)-1
cols(i).c(Vnew(end)) = 0;
end
E = [E; bestedge.row bestedge.col];
if isempty(bestedge.row) || isempty(bestedge.col)
error('prim: Error in Prim''s Algorithm. System is probably separated into multiple island. Ensure connected system and try again.');
end
end
V = Vnew;