import random


def initRandomGraph(ctor, n, m):
    """Constructs and returns a randomly generated graph
    with n vertices and m edges.
    ctor must be a callable that gets the number of vertices and
    creates a graph with the given number of vertces and with no edges"""
    g = ctor(n)
    addedEdges = 0
    while addedEdges < m:
        x = random.randrange(0, n)
        y = random.randrange(0, n)
        if not g.isEdge(x, y):
            g.addEdge(x, y)
            addedEdges += 1
    return g