To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / _FullBNT / BNT / graph / dfs_test.m @ 8:b5b38998ef3b

History | View | Annotate | Download (660 Bytes)

1
% Do the example in fig 23.4 p479 of Cormen, Leiserson and Rivest (1994)
2

    
3
u = 1; v = 2; w = 3; x = 4; y = 5; z = 6;
4
n = 6;
5
dag=zeros(n,n);
6
dag(u,[v x])=1;
7
dag(v,y)=1;
8
dag(w,[y z])=1;
9
dag(x,v)=1;
10
dag(y,x)=1;
11
dag(z,z)=1;
12

    
13
[d, pre, post, cycle, f, pred] = dfs(dag, [], 1);
14
assert(isequal(d, [1 2 9 4 3 10]))
15
assert(isequal(f, [8 7 12 5 6 11])
16
assert(cycle)
17

    
18
% Now give it an undirected cyclic graph
19
G = mk_2D_lattice(2,2,0);
20
% 1 - 3
21
% |   |
22
% 2 - 4
23
[d, pre, post, cycle, f, pred] = dfs(G, [], 0);
24
% d = [1 2 4 3]
25
assert(cycle)
26

    
27
% Now break the cycle
28
G(1,2)=0; G(2,1)=0;
29
[d, pre, post, cycle, f, pred] = dfs(G, [], 0);
30
assert(~cycle)