Changeset 7528:f45713981d79


Ignore:
Timestamp:
12/02/07 17:50:47 (6 years ago)
Author:
R. L. Miller <rlmillster@…>
Branch:
default
Tags:
2.8.15.rc0
Message:

generate trees, fix bugs

Location:
sage/graphs
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sage/graphs/graph.py

    r7495 r7528  
    312312        # inputs must be (di)graphs: 
    313313        if not isinstance(other, GenericGraph): 
    314             raise TypeError("Input (%s) must be a graph."%str(other)) 
     314            raise TypeError("Cannot compare graph to non-graph (%s)."%str(other)) 
    315315        # DiGraphs are "larger" than Graphs: 
    316316        g1_is_graph = isinstance(self, Graph) 
     
    49394939 
    49404940        """ 
     4941        if self.order() == 0: 
     4942            return True 
    49414943        import networkx 
    49424944        return networkx.component.is_connected(self._nxg) 
     
    66366638 
    66376639        """ 
     6640        if self.order() == 0: 
     6641            return True 
    66386642        import networkx 
    66396643        return networkx.component.is_connected(self._nxg.to_undirected()) 
  • sage/graphs/graph_generators.py

    r7495 r7528  
    26622662 
    26632663################################################################################ 
    2664 #   Graphs that are representatives of distinct isomorphism classes 
     2664#   Graph Iterators 
    26652665################################################################################ 
    26662666     
     
    26962696            sage: for G in graphs(3, augment='vertices'): 
    26972697            ...    print G 
    2698             ... 
    26992698            Graph on 0 vertices 
    27002699            Graph on 1 vertex 
     
    27092708            sage: for G in graphs(3): 
    27102709            ...    print G 
    2711             ... 
    27122710            Graph on 3 vertices 
    27132711            Graph on 3 vertices 
     
    27182716            sage: L = list(graphs(5, lambda G: G.size() <= 4)) 
    27192717            sage: len(L) 
    2720             13 
     2718            14 
    27212719            sage: graphs_list.show_graphs(L)    # long time 
    27222720         
     
    27352733            sage: L = list( graphs(8, lambda G: G.is_bipartite()) ) 
    27362734            sage: len(L) 
    2737             38 
    2738          
     2735            142 
     2736         
     2737        Sloane A000088: 
     2738            sage: for i in range(0, 7): 
     2739            ...    print len(list(graphs(i))) 
     2740            1 
     2741            1 
     2742            2 
     2743            4 
     2744            11 
     2745            34 
     2746            156 
     2747 
    27392748        REFERENCE: 
    27402749            Brendan D. McKay, Isomorph-Free Exhaustive generation. Journal of 
     
    27582767        else: 
    27592768            raise NotImplementedError() 
     2769 
     2770    def trees(self, vertices, augment='edges'): 
     2771        """ 
     2772        Accesses the generator of trees (graphs without cycles). Iterates over 
     2773        distinct, exhaustive representatives. 
     2774 
     2775        INPUT: 
     2776            vertices -- natural number 
     2777            augment -- choices: 
     2778                'vertices' -- augments by adding a vertex, and edges incident 
     2779                    to that vertex. 
     2780                    In this case, all trees on up to n=vertices are generated. 
     2781                'edges' -- augments a fixed number of vertices by adding one edge 
     2782                    In this case, all trees on exactly n=vertices are generated. 
     2783 
     2784        EXAMPLES: 
     2785        Sloane A000055: 
     2786            sage: for i in range(0, 10): 
     2787            ...    print len(list(graphs.trees(i))) 
     2788            1 
     2789            1 
     2790            1 
     2791            1 
     2792            2 
     2793            3 
     2794            6 
     2795            11 
     2796            23 
     2797            47 
     2798 
     2799        """ 
     2800        is_tree = lambda g: g.transitive_reduction() == g 
     2801        for g in self(vertices=vertices, property=is_tree, augment=augment): 
     2802            if g.is_connected(): 
     2803                yield g 
    27602804 
    27612805def canaug_traverse_vert(g, aut_gens, max_verts, property): 
     
    28462890            z = g.copy() 
    28472891            z.add_vertex(n) 
    2848              
     2892            if not property(z): 
     2893                continue 
    28492894            index = 0 
    28502895            while (1 << index) <= i: 
     
    29352980        sage: L = list(graphs(5, lambda G: G.size() <= 4)) 
    29362981        sage: len(L) 
    2937         13 
     2982        14 
    29382983        sage: graphs_list.show_graphs(L)              # long time 
    29392984     
     
    29412986        sage: L = list( graphs(7, lambda G: G.is_bipartite()) ) 
    29422987        sage: len(L) 
    2943         23     
     2988        29 
    29442989 
    29452990    """ 
     
    29522997    n_choose_2 = (n*(n-1))>>1 # >> 1 is just / 2 
    29532998    if g.size() < n_choose_2: 
    2954          
    29552999        # build a list representing C(g) - the edge to be added 
    29563000        # is one of n*(n-1)/2 choices 
     
    29603004        # union-find C(g) under Aut(g) 
    29613005        for gen in aut_gens: 
    2962             for i in xrange(n): 
    2963                 for j in xrange(i): # i > j 
    2964                     if children[i][j] == (i,j): 
    2965                         y, x = sorted([gen[i], gen[j]]) 
     3006            for iii in xrange(n): 
     3007                for jjj in xrange(iii): # iii > jjj 
     3008                    i, j = iii, jjj 
     3009                    y, x = sorted([gen[i], gen[j]]) 
     3010                    if children[i][j] != children[x][y]: 
     3011                        x_val, y_val = x, y 
     3012                        i_val, j_val = i, j 
     3013                        while (x_val, y_val) != children[x_val][y_val]: 
     3014                            y_val, x_val = sorted(children[x_val][y_val]) 
     3015                        while (i_val, j_val) != children[i_val][j_val]: 
     3016                            j_val, i_val = sorted(children[i_val][j_val]) 
     3017                        while (x, y) != (x_val, y_val): 
     3018                            xx, yy = x, y 
     3019                            x, y = children[x][y] 
     3020                            children[xx][yy] = (x_val, y_val) 
     3021                        while (i, j) != (i_val, j_val): 
     3022                            ii, jj = i, j 
     3023                            i, j = children[i][j] 
     3024                            children[ii][jj] = (i_val, j_val) 
    29663025                        if x < i: 
    29673026                            children[i][j] = (x, y) 
     
    29783037        # find representatives of orbits of C(g) 
    29793038        roots = [] 
    2980         found_roots = 0 
    2981         i = 0; j = 1 
    2982         while found_roots < num_roots: 
    2983             if children[j][i] == (j, i): 
    2984                 found_roots += 1 
    2985                 roots.append((i, j)) 
    2986             if j < n - 1: 
    2987                 j += 1 
    2988             elif i < j: # j == n-1 
    2989                 i += 1 
    2990                 j = i + 1 
    2991          
     3039        for i in range(n): 
     3040            for j in range(i): 
     3041                if children[i][j] == (i, j): 
     3042                    roots.append((i,j)) 
    29923043        for i, j in roots: 
    29933044            if g.has_edge(i, j): 
     
    29963047            z = g.copy() 
    29973048            z.add_edge(i, j) 
    2998  
     3049            if not property(z): 
     3050                continue 
    29993051            z_aut_gens, _, canonical_relabeling = search_tree(z, [z.vertices()], certify=True) 
    30003052            relabel_inverse = [0]*n 
    3001             for i in xrange(n): 
    3002                 relabel_inverse[canonical_relabeling[i]] = i 
     3053            for ii in xrange(n): 
     3054                relabel_inverse[canonical_relabeling[ii]] = ii 
    30033055            z_can = z.relabel(canonical_relabeling, inplace=False) 
    30043056            cut_edge_can = z_can.edges(labels=False, sort=True)[-1] 
     
    30083060            m_z = z.copy() 
    30093061            m_z.delete_edge(cut_edge) 
    3010  
    30113062            if m_z == g: 
    30123063                for a in canaug_traverse_edge(z, z_aut_gens, property): 
     
    30453096        for gen in aut_gens: 
    30463097            new_perm = copy(perm) 
    3047             for i in xrange(n): 
    3048                 new_perm[i] = gen[perm[i]] 
     3098            for ii in xrange(n): 
     3099                new_perm[ii] = gen[perm[ii]] 
    30493100            if new_perm not in seen_perms: 
    30503101                seen_perms.append(new_perm) 
Note: See TracChangeset for help on using the changeset viewer.