/usr/share/pyshared/igraph/test/cliques.py is in python-igraph 0.6.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | import unittest
from igraph import *
class CliqueTests(unittest.TestCase):
def setUp(self):
self.g=Graph.Full(6)
self.g.delete_edges([(0, 1), (0, 2), (3, 5)])
def testCliques(self):
tests = {(4, -1): [[1, 2, 3, 4], [1, 2, 4, 5]],
(2, 2): [[0, 3], [0, 4], [0, 5],
[1, 2], [1, 3], [1, 4], [1, 5],
[2, 3], [2, 4], [2, 5], [3, 4], [4, 5]],
(-1, -1): [[0], [1], [2], [3], [4], [5],
[0, 3], [0, 4], [0, 5],
[1, 2], [1, 3], [1, 4], [1, 5],
[2, 3], [2, 4], [2, 5], [3, 4], [4, 5],
[0, 3, 4], [0, 4, 5],
[1, 2, 3], [1, 2, 4], [1, 2, 5],
[1, 3, 4], [1, 4, 5], [2, 3, 4], [2, 4, 5],
[1, 2, 3, 4], [1, 2, 4, 5]]}
for (lo, hi), exp in tests.iteritems():
self.assertEqual(sorted(exp), sorted(map(sorted, self.g.cliques(lo, hi))))
def testLargestCliques(self):
self.assertEqual(sorted(map(sorted, self.g.largest_cliques())),
[[1, 2, 3, 4], [1, 2, 4, 5]])
def testMaximalCliques(self):
self.assertEqual(sorted(map(sorted, self.g.maximal_cliques())),
[[0, 3, 4], [0, 4, 5],
[1, 2, 3, 4], [1, 2, 4, 5]])
self.assertEqual(sorted(map(sorted, self.g.maximal_cliques(min=4))),
[[1, 2, 3, 4], [1, 2, 4, 5]])
self.assertEqual(sorted(map(sorted, self.g.maximal_cliques(max=3))),
[[0, 3, 4], [0, 4, 5]])
def testCliqueNumber(self):
self.assertEqual(self.g.clique_number(), 4)
self.assertEqual(self.g.omega(), 4)
class IndependentVertexSetTests(unittest.TestCase):
def setUp(self):
self.g1=Graph.Tree(5, 2, TREE_UNDIRECTED)
self.g2=Graph.Tree(10, 2, TREE_UNDIRECTED)
def testIndependentVertexSets(self):
tests = {(4, -1): [],
(2, 2): [(0, 3), (0, 4), (1, 2), (2, 3), (2, 4), (3, 4)],
(-1, -1): [(0,), (1,), (2,), (3,), (4,),
(0, 3), (0, 4), (1, 2), (2, 3), (2, 4),
(3, 4), (0, 3, 4), (2, 3, 4)]}
for (lo, hi), exp in tests.iteritems():
self.assertEqual(exp, self.g1.independent_vertex_sets(lo, hi))
def testLargestIndependentVertexSets(self):
self.assertEqual(self.g1.largest_independent_vertex_sets(),
[(0, 3, 4), (2, 3, 4)])
def testMaximalIndependentVertexSets(self):
self.assertEqual(self.g2.maximal_independent_vertex_sets(),
[(0, 3, 4, 5, 6), (0, 3, 5, 6, 9),
(0, 4, 5, 6, 7, 8), (0, 5, 6, 7, 8, 9),
(1, 2, 7, 8, 9), (1, 5, 6, 7, 8, 9),
(2, 3, 4), (2, 3, 9), (2, 4, 7, 8)])
def testIndependenceNumber(self):
self.assertEqual(self.g2.independence_number(), 6)
self.assertEqual(self.g2.alpha(), 6)
class MotifTests(unittest.TestCase):
def setUp(self):
self.g = Graph.Erdos_Renyi(100, 0.2, directed=True)
def testDyads(self):
"""
@note: this test is not exhaustive, it only checks whether the
L{DyadCensus} objects "understand" attribute and item accessors
"""
dc = self.g.dyad_census()
accessors = ["mut", "mutual", "asym", "asymm", "asymmetric", "null"]
for a in accessors:
self.failUnless(isinstance(getattr(dc, a), int))
self.failUnless(isinstance(dc[a], int))
self.failUnless(isinstance(list(dc), list))
self.failUnless(isinstance(tuple(dc), tuple))
self.failUnless(len(list(dc)) == 3)
self.failUnless(len(tuple(dc)) == 3)
def testTriads(self):
"""
@note: this test is not exhaustive, it only checks whether the
L{TriadCensus} objects "understand" attribute and item accessors
"""
tc = self.g.triad_census()
accessors = ["003", "012", "021d", "030C"]
for a in accessors:
self.failUnless(isinstance(getattr(tc, "t"+a), int))
self.failUnless(isinstance(tc[a], int))
self.failUnless(isinstance(list(tc), list))
self.failUnless(isinstance(tuple(tc), tuple))
self.failUnless(len(list(tc)) == 16)
self.failUnless(len(tuple(tc)) == 16)
class CliqueBenchmark(object):
"""This is a benchmark, not a real test case. You can run it
using:
>>> from igraph.test.clique import CliqueBenchmark
>>> CliqueBenchmark().run()
"""
def __init__(self):
from time import time
import gc
self.time = time
self.gc_collect = gc.collect
def run(self):
self.printIntro()
self.testRandom()
self.testMoonMoser()
self.testGRG()
def printIntro(self):
print "n = number of vertices"
print "#cliques = number of maximal cliques found"
print "t1 = time required to determine the clique number"
print "t2 = time required to determine and save all maximal cliques"
print
def timeit(self, g):
start = self.time()
omega = g.clique_number()
mid = self.time()
cl = g.maximal_cliques()
end = self.time()
self.gc_collect()
return len(cl), mid-start, end-mid
def testRandom(self):
np = {100: [0.6, 0.7],
300: [0.1, 0.2, 0.3, 0.4],
500: [0.1, 0.2, 0.3],
700: [0.1, 0.2],
1000:[0.1, 0.2],
10000: [0.001, 0.003, 0.005, 0.01, 0.02]}
print
print "Erdos-Renyi random graphs"
print " n p #cliques t1 t2"
for n in sorted(np.keys()):
for p in np[n]:
g = Graph.Erdos_Renyi(n, p)
result = self.timeit(g)
print "%8d %8.3f %8d %8.4fs %8.4fs" % \
tuple([n, p] + list(result))
def testMoonMoser(self):
ns = [15, 27, 33]
print
print "Moon-Moser graphs"
print " n exp_clqs #cliques t1 t2"
for n in ns:
n3 = n/3
types = range(n3) * 3
el = [(i, j) for i in range(n) for j in range(i+1,n) if types[i] != types[j]]
g = Graph(n, el)
result = self.timeit(g)
print "%8d %8d %8d %8.4fs %8.4fs" % \
tuple([n, (3**(n/3))] + list(result))
def testGRG(self):
ns = [100, 1000, 5000, 10000, 25000, 50000]
print
print "Geometric random graphs"
print " n d #cliques t1 t2"
for n in ns:
d = 2. / (n ** 0.5)
g = Graph.GRG(n, d)
result = self.timeit(g)
print "%8d %8.3f %8d %8.4fs %8.4fs" % \
tuple([n, d] + list(result))
def suite():
clique_suite = unittest.makeSuite(CliqueTests)
indvset_suite = unittest.makeSuite(IndependentVertexSetTests)
motif_suite = unittest.makeSuite(MotifTests)
return unittest.TestSuite([clique_suite, indvset_suite, motif_suite])
def test():
runner = unittest.TextTestRunner()
runner.run(suite())
if __name__ == "__main__":
test()
|