/usr/share/pyshared/igraph/test/bipartite.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 | import unittest
from igraph import *
class BipartiteTests(unittest.TestCase):
def testCreateBipartite(self):
g = Graph.Bipartite([0, 1]*5, [(0,1),(2,3),(4,5),(6,7),(8,9)])
self.failUnless(g.vcount() == 10 and g.ecount() == 5 and g.is_directed() == False)
self.failUnless(g.is_bipartite())
self.failUnless(g.vs["type"] == [False, True]*5)
def testFullBipartite(self):
g = Graph.Full_Bipartite(10, 5)
self.failUnless(g.vcount() == 15 and g.ecount() == 50 and g.is_directed() == False)
expected = sorted([(i, j) for i in xrange(10) for j in xrange(10, 15)])
self.failUnless(sorted(g.get_edgelist()) == expected)
self.failUnless(g.vs["type"] == [False]*10 + [True]*5)
g = Graph.Full_Bipartite(10, 5, directed=True, mode=OUT)
self.failUnless(g.vcount() == 15 and g.ecount() == 50 and g.is_directed() == True)
self.failUnless(sorted(g.get_edgelist()) == expected)
self.failUnless(g.vs["type"] == [False]*10 + [True]*5)
g = Graph.Full_Bipartite(10, 5, directed=True, mode=IN)
self.failUnless(g.vcount() == 15 and g.ecount() == 50 and g.is_directed() == True)
self.failUnless(sorted(g.get_edgelist()) == sorted([(i,j) for j, i in expected]))
self.failUnless(g.vs["type"] == [False]*10 + [True]*5)
g = Graph.Full_Bipartite(10, 5, directed=True)
self.failUnless(g.vcount() == 15 and g.ecount() == 100 and g.is_directed() == True)
expected.extend([(j, i) for i, j in expected])
expected.sort()
self.failUnless(sorted(g.get_edgelist()) == expected)
self.failUnless(g.vs["type"] == [False]*10 + [True]*5)
def testIncidence(self):
g = Graph.Incidence([[0, 1, 1], [1, 2, 0]])
self.failUnless(g.vcount() == 5 and g.ecount() == 4 and g.is_directed() == False)
self.failUnless(g.vs["type"] == [False]*2 + [True]*3)
self.failUnless(sorted(g.get_edgelist()) == [(0,3),(0,4),(1,2),(1,3)])
g = Graph.Incidence([[0, 1, 1], [1, 2, 0]], multiple=True)
self.failUnless(g.vcount() == 5 and g.ecount() == 5 and g.is_directed() == False)
self.failUnless(g.vs["type"] == [False]*2 + [True]*3)
self.failUnless(sorted(g.get_edgelist()) == [(0,3),(0,4),(1,2),(1,3),(1,3)])
g = Graph.Incidence([[0, 1, 1], [1, 2, 0]], directed=True)
self.failUnless(g.vcount() == 5 and g.ecount() == 4 and g.is_directed() == True)
self.failUnless(g.vs["type"] == [False]*2 + [True]*3)
self.failUnless(sorted(g.get_edgelist()) == [(0,3),(0,4),(1,2),(1,3)])
g = Graph.Incidence([[0, 1, 1], [1, 2, 0]], directed=True, mode="in")
self.failUnless(g.vcount() == 5 and g.ecount() == 4 and g.is_directed() == True)
self.failUnless(g.vs["type"] == [False]*2 + [True]*3)
self.failUnless(sorted(g.get_edgelist()) == [(2,1),(3,0),(3,1),(4,0)])
def testGetIncidence(self):
mat = [[0, 1, 1], [1, 1, 0]]
v1, v2 = [0, 1], [2, 3, 4]
g = Graph.Incidence(mat)
self.failUnless(g.get_incidence() == (mat, v1, v2))
g.vs["type2"] = g.vs["type"]
self.failUnless(g.get_incidence("type2") == (mat, v1, v2))
self.failUnless(g.get_incidence(g.vs["type2"]) == (mat, v1, v2))
def testBipartiteProjection(self):
g = Graph.Full_Bipartite(10, 5)
g1, g2 = g.bipartite_projection()
self.failUnless(g1.isomorphic(Graph.Full(10)))
self.failUnless(g2.isomorphic(Graph.Full(5)))
self.failUnless(g1.es["weight"] == [5] * 45)
self.failUnless(g2.es["weight"] == [10] * 10)
self.failUnless(g.bipartite_projection_size() == (10, 45, 5, 10))
g1, g2 = g.bipartite_projection(probe1=10)
self.failUnless(g1.isomorphic(Graph.Full(5)))
self.failUnless(g2.isomorphic(Graph.Full(10)))
g1, g2 = g.bipartite_projection(multiplicity=False)
self.failUnless(g1.isomorphic(Graph.Full(10)))
self.failUnless(g2.isomorphic(Graph.Full(5)))
self.failUnless("weight" not in g1.edge_attributes())
self.failUnless("weight" not in g2.edge_attributes())
def testIsBipartite(self):
g = Graph.Star(10)
self.failUnless(g.is_bipartite() == True)
self.failUnless(g.is_bipartite(True) == (True, [False] + [True]*9))
g = Graph.Tree(100, 3)
self.failUnless(g.is_bipartite() == True)
g = Graph.Ring(9)
self.failUnless(g.is_bipartite() == False)
self.failUnless(g.is_bipartite(True) == (False, None))
g = Graph.Ring(10)
self.failUnless(g.is_bipartite() == True)
g += (2, 0)
self.failUnless(g.is_bipartite(True) == (False, None))
def suite():
bipartite_suite = unittest.makeSuite(BipartiteTests)
return unittest.TestSuite([bipartite_suite])
def test():
runner = unittest.TextTestRunner()
runner.run(suite())
if __name__ == "__main__":
test()
|