annotate toolboxes/FullBNT-1.0.7/graph/dfs_test.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 % Do the example in fig 23.4 p479 of Cormen, Leiserson and Rivest (1994)
Daniel@0 2
Daniel@0 3 u = 1; v = 2; w = 3; x = 4; y = 5; z = 6;
Daniel@0 4 n = 6;
Daniel@0 5 dag=zeros(n,n);
Daniel@0 6 dag(u,[v x])=1;
Daniel@0 7 dag(v,y)=1;
Daniel@0 8 dag(w,[y z])=1;
Daniel@0 9 dag(x,v)=1;
Daniel@0 10 dag(y,x)=1;
Daniel@0 11 dag(z,z)=1;
Daniel@0 12
Daniel@0 13 [d, pre, post, cycle, f, pred] = dfs(dag, [], 1);
Daniel@0 14 assert(isequal(d, [1 2 9 4 3 10]))
Daniel@0 15 assert(isequal(f, [8 7 12 5 6 11])
Daniel@0 16 assert(cycle)
Daniel@0 17
Daniel@0 18 % Now give it an undirected cyclic graph
Daniel@0 19 G = mk_2D_lattice(2,2,0);
Daniel@0 20 % 1 - 3
Daniel@0 21 % | |
Daniel@0 22 % 2 - 4
Daniel@0 23 [d, pre, post, cycle, f, pred] = dfs(G, [], 0);
Daniel@0 24 % d = [1 2 4 3]
Daniel@0 25 assert(cycle)
Daniel@0 26
Daniel@0 27 % Now break the cycle
Daniel@0 28 G(1,2)=0; G(2,1)=0;
Daniel@0 29 [d, pre, post, cycle, f, pred] = dfs(G, [], 0);
Daniel@0 30 assert(~cycle)