# Ticket #13862: trac_13862-cleaning_and_moving.patch

File trac_13862-cleaning_and_moving.patch, 30.8 KB (added by ncohen, 7 years ago)
• ## sage/graphs/generators/basic.py

# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1356480205 -3600
# Node ID bede57db4217aa72362b41bebde40684b320fe8b
Split graph_generators into several files -- cleaning, moving some more stuff

diff --git a/sage/graphs/generators/basic.py b/sage/graphs/generators/basic.py
 a Basic Graphs ================== circle/line embeddings should not be here ! :meth:LCFGraph  was in the list of basic graphs. """ ########################################################################### # from sage.graphs.graph import Graph from sage.graphs import graph from math import sin, cos, pi #################### # Helper functions # #################### def _circle_embedding(g, vertices, center=(0, 0), radius=1, shift=0): r""" Set some vertices on a circle in the embedding of a graph G. This method modifies the graph's embedding so that the vertices listed in vertices appear in this ordering on a circle of given radius and center. The shift parameter is actually a rotation of the circle. A value of shift=1 will replace in the drawing the i-th element of the list by the (i-1)-th. Non-integer values are admissible, and a value of \alpha corresponds to a rotation of the circle by an angle of \alpha 2\pi/n (where n is the number of vertices set on the circle). EXAMPLE:: sage: from sage.graphs.graph_generators import _circle_embedding sage: g = graphs.CycleGraph(5) sage: _circle_embedding(g, [0, 2, 4, 1, 3], radius=2, shift=.5) sage: g.show() """ c_x, c_y = center n = len(vertices) d = g.get_pos() if d is None: d = {} for i,v in enumerate(vertices): i += shift v_x = c_x + radius * cos(2*i*pi / n) v_y = c_y + radius * sin(2*i*pi / n) d[v] = (v_x, v_y) g.set_pos(d) def _line_embedding(g, vertices, first=(0, 0), last=(0, 1)): r""" Sets some vertices on a line in the embedding of a graph G. This method modifies the graph's embedding so that the vertices of vertices appear on a line, where the position of vertices[0] is the pair first and the position of vertices[-1] is last. The vertices are evenly spaced. EXAMPLE:: sage: from sage.graphs.graph_generators import _line_embedding sage: g = graphs.PathGraph(5) sage: _line_embedding(g, [0, 2, 4, 1, 3], first=(-1, -1), last=(1, 1)) sage: g.show() """ n = len(vertices) - 1. fx, fy = first dx = (last[0] - first[0])/n dy = (last[1] - first[1])/n d = g.get_pos() if d is None: d = {} for v in vertices: d[v] = (fx, fy) fx += dx fy += dy from sage.graphs.graph_plot import _circle_embedding, _line_embedding ####################################################################### #   Basic Structures
• ## sage/graphs/generators/families.py

diff --git a/sage/graphs/generators/families.py b/sage/graphs/generators/families.py
 a """ ########################################################################### # #           Copyright (C) 2006 Robert L. Miller from sage.graphs import graph from math import sin, cos, pi def HararyGraph( self, k, n ): r""" Returns the Harary graph on n vertices and connectivity k, where 2 \leq k < n. A k-connected graph G on n vertices requires the minimum degree \delta(G)\geq k, so the minimum number of edges G should have is \lceil kn/2\rceil. Harary graphs achieve this lower bound, that is, Harary graphs are minimal k-connected graphs on n vertices. The construction provided uses the method CirculantGraph.  For more details, see the book D. B. West, Introduction to Graph Theory, 2nd Edition, Prentice Hall, 2001, p. 150--151; or the MathWorld article on Harary graphs _. EXAMPLES: Harary graphs H_{k,n}:: sage: h = graphs.HararyGraph(5,9); h Harary graph 5, 9: Graph on 9 vertices sage: h.order() 9 sage: h.size() 23 sage: h.vertex_connectivity() 5 TESTS: Connectivity of some Harary graphs:: sage: n=10 sage: for k in range(2,n): ...       g = graphs.HararyGraph(k,n) ...       if k != g.vertex_connectivity(): ...          print "Connectivity of Harary graphs not satisfied." """ if k < 2: raise ValueError("Connectivity parameter k should be at least 2.") if k >= n: raise ValueError("Number of vertices n should be greater than k.") if k%2 == 0: G = self.CirculantGraph( n, range(1,k/2+1) ) else: if n%2 == 0: G = self.CirculantGraph( n, range(1,(k-1)/2+1) ) for i in range(n): G.add_edge( i, (i+n/2)%n ) else: G = self.HararyGraph( k-1, n ) for i in range((n-1)/2+1): G.add_edge( i, (i+(n-1)/2)%n ) G.name('Harary graph {0}, {1}'.format(k,n)) return G def DorogovtsevGoltsevMendesGraph(self, n): """ Construct the n-th generation of the Dorogovtsev-Goltsev-Mendes graph. EXAMPLE:: sage: G = graphs.DorogovtsevGoltsevMendesGraph(8) sage: G.size() 6561 REFERENCE: - [1] Dorogovtsev, S. N., Goltsev, A. V., and Mendes, J. F. F., Pseudofractal scale-free web, Phys. Rev. E 066122 (2002). """ import networkx return graph.Graph(networkx.dorogovtsev_goltsev_mendes_graph(n),\ name="Dorogovtsev-Goltsev-Mendes Graph, %d-th generation"%n) def IntervalGraph(self,intervals): r""" Returns the graph corresponding to the given intervals. An interval graph is built from a list (a_i,b_i)_{1\leq i \leq n} of intervals : to each interval of the list is associated one vertex, two vertices being adjacent if the two corresponding (closed) intervals intersect. INPUT: - intervals -- the list of pairs (a_i,b_i) defining the graph. .. NOTE:: * The vertices are named 0, 1, 2, and so on. The intervals used to create the graph are saved with the graph and can be recovered using get_vertex() or get_vertices(). * The intervals (a_i,b_i) need not verify a_i= 1.') from sage.graphs.graph_generators import _circle_embedding from sage.graphs.graph_generators import GraphGenerators from sage.graphs.graph_plot import _circle_embedding from sage.graphs.graph_plot import GraphGenerators # Creating the Balanced tree, which contains most edges already g = GraphGenerators().BalancedTree(2,k-1)
• ## sage/graphs/generators/platonic_solids.py

diff --git a/sage/graphs/generators/platonic_solids.py b/sage/graphs/generators/platonic_solids.py
 a Platonic solids ================== circle/line embeddings should not be here ! Platonic solids --------------- from sage.graphs.graph import Graph from sage.graphs import graph from math import sin, cos, pi #################### # Helper functions # #################### def _circle_embedding(g, vertices, center=(0, 0), radius=1, shift=0): r""" Set some vertices on a circle in the embedding of a graph G. This method modifies the graph's embedding so that the vertices listed in vertices appear in this ordering on a circle of given radius and center. The shift parameter is actually a rotation of the circle. A value of shift=1 will replace in the drawing the i-th element of the list by the (i-1)-th. Non-integer values are admissible, and a value of \alpha corresponds to a rotation of the circle by an angle of \alpha 2\pi/n (where n is the number of vertices set on the circle). EXAMPLE:: sage: from sage.graphs.graph_generators import _circle_embedding sage: g = graphs.CycleGraph(5) sage: _circle_embedding(g, [0, 2, 4, 1, 3], radius=2, shift=.5) sage: g.show() """ c_x, c_y = center n = len(vertices) d = g.get_pos() if d is None: d = {} for i,v in enumerate(vertices): i += shift v_x = c_x + radius * cos(2*i*pi / n) v_y = c_y + radius * sin(2*i*pi / n) d[v] = (v_x, v_y) g.set_pos(d) def _line_embedding(g, vertices, first=(0, 0), last=(0, 1)): r""" Sets some vertices on a line in the embedding of a graph G. This method modifies the graph's embedding so that the vertices of vertices appear on a line, where the position of vertices[0] is the pair first and the position of vertices[-1] is last. The vertices are evenly spaced. EXAMPLE:: sage: from sage.graphs.graph_generators import _line_embedding sage: g = graphs.PathGraph(5) sage: _line_embedding(g, [0, 2, 4, 1, 3], first=(-1, -1), last=(1, 1)) sage: g.show() """ n = len(vertices) - 1. fx, fy = first dx = (last[0] - first[0])/n dy = (last[1] - first[1])/n d = g.get_pos() if d is None: d = {} for v in vertices: d[v] = (fx, fy) fx += dx fy += dy from sage.graphs.graph_plot import _circle_embedding, _line_embedding ################################################################################ #   Platonic Solids
• ## sage/graphs/generators/random.py

diff --git a/sage/graphs/generators/random.py b/sage/graphs/generators/random.py
 a Random Graphs ================== circle/line embeddings should not be here ! Where the hell should InterverGraph be ? Is it listed at the right place ? """ ########################################################################### # from sage.graphs import graph from math import sin, cos, pi from sage.misc.randstate import current_randstate #################### # Helper functions # #################### def _circle_embedding(g, vertices, center=(0, 0), radius=1, shift=0): r""" Set some vertices on a circle in the embedding of a graph G. This method modifies the graph's embedding so that the vertices listed in vertices appear in this ordering on a circle of given radius and center. The shift parameter is actually a rotation of the circle. A value of shift=1 will replace in the drawing the i-th element of the list by the (i-1)-th. Non-integer values are admissible, and a value of \alpha corresponds to a rotation of the circle by an angle of \alpha 2\pi/n (where n is the number of vertices set on the circle). EXAMPLE:: sage: from sage.graphs.graph_generators import _circle_embedding sage: g = graphs.CycleGraph(5) sage: _circle_embedding(g, [0, 2, 4, 1, 3], radius=2, shift=.5) sage: g.show() """ c_x, c_y = center n = len(vertices) d = g.get_pos() if d is None: d = {} for i,v in enumerate(vertices): i += shift v_x = c_x + radius * cos(2*i*pi / n) v_y = c_y + radius * sin(2*i*pi / n) d[v] = (v_x, v_y) g.set_pos(d) def _line_embedding(g, vertices, first=(0, 0), last=(0, 1)): r""" Sets some vertices on a line in the embedding of a graph G. This method modifies the graph's embedding so that the vertices of vertices appear on a line, where the position of vertices[0] is the pair first and the position of vertices[-1] is last. The vertices are evenly spaced. EXAMPLE:: sage: from sage.graphs.graph_generators import _line_embedding sage: g = graphs.PathGraph(5) sage: _line_embedding(g, [0, 2, 4, 1, 3], first=(-1, -1), last=(1, 1)) sage: g.show() """ n = len(vertices) - 1. fx, fy = first dx = (last[0] - first[0])/n dy = (last[1] - first[1])/n d = g.get_pos() if d is None: d = {} for v in vertices: d[v] = (fx, fy) fx += dx fy += dy ################################################################################ #   Random Graphs
• ## sage/graphs/generators/smallgraphs.py

diff --git a/sage/graphs/generators/smallgraphs.py b/sage/graphs/generators/smallgraphs.py
 a ================== Add naury graphs to the list Harary graph is not a small graph ! circle/line embeddings should not be here ! Virer GraphGenerators() from the code """ from sage.graphs.graph import Graph from sage.graphs import graph from math import sin, cos, pi #################### # Helper functions # #################### def _circle_embedding(g, vertices, center=(0, 0), radius=1, shift=0): r""" Set some vertices on a circle in the embedding of a graph G. This method modifies the graph's embedding so that the vertices listed in vertices appear in this ordering on a circle of given radius and center. The shift parameter is actually a rotation of the circle. A value of shift=1 will replace in the drawing the i-th element of the list by the (i-1)-th. Non-integer values are admissible, and a value of \alpha corresponds to a rotation of the circle by an angle of \alpha 2\pi/n (where n is the number of vertices set on the circle). EXAMPLE:: sage: from sage.graphs.graph_generators import _circle_embedding sage: g = graphs.CycleGraph(5) sage: _circle_embedding(g, [0, 2, 4, 1, 3], radius=2, shift=.5) sage: g.show() """ c_x, c_y = center n = len(vertices) d = g.get_pos() if d is None: d = {} for i,v in enumerate(vertices): i += shift v_x = c_x + radius * cos(2*i*pi / n) v_y = c_y + radius * sin(2*i*pi / n) d[v] = (v_x, v_y) g.set_pos(d) def _line_embedding(g, vertices, first=(0, 0), last=(0, 1)): r""" Sets some vertices on a line in the embedding of a graph G. This method modifies the graph's embedding so that the vertices of vertices appear on a line, where the position of vertices[0] is the pair first and the position of vertices[-1] is last. The vertices are evenly spaced. EXAMPLE:: sage: from sage.graphs.graph_generators import _line_embedding sage: g = graphs.PathGraph(5) sage: _line_embedding(g, [0, 2, 4, 1, 3], first=(-1, -1), last=(1, 1)) sage: g.show() """ n = len(vertices) - 1. fx, fy = first dx = (last[0] - first[0])/n dy = (last[1] - first[1])/n d = g.get_pos() if d is None: d = {} for v in vertices: d[v] = (fx, fy) fx += dx fy += dy from sage.graphs.graph_plot import _circle_embedding, _line_embedding ####################################################################### #   Named Graphs ####################################################################### def HararyGraph( self, k, n ): r""" Returns the Harary graph on n vertices and connectivity k, where 2 \leq k < n. A k-connected graph G on n vertices requires the minimum degree \delta(G)\geq k, so the minimum number of edges G should have is \lceil kn/2\rceil. Harary graphs achieve this lower bound, that is, Harary graphs are minimal k-connected graphs on n vertices. The construction provided uses the method CirculantGraph.  For more details, see the book D. B. West, Introduction to Graph Theory, 2nd Edition, Prentice Hall, 2001, p. 150--151; or the MathWorld article on Harary graphs _. EXAMPLES: Harary graphs H_{k,n}:: sage: h = graphs.HararyGraph(5,9); h Harary graph 5, 9: Graph on 9 vertices sage: h.order() 9 sage: h.size() 23 sage: h.vertex_connectivity() 5 TESTS: Connectivity of some Harary graphs:: sage: n=10 sage: for k in range(2,n): ...       g = graphs.HararyGraph(k,n) ...       if k != g.vertex_connectivity(): ...          print "Connectivity of Harary graphs not satisfied." """ if k < 2: raise ValueError("Connectivity parameter k should be at least 2.") if k >= n: raise ValueError("Number of vertices n should be greater than k.") if k%2 == 0: G = self.CirculantGraph( n, range(1,k/2+1) ) else: if n%2 == 0: G = self.CirculantGraph( n, range(1,(k-1)/2+1) ) for i in range(n): G.add_edge( i, (i+n/2)%n ) else: G = self.HararyGraph( k-1, n ) for i in range((n-1)/2+1): G.add_edge( i, (i+(n-1)/2)%n ) G.name('Harary graph {0}, {1}'.format(k,n)) return G def HarriesGraph(self, embedding=1): r""" Returns the Harries Graph.
• ## sage/graphs/graph_generators.py

diff --git a/sage/graphs/graph_generators.py b/sage/graphs/graph_generators.py
 a # -*- coding: utf-8 -*- r""" Common graphs - :meth:HouseXGraph  - :meth:KrackhardtKiteGraph  - :meth:LadderGraph  - :meth:LCFGraph  - :meth:LollipopGraph  - :meth:PathGraph  - :meth:StarGraph  - :meth:McGeeGraph  - :meth:MoebiusKantorGraph  - :meth:MoserSpindle  - :meth:NauruGraph  - :meth:PappusGraph  - :meth:PetersenGraph  - :meth:ShrikhandeGraph  - :meth:GeneralizedPetersenGraph  - :meth:HanoiTowerGraph  - :meth:HyperStarGraph  - :meth:IntervalGraph  - :meth:KneserGraph  - :meth:LCFGraph  - :meth:MycielskiGraph  of Algorithms*, Volume 26, Issue 2, February 1998, pages 306-324. """ ################################################################################ #   Pseudofractal Graphs ################################################################################ def DorogovtsevGoltsevMendesGraph(self, n): """ Construct the n-th generation of the Dorogovtsev-Goltsev-Mendes graph. EXAMPLE:: sage: G = graphs.DorogovtsevGoltsevMendesGraph(8) sage: G.size() 6561 REFERENCE: - [1] Dorogovtsev, S. N., Goltsev, A. V., and Mendes, J. F. F., Pseudofractal scale-free web, Phys. Rev. E 066122 (2002). """ import networkx return graph.Graph(networkx.dorogovtsev_goltsev_mendes_graph(n),\ name="Dorogovtsev-Goltsev-Mendes Graph, %d-th generation"%n) def IntervalGraph(self,intervals): r""" Returns the graph corresponding to the given intervals. An interval graph is built from a list (a_i,b_i)_{1\leq i \leq n} of intervals : to each interval of the list is associated one vertex, two vertices being adjacent if the two corresponding (closed) intervals intersect. INPUT: - intervals -- the list of pairs (a_i,b_i) defining the graph. .. NOTE:: * The vertices are named 0, 1, 2, and so on. The intervals used to create the graph are saved with the graph and can be recovered using get_vertex() or get_vertices(). * The intervals (a_i,b_i) need not verify a_i
• ## sage/graphs/graph_plot.py

diff --git a/sage/graphs/graph_plot.py b/sage/graphs/graph_plot.py
 a x = ox obstruction[y] = x+1 continue t = C.pop() pt = parent[t] ct = [u for u in T.neighbors(t) if u != pt] for c in ct: parent[c] = t children[t] = ct stack.append([c for c in ct]) stick.append(t) return pos #################### # Helper functions # #################### def _circle_embedding(g, vertices, center=(0, 0), radius=1, shift=0): r""" Set some vertices on a circle in the embedding of a graph G. This method modifies the graph's embedding so that the vertices listed in vertices appear in this ordering on a circle of given radius and center. The shift parameter is actually a rotation of the circle. A value of shift=1 will replace in the drawing the i-th element of the list by the (i-1)-th. Non-integer values are admissible, and a value of \alpha corresponds to a rotation of the circle by an angle of \alpha 2\pi/n (where n is the number of vertices set on the circle). EXAMPLE:: sage: from sage.graphs.graph_plot import _circle_embedding sage: g = graphs.CycleGraph(5) sage: _circle_embedding(g, [0, 2, 4, 1, 3], radius=2, shift=.5) sage: g.show() """ c_x, c_y = center n = len(vertices) d = g.get_pos() if d is None: d = {} for i,v in enumerate(vertices): i += shift v_x = c_x + radius * cos(2*i*pi / n) v_y = c_y + radius * sin(2*i*pi / n) d[v] = (v_x, v_y) g.set_pos(d) def _line_embedding(g, vertices, first=(0, 0), last=(0, 1)): r""" Sets some vertices on a line in the embedding of a graph G. This method modifies the graph's embedding so that the vertices of vertices appear on a line, where the position of vertices[0] is the pair first and the position of vertices[-1] is last. The vertices are evenly spaced. EXAMPLE:: sage: from sage.graphs.graph_plot import _line_embedding sage: g = graphs.PathGraph(5) sage: _line_embedding(g, [0, 2, 4, 1, 3], first=(-1, -1), last=(1, 1)) sage: g.show() """ n = len(vertices) - 1. fx, fy = first dx = (last[0] - first[0])/n dy = (last[1] - first[1])/n d = g.get_pos() if d is None: d = {} for v in vertices: d[v] = (fx, fy) fx += dx fy += dy