本文整理汇总了Python中apgl.graph.SparseGraph.SparseGraph.subgraph方法的典型用法代码示例。如果您正苦于以下问题:Python SparseGraph.subgraph方法的具体用法?Python SparseGraph.subgraph怎么用?Python SparseGraph.subgraph使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apgl.graph.SparseGraph.SparseGraph
的用法示例。
在下文中一共展示了SparseGraph.subgraph方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testMatch
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import subgraph [as 别名]
def testMatch(self):
matcher = GraphMatch(algorithm="U", alpha=0.3)
permutation, distance, time = matcher.match(self.graph1, self.graph2)
#Checked output file - seems correct
distance2 = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2, permutation)
self.assertAlmostEquals(distance[0], distance2)
#Now test case in which alpha is different
matcher = GraphMatch(algorithm="U", alpha=0.5)
permutation, distance, time = matcher.match(self.graph1, self.graph2)
distance2 = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2, permutation)
self.assertAlmostEquals(distance[0], distance2)
#Test normalised distance
alpha = 0.0
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(self.graph1, self.graph2)
distance2 = GraphMatch(alpha=alpha).distance(self.graph1, self.graph2, permutation, True)
self.assertAlmostEquals(distance[1], distance2)
alpha = 1.0
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(self.graph1, self.graph2)
distance2 = GraphMatch(alpha=alpha).distance(self.graph1, self.graph2, permutation, True)
self.assertAlmostEquals(distance[1], distance2, 5)
#Test empty graph
alpha = 0.0
graph1 = SparseGraph(VertexList(0, 0))
graph2 = SparseGraph(VertexList(0, 0))
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(graph1, graph2)
nptst.assert_array_equal(permutation, numpy.array([], numpy.int))
self.assertEquals(distance, [0, 0, 0])
#Test where 1 graph is empty
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(graph1, self.graph1)
self.assertEquals(numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
self.assertEquals(distance[1], 1)
self.assertEquals(distance[2], 1)
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(self.graph1, graph1)
self.assertEquals(numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
self.assertEquals(distance[1], 1)
self.assertEquals(distance[2], 1)
alpha = 1.0
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(graph1, self.graph1)
self.assertEquals(numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
V2 = self.graph1.vlist.getVertices()
V1 = numpy.zeros(V2.shape)
C = GraphMatch(algorithm="U", alpha=alpha).matrixSimilarity(V1, V2)
dist = numpy.trace(C)/numpy.linalg.norm(C)
self.assertAlmostEquals(distance[1], -dist, 4)
self.assertAlmostEquals(distance[2], -dist, 4)
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(self.graph1, graph1)
self.assertEquals(numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
self.assertAlmostEquals(distance[1], -dist, 4)
self.assertAlmostEquals(distance[2], -dist, 4)
#Test one graph which is a subgraph of another
p = 0.2
k = 10
numVertices = 20
generator = SmallWorldGenerator(p, k)
graph = SparseGraph(VertexList(numVertices, 2))
graph = generator.generate(graph)
subgraphInds = numpy.random.permutation(numVertices)[0:10]
subgraph = graph.subgraph(subgraphInds)
matcher = GraphMatch(algorithm="U", alpha=0.0)
permutation, distance, time = matcher.match(graph, subgraph)
distance = matcher.distance(graph, subgraph, permutation, True, True)
self.assertTrue(distance < 1)
示例2: GraphMatchTest
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import subgraph [as 别名]
#.........这里部分代码省略.........
self.assertEquals(distance[1], 1)
self.assertEquals(distance[2], 1)
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(self.graph1, graph1)
self.assertEquals(numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
self.assertEquals(distance[1], 1)
self.assertEquals(distance[2], 1)
alpha = 1.0
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(graph1, self.graph1)
self.assertEquals(numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
V2 = self.graph1.vlist.getVertices()
V1 = numpy.zeros(V2.shape)
C = GraphMatch(algorithm="U", alpha=alpha).matrixSimilarity(V1, V2)
dist = numpy.trace(C)/numpy.linalg.norm(C)
self.assertAlmostEquals(distance[1], -dist, 4)
self.assertAlmostEquals(distance[2], -dist, 4)
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(self.graph1, graph1)
self.assertEquals(numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
self.assertAlmostEquals(distance[1], -dist, 4)
self.assertAlmostEquals(distance[2], -dist, 4)
#Test one graph which is a subgraph of another
p = 0.2
k = 10
numVertices = 20
generator = SmallWorldGenerator(p, k)
graph = SparseGraph(VertexList(numVertices, 2))
graph = generator.generate(graph)
subgraphInds = numpy.random.permutation(numVertices)[0:10]
subgraph = graph.subgraph(subgraphInds)
matcher = GraphMatch(algorithm="U", alpha=0.0)
permutation, distance, time = matcher.match(graph, subgraph)
distance = matcher.distance(graph, subgraph, permutation, True, True)
self.assertTrue(distance < 1)
def testDistance(self):
permutation = numpy.arange(self.numVertices)
dist = GraphMatch(alpha=0.0).distance(self.graph1, self.graph1, permutation)
self.assertEquals(dist, 0.0)
dist = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2, permutation)
self.assertAlmostEquals(dist, 50.0)
permutation = numpy.arange(self.numVertices)
permutation[8] = 9
permutation[9] = 8
dist = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2, permutation)
self.assertAlmostEquals(dist, 54.0)
#Try graphs of unequal size
graph3 = self.graph1.subgraph(range(8))
permutation = numpy.arange(self.numVertices)
dist1 = GraphMatch(alpha=0.0).distance(self.graph1, graph3, permutation)
dist1a = GraphMatch(alpha=0.0).distance(graph3, self.graph1, permutation)
self.assertEquals(dist1, dist1a)
graph3 = self.graph1.subgraph(range(5))
dist2 = GraphMatch(alpha=0.0).distance(self.graph1, graph3, permutation)