Ejemplo n.º 1
0
def load(fileName: str) -> Graph:
    print('loader: load: loading graph ' + fileName + '...')
    with open(fileName, 'r') as inputFile:
        inputData = list(map(float, inputFile.read().split()))
        n, m = int(inputData[0]), int(inputData[1])

        if (n < 1):
            raise Exception('loader: load: invalid number of nodes', n)

        if (m < (n - 1)):
            raise Exception('loader: load: invalid number of edges', m)

        print(len(inputData))

        g = Graph(n, m)
        inputIndex, edgeIndex = 2, 0
        while (edgeIndex < m):
            u, inputIndex = int(inputData[inputIndex]), inputIndex + 1
            v, inputIndex = int(inputData[inputIndex]), inputIndex + 1
            c, inputIndex = inputData[inputIndex], inputIndex + 1

            if (u >= n):
                raise Exception('loader: load: invalid node id', u)

            if (v >= n):
                raise Exception('loader: load: invalid node id', v)

            g.addEdge(u, v, c)
            g.addEdge(v, u, c)
            edgeIndex += 1

    print('loader: load: loading graph done', g)
    return g
def generate_random_graph(n, p):
    g = Graph()
    for u, v in combinations(range(n), 2):
        if random.random() < p:
            g.addEdge(u, v)
    return g