示例#1
0
def test_noEdges():
	name = 'noEdges'
	g = makeGraph(edgelessGraph)
	want = [(0, 1), (0, 2)]

	graph.complement(g,0)

	assert sortedPairList(g.edges) == want, name
示例#2
0
def test_populated():
	name = 'populated'
	g = makeGraph(simpleGraph)
	want = [(0, 3), (0, 4), (1, 3), (1, 4), (3, 4)]

	graph.complement(g,0)

	assert sortedPairList(g.edges) == want, name
示例#3
0
def test_badNode():
	name = 'badNode'
	g = makeGraph(simpleGraph)
	want = [(0, 1), (0, 2), (1, 3), (1, 4), (3, 4)]

	# no-op
	graph.complement(g,6)

	assert sortedPairList(g.edges) == want, name
示例#4
0
def test_empty():
	name = 'empty'
	g = makeGraph(None)
	want = []

	# no-op
	graph.complement(g,0)

	assert sortedPairList(g.edges) == want, name
示例#5
0
def test_complementTwice():
	name = 'complementTwice'
	g = makeGraph(simpleGraph)
	want = [(0, 1), (0, 2), (1, 3), (1, 4), (3, 4)]

	graph.complement(g,0)
	graph.complement(g,0)

	assert sortedPairList(g.edges) == want, name
def unlabeled_complement(vertices):
    """
    A slightly more efficient generator which generates all unlabeled (structurally different) graphs with a given
    number of vertices.

    This generator is equivalent to the 'unlabeled' generator except that it utilizes small shortcut by only generating
    the first half of the unlabeled graphs using the algorithm in 'unlabeled.' It generates the second half by taking
    the complement of all graphs in the first half. The complement in this case simply meaning: "for every pair of
    vertices with an edge, remove that edge and for every pair of vertices with no edge, add an edge."

    :param vertices: The number of vertices for which to generate unlabeled graphs over.
    """

    g0 = {}

    edgeclasses = vertices * (vertices - 1) / 2 + 1
    firsthalf = edgeclasses / 2
    odd = edgeclasses % 2
    print "graphs complement: odd=", odd, "max edges: ", edgeclasses - 1

    # initialize the first graph with the number of vertices but no edges
    for i in range(1, vertices + 1, 1):
        g0[i] = []

    # yield the first graph, then yield it's compliment
    yield g0
    yield graph.complement(g0)

    # Start with a list on m (in this case 0) edges.
    Lm = [g0]

    while graph.edgecount(Lm[0]) < firsthalf - 1:

        # start with an empty list (L) on m+1 edges
        L = []

        # for each graphs graph on m edges, add an edge in all possible ways
        for g in Lm:
            # for each graph generated by adding a single edge
            for trial in augmenter(g):
                if len(L) > 0:
                    if code(trial) < code(L[-1]) and is_canonical(trial) == True:
                        L.append(trial)
                        yield (L[-1])
                        yield (graph.complement(L[-1]))
                elif is_canonical(trial) == True:
                    L.append(trial)
                    yield (L[-1])
                    yield (graph.complement(L[-1]))
        print "Generating: graphs on", graph.edgecount(Lm[0]) + 1, "edges(" + str(len(L)) + ")"

        Lm = L
        L = []

    if odd == 1:
        L = []
        for g in Lm:
            for trial in augmenter(g):
                if len(L) > 0:
                    if code(trial) < code(L[-1]) and is_canonical(trial) == True:
                        L.append(trial)
                        yield (L[-1])
                elif is_canonical(trial) == True:
                    L.append(trial)
                    yield (L[-1])
        Lm = L
        L = []