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


Python DictGraph.getNumEdges方法代码示例

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


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

示例1: testGetNumEdges

# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getNumEdges [as 别名]
    def testGetNumEdges(self):
        dictGraph = DictGraph(True)
        self.assertEquals(dictGraph.getNumEdges(), 0)

        dictGraph.addEdge(1, 2, 12)
        dictGraph.addEdge(1, 3, 18)
        dictGraph.addEdge(3, 4, 1)
        self.assertEquals(dictGraph.getNumEdges(), 3)

        dictGraph.addEdge(3, 4, 1)
        self.assertEquals(dictGraph.getNumEdges(), 3)

        dictGraph.addEdge(3, 5, 1)
        self.assertEquals(dictGraph.getNumEdges(), 4)

        dictGraph.addEdge(3, 3, 1)
        self.assertEquals(dictGraph.getNumEdges(), 5)

        #Identical tests with directed graphs
        dictGraph = DictGraph(False)
        self.assertEquals(dictGraph.getNumEdges(), 0)

        dictGraph.addEdge(1, 2, 12)
        dictGraph.addEdge(1, 3, 18)
        dictGraph.addEdge(3, 4, 1)
        self.assertEquals(dictGraph.getNumEdges(), 3)

        dictGraph.addEdge(3, 4, 1)
        self.assertEquals(dictGraph.getNumEdges(), 3)

        dictGraph.addEdge(3, 5, 1)
        self.assertEquals(dictGraph.getNumEdges(), 4)

        dictGraph.addEdge(3, 3, 1)
        self.assertEquals(dictGraph.getNumEdges(), 5)
开发者ID:charanpald,项目名称:APGL,代码行数:37,代码来源:DictGraphTest.py

示例2: testAddEdges

# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getNumEdges [as 别名]
    def testAddEdges(self):
        graph = DictGraph()

        edgeList = [(1, 2), (2, 1), (5, 2), (8, 8)]

        graph.addEdges(edgeList)
        self.assertEquals(graph.getNumEdges(), 3)
        self.assertEquals(graph.getEdge(1, 2), 1)
        self.assertEquals(graph.getEdge(5, 2), 1)
        self.assertEquals(graph.getEdge(2, 1), 1)
        self.assertEquals(graph.getEdge(8, 8), 1)

        edgeValues = [1, 2, 3, 4]
        graph.addEdges(edgeList, edgeValues)
        self.assertEquals(graph.getEdge(1, 2), 2)
        self.assertEquals(graph.getEdge(5, 2), 3)
        self.assertEquals(graph.getEdge(2, 1), 2)
        self.assertEquals(graph.getEdge(8, 8), 4)

        #Now test directed graphs
        graph = DictGraph(False)
        graph.addEdges(edgeList)
        self.assertEquals(graph.getNumEdges(), 4)
        self.assertEquals(graph.getEdge(1, 2), 1)
        self.assertEquals(graph.getEdge(5, 2), 1)
        self.assertEquals(graph.getEdge(2, 1), 1)
        self.assertEquals(graph.getEdge(8, 8), 1)

        edgeValues = [1, 2, 3, 4]
        graph.addEdges(edgeList, edgeValues)
        self.assertEquals(graph.getEdge(1, 2), 1)
        self.assertEquals(graph.getEdge(5, 2), 3)
        self.assertEquals(graph.getEdge(2, 1), 2)
        self.assertEquals(graph.getEdge(8, 8), 4)
开发者ID:charanpald,项目名称:APGL,代码行数:36,代码来源:DictGraphTest.py

示例3: testGetAllEdgeIndices

# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getNumEdges [as 别名]
    def testGetAllEdgeIndices(self):
        graph = DictGraph()
        graph.addEdge("a", "b")
        graph.addEdge("a", "c")
        graph.addEdge("a", "d")
        graph.addEdge("d", "e")

        edgeIndices = graph.getAllEdgeIndices() 
        keys = graph.getAllVertexIds() 

        self.assertEquals(edgeIndices.shape[0], graph.getNumEdges())
        for i in range(edgeIndices.shape[0]):
            self.assertTrue(graph.getEdge(keys[int(edgeIndices[i, 0])], keys[edgeIndices[i, 1]]) == 1)

        graph = DictGraph(False)
        graph.addEdge("a", "b")
        graph.addEdge("b", "a")
        graph.addEdge("a", "c")
        graph.addEdge("a", "d")
        graph.addEdge("d", "e")

        edgeIndices = graph.getAllEdgeIndices() 
        keys = graph.getAllVertexIds()
        self.assertEquals(edgeIndices.shape[0], graph.getNumEdges())
        for i in range(edgeIndices.shape[0]):
            self.assertTrue(graph.getEdge(keys[int(edgeIndices[i, 0])], keys[edgeIndices[i, 1]]) == 1)
开发者ID:charanpald,项目名称:APGL,代码行数:28,代码来源:DictGraphTest.py

示例4: testSubgraph

# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getNumEdges [as 别名]
    def testSubgraph(self):
        graph = DictGraph()

        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(0, 3)
        graph.addEdge(1, 2)
        graph.addEdge(2, 3)
        graph.setVertex(0, "abc")
        graph.setVertex(3, "cde")

        self.assertEquals(graph.getNumEdges(), 5)

        subgraph = graph.subgraph([0, 1, 2])
        self.assertEquals(subgraph.getNumVertices(), 3)
        self.assertEquals(subgraph.getNumEdges(), 3)
        self.assertEquals(subgraph.isUndirected(), True)
        self.assertEquals(subgraph.getEdge(0, 1), 1)
        self.assertEquals(subgraph.getEdge(0, 2), 1)
        self.assertEquals(subgraph.getEdge(1, 2), 1)
        self.assertEquals(subgraph.getVertex(0), "abc")

        #Check the original graph is fine
        self.assertEquals(graph.getNumVertices(), 4)
        self.assertEquals(graph.getNumEdges(), 5)
        self.assertEquals(graph.getVertex(0), "abc")
        self.assertEquals(graph.getVertex(3), "cde")

        #Now a quick test for directed graphs
        graph = DictGraph(False)

        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(0, 3)
        graph.addEdge(1, 2)
        graph.addEdge(2, 3)

        subgraph = graph.subgraph([0, 1, 2])
        self.assertEquals(subgraph.getNumEdges(), 3)
        self.assertEquals(subgraph.isUndirected(), False)
        self.assertEquals(subgraph.getEdge(0, 1), 1)
        self.assertEquals(subgraph.getEdge(0, 2), 1)
        self.assertEquals(subgraph.getEdge(1, 2), 1)
开发者ID:charanpald,项目名称:APGL,代码行数:45,代码来源:DictGraphTest.py

示例5: __init__

# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getNumEdges [as 别名]
    def __init__(self, minGraphSize=500, maxGraphSize=None, dayStep=30):
        
        dataDir = PathDefaults.getDataDir() + "cluster/"
        edgesFilename = dataDir + "Cit-HepTh.txt"
        dateFilename = dataDir + "Cit-HepTh-dates.txt"

        #Note the IDs are integers but can start with zero so we prefix "1" to each ID 
        edges = []
        file = open(edgesFilename, 'r')
        file.readline()
        file.readline()
        file.readline()
        file.readline()

        for line in file:
            (vertex1, sep, vertex2) = line.partition("\t")
            vertex1 = vertex1.strip()
            vertex2 = vertex2.strip()
            edges.append([vertex1, vertex2])
            
            #if vertex1 == vertex2: 
            #    print(vertex1)

        file.close()

        logging.info("Loaded edge file " + str(edgesFilename) + " with " + str(len(edges)) + " edges")

        #Keep an edge graph 
        graph = DictGraph(False)
        graph.addEdges(edges)
        logging.info("Created directed citation graph with " + str(graph.getNumEdges()) + " edges and " + str(graph.getNumVertices()) + " vertices")

        #Read in the dates articles appear in a dict which used the year and month
        #as the key and the value is a list of vertex ids. For each month we include
        #all papers uploaded that month and those directed cited by those uploads. 
        startDate = datetime.date(1990, 1, 1)

        file = open(dateFilename, 'r')
        file.readline()
        numLines = 0 
        subgraphIds = []

        for line in file:
            (id, sep, date) = line.partition("\t")
            id = id.strip()
            date = date.strip()
            

            inputDate = datetime.datetime.strptime(date.strip(), "%Y-%m-%d")
            inputDate = inputDate.date()

            if graph.vertexExists(id):
                tDelta = inputDate - startDate
                            
                graph.vertices[id] = tDelta.days 
                subgraphIds.append(id)
                
                #If a paper cites another, it must have been written before 
                #the citing paper - enforce this rule. 
                for neighbour in graph.neighbours(id): 
                    if graph.getVertex(neighbour) == None: 
                        graph.setVertex(neighbour, tDelta.days) 
                        subgraphIds.append(neighbour)
                    elif tDelta.days < graph.getVertex(neighbour): 
                        graph.setVertex(neighbour, tDelta.days) 
                        
            numLines += 1 
            
        file.close()
        
        subgraphIds = set(subgraphIds)
        graph = graph.subgraph(list(subgraphIds))
        logging.debug(graph)
        logging.info("Loaded date file " + str(dateFilename) + " with " + str(len(subgraphIds)) + " dates and " + str(numLines) + " lines")

        W = graph.getSparseWeightMatrix()
        W = W + W.T
        
        vList = VertexList(W.shape[0], 1)
        vList.setVertices(numpy.array([graph.getVertices(graph.getAllVertexIds())]).T)
        
        #Note: we have 16 self edges and some two-way citations so this graph has fewer edges than the directed one 
        self.graph = SparseGraph(vList, W=W)
        logging.debug(self.graph)
        
        #Now pick the max component 
        components = self.graph.findConnectedComponents()
        self.graph = self.graph.subgraph(components[0])
        
        logging.debug("Largest component graph: " + str(self.graph))
        
        self.minGraphSize = minGraphSize
        self.maxGraphSize = maxGraphSize 
        self.dayStep = dayStep 
开发者ID:charanpald,项目名称:wallhack,代码行数:96,代码来源:CitationIterGenerator.py


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