File: BronKerboschCliqueFinderTest.java

package info (click to toggle)
libjgrapht0.8-java 0.8.3-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,192 kB
  • sloc: java: 18,365; xml: 501; makefile: 9
file content (179 lines) | stat: -rw-r--r-- 5,001 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* ==========================================
 * JGraphT : a free Java graph-theory library
 * ==========================================
 *
 * Project Info:  http://jgrapht.sourceforge.net/
 * Project Creator:  Barak Naveh (http://sourceforge.net/users/barak_naveh)
 *
 * (C) Copyright 2003-2008, by Barak Naveh and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/* ------------------------------
 * BronKerboschCliqueFinderTest.java
 * ------------------------------
 * (C) Copyright 2005-2008, by John V. Sichi and Contributors.
 *
 * Original Author:  John V. Sichi
 * Contributor(s):   -
 *
 * $Id: BronKerboschCliqueFinderTest.java 645 2008-09-30 19:44:48Z perfecthash $
 *
 * Changes
 * -------
 * 26-July-2005 : Initial revision (JVS);
 *
 */
package org.jgrapht.alg;

import java.util.*;

import junit.framework.*;

import org.jgrapht.*;
import org.jgrapht.graph.*;


/**
 * .
 *
 * @author John V. Sichi
 */
public class BronKerboschCliqueFinderTest
    extends TestCase
{
    //~ Static fields/initializers ---------------------------------------------

    private static final String V1 = "v1";
    private static final String V2 = "v2";
    private static final String V3 = "v3";
    private static final String V4 = "v4";
    private static final String V5 = "v5";
    private static final String V6 = "v6";
    private static final String V7 = "v7";
    private static final String V8 = "v8";

    //~ Methods ----------------------------------------------------------------

    /**
     * .
     *
     * @param g
     */
    public void createGraph(Graph<String, DefaultEdge> g)
    {
        g.addVertex(V1);
        g.addVertex(V2);
        g.addVertex(V3);
        g.addVertex(V4);
        g.addVertex(V5);
        g.addVertex(V6);
        g.addVertex(V7);
        g.addVertex(V8);

        // biggest clique:  { V1, V2, V3, V4 }
        g.addEdge(V1, V2);
        g.addEdge(V1, V3);
        g.addEdge(V1, V4);
        g.addEdge(V2, V3);
        g.addEdge(V2, V4);
        g.addEdge(V3, V4);

        // smaller clique:  { V5, V6, V7 }
        g.addEdge(V5, V6);
        g.addEdge(V5, V7);
        g.addEdge(V6, V7);

        // for fun, add an overlapping clique { V3, V4, V5 }
        g.addEdge(V3, V5);
        g.addEdge(V4, V5);

        // make V8 less lonely
        g.addEdge(V7, V8);
    }

    public void testFindBiggest()
    {
        SimpleGraph<String, DefaultEdge> g =
            new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);
        createGraph(g);

        BronKerboschCliqueFinder<String, DefaultEdge> finder =
            new BronKerboschCliqueFinder<String, DefaultEdge>(g);

        Collection<Set<String>> cliques = finder.getBiggestMaximalCliques();

        assertEquals(1, cliques.size());

        Set<String> expected = new HashSet<String>();
        expected.add(V1);
        expected.add(V2);
        expected.add(V3);
        expected.add(V4);

        Set<String> actual = cliques.iterator().next();

        assertEquals(expected, actual);
    }

    public void testFindAll()
    {
        SimpleGraph<String, DefaultEdge> g =
            new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);
        createGraph(g);

        BronKerboschCliqueFinder<String, DefaultEdge> finder =
            new BronKerboschCliqueFinder<String, DefaultEdge>(g);

        Collection<Set<String>> cliques = finder.getAllMaximalCliques();

        assertEquals(4, cliques.size());

        Set<Set<String>> expected = new HashSet<Set<String>>();

        Set<String> set = new HashSet<String>();
        set.add(V1);
        set.add(V2);
        set.add(V3);
        set.add(V4);
        expected.add(set);

        set = new HashSet<String>();
        set.add(V5);
        set.add(V6);
        set.add(V7);
        expected.add(set);

        set = new HashSet<String>();
        set.add(V3);
        set.add(V4);
        set.add(V5);
        expected.add(set);

        set = new HashSet<String>();
        set.add(V7);
        set.add(V8);
        expected.add(set);

        // convert result from Collection to Set because we don't want
        // order to be significant
        Set<Set<String>> actual = new HashSet<Set<String>>(cliques);

        assertEquals(expected, actual);
    }
}

// End BronKerboschCliqueFinderTest.java