annotate toolboxes/FullBNT-1.0.7/graph/dfs_test.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 % Do the example in fig 23.4 p479 of Cormen, Leiserson and Rivest (1994)
wolffd@0 2
wolffd@0 3 u = 1; v = 2; w = 3; x = 4; y = 5; z = 6;
wolffd@0 4 n = 6;
wolffd@0 5 dag=zeros(n,n);
wolffd@0 6 dag(u,[v x])=1;
wolffd@0 7 dag(v,y)=1;
wolffd@0 8 dag(w,[y z])=1;
wolffd@0 9 dag(x,v)=1;
wolffd@0 10 dag(y,x)=1;
wolffd@0 11 dag(z,z)=1;
wolffd@0 12
wolffd@0 13 [d, pre, post, cycle, f, pred] = dfs(dag, [], 1);
wolffd@0 14 assert(isequal(d, [1 2 9 4 3 10]))
wolffd@0 15 assert(isequal(f, [8 7 12 5 6 11])
wolffd@0 16 assert(cycle)
wolffd@0 17
wolffd@0 18 % Now give it an undirected cyclic graph
wolffd@0 19 G = mk_2D_lattice(2,2,0);
wolffd@0 20 % 1 - 3
wolffd@0 21 % | |
wolffd@0 22 % 2 - 4
wolffd@0 23 [d, pre, post, cycle, f, pred] = dfs(G, [], 0);
wolffd@0 24 % d = [1 2 4 3]
wolffd@0 25 assert(cycle)
wolffd@0 26
wolffd@0 27 % Now break the cycle
wolffd@0 28 G(1,2)=0; G(2,1)=0;
wolffd@0 29 [d, pre, post, cycle, f, pred] = dfs(G, [], 0);
wolffd@0 30 assert(~cycle)