本文整理汇总了Python中Graph.Graph.isTree方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.isTree方法的具体用法?Python Graph.isTree怎么用?Python Graph.isTree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph.Graph
的用法示例。
在下文中一共展示了Graph.isTree方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testIfItIsTree
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import isTree [as 别名]
def testIfItIsTree(self):
self.assertFalse(self.graph.isTree(), 'should not be a tree')
# (1) (2) (3)
# \ | /
# \ | /
# (4)
# |
# (5)
newGraph = Graph()
newGraph.addVertex(1)
newGraph.addVertex(2)
newGraph.addVertex(3)
newGraph.addVertex(4)
newGraph.addVertex(5)
newGraph.connect(vertexid1=1, vertexid2=4, cost=None)
newGraph.connect(vertexid1=2, vertexid2=4, cost=None)
newGraph.connect(vertexid1=3, vertexid2=4, cost=None)
newGraph.connect(vertexid1=5, vertexid2=4, cost=None)
self.assertTrue(newGraph.isTree(), 'should be a tulip right, its a flower sort of...')
del newGraph
示例2: GraphTest
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import isTree [as 别名]
#.........这里部分代码省略.........
The great thing about graphs is that you can save a lot of cpu power
by just connecting nodes right, when we have a regular graph we dont
need to waste cpu power to connect a lot of nodes in a case where the
edges dont have direction of course, and we can come and go through
the same edge, the connections do the heuristic job of anticipating
useful connections.
"""
self.assertEquals(self.graph.isRegular(), False, 'should return false a first attempt')
self.graph.connect(vertexid1='v3', vertexid2='v4', cost=None)
self.graph.connect(vertexid1='v3', vertexid2='v5', cost=None)
self.graph.connect(vertexid1='v4', vertexid2='v5', cost=None)
self.assertEquals(self.graph.isRegular(), True, 'should return false a first attempt')
def testIfItIsCompleteGraph(self):
self.graph.connect(vertexid1='v3', vertexid2='v4', cost=None)
self.graph.connect(vertexid1='v3', vertexid2='v5', cost=None)
self.graph.connect(vertexid1='v4', vertexid2='v5', cost=None)
self.assertEquals(self.graph.isComplete(), True, 'should return false a first attempt')
def testIfItIsCompleteGraphAfterAnotherConnection(self):
self.graph.connect(vertexid1='v3', vertexid2='v4', cost=None)
self.graph.connect(vertexid1='v3', vertexid2='v5', cost=None)
self.graph.connect(vertexid1='v4', vertexid2='v5', cost=None)
self.graph.connect(vertexid1='v4', vertexid2='v4', cost=None)
self.assertEquals(self.graph.isComplete(), False,
'this really should fail because we just added an adjacency to v4 itself')
def testFindTransitiveClosure(self):
newGraph = Graph()
newGraph.addVertex(1)
newGraph.addVertex(2)
newGraph.connect(vertexid1=1, vertexid2=2, cost=None)
testTransitiveClosureSet = newGraph.transitiveClosure(vertexid=1)
self.assertTrue(1 in testTransitiveClosureSet and
2 in testTransitiveClosureSet,
'should be inside the transitive closure')
del newGraph
def testIfItIsRelational(self):
self.assertTrue(self.graph.isRelational(), 'this graph in this state should not be relational because\
v4 and v5 dont have connection in between')
# diconnect some vertexes and try the test again
self.graph.disconnect(vertexid1='v1', vertexid2='v2')
self.graph.disconnect(vertexid1='v1', vertexid2='v3')
self.graph.disconnect(vertexid1='v2', vertexid2='v2')
self.graph.disconnect(vertexid1='v2', vertexid2='v3')
self.assertFalse(self.graph.isRelational(), 'should not be relational')
def testIfItIsTree(self):
self.assertFalse(self.graph.isTree(), 'should not be a tree')
# (1) (2) (3)
# \ | /
# \ | /
# (4)
# |
# (5)
newGraph = Graph()
newGraph.addVertex(1)
newGraph.addVertex(2)
newGraph.addVertex(3)
newGraph.addVertex(4)
newGraph.addVertex(5)
newGraph.connect(vertexid1=1, vertexid2=4, cost=None)
newGraph.connect(vertexid1=2, vertexid2=4, cost=None)
newGraph.connect(vertexid1=3, vertexid2=4, cost=None)
newGraph.connect(vertexid1=5, vertexid2=4, cost=None)
self.assertTrue(newGraph.isTree(), 'should be a tulip right, its a flower sort of...')
del newGraph
def testConvertEdgesListToGraph(self):
edges_map = [[ 2 , 0 , 44 ],
[ 2 , 1 , 57 ],
[ 3 , 1 , 73 ],
[ 3 , 2 , 56 ],
[ 4 , 0 , 74 ],
[ 4 , 1 , 51 ],
[ 4 , 2 , 66 ],
[ 4 , 3 , 71 ],
[ 5 , 2 , 70 ],
[ 5 , 4 , 62 ],
[ 6 , 0 , 34 ],
[ 6 , 1 , 74 ],
[ 6 , 2 , 58 ],
[ 6 , 3 , 80 ],
[ 6 , 4 , 87 ],
[ 6 , 5 , 76 ],
[ 2, 4 , 0 ]]
newGraph = Graph.newGraphFromEdgesMap(edges_map, len(edges_map))
# this test the graph constrution from a mapping of edges with its costs
for index in range(len(edges_map) - 1):
v1, v2, cost = edges_map[index]
self.assertTrue(cost == newGraph.vertexAdjacencies(v1).get(v2).getcost(),
'test graph\'s consistence in costs and coesion in connections')
def testGetRandomVertexId(self):
self.assertTrue(self.graph.getRandomVertexId() in self.graph.graph,
'retrieve random vertex must be inside the graph')