Exemple #1
0
def benchmark():
    "Run few simple benchmarks for extend_hom()."
    logging.getLogger().setLevel(logging.DEBUG)

    from sage.graphs.graph_generators import graphs
    from sage.misc.sage_timeit import sage_timeit

    K6 = graphs.CompleteGraph(6)
    K2 = graphs.CompleteGraph(2)
    K7_6 = graphs.CompleteBipartiteGraph(7, 6)
    C16 = graphs.CycleGraph(16)
    C13 = graphs.CycleGraph(13)
    C5 = graphs.CycleGraph(5)
    eh = extend_hom

    logging.info(
        "All homs K6 -> K6: %s",
        sage_timeit("assert len(eh(K6, K6, limit=10000)) == 720",
                    locals(),
                    preparse=False))

    logging.info(
        "All homs K7,6 -> K2: %s",
        sage_timeit("assert len(eh(K7_6, K2, limit=10000)) == 2",
                    locals(),
                    preparse=False))

    logging.info(
        "All homs C13 -> C5: %s",
        sage_timeit("assert len(eh(C13, C5, limit=10000)) == 7150",
                    locals(),
                    preparse=False))
def test():
  "Some unit tests"

  from sage.graphs.graph_generators import graphs
  K2 = graphs.CompleteGraph(2)
  K4 = graphs.CompleteGraph(4)
  K24 = graphs.CompleteGraph(24)
  C4 = graphs.CycleGraph(4)
  C16 = graphs.CycleGraph(16)

  # parallel_extend_hom
  assert len(parallel_extend_hom(K4, K4, limit=100)) == 24
  assert parallel_extend_hom(K2, K4, partmap={0:0}, limit=10, onlycount=True) == 3
  assert len(parallel_extend_hom(C16, K2, limit=10)) == 2
  assert len(parallel_extend_hom(C16, K2, partmap={0:0, 2:1}, limit=10)) == 0

  # parallel_extend_hom giving partial mappings
  assert len(parallel_extend_hom(C16, C4, partmap={0:0}, order=[0,2,1], limit=100)) == 4
  assert len(parallel_extend_hom(C16, C4, partmap={8:0}, order=[0], limit=100)) == 4
  assert parallel_extend_hom(C16, C4, partmap={4:1}, order=[], limit=100) == [{4:1}]
  assert parallel_extend_hom(C16, C4, order=[], limit=100) == [{}]

  # differen branching parameters
  assert parallel_extend_hom(C4, K24, branchdepth=[0,0,1], limit=1000000, onlycount=True, check_automorphisms=1) == 11661
  assert parallel_extend_hom(C4, K24, branchdepth=[0,1,2,3], limit=1000000, onlycount=True, check_automorphisms=2) == 507
  assert (sorted(parallel_extend_hom(C4, K24, branchdepth=[1,0,2,3,2], limit=10000, check_automorphisms=14)) ==
      [{0: 1, 1: 0, 2: 1, 3: 0}, {0: 1, 1: 2, 2: 1, 3: 0}, {0: 2, 1: 0, 2: 1, 3: 0}, {0: 3, 1: 2, 2: 1, 3: 0}])

  log.info("All tests passed.")
Exemple #3
0
    def Cycle(self, n):
        """
        Sandpile on the cycle graph with `n` vertices.

        INPUT:

        -  ``n`` -- a non-negative integer

        OUTPUT:

        - Sandpile

        EXAMPLES::

            sage: s = sandpiles.Cycle(4)
            sage: s.edges()
            [(0, 1, 1),
             (0, 3, 1),
             (1, 0, 1),
             (1, 2, 1),
             (2, 1, 1),
             (2, 3, 1),
             (3, 0, 1),
             (3, 2, 1)]
        """
        return Sandpile(graphs.CycleGraph(n), 0)
Exemple #4
0
def test():
    "Run unit tests for the module."
    logging.getLogger().setLevel(logging.DEBUG)

    from sage.graphs.graph_generators import graphs
    K2 = graphs.CompleteGraph(2)
    K4 = graphs.CompleteGraph(4)
    K24 = graphs.CompleteGraph(24)
    C4 = graphs.CycleGraph(4)
    C16 = graphs.CycleGraph(16)

    # extend_hom
    assert len(extend_hom(K4, K4, limit=100)) == 24
    assert len(extend_hom(K2, K4, partmap={0: 0}, limit=10)) == 3
    assert len(extend_hom(C16, K2, limit=10)) == 2
    assert len(extend_hom(C16, K2, partmap={0: 0, 2: 1}, limit=10)) == 0

    # extend_hom giving partial mappings
    assert len(extend_hom(C16, C4, partmap={0: 0}, order=[0, 2, 1],
                          limit=100)) == 4
    assert len(extend_hom(C16, C4, partmap={8: 0}, order=[0], limit=100)) == 4
    assert extend_hom(C16, C4, partmap={4: 1}, order=[], limit=100) == [{4: 1}]
    assert extend_hom(C16, C4, order=[], limit=100) == [{}]

    # extend_hom only counting
    assert extend_hom(K2, K4, partmap={0: 0}, limit=10, onlycount=True) == 3
    # Following would probably segfault if actually allocating 1G entries
    assert extend_hom(K4, K4, limit=2**30, onlycount=True) == 24

    # extend_hom with check_automorphisms
    assert len(extend_hom(K4, K4, limit=100, check_automorphisms=1)) == 6
    assert len(extend_hom(K4, K4, limit=100, check_automorphisms=2)) == 2
    assert len(extend_hom(K4, K4, limit=100, check_automorphisms=3)) == 1
    assert len(extend_hom(K4, K4, limit=100, check_automorphisms=4)) == 1
    assert len(
        extend_hom(K2, K4, partmap={0: 0}, limit=10,
                   check_automorphisms=1)) == 1
    assert len(extend_hom(C16, K2, limit=10, check_automorphisms=1)) == 1
    assert len(
        extend_hom(C16,
                   K2,
                   partmap={
                       0: 0,
                       2: 1
                   },
                   limit=10,
                   check_automorphisms=2)) == 0
    # This might be slow
    assert extend_hom(K24,
                      K24,
                      limit=10,
                      check_automorphisms=42,
                      onlycount=True) == 1

    # find_hom_image
    K4c = find_hom_image(K4.disjoint_union(K4))
    assert K4.is_isomorphic(K4c)

    # find_core
    K4d = find_core(K4.disjoint_union(K4).disjoint_union(K4))
    assert K4.is_isomorphic(K4d)

    # is_hom
    for hom in extend_hom(C16, K2, limit=10):
        assert is_hom(C16, K2, hom)
    assert raises(lambda: is_hom(K2, K2, {0: 0}))  # not all vertices mapped
    assert raises(lambda: is_hom(K2, K2, {0: 4}))  # some vertices outside K2
    assert not is_hom(K2, K2, {0: 0, 1: 0})  # creates a loop

    logging.info("All tests passed.")