def generateGraph(self, alpha, p, dim):
        Parameter.checkFloat(alpha, 0.0, float('inf'))
        Parameter.checkFloat(p, 0.0, 1.0)
        Parameter.checkInt(dim, 0, float('inf'))

        numVertices = self.graph.getNumVertices()
        self.X = numpy.random.rand(numVertices, dim)

        D = KernelUtils.computeDistanceMatrix(numpy.dot(self.X, self.X.T))
        P = numpy.exp(-alpha * D)
        diagIndices = numpy.array(list(range(0, numVertices)))
        P[(diagIndices, diagIndices)] = numpy.zeros(numVertices)

        B = numpy.random.rand(numVertices, numVertices) <= P

        #Note that B is symmetric - could just go through e.g. upper triangle
        for i in range(numpy.nonzero(B)[0].shape[0]):
            v1 = numpy.nonzero(B)[0][i]
            v2 = numpy.nonzero(B)[1][i]

            self.graph.addEdge(v1, v2)

        erdosRenyiGenerator = ErdosRenyiGenerator(p)
        self.graph = erdosRenyiGenerator.generate(self.graph, False)

        return self.graph
Exemple #2
0
    def testWriteToFile3(self):
        """
        We will test out writing out some random graphs to Pajek
        """
        numVertices = 20
        numFeatures = 0
        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList)

        p = 0.1
        generator = ErdosRenyiGenerator(p)
        graph = generator.generate(graph)

        pw = PajekWriter()
        directory = PathDefaults.getOutputDir() + "test/"
        pw.writeToFile(directory + "erdosRenyi20", graph)

        #Now write a small world graph
        p = 0.2
        k = 3

        graph.removeAllEdges()
        generator = SmallWorldGenerator(p, k)
        graph = generator.generate(graph)

        pw.writeToFile(directory + "smallWorld20", graph)
    def testMeanSeqScalarStats(self):
        numFeatures = 1
        numVertices = 10
        vList = VertexList(numVertices, numFeatures)
        
        p = 0.1 
        generator = ErdosRenyiGenerator(p)

        numGraphs = 50
        graphList = []
        subgraphIndices = [list(range(3)), list(range(6)), list(range(10))]

        for i in range(numGraphs): 
            graph = generator.generate(SparseGraph(vList))
            graphList.append((graph, subgraphIndices))

        growthStatistics = GraphStatistics()
        meanStats, stdStats = growthStatistics.meanSeqScalarStats(graphList)

        #Check some of the stats
        for i in range(len(subgraphIndices)):
            self.assertEquals(meanStats[i, growthStatistics.numVerticesIndex], len(subgraphIndices[i]))
            self.assertEquals(stdStats[i, growthStatistics.numVerticesIndex], 0)

            self.assertAlmostEquals(meanStats[i, growthStatistics.numEdgesIndex], 0.5*(len(subgraphIndices[i])*(len(subgraphIndices[i])-1))*p, places=0)
Exemple #4
0
    def __init__(self):
        numVertices = 1000
        graph = SparseGraph(GeneralVertexList(numVertices))

        p = 0.1
        generator = ErdosRenyiGenerator(p)
        graph = generator.generate(graph)

        subgraphIndicesList = []
        for i in range(100, numVertices, 10):
            subgraphIndicesList.append(range(i))

        k1 = 5
        k2 = 100

        self.graph = graph
        self.subgraphIndicesList = subgraphIndicesList
        self.clusterer = IterativeSpectralClustering(k1, k2, T=10, alg="IASC")
Exemple #5
0
import sys
import struct
#from apgl.graph import *
from apgl.graph.DenseGraph import DenseGraph
from apgl.graph.VertexList import VertexList
from apgl.generator.ErdosRenyiGenerator import ErdosRenyiGenerator
import numpy

if len(sys.argv) != 2:
    print('Usage {} <num-vertices>'.format(sys.argv[0]))
    sys.exit(1)

# Generate a random graph
p = 0.2
graph = DenseGraph(VertexList(int(sys.argv[1]), 1))
generator = ErdosRenyiGenerator(p)
graph = generator.generate(graph)

numVertices = graph.getNumVertices()
numEdges = graph.getNumEdges()
print("{} vertices {} edges".format(numVertices, numEdges))

# Write packed adjacency list to a binary file
outfile = 'graph'
f = open(outfile, "wb")
try:
    f.write(struct.pack('i', numVertices))
    f.write(struct.pack('i', numEdges))
    index = 0
    # Vertices
    for i in range(numVertices):