-
Notifications
You must be signed in to change notification settings - Fork 249
/
dag_to_essential_graph.m
112 lines (85 loc) · 3.12 KB
/
dag_to_essential_graph.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function [eg] = dag_to_essential_graph(dag)
cpdag = dag_to_cpdag(dag);
eg = dag + dag .* (cpdag + cpdag');
return;
% Coverts a DAG into Essential Graph where edges are coded by 2 and 3, 2 is
% directed edge and 3 is bidirected edge and is at one (the same as the original DAG) of the two
% symetrical places.
% Is implemented by the algorithm of Max Chickering in D.M.Chickering (1995).
% A transformational characterization of equivalent Bayesian network structures.
% In Proceedings of Eleventh Conference on Uncertainty in Artificial Intelligence, Montreal, QU,
% pages 87-98. Morgan Kaufmann
% http://research.microsoft.com/~dmax/publications/uai95.pdf
% Implemented by Tomas Kocka, AAU.
function [eg] = dag_to_essential_graph(dagx)
%print_dag(dagx); % Just checking input
order = topological_sort(dagx); % get the topological order of nodes and their number
% fprintf('the topological order is: %d',order);
% fprintf('\n');
[nx,ny] = size(dagx); % gets the number of nodes, note that nx == ny
[I,J] = find(dagx); % finds all nonzero elements in the adjacency matrix, i.e. arcs in the DAG - however we will overwrite it in a special order
% we will sort the arcs from lowest possible y and highest possible x, arcs are x->y
e = 1;
for y = 1:ny
for x = nx:-1:1
%fprintf('x %d ',order(x)); fprintf('y %d ',order(y));
if dagx(order(x),order(y)) == 1
I(e) = order(x);
J(e) = order(y);
e = e + 1;
%fprintf('x order %d',x);
%fprintf('y order %d',y);
%fprintf('\n');
end
end
end
% fprintf('the arcs are: %d',I);
% fprintf('\n');
% fprintf('the arcs are: %d',J);
% fprintf('\n');
% Now we have to decide which arcs are part of the essential graph and
% which are undirected edges in the essential graph.
% Undecided arc in the DAG are 1, directed in EG are 2 and undirected in EG
% are 3.
for e = 1:length(I)
if dagx(I(e),J(e)) == 1
cont = true;
for w = 1:nx
if dagx(w,I(e)) == 2
if dagx(w,J(e)) ~= 0
dagx(w,J(e)) = 2;
else
for ww = 1:nx
if dagx(ww,J(e)) ~= 0
dagx(ww,J(e)) = 2;
end
end % and now skip the rest and start with another arc from the list
w = nx;
cont = false;
end
end
end
if cont
exists = false;
for z = 1:nx
%fprintf('test %d',dagx(z,J(e)));
if dagx(z,J(e)) ~= 0 & z ~= I(e) & dagx(z,I(e)) == 0
exists = true;
for ww = 1:nx
if dagx(ww,J(e)) == 1
dagx(ww,J(e)) = 2;
end
end
end
end
if ~ exists
for ww = 1:nx
if dagx(ww,J(e)) == 1
dagx(ww,J(e)) = 3;
end
end
end
end
end
end
%print_dag(dagx); % Just checking output