comparison toolboxes/FullBNT-1.0.7/graph/triangulate_2Dlattice_demo.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 % Consider a 3x3 lattice with 4-nearest neighbor connectivity
2
3 % 1 - 2 - 3
4 % | | |
5 % 4 - 5 - 6
6 % | | |
7 % 7 - 8 - 9
8
9 N = 3;
10 G = mk_2D_lattice(N,N,4);
11 G0 = G;
12
13 % Now add in the diagonal edges
14
15 if 0
16 % 1 - 2 - 3
17 % | x | x |
18 % 4 - 5 - 6
19 % | x | x |
20 % 7 - 8 - 9
21
22 G(1,5)=1; G(5,1)=1;
23 G(2,6)=1; G(6,2)=1;
24 G(4,2)=1; G(2,4)=1;
25 G(5,3)=1; G(3,5)=1;
26
27 G(4,8)=1; G(8,4)=1;
28 G(5,9)=1; G(9,5)=1;
29 G(7,5)=1; G(5,7)=1;
30 G(8,6)=1; G(6,8)=1;
31 end
32
33 % 1 - 2 - 3
34 % | / | \ |
35 % 4 - 5 - 6
36 % | \ | / |
37 % 7 - 8 - 9
38
39 G(2,6)=1; G(6,2)=1;
40 G(4,2)=1; G(2,4)=1;
41 G(4,8)=1; G(8,4)=1;
42 G(8,6)=1; G(6,8)=1;
43
44 % Is this a chordal (triangulated) graph? No!
45
46 assert(~check_triangulated(G))
47
48 % The reason is that there is a chordless cycle around the outside nodes.
49 % To see this, imagine "picking up" node 5, leaving the rest on the plane
50 % (like a hoop skirt, or a tent), as shown below
51
52 % 1 - 2 - 3
53 % | / \ |
54 % 4 6
55 % | \ / |
56 % 7 - 8 - 9
57
58
59 % However, if we add in the 4-6 arc, it will be chordal.
60
61 G2 = G;
62 G2(4,6)=1; G2(6,4)=1;
63 assert(check_triangulated(G2))
64
65 % Or we can add in the 2-8 arc
66 G2 = G;
67 G2(2,8)=1; G2(8,2)=1;
68 assert(check_triangulated(G2))
69
70
71 if 0
72 % 4x4 lattice with cross arcs
73 N=4;G0 = mk_2D_lattice(N,N,4);
74 vs = [1 6; 2 5; 2 7; 3 6; 3 8; 4 7; ...
75 5 10; 6 9; 6 11; 7 10; 7 12; 8 11;...
76 9 14; 10 13; 10 15; 11 14; 11 16; 12 15];
77 for i=1:size(vs,1)
78 u = vs(i,1); v= vs(i,2);
79 G0(u,v) = 1; G0(v,u) = 1;
80 end
81 end
82
83 % Here is how we can discover which edges to fill in automatically
84 % (although possibly sub-optimally)
85 weights = 2*ones(1,N*N); % all nodes are binar
86
87 % fill-ins = 2-4, 2-6, 4-8, 6-8 and 4-6
88 % cliques = 124, etc and 2456 4568
89 greedy_order = best_first_elim_order(G0, weights);
90 [GT, cliques, fill_ins] = triangulate(G0, greedy_order)
91 assert(check_triangulated(GT))
92
93
94
95 greedy_order = best_first_elim_order(G, weights);
96 [GT, cliques, fill_ins] = triangulate(G, greedy_order)
97 assert(check_triangulated(GT))
98
99 % fill-ins = [4 6]
100
101 % Cliques are the overlapping squares [1,2,4,5], [2 3 5 6], [4 5 7 8], [5 6 8 9]
102 % and the following caused by the fill-in: [2 4 5 6], [4 5 6 8]
103
104 % Connect the maximal cliques of the triangulate graph into a junction tree
105 [jtree, root, B, clq_weights] = cliques_to_jtree(cliques, weights);
106
107 % In this case, all cliques have weight 2^4 = 16
108
109
110 % Now consider size of max clique as a function of grid size
111 % Note: this is not necessarily the optimal triangulation
112
113 % N 5 10 15 16 17 18
114 % m 6 15 23 25 28 28
115 Ns = [5 10 15 16 17 18];
116 for i=1:length(Ns)
117 N = Ns(i)
118 G = mk_2D_lattice(N,N,4);
119 weights = 2*ones(1,N*N); % all nodes are binary
120 greedy_order = best_first_elim_order(G, weights); % slow!
121 [GT, cliques, fill_ins] = triangulate(G, greedy_order);
122 %assert(check_triangulated(GT))
123 [jtree, root, B, clq_weights] = cliques_to_jtree(cliques, weights);
124 m(i) = log2(max(clq_weights));
125 end
126
127 % plot distribution of clique sizes for fixed N
128 for c=1:length(cliques)
129 l(c) = length(cliques{c});
130 end
131 hist(l)