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