/usr/share/pyshared/igraph/test/flow.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 | import unittest
from igraph import *
class MaxFlowTests(unittest.TestCase):
def setUp(self):
self.g = Graph(4, [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)])
self.capacities = [4, 2, 10, 2, 2]
self.g.es["capacity"] = self.capacities
def testCapacities(self):
self.failUnless(self.capacities == \
self.g.es.get_attribute_values("capacity"))
def testEdgeConnectivity(self):
self.failUnless(self.g.edge_connectivity(0, 3) == 2)
self.failUnless(Graph.Barabasi(50).edge_connectivity() == 1)
self.failUnless(self.g.adhesion() == 2)
self.failUnless(Graph.Tree(10, 3).adhesion() == 1)
self.failUnless(Graph.Tree(10, 3, TREE_OUT).adhesion() == 0)
self.assertRaises(ValueError, self.g.edge_connectivity, 0)
def testVertexConnectivity(self):
self.failUnless(self.g.vertex_connectivity(0, 3) == 2)
self.failUnless(Graph.Barabasi(50).vertex_connectivity() == 1)
self.failUnless(self.g.cohesion() == 2)
self.failUnless(Graph.Tree(10, 3).cohesion() == 1)
self.failUnless(Graph.Tree(10, 3, TREE_OUT).cohesion() == 0)
self.assertRaises(ValueError, self.g.vertex_connectivity, 0)
self.assertRaises(InternalError, self.g.vertex_connectivity, 0, 1)
self.failUnless(self.g.vertex_connectivity(0, 1, neighbors="nodes") == 4)
self.failUnless(self.g.vertex_connectivity(0, 1, neighbors="negative") == -1)
def testMaxFlowValue(self):
self.failUnless(self.g.maxflow_value(0, 3) == 2)
self.failUnless(self.g.maxflow_value(0, 3, self.capacities) == 4)
self.failUnless(self.g.maxflow_value(0, 3, "capacity") == 4)
self.assertRaises(KeyError, self.g.maxflow_value, 0, 3, "unknown")
def testMaxFlow(self):
flow = self.g.maxflow(0, 3)
self.assertEquals(flow.value, 2)
self.assertEquals(flow.flow, [1, 1, 0, 1, 1])
flow = self.g.maxflow(0, 3, "capacity")
self.assertEquals(flow.value, 4)
self.assertEquals(flow.cut, [3, 4])
self.assertEquals([e.index for e in flow.es], [3, 4])
self.failUnless(set(flow.partition[0]).union(flow.partition[1]) == \
set(range(self.g.vcount())))
self.assertRaises(KeyError, self.g.maxflow, 0, 3, "unknown")
class CutTests(unittest.TestCase):
def constructSimpleGraph(self, directed=False):
g = Graph(4, [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)], directed)
g.es["capacity"] = [4, 2, 10, 2, 2]
return g
def constructLadderGraph(self, directed=False):
el = zip(range(0, 5), range(1, 6))
el += zip(range(6, 11), range(7, 12))
el += zip(range(0, 6), range(6, 12))
g = Graph(el, directed=directed)
return g
def testMinCutValue(self):
g = self.constructSimpleGraph()
self.failUnless(g.mincut_value(0, 3) == 2)
self.failUnless(g.mincut_value(0, 3, g.es["capacity"]) == 4)
self.failUnless(g.mincut_value(0, 3, "capacity") == 4)
self.assertRaises(KeyError, g.mincut_value, 0, 3, "unknown")
self.failUnless(g.mincut_value() == 2)
self.failUnless(g.mincut_value(source=0) == 2)
self.failUnless(g.mincut_value(target=2) == 2)
def testMinCut(self):
g = self.constructSimpleGraph()
mc = g.mincut()
self.failUnless(isinstance(mc, Cut))
self.failUnless(mc.value == 2)
self.failUnless(set(mc.partition[0]).union(mc.partition[1]) == \
set(range(g.vcount())))
self.failUnless(isinstance(str(mc), str))
self.failUnless(isinstance(repr(mc), str))
self.failUnless(isinstance(mc.es, EdgeSeq))
self.failUnless(len(mc.es) == 2)
mc = g.mincut("capacity")
self.failUnless(mc.value == 4)
self.assertRaises(KeyError, g.mincut, "unknown")
def testAllSTCuts1(self):
# Simple graph with four vertices
g = self.constructSimpleGraph(directed=True)
partitions = [((0, 1, 1, 1), 2), ((0, 0, 1, 1), 3),
((0, 1, 0, 1), 2), ((0, 0, 0, 1), 2)]
values = dict(partitions)
partitions = [partition for partition, _ in partitions]
for cut in g.all_st_cuts(0,3):
membership = tuple(cut.membership)
self.failUnless(membership in partitions,
"%r not found among expected partitions" % (membership,))
self.assertEquals(cut.value, values[membership])
self.assertEquals(len(cut.es), values[membership])
partitions.remove(membership)
self.failUnless(partitions == [],
"expected partitions not seen: %r" % (partitions, ))
def testAllSTCuts2(self):
# "Ladder graph"
g = self.constructLadderGraph(directed=True)
cuts = g.all_st_cuts(0, 11)
self.assertEquals(len(cuts), 36)
self.assertEquals(len(set(tuple(cut.membership) for cut in cuts)), 36)
for cut in cuts:
g2 = g.copy()
g2.delete_edges(cut.es)
self.failIf(g2.is_connected(),
"%r is not a real cut" % (cut.membership,))
self.failIf(cut.value < 2 or cut.value > 6)
def testAllSTMinCuts2(self):
# "Ladder graph"
g = self.constructLadderGraph()
g.to_directed("mutual")
cuts = g.all_st_mincuts(0, 11)
self.assertEquals(len(cuts), 7)
self.assertEquals(len(set(tuple(cut.membership) for cut in cuts)), 7)
for cut in cuts:
self.assertEquals(cut.value, 2)
g2 = g.copy()
g2.delete_edges(cut.es)
self.failIf(g2.is_connected(),
"%r is not a real cut" % (cut.membership,))
g.es["capacity"] = [2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1]
cuts = g.all_st_mincuts(0, 11, "capacity")
self.assertEquals(len(cuts), 2)
self.assertEquals(cuts[0].membership, [0,0,1,1,1,1,0,0,1,1,1,1])
self.assertEquals(cuts[1].membership, [0,0,0,0,1,1,0,0,0,0,1,1])
self.assertEquals(cuts[0].value, 2)
self.assertEquals(cuts[1].value, 2)
def suite():
flow_suite = unittest.makeSuite(MaxFlowTests)
cut_suite = unittest.makeSuite(CutTests)
return unittest.TestSuite([flow_suite, cut_suite])
def test():
runner = unittest.TextTestRunner()
runner.run(suite())
if __name__ == "__main__":
test()
|