Exemple #1
0
def random_fractionally_isomorphic_graph(graph, seed=None):
    """Returns a random graph that is fractionally isomorphic to the specified
    graph.

    This function may return the same graph it received as input. If the
    coarsest equitable partition of the input graph is the trivial partition
    (the partition in which each block contains exactly one vertex), then this
    function always returns the same graph it received as input.

    """
    # Get the parameters for the coarsest equitable partition of the graph; n
    # is the vector containing number of vertices in each block of the
    # partition and D_ij is the matrix containing the degree of any vertex in
    # block i relative to block j. p is the number of blocks in the partition.
    partition = coarsest_equitable_partition(graph)
    n, D = partition_parameters(graph, partition, as_matrices=True)
    p = len(n)
    # Permuting the parameters in an arbitrary fashion may produce parameters
    # for which there is no possible graph. For now, just stick with a random
    # graph on the same parameters.
    #
    ## Choose a random permutation matrix and permute the parameters.
    #P = random_permutation_matrix(p, seed)
    #n, D = P * n, P * D
    #
    # Get a random graph with those parameters.
    return random_graph_from_parameters(n, D, seed)
Exemple #2
0
def random_fractionally_isomorphic_graph(graph, seed=None):
    """Returns a random graph that is fractionally isomorphic to the specified
    graph.

    This function may return the same graph it received as input. If the
    coarsest equitable partition of the input graph is the trivial partition
    (the partition in which each block contains exactly one vertex), then this
    function always returns the same graph it received as input.

    """
    # Get the parameters for the coarsest equitable partition of the graph; n
    # is the vector containing number of vertices in each block of the
    # partition and D_ij is the matrix containing the degree of any vertex in
    # block i relative to block j. p is the number of blocks in the partition.
    partition = coarsest_equitable_partition(graph)
    n, D = partition_parameters(graph, partition, as_matrices=True)
    p = len(n)
    # Permuting the parameters in an arbitrary fashion may produce parameters
    # for which there is no possible graph. For now, just stick with a random
    # graph on the same parameters.
    #
    ## Choose a random permutation matrix and permute the parameters.
    #P = random_permutation_matrix(p, seed)
    #n, D = P * n, P * D
    #
    # Get a random graph with those parameters.
    return random_graph_from_parameters(n, D, seed)
Exemple #3
0
def _cep_solver(G, H):
    """Solves the fractional graph isomorphism problem by comparing the
    coarsest equitable partitions of G and H.

    Returns a two-tuple whose left element is a Boolean representing whether
    the two graphs have a common coarsest partition and whose right element is
    the pair of partitions of the given graphs (in the same order as the inputs
    are given). If the left element is ``False``, the right element is
    ``None``, since there is no witness that the graphs are fractionally
    isomorphic.

    """
    partition1 = coarsest_equitable_partition(G)
    partition2 = coarsest_equitable_partition(H)
    result = are_common_partitions(G, partition1, H, partition2)
    witness = (partition1, partition2) if result else None
    return result, witness
Exemple #4
0
def _cep_solver(G, H):
    """Solves the fractional graph isomorphism problem by comparing the
    coarsest equitable partitions of G and H.

    Returns a two-tuple whose left element is a Boolean representing whether
    the two graphs have a common coarsest partition and whose right element is
    the pair of partitions of the given graphs (in the same order as the inputs
    are given). If the left element is ``False``, the right element is
    ``None``, since there is no witness that the graphs are fractionally
    isomorphic.

    """
    partition1 = coarsest_equitable_partition(G)
    partition2 = coarsest_equitable_partition(H)
    result = are_common_partitions(G, partition1, H, partition2)
    witness = (partition1, partition2) if result else None
    return result, witness
Exemple #5
0
def random_fractionally_isomorphic_graphs(graph, times=None, seed=None):
    """Returns an iterator that generates random graphs that are fractionally
    isomorphic to the specified graph.

    If `times` is specified, the iterator only generates that number of graphs
    before it terminates. If it is not specified, it will generate graphs
    forever!

    If `seed` is specified, it must be an integer provided as the seed to the
    pseudorandom number generator used to generate the graph.

    This function may return the same graph.

    """
    # Get the parameters for the coarsest equitable partition of the graph; n
    # is the vector containing number of vertices in each block of the
    # partition and D_ij is the matrix containing the degree of any vertex in
    # block i relative to block j. p is the number of blocks in the partition.
    partition = coarsest_equitable_partition(graph)
    n, D = partition_parameters(graph, partition, as_matrices=True)
    p = len(n)
    # This code is almost exactly the same as itertools.repeat(), except that
    # we execute the random_graph_from_parameters() function every time
    # through the loop.
    if times is None:
        while True:
            # Permuting the parameters in an arbitrary fashion may produce
            # parameters for which there is no possible graph. For now, just
            # stick with a random graph on the same parameters.
            #
            ## Choose a random permutation matrix and permute the parameters.
            #P = random_permutation_matrix(p, seed)
            #n_prime, D_prime = P * n, P * D
            #
            # Get a random graph with those parameters.
            yield random_graph_from_parameters(n, D, seed)
    else:
        for i in range(times):
            # (See the note on permutations above.)
            #
            # Get a random graph with those parameters.
            yield random_graph_from_parameters(n, D, seed)
Exemple #6
0
def random_fractionally_isomorphic_graphs(graph, times=None, seed=None):
    """Returns an iterator that generates random graphs that are fractionally
    isomorphic to the specified graph.

    If `times` is specified, the iterator only generates that number of graphs
    before it terminates. If it is not specified, it will generate graphs
    forever!

    If `seed` is specified, it must be an integer provided as the seed to the
    pseudorandom number generator used to generate the graph.

    This function may return the same graph.

    """
    # Get the parameters for the coarsest equitable partition of the graph; n
    # is the vector containing number of vertices in each block of the
    # partition and D_ij is the matrix containing the degree of any vertex in
    # block i relative to block j. p is the number of blocks in the partition.
    partition = coarsest_equitable_partition(graph)
    n, D = partition_parameters(graph, partition, as_matrices=True)
    p = len(n)
    # This code is almost exactly the same as itertools.repeat(), except that
    # we execute the random_graph_from_parameters() function every time
    # through the loop.
    if times is None:
        while True:
            # Permuting the parameters in an arbitrary fashion may produce
            # parameters for which there is no possible graph. For now, just
            # stick with a random graph on the same parameters.
            #
            ## Choose a random permutation matrix and permute the parameters.
            #P = random_permutation_matrix(p, seed)
            #n_prime, D_prime = P * n, P * D
            #
            # Get a random graph with those parameters.
            yield random_graph_from_parameters(n, D, seed)
    else:
        for i in range(times):
            # (See the note on permutations above.)
            #
            # Get a random graph with those parameters.
            yield random_graph_from_parameters(n, D, seed)
Exemple #7
0
def test_coarsest_equitable_partition():
    G = graph_from_file('data/graph1.txt')
    partition = coarsest_equitable_partition(G)
    block1 = frozenset(range(6))
    block2 = frozenset(range(6, 12))
    assert frozenset((block1, block2)) == partition