annotate toolboxes/FullBNT-1.0.7/graph/acyclic.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 b = acyclic(adj_mat, directed)
Daniel@0 2 % ACYCLIC Returns true iff the graph has no (directed) cycles.
Daniel@0 3 % b = acyclic(adj_mat, directed)
Daniel@0 4
Daniel@0 5 adj_mat = double(adj_mat);
Daniel@0 6 if nargin < 2, directed = 1; end
Daniel@0 7
Daniel@0 8 % e.g., G =
Daniel@0 9 % 1 -> 3
Daniel@0 10 % |
Daniel@0 11 % v
Daniel@0 12 % 2 <- 4
Daniel@0 13 % In this case, 1->2 in the transitive closure, but 1 cannot get to itself.
Daniel@0 14 % If G was undirected, 1 could get to itself, but this graph is not cyclic.
Daniel@0 15 % So we cannot use the closure test in the undirected case.
Daniel@0 16
Daniel@0 17 if directed
Daniel@0 18 R = reachability_graph(adj_mat);
Daniel@0 19 b = ~any(diag(R)==1);
Daniel@0 20 else
Daniel@0 21 [d, pre, post, cycle] = dfs(adj_mat,[],directed);
Daniel@0 22 b = ~cycle;
Daniel@0 23 end