当前位置: 首页>>代码示例>>Python>>正文


Python Graph.vertexMagnitude方法代码示例

本文整理汇总了Python中Graph.Graph.vertexMagnitude方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.vertexMagnitude方法的具体用法?Python Graph.vertexMagnitude怎么用?Python Graph.vertexMagnitude使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Graph.Graph的用法示例。


在下文中一共展示了Graph.vertexMagnitude方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: GraphTest

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import vertexMagnitude [as 别名]
class GraphTest(unittest.TestCase):

    def setUp(self):
        self.graph = Graph()
        self.graph.addVertex(vertexid='v1')
        self.graph.addVertex(vertexid='v2')
        self.graph.addVertex(vertexid='v3')
        self.graph.addVertex(vertexid='v4')
        self.graph.addVertex(vertexid='v5')
        self.graph.connect(vertexid1='v1', vertexid2='v2', cost=None)
        self.graph.connect(vertexid1='v1', vertexid2='v3', cost=None)
        self.graph.connect(vertexid1='v1', vertexid2='v4', cost=None)
        self.graph.connect(vertexid1='v1', vertexid2='v5', cost=None)
        self.graph.connect(vertexid1='v2', vertexid2='v1', cost=None)
        self.graph.connect(vertexid1='v2', vertexid2='v3', cost=None)
        self.graph.connect(vertexid1='v2', vertexid2='v4', cost=None)
        self.graph.connect(vertexid1='v2', vertexid2='v5', cost=None)
        # for node in self.graph.graph:
        #    print('sucessor: {}\n'.format(self.graph.graph.get(node).sucessor),
        #          'predecessor: {}\n'.format(self.graph.graph.get(node).predecessor))

    def tearDown(self):
        del self.graph

    def testWhenAddVertexShouldHaveId(self):
        self.assertEqual(self.graph.getVertex(vertexid='v1').vertexid,
                         'v1',
                         'should have the same id')

    def testDiferentVertexesShouldHaveDiferentIds(self):
        self.assertNotEqual(self.graph.getVertex(vertexid='v1').vertexid,
                            self.graph.getVertex(vertexid='v2').vertexid,
                            'should have diferent ids')

    def testWeigth(self):
        self.graph.addVertex(vertexid='v6')
        self.graph.addVertex(vertexid='v7')
        self.graph.connect('v6', 'v7', cost=1290)
        self.assertEquals(self.graph.vertexAdjacencies('v6').get('v7').getcost(), 1290,
                          'v6 should have weigth equal to 1290 linked to v7')
        self.assertEquals(self.graph.vertexAdjacencies('v7').get('v6').getcost(), 1290,
                          'v7 should have weigth equal to 1290 linked to v6')

    def testSizeShouldBeEqualToGraphLength(self):
        self.assertEqual(self.graph.size, len(self.graph.graph),
                         'size must be equal to length(self.graph.graph)')

    def testShouldConnectUsingNumbers(self):
        self.graph.addVertex(vertexid=1)
        self.graph.addVertex(vertexid=2)
        self.graph.connect(vertexid1=1, vertexid2=2, cost=None)
        self.assertTrue(2 in self.graph.vertexAdjacencies(vertexid=1),
                         'after insertion and connection 2 must be \"sucessor\" of 1')

    def testAddVertexObjectInsteadOfId(self):
        #with self.assertRaises(GraphValidationError):
        self.assertEqual(self.graph.addVertex(vertexid=Vertex('v1')),
                         None, 'should get None after trying to input another \"v1\" in the graph')

    def testMakeDisconnectOfVertexes(self):
        self.graph.disconnect(vertexid1='v1', vertexid2='v5')
        self.assertTrue('v5' not in self.graph.vertexAdjacencies('v1'),
                        'should not have a connection in between v1 and v5')
        self.assertTrue('v1' not in self.graph.vertexAdjacencies('v5'),
                        'should not have a connection in between v1 and v5')
        self.assertTrue(self.graph.size == len(self.graph.graph) == self.graph.graphMagnitude(),
                        'should return true for all ways to get the graph size')

    def testVertexRemoval(self):
        self.graph.removeVertex(vertexid='v1')
        self.assertFalse('v1' in self.graph.vertexAdjacencies('v3'),
                         'should return nothing because v1 was removed before this')
        self.assertFalse('v1' in self.graph.vertexAdjacencies('v4'),
                         'should return nothing because v1 was removed before this')
        #with self.assertRaises(GraphValidationError):
        self.assertEquals(self.graph.vertexAdjacencies(vertexid='v1'), None,
                          'should return nothign because it has been already deleted')
        self.assertTrue(self.graph.size == len(self.graph.graph) == self.graph.graphMagnitude(),
                        'should return true for all ways to get the graph size')

    def testGraphMagnitude(self):
        self.assertFalse(self.graph.graphMagnitude() == 7,
                         'should have take the number of nodes already inserted in')
        self.graph.disconnect(vertexid1='v1', vertexid2='v5')
        self.assertEquals(self.graph.graphMagnitude(), 5,
                          'should have take the number of nodes already inserted in')
        self.assertFalse(self.graph.size == 3,
                          'should return true by just getting size property')

    def testVertexMagnitude(self):
        self.assertEquals(self.graph.vertexMagnitude('v1'), 4,
                          'should have a vertex with a number of adjacencies')

    def testIfItIsRegularGraph(self):
        """
        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
#.........这里部分代码省略.........
开发者ID:tonussi,项目名称:grafos,代码行数:103,代码来源:GraphTest.py


注:本文中的Graph.Graph.vertexMagnitude方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。