image thumbnail
from NPC algorithm for learning DAG in Bayesian network by Guangdi Li
NPC algorithm is designed for learning Bayesian network formed as DAG in 2001, by Steck

TestPathInGraph(DAG,Start,End)
function Path = TestPathInGraph(DAG,Start,End)
% Input:DAG is the graph, p the start point, q the end point.
% Output:if there is a path from p to q, return Path = 1,otherwise Path = 0;  

% test the cycle by DFS algorithm
Path = 0;
Visited = zeros( 1,size( DAG,1 ) );
[ List,Visited ] = SearchPath( DAG,Start,[],Visited );

    while isempty( List ) == 0
         %Node = List(1)
         Descendant = List(1);
         if Descendant == End 
             Path = 1;
             break;
         end
         List( 1 )=[];
         if Visited( Descendant ) ==0
            [List,Visited] = SearchPath(DAG,Descendant,List,Visited); 
         end
    end
end

function [ List,Visited ] = SearchPath( DAG,Start,L,V )
 V( Start ) = 1;
 Visited=V;
 Descendant = find( DAG(Start,:) == 1 );
 List = [L Descendant];
end

Contact us at files@mathworks.com