To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / _FullBNT / BNT / graph / dag_to_essential_graph.m @ 8:b5b38998ef3b

History | View | Annotate | Download (3.23 KB)

1

    
2
function [eg] = dag_to_essential_graph(dag)
3
cpdag = dag_to_cpdag(dag);
4
eg = dag + dag .* (cpdag + cpdag');
5

    
6
return;
7

    
8

    
9

    
10

    
11
% Coverts a DAG into Essential Graph where edges are coded by 2 and 3, 2 is
12
% directed edge and 3 is bidirected edge and is at one (the same as the original DAG) of the two
13
% symetrical places. 
14

    
15
% Is implemented by the algorithm of Max Chickering in D.M.Chickering (1995). 
16
% A transformational characterization of equivalent Bayesian network structures. 
17
% In Proceedings of Eleventh Conference on Uncertainty in Artificial Intelligence, Montreal, QU,
18
% pages 87-98. Morgan Kaufmann 
19
% http://research.microsoft.com/~dmax/publications/uai95.pdf 
20

    
21
% Implemented by Tomas Kocka, AAU.
22

    
23
function [eg] = dag_to_essential_graph(dagx)
24

    
25
%print_dag(dagx); % Just checking input
26

    
27
order = topological_sort(dagx); % get the topological order of nodes and their number
28

    
29
% fprintf('the topological order is: %d',order);
30
% fprintf('\n');
31

    
32
[nx,ny] = size(dagx); % gets the number of nodes, note that nx == ny
33
[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
34
% we will sort the arcs from lowest possible y and highest possible x, arcs are x->y
35
e = 1;
36
for y = 1:ny
37
    for x = nx:-1:1
38
        %fprintf('x %d ',order(x)); fprintf('y %d ',order(y));
39
        if dagx(order(x),order(y)) == 1 
40
            I(e) = order(x);
41
            J(e) = order(y);
42
            e = e + 1;
43
            %fprintf('x order %d',x);
44
            %fprintf('y order %d',y);
45
            %fprintf('\n');
46
        end
47
    end
48
end
49

    
50

    
51
% fprintf('the arcs are: %d',I);
52
% fprintf('\n');
53
% fprintf('the arcs are: %d',J);
54
% fprintf('\n');
55

    
56

    
57
% Now we have to decide which arcs are part of the essential graph and
58
% which are undirected edges in the essential graph.
59
% Undecided arc in the DAG are 1, directed in EG are 2 and undirected in EG
60
% are 3.
61

    
62

    
63
for e = 1:length(I)
64
    if dagx(I(e),J(e)) == 1
65
        cont = true;
66
        for w = 1:nx 
67
            if dagx(w,I(e)) == 2
68
                if dagx(w,J(e)) ~= 0
69
                    dagx(w,J(e)) = 2;
70
                else
71
                    for ww = 1:nx
72
                        if dagx(ww,J(e)) ~= 0
73
                           dagx(ww,J(e)) = 2;
74
                        end
75
                    end % and now skip the rest and start with another arc from the list
76
                    w = nx;
77
                    cont = false;
78
                end
79
            end
80
        end
81
        if cont
82
           exists = false;
83
           for z = 1:nx
84
               %fprintf('test %d',dagx(z,J(e)));
85
               if dagx(z,J(e)) ~= 0 & z ~= I(e) & dagx(z,I(e)) == 0
86
                  exists = true; 
87
                  for ww = 1:nx
88
                        if dagx(ww,J(e)) == 1
89
                           dagx(ww,J(e)) = 2;
90
                        end 
91
                  end
92
               end
93
           end
94
           if ~ exists
95
               for ww = 1:nx
96
                   if dagx(ww,J(e)) == 1
97
                      dagx(ww,J(e)) = 3;
98
                   end 
99
               end  
100
           end
101
        end
102
    end            
103
end
104

    
105
%print_dag(dagx); % Just checking output
106

    
107

    
108

    
109

    
110

    
111

    
112