about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/SLP/misc/pdag_to_dag.m
diff options
context:
space:
mode:
authorziejd22017-09-28 15:04:40 -0500
committerziejd22017-09-28 15:04:40 -0500
commit8070dc963753142bb86c4ed698d91fd623ed28e7 (patch)
treed0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/SLP/misc/pdag_to_dag.m
parent7cc31810d53176e805532b2789955f4eedbce6bb (diff)
downloadBNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning.

I am calling this BNW_1.02. It can be accessed at:
compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/bnt-master/SLP/misc/pdag_to_dag.m')
-rw-r--r--sourcecodes/bnt-master/SLP/misc/pdag_to_dag.m87
1 files changed, 87 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/SLP/misc/pdag_to_dag.m b/sourcecodes/bnt-master/SLP/misc/pdag_to_dag.m
new file mode 100644
index 00000000..00a73aac
--- /dev/null
+++ b/sourcecodes/bnt-master/SLP/misc/pdag_to_dag.m
@@ -0,0 +1,87 @@
+function G2 = pdag_to_dag(pdags)
+% (also works with a cell array of pdags, returning a cell array of dags)
+% dag = pdag_to_dag(pdag)
+%
+% cf Dor and Tarsi (1992) :
+%    A simple algorithm to construct a consistent extention of a partially oriented graph.
+%
+% francois.olivier.c.h@gmail.com
+
+
+if ~iscell(pdags)
+    pdag=cell(1,1);
+    pdag{1}=pdags;
+else
+    pdag=pdags;
+end
+
+for da=1:length(pdag)
+    %fprintf('%d ',da)
+    G=pdag{da};
+    G2 = G; A = G;
+    N = size(G,1);
+    empty_loop = 0;
+
+    while ~isempty(find(A))
+        [x x_y_undirected] = select_vertex(A);
+
+        if x==0
+            fprintf('pdag_to_dag error : This pdag does not admit any extension.\n');
+            G2=[];
+            break
+        end
+        G2(x,x_y_undirected) = 0; G2(x_y_undirected,x) = 1;
+
+        A(x,:) = 0;
+        A(:,x) = 0;
+    end
+    dags{da}=G2;
+end
+if ~iscell(pdags)
+    G2=dags{1};
+else
+    G2=dags;
+end
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+function [sol, x_y] = select_vertex(G)
+N = size(G,1);
+sol = 0;
+x = 0;
+fini=0 ;
+while ~fini
+
+    x = x+1;
+    if x>N
+        fini=1;
+    else
+        beforex=find(G(:,x));
+        afterx=find(G(x,:));
+
+        if ~(isempty(beforex)&isempty(afterx))
+            x_y = myintersect(beforex,afterx);
+            beforex=mysetdiff(beforex,x_y);
+            afterx=mysetdiff(afterx,x_y);
+
+            if isempty(afterx) % x is a sink
+                Ax=  myunion(x_y,beforex);
+                for y=x_y
+                    % Adjacents of y
+                    Ay = myunion(find(G(:,y)), find(G(y,:)));
+                    Ay = myunion(Ay,y);
+                    if isempty(setdiff(Ax,Ay))
+                        fini=fini+1; 
+                    else
+                        break;
+                    end
+                end
+                if fini==length(x_y)
+                    sol=x; fini=1;
+                else
+                    fini=0;
+                end
+            end
+        end
+    end
+end % while