Changeset 7432:0aef5d76bc24


Ignore:
Timestamp:
11/27/07 22:39:42 (5 years ago)
Author:
Jason Grout <jason-sage@…>
Branch:
default
Message:

[mq]: butterfly-graph.patch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sage/graphs/graph_generators.py

    r7395 r7432  
    307307        return graph.Graph(G, pos=pos_dict, name="Bull Graph") 
    308308         
     309 
     310    def ButterflyGraph(self, n, vertices='strings'): 
     311        """ 
     312        Returns a n-dimensional butterfly graph.  The vertices consist 
     313        of pairs (v,i), where v is an n-dimensional tuple (vector) 
     314        with binary entries (or a string representation of such)  
     315        and i is an integer in [0..n].  A directed 
     316        edge goes from (v,i) to (w,i+1) if v and w are identical 
     317        except for possibly v[i] != w[i]. 
     318 
     319        A butterfly graph has (2^n)(n+1) vertices and n2^(n+1) edges. 
     320 
     321        INPUT: 
     322            vertices -- 'strings' (default) or 'vectors', specifying 
     323            whether the vertices are zero-one strings or actually 
     324            tuples over GF(2). 
     325         
     326        EXAMPLES: 
     327        sage: graphs.ButterflyGraph(2).edges(labels=False) 
     328        [(('00', 0), ('00', 1)), 
     329        (('00', 0), ('10', 1)), 
     330        (('00', 1), ('00', 2)), 
     331        (('00', 1), ('01', 2)), 
     332        (('01', 0), ('01', 1)), 
     333        (('01', 0), ('11', 1)), 
     334        (('01', 1), ('00', 2)), 
     335        (('01', 1), ('01', 2)), 
     336        (('10', 0), ('00', 1)), 
     337        (('10', 0), ('10', 1)), 
     338        (('10', 1), ('10', 2)), 
     339        (('10', 1), ('11', 2)), 
     340        (('11', 0), ('01', 1)), 
     341        (('11', 0), ('11', 1)), 
     342        (('11', 1), ('10', 2)), 
     343        (('11', 1), ('11', 2))] 
     344        sage: graphs.ButterflyGraph(2,vertices='vectors').edges(labels=False) 
     345        [(((0, 0), 0), ((0, 0), 1)), 
     346        (((0, 0), 0), ((1, 0), 1)), 
     347        (((0, 0), 1), ((0, 0), 2)), 
     348        (((0, 0), 1), ((0, 1), 2)), 
     349        (((0, 1), 0), ((0, 1), 1)), 
     350        (((0, 1), 0), ((1, 1), 1)), 
     351        (((0, 1), 1), ((0, 0), 2)), 
     352        (((0, 1), 1), ((0, 1), 2)), 
     353        (((1, 0), 0), ((0, 0), 1)), 
     354        (((1, 0), 0), ((1, 0), 1)), 
     355        (((1, 0), 1), ((1, 0), 2)), 
     356        (((1, 0), 1), ((1, 1), 2)), 
     357        (((1, 1), 0), ((0, 1), 1)), 
     358        (((1, 1), 0), ((1, 1), 1)), 
     359        (((1, 1), 1), ((1, 0), 2)), 
     360        (((1, 1), 1), ((1, 1), 2))] 
     361         
     362        """ 
     363        # We could switch to Sage integers to handle arbitrary n. 
     364        if vertices=='strings': 
     365            if n>=31: 
     366                raise NotImplementedError, "vertices='strings' is only valid for n<=30." 
     367            from sage.graphs.graph_fast import binary 
     368            butterfly = {} 
     369            for v in xrange(2**n): 
     370                for i in range(n): 
     371                    w = v 
     372                    w ^= (1 << i)   # push 1 to the left by i and xor with w 
     373                    bv = binary(v) 
     374                    bw = binary(w) 
     375                    # pad and reverse the strings 
     376                    padded_bv = ('0'*(n-len(bv))+bv)[::-1] 
     377                    padded_bw = ('0'*(n-len(bw))+bw)[::-1] 
     378                    butterfly[(padded_bv,i)]=[(padded_bv,i+1), (padded_bw,i+1)] 
     379        elif vertices=='vectors': 
     380            from sage.modules.free_module import VectorSpace 
     381            from sage.rings.finite_field import FiniteField 
     382            from copy import copy 
     383            butterfly = {} 
     384            for v in VectorSpace(FiniteField(2),n): 
     385                for i in xrange(n): 
     386                    w=copy(v) 
     387                    w[i] += 1 # Flip the ith bit 
     388                    # We must call tuple since vectors are mutable.  To obtain 
     389                    # a vector from the tuple t, just call vector(t). 
     390                    butterfly[(tuple(v),i)]=[(tuple(v),i+1), (tuple(w),i+1)] 
     391        else: 
     392            raise NotImplementedError, "vertices must be 'strings' or 'vectors'." 
     393        return graph.DiGraph(butterfly) 
    309394         
    310395    def CircularLadderGraph(self, n): 
Note: See TracChangeset for help on using the changeset viewer.