wolffd@0: function [d, pre, post, cycle, f, pred] = dfs(adj_mat, start, directed) wolffd@0: % DFS Perform a depth-first search of the graph starting from 'start'. wolffd@0: % [d, pre, post, cycle, f, pred] = dfs(adj_mat, start, directed) wolffd@0: % wolffd@0: % Input: wolffd@0: % adj_mat(i,j)=1 iff i is connected to j. wolffd@0: % start is the root vertex of the dfs tree; if [], all nodes are searched wolffd@0: % directed = 1 if the graph is directed wolffd@0: % wolffd@0: % Output: wolffd@0: % d(i) is the time at which node i is first discovered. wolffd@0: % pre is a list of the nodes in the order in which they are first encountered (opened). wolffd@0: % post is a list of the nodes in the order in which they are last encountered (closed). wolffd@0: % 'cycle' is true iff a (directed) cycle is found. wolffd@0: % f(i) is the time at which node i is finished. wolffd@0: % pred(i) is the predecessor of i in the dfs tree. wolffd@0: % wolffd@0: % If the graph is a tree, preorder is parents before children, wolffd@0: % and postorder is children before parents. wolffd@0: % For a DAG, topological order = reverse(postorder). wolffd@0: % wolffd@0: % See Cormen, Leiserson and Rivest, "An intro. to algorithms" 1994, p478. wolffd@0: wolffd@0: n = length(adj_mat); wolffd@0: wolffd@0: global white gray black color wolffd@0: white = 0; gray = 1; black = 2; wolffd@0: color = white*ones(1,n); wolffd@0: wolffd@0: global time_stamp wolffd@0: time_stamp = 0; wolffd@0: wolffd@0: global d f wolffd@0: d = zeros(1,n); wolffd@0: f = zeros(1,n); wolffd@0: wolffd@0: global pred wolffd@0: pred = zeros(1,n); wolffd@0: wolffd@0: global cycle wolffd@0: cycle = 0; wolffd@0: wolffd@0: global pre post wolffd@0: pre = []; wolffd@0: post = []; wolffd@0: wolffd@0: if ~isempty(start) wolffd@0: dfs_visit(start, adj_mat, directed); wolffd@0: end wolffd@0: for u=1:n wolffd@0: if color(u)==white wolffd@0: dfs_visit(u, adj_mat, directed); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: wolffd@0: %%%%%%%%%% wolffd@0: wolffd@0: function dfs_visit(u, adj_mat, directed) wolffd@0: wolffd@0: global white gray black color time_stamp d f pred cycle pre post wolffd@0: wolffd@0: pre = [pre u]; wolffd@0: color(u) = gray; wolffd@0: time_stamp = time_stamp + 1; wolffd@0: d(u) = time_stamp; wolffd@0: if directed wolffd@0: ns = children(adj_mat, u); wolffd@0: else wolffd@0: ns = neighbors(adj_mat, u); wolffd@0: ns = setdiff(ns, pred(u)); % don't go back to visit the guy who called you! wolffd@0: end wolffd@0: for v=ns(:)' wolffd@0: %fprintf('u=%d, v=%d, color(v)=%d\n', u, v, color(v)) wolffd@0: switch color(v) wolffd@0: case white, % not visited v before (tree edge) wolffd@0: pred(v)=u; wolffd@0: dfs_visit(v, adj_mat, directed); wolffd@0: case gray, % back edge - v has been visited, but is still open wolffd@0: cycle = 1; wolffd@0: %fprintf('cycle: back edge from v=%d to u=%d\n', v, u); wolffd@0: case black, % v has been visited, but is closed wolffd@0: % no-op wolffd@0: end wolffd@0: end wolffd@0: color(u) = black; wolffd@0: post = [post u]; wolffd@0: time_stamp = time_stamp + 1; wolffd@0: f(u) = time_stamp; wolffd@0: wolffd@0: