annotate toolboxes/FullBNT-1.0.7/graph/dfs.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [d, pre, post, cycle, f, pred] = dfs(adj_mat, start, directed)
Daniel@0 2 % DFS Perform a depth-first search of the graph starting from 'start'.
Daniel@0 3 % [d, pre, post, cycle, f, pred] = dfs(adj_mat, start, directed)
Daniel@0 4 %
Daniel@0 5 % Input:
Daniel@0 6 % adj_mat(i,j)=1 iff i is connected to j.
Daniel@0 7 % start is the root vertex of the dfs tree; if [], all nodes are searched
Daniel@0 8 % directed = 1 if the graph is directed
Daniel@0 9 %
Daniel@0 10 % Output:
Daniel@0 11 % d(i) is the time at which node i is first discovered.
Daniel@0 12 % pre is a list of the nodes in the order in which they are first encountered (opened).
Daniel@0 13 % post is a list of the nodes in the order in which they are last encountered (closed).
Daniel@0 14 % 'cycle' is true iff a (directed) cycle is found.
Daniel@0 15 % f(i) is the time at which node i is finished.
Daniel@0 16 % pred(i) is the predecessor of i in the dfs tree.
Daniel@0 17 %
Daniel@0 18 % If the graph is a tree, preorder is parents before children,
Daniel@0 19 % and postorder is children before parents.
Daniel@0 20 % For a DAG, topological order = reverse(postorder).
Daniel@0 21 %
Daniel@0 22 % See Cormen, Leiserson and Rivest, "An intro. to algorithms" 1994, p478.
Daniel@0 23
Daniel@0 24 n = length(adj_mat);
Daniel@0 25
Daniel@0 26 global white gray black color
Daniel@0 27 white = 0; gray = 1; black = 2;
Daniel@0 28 color = white*ones(1,n);
Daniel@0 29
Daniel@0 30 global time_stamp
Daniel@0 31 time_stamp = 0;
Daniel@0 32
Daniel@0 33 global d f
Daniel@0 34 d = zeros(1,n);
Daniel@0 35 f = zeros(1,n);
Daniel@0 36
Daniel@0 37 global pred
Daniel@0 38 pred = zeros(1,n);
Daniel@0 39
Daniel@0 40 global cycle
Daniel@0 41 cycle = 0;
Daniel@0 42
Daniel@0 43 global pre post
Daniel@0 44 pre = [];
Daniel@0 45 post = [];
Daniel@0 46
Daniel@0 47 if ~isempty(start)
Daniel@0 48 dfs_visit(start, adj_mat, directed);
Daniel@0 49 end
Daniel@0 50 for u=1:n
Daniel@0 51 if color(u)==white
Daniel@0 52 dfs_visit(u, adj_mat, directed);
Daniel@0 53 end
Daniel@0 54 end
Daniel@0 55
Daniel@0 56
Daniel@0 57 %%%%%%%%%%
Daniel@0 58
Daniel@0 59 function dfs_visit(u, adj_mat, directed)
Daniel@0 60
Daniel@0 61 global white gray black color time_stamp d f pred cycle pre post
Daniel@0 62
Daniel@0 63 pre = [pre u];
Daniel@0 64 color(u) = gray;
Daniel@0 65 time_stamp = time_stamp + 1;
Daniel@0 66 d(u) = time_stamp;
Daniel@0 67 if directed
Daniel@0 68 ns = children(adj_mat, u);
Daniel@0 69 else
Daniel@0 70 ns = neighbors(adj_mat, u);
Daniel@0 71 ns = setdiff(ns, pred(u)); % don't go back to visit the guy who called you!
Daniel@0 72 end
Daniel@0 73 for v=ns(:)'
Daniel@0 74 %fprintf('u=%d, v=%d, color(v)=%d\n', u, v, color(v))
Daniel@0 75 switch color(v)
Daniel@0 76 case white, % not visited v before (tree edge)
Daniel@0 77 pred(v)=u;
Daniel@0 78 dfs_visit(v, adj_mat, directed);
Daniel@0 79 case gray, % back edge - v has been visited, but is still open
Daniel@0 80 cycle = 1;
Daniel@0 81 %fprintf('cycle: back edge from v=%d to u=%d\n', v, u);
Daniel@0 82 case black, % v has been visited, but is closed
Daniel@0 83 % no-op
Daniel@0 84 end
Daniel@0 85 end
Daniel@0 86 color(u) = black;
Daniel@0 87 post = [post u];
Daniel@0 88 time_stamp = time_stamp + 1;
Daniel@0 89 f(u) = time_stamp;
Daniel@0 90
Daniel@0 91