Changeset 7528:f45713981d79
- Timestamp:
- 12/02/07 17:50:47 (6 years ago)
- Branch:
- default
- Tags:
- 2.8.15.rc0
- Location:
- sage/graphs
- Files:
-
- 2 edited
-
graph.py (modified) (3 diffs)
-
graph_generators.py (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sage/graphs/graph.py
r7495 r7528 312 312 # inputs must be (di)graphs: 313 313 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)) 315 315 # DiGraphs are "larger" than Graphs: 316 316 g1_is_graph = isinstance(self, Graph) … … 4939 4939 4940 4940 """ 4941 if self.order() == 0: 4942 return True 4941 4943 import networkx 4942 4944 return networkx.component.is_connected(self._nxg) … … 6636 6638 6637 6639 """ 6640 if self.order() == 0: 6641 return True 6638 6642 import networkx 6639 6643 return networkx.component.is_connected(self._nxg.to_undirected()) -
sage/graphs/graph_generators.py
r7495 r7528 2662 2662 2663 2663 ################################################################################ 2664 # Graph s that are representatives of distinct isomorphism classes2664 # Graph Iterators 2665 2665 ################################################################################ 2666 2666 … … 2696 2696 sage: for G in graphs(3, augment='vertices'): 2697 2697 ... print G 2698 ...2699 2698 Graph on 0 vertices 2700 2699 Graph on 1 vertex … … 2709 2708 sage: for G in graphs(3): 2710 2709 ... print G 2711 ...2712 2710 Graph on 3 vertices 2713 2711 Graph on 3 vertices … … 2718 2716 sage: L = list(graphs(5, lambda G: G.size() <= 4)) 2719 2717 sage: len(L) 2720 1 32718 14 2721 2719 sage: graphs_list.show_graphs(L) # long time 2722 2720 … … 2735 2733 sage: L = list( graphs(8, lambda G: G.is_bipartite()) ) 2736 2734 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 2739 2748 REFERENCE: 2740 2749 Brendan D. McKay, Isomorph-Free Exhaustive generation. Journal of … … 2758 2767 else: 2759 2768 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 2760 2804 2761 2805 def canaug_traverse_vert(g, aut_gens, max_verts, property): … … 2846 2890 z = g.copy() 2847 2891 z.add_vertex(n) 2848 2892 if not property(z): 2893 continue 2849 2894 index = 0 2850 2895 while (1 << index) <= i: … … 2935 2980 sage: L = list(graphs(5, lambda G: G.size() <= 4)) 2936 2981 sage: len(L) 2937 1 32982 14 2938 2983 sage: graphs_list.show_graphs(L) # long time 2939 2984 … … 2941 2986 sage: L = list( graphs(7, lambda G: G.is_bipartite()) ) 2942 2987 sage: len(L) 2943 2 32988 29 2944 2989 2945 2990 """ … … 2952 2997 n_choose_2 = (n*(n-1))>>1 # >> 1 is just / 2 2953 2998 if g.size() < n_choose_2: 2954 2955 2999 # build a list representing C(g) - the edge to be added 2956 3000 # is one of n*(n-1)/2 choices … … 2960 3004 # union-find C(g) under Aut(g) 2961 3005 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) 2966 3025 if x < i: 2967 3026 children[i][j] = (x, y) … … 2978 3037 # find representatives of orbits of C(g) 2979 3038 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)) 2992 3043 for i, j in roots: 2993 3044 if g.has_edge(i, j): … … 2996 3047 z = g.copy() 2997 3048 z.add_edge(i, j) 2998 3049 if not property(z): 3050 continue 2999 3051 z_aut_gens, _, canonical_relabeling = search_tree(z, [z.vertices()], certify=True) 3000 3052 relabel_inverse = [0]*n 3001 for i in xrange(n):3002 relabel_inverse[canonical_relabeling[i ]] =i3053 for ii in xrange(n): 3054 relabel_inverse[canonical_relabeling[ii]] = ii 3003 3055 z_can = z.relabel(canonical_relabeling, inplace=False) 3004 3056 cut_edge_can = z_can.edges(labels=False, sort=True)[-1] … … 3008 3060 m_z = z.copy() 3009 3061 m_z.delete_edge(cut_edge) 3010 3011 3062 if m_z == g: 3012 3063 for a in canaug_traverse_edge(z, z_aut_gens, property): … … 3045 3096 for gen in aut_gens: 3046 3097 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]] 3049 3100 if new_perm not in seen_perms: 3050 3101 seen_perms.append(new_perm)
Note: See TracChangeset
for help on using the changeset viewer.
