-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg_schedule.m
68 lines (66 loc) · 1.63 KB
/
msg_schedule.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 queue = msg_schedule(root, ch)
% function queue = msg_schedule(root, ch)
%
% This function creates the message schedule starting from the root and
% propagating down to the leaves.
%
% INPUT
% N: 1x1 scalar - Number of nodes (variables)
% root: 1x1 scalar - Root
% ch: Nx1 cell array - Children
% Each element lists the children of each node
% based on the chosen root.
%
%
% OUTPUT
% queue: (N-1)x2 matrix - Message queue
% Each row has two elements. The first element
% denotes the origin node and the second the
% target node.
%
%
% EXAMPLES
%
% E.g. assume the simple graphical model:
%
% 1
% / \
% 2 3
% \
% 4
%
% with root:
% root = 1;
%
% The ch cell array will be:
% ch{1} = [2 3]; ch{2} = zeros(1,0); ch{3} = [4]; ch{4} = zeros(1,0);
%
% queue = msg_schedule(root, ch);
%
% This will produce output:
% queue = [3 1;
% 2 1;
% 4 3]
%
% That is, we start from the messages that originate from the root (1) and
% propagate downwards.
%
% Author: geopapa
for i = 1:length(ch)
ch{i} = ch{i}(:)';
end
N = length(ch);
i = root;
count = 1;
stack = [];
queue = zeros(N-1,2);
while count <= N-1
for k = length(ch{i}):-1:1
queue(count, :) = [ch{i}(k), i];
count = count + 1;
end
stack = [stack, ch{i}];
i = stack(end);
stack(end) = [];
end
end