Ticket #8284: trac_8284.patch
File trac_8284.patch, 5.3 KB (added by , 11 years ago) 


sage/graphs/generic_graph.py
# HG changeset patch # User Nathann Cohen <nathann.cohen@gmail.com> # Date 1275999583 7200 # Node ID 1f6e22e68ddb753517cb9ad54ee58fa3e9de8423 # Parent 556bb66e4c6dbb92a4ee37c1750d82a5c6298eeb trac 8284  IntervalGraph generator and a bug in is_chordal diff r 556bb66e4c6d r 1f6e22e68ddb sage/graphs/generic_graph.py
a b 7091 7091 sage: g.is_chordal() 7092 7092 True 7093 7093 7094 The disjoint union of chordal graphs is still chordal:: 7095 7096 sage: (2*g).is_chordal() 7097 True 7098 7099 7094 7100 Of course, the Petersen Graph is not chordal as it has girth 5 :: 7095 7101 7096 7102 sage: g = graphs.PetersenGraph() … … 7111 7117 Pacific J. Math 1965 7112 7118 Vol. 15, number 3, pages 835855 7113 7119 """ 7120 7121 if not self.is_connected(): 7122 for gg in self.connected_components_subgraphs(): 7123 if not gg.is_chordal(): 7124 return False 7125 7126 return True 7127 7114 7128 7115 7129 peo,t_peo = self.lex_BFS(tree=True) 7116 7130 … … 8777 8791 if w not in seen: 8778 8792 queue.append((w, d+1)) 8779 8793 8780 def lex_BFS(self,reverse=False,tree=False ):8794 def lex_BFS(self,reverse=False,tree=False, initial_vertex = None): 8781 8795 r""" 8782 8796 Performs a Lex BFS on the graph. 8783 8797 … … 8789 8803 8790 8804 INPUT: 8791 8805 8792  ``reverse`` ( boolean)  whether to return the vertices8806  ``reverse`` (boolean)  whether to return the vertices 8793 8807 in discovery order, or the reverse. 8808 8809 ``False`` by default. 8810 8811  ``tree`` (boolean)  whether to return the discovery 8812 directed tree (each vertex being linked to the one that 8813 saw it for the first time) 8814 8815 ``False`` by default. 8816 8817  ``initial_vertex``  the first vertex to consider. 8818 8819 ``None`` by default. 8794 8820 8795 8821 ALGORITHM: 8796 8822 … … 8821 8847 sage: g = graphs.PathGraph(3).lexicographic_product(graphs.CompleteGraph(2)) 8822 8848 sage: g.lex_BFS(reverse=True) 8823 8849 [(2, 1), (2, 0), (1, 1), (1, 0), (0, 1), (0, 0)] 8850 8851 8852 And the vertices at the end of the tree of discovery are, for 8853 chordal graphs, simplicial vertices (their neighborhood is 8854 a complete graph):: 8855 8856 sage: g = graphs.ClawGraph().lexicographic_product(graphs.CompleteGraph(2)) 8857 sage: v = g.lex_BFS()[1] 8858 sage: peo, tree = g.lex_BFS(initial_vertex = v, tree=True) 8859 sage: leaves = [v for v in tree if tree.in_degree(v) ==0] 8860 sage: all([g.subgraph(g.neighbors(v)).is_clique() for v in leaves]) 8861 True 8862 8824 8863 """ 8825 8864 id_inv = dict([(i,v) for (v,i) in zip(self.vertices(),range(self.order()))]) 8826 8865 code = [[] for i in range(self.order())] … … 8833 8872 pred = [1]*self.order() 8834 8873 8835 8874 add_element = (lambda y:value.append(id_inv[y])) if not reverse else (lambda y: value.insert(0,id_inv[y])) 8875 8876 # Should we take care of the first vertex we pick ? 8877 first = True if initial_vertex is not None else False 8878 8836 8879 8837 8880 while vertices: 8838 v = max(vertices,key=l) 8881 8882 if not first: 8883 v = max(vertices,key=l) 8884 else: 8885 v = self.vertices().index(initial_vertex) 8886 first = False 8887 8839 8888 vertices.remove(v) 8840 8889 vector = m.column(v) 8841 8890 for i in vertices: 
sage/graphs/graph_generators.py
diff r 556bb66e4c6d r 1f6e22e68ddb sage/graphs/graph_generators.py
a b 3793 3793 from sage.misc.prandom import random 3794 3794 3795 3795 intervals = [tuple(sorted((random(), random()))) for i in range(n)] 3796 intervals.sort() 3796 3797 return self.IntervalGraph(intervals) 3798 3799 def IntervalGraph(self,intervals): 3800 r""" 3801 Returns the graph corresponding to the given intervals. 3802 3803 An interval graph is built from a list `(a_i,b_i)_{1\leq i \leq n}` 3804 of intervals : to each interval of the list is associated one 3805 vertex, two vertices being adjacent if the two corresponding 3806 (closed) intervals intersect. 3807 3808 INPUT: 3809 3810  ``intervals``  the list of pairs `(a_i,b_i)` 3811 defining the graph. 3812 3813 NOTE: 3814 3815 The intervals `(a_i,b_i)` must verify `a_i<b_i`. 3816 3817 EXAMPLE: 3818 3819 The following line creates the sequence of intervals 3820 `(i, i+2)` for i in `[0, ..., 8]`:: 3821 3822 sage: intervals = [(i,i+2) for i in range(9)] 3823 3824 In the corresponding graph...:: 3825 3826 sage: g = graphs.IntervalGraph(intervals) 3827 sage: sorted(g.neighbors((3,5))) 3828 [(1, 3), (2, 4), (4, 6), (5, 7)] 3829 3830 And the clique number is as expected :: 3831 3832 sage: g.clique_number() 3833 3 3834 """ 3835 3836 intervals = sorted(intervals) 3837 3838 3797 3839 edges = [] 3798 3840 while intervals: 3799 x = intervals.pop( )3841 x = intervals.pop(0) 3800 3842 for y in intervals: 3801 3843 if y[0] <= x[1]: 3802 3844 edges.append((x,y))