-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducer.m
80 lines (69 loc) · 2.52 KB
/
reducer.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
69
70
71
72
73
74
75
76
77
78
79
80
function out = reducer( G, is_ext_node, options )
% REDUCER Performs analysis and reduction of G network.
% Input:
% G - conductance matrix
% IsExtNode - mask vector
% Output:
% out.options - options used to this run
% out.conn_components_count - total count of connected components in G
% out.c - cells of reduction results for each connected component
assert(length(is_ext_node) == size(G, 1));
if nargin < 3
options = struct;
end
options = build_reducer_options(options);
if options.verbose
options %#ok<NOPRT>
end
try
load(options.output_file, 'out')
catch
out = struct;
out.options = options;
% 1. Compute connected components Gi of G
out.G = G;
out.is_ext_node = G;
out.A = adj(G);
out.components = components(out.A);
end
unique_connected_components = unique(out.components);
connected_components_count = length(unique_connected_components);
out.conn_components_count = connected_components_count;
out.c = cell(connected_components_count, 1);
out.options = options;
% find not yet processed components
components_to_process = find(cellfun (@(c) isempty(c), cell(connected_components_count, 1)));
for conn_comp_i = 1:length(components_to_process)
conn_comp_id = components_to_process(conn_comp_i);
conn_comp_sel = (out.components == conn_comp_id);
if options.verbose
fprintf('Component %d/%d (%d nodes)\n', conn_comp_id, connected_components_count, nnz(conn_comp_sel));
end
conn_comp_A = out.A(conn_comp_sel, conn_comp_sel);
conn_comp_G = G(conn_comp_sel, conn_comp_sel);
conn_comp_is_ext_node = is_ext_node(conn_comp_sel);
global_context = find(conn_comp_sel);
if ~any(conn_comp_is_ext_node)
d.G = sparse(0, 0);
d.is_ext_node = [];
d.new_nodes = [];
d.graph_processing_time = 0;
d.nodewise_processing_time = 0;
out.c{conn_comp_id} = d;
continue
end
graph_timer = tic;
after_graph = options.graph_algorithm(conn_comp_G,conn_comp_is_ext_node, conn_comp_A);
graph_processing_time = toc(graph_timer);
% Eliminate nodes one-by-one
nodewise_timer = tic;
d = options.nodewise_algorithm(after_graph.G, after_graph.is_ext_node, options);
nodewise_processing_time = toc(nodewise_timer);
d.new_nodes = global_context(after_graph.new_nodes(d.new_nodes));
d.graph_processing_time = graph_processing_time;
d.nodewise_processing_time = nodewise_processing_time;
out.c{conn_comp_id} = d;
if (options.auto_save)
save(options.output_file, 'out');
end
end