-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindwcmp.m
More file actions
34 lines (32 loc) · 938 Bytes
/
Copy pathfindwcmp.m
File metadata and controls
34 lines (32 loc) · 938 Bytes
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
function [p, r] = findwcmp(A)
[m, n] = size(A);
N = m * n;
K = reshape(1 : N, m, n);
East = [(K(:, 2 : n) .* (A(:, 1 : n - 1) & A (:, 2 : n))), zeros(m,1)];
E = find(East);
South = [(K(2 : m, :) .* (A(1 : m - 1, :) & A (2 : m, :))); zeros(1, n)];
S = find(South);
G = sparse([K(E); K(S)], [East(E); South(S)], 1, N, N);
G = G + G' + speye(N);
[p, ~, r, ~] = dmperm(G);
nc = length(r) - 1;
[~, i] = sortrows([diff(r)', double(A(p(r(1:end-1)))')], [-1, -2]);
p2 = zeros(1, N);
r2 = zeros(1, nc + 1);
k2 = 0;
k = 1;
while true
c = i(k);
if ~A(p(r(c)))
break;
end
nodes = p(r(c) : r(c + 1) - 1);
csize = length(nodes);
p2(k2 + 1 : k2 + csize) = nodes;
r2(k) = k2 + 1;
k2 = k2 + csize;
k = k + 1;
end
p = p2(logical(p2));
r2(k + 1) = length(p) + 1;
r = r2(logical(r2));