source: sage/graphs/graph_database.py @ 5672:0d6dc2988a58

Revision 5672:0d6dc2988a58, 153.4 KB checked in by Robert L Miller <rlm@…>, 6 years ago (diff)

default to see whether notebook mode or not for graph database show function

Line 
1r"""
2Graph Database Module
3
4INFO:
5
6    This module implements classes (GraphDatabase, GraphQuery, GenericGraphQuery)
7    for interfacing with the sqlite database graphs.db.
8
9    The GraphDatabase class interfaces with the sqlite database graphs.db. 
10    It is an immutable database that inherits from SQLDatabase (see
11    sage.databases.database.py).  The display functions and get_list create
12    their own queries, but it is also possible to query the database by
13    constructing either a GenericSQLQuery or a SQLQuery.
14   
15    The database contains all unlabeled graphs with 7 or fewer nodes. 
16    This class will also interface with the optional database package
17    containing all unlabeled graphs with 8 or fewer nodes. 
18    The database(s) consists of five tables, and has the structure given
19    by the skeleton:
20    \begin{verbatim}
21    {'aut_grp': {'aut_grp_size': {'index': True,
22                                    'primary_key': False,
23                                    'sql': 'INTEGER'},
24                  'edge_transitive': {'index': True,
25                                       'primary_key': False,
26                                       'sql': 'BOOLEAN'},
27                  'graph_id': {'index': False,
28                                'primary_key': False,
29                                'sql': 'INTEGER'},
30                  'num_fixed_points': {'index': True,
31                                        'primary_key': False,
32                                        'sql': 'INTEGER'},
33                  'num_orbits': {'index': True,
34                                  'primary_key': False,
35                                  'sql': 'INTEGER'},
36                  'vertex_transitive': {'index': True,
37                                         'primary_key': False,
38                                         'sql': 'BOOLEAN'}},
39     'degrees': {'average_degree': {'index': True,
40                                      'primary_key': False,
41                                      'sql': 'REAL'},
42                  'degree_sequence': {'index': False,
43                                       'primary_key': False,
44                                       'sql': 'INTEGER'},
45                  'degrees_sd': {'index': True,
46                                  'primary_key': False,
47                                  'sql': 'REAL'},
48                  'graph_id': {'index': False,
49                                'primary_key': False,
50                                'sql': 'INTEGER'},
51                  'max_degree': {'index': True,
52                                  'primary_key': False,
53                                  'sql': 'INTEGER'},
54                  'min_degree': {'index': True,
55                                  'primary_key': False,
56                                  'sql': 'INTEGER'},
57                  'regular': {'index': True,
58                               'primary_key': False,
59                               'sql': 'BOOLEAN'}},
60     'graph_data': {'complement_graph6': {'index': True,
61                                            'primary_key': False,
62                                            'sql': 'TEXT'},
63                     'eulerian': {'index': True,
64                                   'primary_key': False,
65                                   'sql': 'BOOLEAN'},
66                     'graph6': {'index': True,
67                                 'primary_key': False,
68                                 'sql': 'TEXT'},
69                     'graph_id': {'index': True,
70                                   'primary_key': False,
71                                   'sql': 'INTEGER'},
72                     'lovasz_number': {'index': True,
73                                        'primary_key': False,
74                                        'sql': 'REAL'},
75                     'num_cycles': {'index': True,
76                                     'primary_key': False,
77                                     'sql': 'INTEGER'},
78                     'num_edges': {'index': True,
79                                    'primary_key': False,
80                                    'sql': 'INTEGER'},
81                     'num_hamiltonian_cycles': {'index': True,
82                                                 'primary_key': False,
83                                                 'sql': 'INTEGER'},
84                     'num_vertices': {'index': True,
85                                       'primary_key': False,
86                                       'sql': 'INTEGER'},
87                     'perfect': {'index': True,
88                                  'primary_key': False,
89                                  'sql': 'BOOLEAN'},
90                     'planar': {'index': True,
91                                 'primary_key': False,
92                                 'sql': 'BOOLEAN'}},
93     'misc': {'clique_number': {'index': True,
94                                  'primary_key': False,
95                                  'sql': 'INTEGER'},
96               'diameter': {'index': True,
97                             'primary_key': False,
98                             'sql': 'INTEGER'},
99               'edge_connectivity': {'index': True,
100                                      'primary_key': False,
101                                      'sql': 'BOOLEAN'},
102               'girth': {'index': True, 'primary_key': False, 'sql': 'INTEGER'},
103               'graph_id': {'index': False,
104                             'primary_key': False,
105                             'sql': 'INTEGER'},
106               'independence_number': {'index': True,
107                                        'primary_key': False,
108                                        'sql': 'INTEGER'},
109               'induced_subgraphs': {'index': True,
110                                      'primary_key': False,
111                                      'sql': 'TEXT'},
112               'min_vertex_cover_size': {'index': True,
113                                          'primary_key': False,
114                                          'sql': 'INTEGER'},
115               'num_components': {'index': True,
116                                   'primary_key': False,
117                                   'sql': 'INTEGER'},
118               'num_cut_vertices': {'index': True,
119                                     'primary_key': False,
120                                     'sql': 'INTEGER'},
121               'num_spanning_trees': {'index': True,
122                                       'primary_key': False,
123                                       'sql': 'INTEGER'},
124               'radius': {'index': True,
125                           'primary_key': False,
126                           'sql': 'INTEGER'},
127               'vertex_connectivity': {'index': True,
128                                        'primary_key': False,
129                                        'sql': 'BOOLEAN'}},
130     'spectrum': {'eigenvalues_sd': {'index': True,
131                                       'primary_key': False,
132                                       'sql': 'REAL'},
133                   'energy': {'index': True,
134                               'primary_key': False,
135                               'sql': 'REAL'},
136                   'graph_id': {'index': False,
137                                 'primary_key': False,
138                                 'sql': 'INTEGER'},
139                   'max_eigenvalue': {'index': True,
140                                       'primary_key': False,
141                                       'sql': 'REAL'},
142                   'min_eigenvalue': {'index': True,
143                                       'primary_key': False,
144                                       'sql': 'REAL'},
145                   'spectrum': {'index': False,
146                                 'primary_key': False,
147                                 'sql': 'TEXT'}}}
148    \end{verbatim}
149
150    The automatically generated queries will search graph_data
151    automatically, and the other tables will be searched as necessary. 
152    (Display functions require that all tablesbe searched).
153   
154USE:
155    The tables are associated by the unique primary key graph_id (int).
156   
157    For the query generating functions (display functions and get_list),
158    the query parameter allows the user to input any GenericSQLQuery
159    associated with this database.  Otherwise, the user can enter
160    the individual parameters and the sqlite call will be generated by
161    the function.
162
163    The properties currently used as search parameters are:
164    \begin{verbatim}
165        Table: aut_grp
166            - aut_grp_size (The size of the automorphism group - Integer)
167            - edge_transitive (Boolean)
168            - num_fixed_points (Integer)
169            - num_orbits (Integer)
170            - vertex_transitive (Boolean)
171        Table: degrees
172            - average_degree (Real)
173            - degree_sequence (Integer)
174            - degrees_sd (Standard Deviation of degrees - Real)
175            - max_degree (Integer)
176            - min_degree (Integer)
177            - regular (Boolean)
178        Table: graph_data
179            - complement_graph6 (graph6 canonical label of the complement
180              graph - String)
181            - eulerian (Boolean)
182            - graph6 (canonical label - String)
183            - lovasz_number (Real)
184            - num_cycles (Integer)
185            - num_edges (Integer)
186            - num_hamiltonian_cycles (Integer)
187            - num_vertices (Integer)
188            - perfect (Boolean)
189            - planar (Boolean)
190        Table: misc
191            - clique_number (Integer)
192            - diameter (Real)
193            - edge_connectivity (Integer)
194            - girth (Integer)
195            - independence_number (Integer)
196            - induced_subgraphs (canonical label, use regexp - String)
197            - min_vertex_cover_size (Integer)
198            - num_components (Integer)
199            - num_cut_vertices (Integer)
200            - num_spanning_trees (Integer)
201            - radius (Real)
202            - vertex_connectivity (Integer)
203        Table: spectrum
204            - eigenvalues_sd (Standard Deviation of eigenvalues - Real)
205            - energy (Real)
206            - max_eigenvalue (Real)
207            - min_eigenvalue (Real)
208            - spectrum (String)
209    \end{verbatim}
210       
211VISUALIZATION:
212
213    Beyond the typical show function, there are three options for displaying
214    the results of a query.  When running the notebook, each of these functions
215    displays an image of the graph and it's (canonical label) graph6 string
216    in an html results table.
217   
218    \begin{verbatim}
219        - display_all (displays all the database properties in the results
220          table). 
221        - display_tables (displays all the properties in the database tables
222          that are listed by the user).
223        - display_properties (displays all the individual properties
224          specified by the user).
225    \end{verbatim}
226   
227AUTHORS:
228    -- Emily A. Kirkman (2007-07-23): inherits GenericSQLDatabase, also added
229                                    classes: GraphQuery and GenericGraphQuery
230    -- Emily A. Kirkman (2007-05-11): initial sqlite version
231    -- Emily A. Kirkman (2007-02-13): initial version (non-sqlite)
232   
233REFERENCES:
234    -- Data provided by Jason Grout (Brigham Young University).
235       [Online] Available: http://math.byu.edu/~grout/graphs/
236   
237"""
238
239################################################################################
240#           Copyright (C) 2007 Emily A. Kirkman
241#
242#
243# Distributed  under  the  terms  of  the  GNU  General  Public  License (GPL)
244#                         http://www.gnu.org/licenses/
245################################################################################
246import graph
247import re
248from sqlite3 import dbapi2 as sqlite
249import os
250from sage.databases.database import GenericSQLDatabase, GenericSQLQuery, SQLQuery
251
252from sage.databases.db import DB_HOME
253dblocation = DB_HOME + '/graphs/graphs.db'
254
255def regexp(expr, item):
256    """
257    Function to define regular expressions in pysqlite.
258    Returns 1 if parameter `item` matches the regular expression parameter `expr`.
259    Returns 0 otherwise (i.e.: no match).
260   
261    REFERENCES:
262        Gerhard Haring. [Online] Available: http://lists.initd.org/pipermail/pysqlite/2005-November/000253.html
263    """
264    r = re.compile(expr)
265    return r.match(item) is not None
266
267class GenericGraphQuery(GenericSQLQuery):
268    """
269    A query for a GraphDatabase.
270   
271    INPUT:
272        database -- the GraphDatabase instance to query
273        query_string -- a string representing the SQL query
274        param_tuple -- a tuple of strings - what to replace question marks in
275                query_string with (optional, but a good idea)
276   
277    NOTE:
278        This query class is generally intended for developers and more
279        advanced users.  It allows you to execute any query, and so may be
280        considered unsafe... 
281       
282        See GraphDatabase class docstrings or enter:
283       
284        sage: G = GraphDatabase()
285        sage.: G.get_skeleton()
286       
287        to see the underlying structure of the database.  Also see
288        GenericSQLQuery in sage.databases.database for more info and a tutorial.
289       
290        A piece of advice about '?' and param_tuple: 
291        It is generally considered safer to query with a '?' in place of
292        each value parameter, and using a second argument (a tuple of strings)
293        in a call to the sqlite database.  Successful use of the param_tuple
294        argument is exemplified:
295       
296        sage: G = GraphDatabase()
297        sage: q = 'select graph_id,graph6,num_vertices,num_edges from graph_data where graph_id<=(?) and num_vertices=(?)'
298        sage: param = (22,5)
299        sage: Q = GenericSQLQuery(G,q,param)
300        sage: Q.show()
301        graph_id             graph6               num_vertices         num_edges           
302        --------------------------------------------------------------------------------
303        18                   D??                  5                    0                   
304        19                   D?C                  5                    1                   
305        20                   D?K                  5                    2                   
306        21                   D@O                  5                    2                   
307        22                   D?[                  5                    3                   
308
309    """
310    def __init__(self, database, query_string, param_tuple=None):
311        if not isinstance(database, GraphDatabase):
312            raise TypeError('%s is not a valid GraphDatabase'%database)
313        GenericSQLQuery.__init__(self,database,query_string,param_tuple)
314   
315    def show(self, max_field_size=20, html_table=False, with_picture=None):
316        """
317        Displays the results of a query in table format.
318       
319        INPUT:
320            max_field_size -- width of fields in command prompt version
321            html_table -- whether or not to draw table in html format
322            with_picture -- whether or not to display results with a picture
323                of the graph
324               
325        EXAMPLES:
326        TODO
327        """
328        if with_picture is None:
329            from sage.server.support import EMBEDDED_MODE
330            with_picture = EMBEDDED_MODE
331        if with_picture:
332            from sage.plot.plot import plot
333           
334            s = (self.__query_string__).lower()
335            s = re.sub('select ','select graph_data.graph6,',s)
336            s = re.sub(' from .* where ', ' from graph_data inner join aut_grp on aut_grp.graph_id=graph_data.graph_id inner join degrees on degrees.graph_id=graph_data.graph_id inner join misc on misc.graph_id=graph_data.graph_id inner join spectrum on spectrum.graph_id=graph_data.graph_id where ',s)
337           
338            try:
339                cur = self.__database__.__connection__.cursor()
340                if self.__param_tuple__ is not None:
341                    tup = []
342                    # make it a tuple of strings:
343                    for i in range (len(self.__param_tuple__)):
344                        tup.append(str(self.__param_tuple__[i]))
345                    cur.execute(s, tuple(tup))
346                else:
347                    cur.execute(s)
348                b = cur.fetchall()
349            except:
350                raise RuntimeError('Failure to fetch query.')
351
352            # Picture drawn here
353            graph6list = []
354            for i in range (len(b)):
355                graph6 = str(b[i][0])
356                g = graph.Graph('%s'%graph6)
357                graph6list.append(g.graph6_string())
358                p = g.plot(layout='circular', vertex_size=30, vertex_labels=False, graph_border=False)
359                p.save('%s.png'%i, figsize=[1,1])
360       
361            print '<html><table bgcolor=lightgrey cellpadding=0><tr>'
362            print '<td bgcolor=white align=center> Picture </td>'
363            for des in cur.description[1:]:
364                print '<td bgcolor=white align=center> ' + des[0] + ' </td>'
365            print '</tr>'
366            field_indices = range(len(cur.description))
367            count = 0
368            for row in b:
369                print '<tr><td bgcolor=white align=center><img src="cell://%d.png"></td>'%count
370                for index in field_indices[1:]:
371                    print '<td bgcolor=white align=center> ' + str(row[index]) + ' </td>'
372                print '</tr>'
373                count += 1
374            print '</table></html>'
375
376        else:
377            GenericSQLQuery.show(self, max_field_size, html_table)
378                   
379class GraphQuery(SQLQuery):
380    """
381    A query for an instance of GraphDatabase.  This class nicely wraps
382    the SQLQuery class located in sage.databases.database.py to make the
383    query constraints intuitive and with as many predefinitions as posible.
384    (i.e.: since it has to be a GraphDatabase, we already know the table
385    structure and types; and since it is immutable, we can treat these as
386    a guarantee).
387   
388    NOTE:
389        SQLQuery functions are available for GraphQuery.
390   
391    TUTORIAL:
392    TODO
393   
394    INPUT:
395        database -- the instance of GraphDatabase to apply query to
396        query -- (GenericSQLQuery) A sqlite query for graphs.db (See examples below).
397        layout -- (String) The layout option for the graph image.  Options include:
398                           'circular' -- plots the graph with vertices evenly
399                                         distributed on a circle
400                           'spring' -- uses the traditional spring layout
401        aut_grp_size -- (Integer) The desired size of the automorphism group.
402                        (List) Format: [<String>,<Integer>] WHERE the first
403                                       entry represents an inequality:
404                                       '=','>','<','>=','<='
405        average_degree -- (Real) The desired average degree.
406                          (List) Format: [<String>,<Real>] WHERE the first
407                                         entry represents an inequality:
408                                         '=','>','<','>=','<='
409        clique_number -- (Integer) The desired clique number.
410                         (List) Format: [<String>,<Integer>] WHERE the first
411                                        entry represents an inequality:
412                                        '=','>','<','>=','<='
413        complement_graph6 -- (String) A graph6 string isomorphic to the
414                                      desired complement graph.
415                             (List) A list of graph6 strings.  Will search
416                                    for graphs with complement isomorphic to
417                                    any string in the list.
418        degree_sequence -- (Integer) The desired sequence of degrees.
419                                     (Ordered highest to lowest).
420        degrees_sd -- (Real) The desired standard deviation of degrees.
421                      (List) Format: [<String>,<Real>] WHERE the first
422                                     entry represents an inequality:
423                                     '=','>','<','>=','<='
424        diameter -- (Real) The desired diameter.
425                    (List) Format: [<String>,<Real>] WHERE the first
426                                    entry represents an inequality:
427                                    '=','>','<','>=','<='
428        edge_connectivity -- (Integer) The desired edge connectivity.
429                             (List) Format: [<String>,<Integer>] WHERE the first
430                                    entry represents an inequality:
431                                    '=','>','<','>=','<='
432        edge_transitive -- (Boolean)
433        eigenvalues_sd -- (Real) The desired standard deviation of eigenvalues.
434                          (List) Format: [<String>,<Real>] WHERE the first
435                                 entry represents an inequality:
436                                 '=','>','<','>=','<='
437        energy -- (Real) The desired energy.
438                  (List) Format: [<String>,<Real>] WHERE the first entry
439                                 represents an inequality: '=','>','<','>=','<='
440        eulerian -- (Boolean)
441        girth -- (Integer) The desired girth.
442                 (List) Format: [<String>,<Integer>] WHERE the first entry
443                                represents an inequality: '=','>','<','>=','<='
444        graph6 -- (String) A graph6 string isomorphic to the desired graph.
445                  (List) A list of graph6 strings.  Will search for graphs
446                         isomorphic to any string in the list.
447        independence_number -- (Integer) The desired independence number.
448                               (List) Format: [<String>,<Integer>] WHERE the
449                               first entry represents an inequality:
450                               '=','>','<','>=','<='
451        induced_subgraphs -- (String) graph6 string isomorphic to desired subgraph.
452                             (List) Format options:
453                                    1. ['one_of',<String>,...,<String>]
454                                       Will search for graphs containing a subgraph
455                                       isomorphic to any of the graph6 strings in
456                                       the list.
457                                    2. ['all_of',<String>,...,<String>]
458                                       Will search for graphs containing a subgraph
459                                       isomorphic to each of the graph6 strings in
460                                       the list.
461        lovasz_number -- (Real) The desired lovasz number.
462                         (List) Format: [<String>,<Real>] WHERE the first entry
463                                represents an inequality: '=','>','<','>=','<='
464        max_degree -- (Integer) The desired maximum degree.
465                      (List) Format: [<String>,<Integer>] WHERE the first entry
466                             represents an inequality: '=','>','<','>=','<='
467        max_eigenvalue -- (Real) The desired maximum eigenvalue.
468                          (List) Format: [<String>,<Real>] WHERE the first entry
469                                 represents an inequality: '=','>','<','>=','<='
470        min_degree -- (Integer) The desired minimum degree.
471                      (List) Format: [<String>,<Integer>] WHERE the first entry
472                             represents an inequality: '=','>','<','>=','<='
473        min_eigenvalue -- (Real) The desired minimum eigenvalue.
474                          (List) Format: [<String>,<Real>] WHERE the first entry
475                                 represents an inequality: '=','>','<','>=','<='
476        min_vertex_cover_size -- (Integer) The desired minimum vertex cover size.
477                                 (List) Format: [<String>,<Integer>] WHERE the
478                                        first entry represents an inequality:
479                                        '=','>','<','>=','<='
480        num_components -- (Integer) The desired number of components.
481                          (List) Format: [<String>,<Integer>] WHERE the first
482                                 entry represents an inequality:
483                                 '=','>','<','>=','<='
484        num_cut_vertices -- (Integer) The desired number of cut vertices.
485                            (List) Format: [<String>,<Integer>] WHERE the first
486                                 entry represents an inequality:
487                                 '=','>','<','>=','<='
488        num_cycles -- (Integer) The desired number of cycles.
489                      (List) Format: [<String>,<Integer>] WHERE the first entry
490                             represents an inequality: '=','>','<','>=','<='
491        num_edges -- (Integer) The desired number of edges.
492                     (List) Format: [<String>,<Integer>] WHERE the first entry
493                            represents an inequality: '=','>','<','>=','<='
494        num_fixed_points -- (Integer) The desired number of fixed points.
495                            (List) Format: [<String>,<Integer>] WHERE the first
496                                   entry represents an inequality:
497                                   '=','>','<','>=','<='
498        num_hamiltonian_cycles -- (Integer) The desired number of hamiltonian cycles.
499                                  (List) Format: [<String>,<Integer>] WHERE the first
500                                         entry represents an inequality:
501                                         '=','>','<','>=','<='
502        num_orbits -- (Integer) The desired number of orbits.
503                      (List) Format: [<String>,<Integer>] WHERE the first entry
504                             represents an inequality: '=','>','<','>=','<='
505        num_spanning_trees -- (Integer) The desired number of spanning trees.
506                              (List) Format: [<String>,<Integer>] WHERE the first
507                                     entry represents an inequality:
508                                     '=','>','<','>=','<='
509        num_vertices -- (Integer) The desired number of vertices.
510                        (List) Format: [<String>,<Integer>] WHERE the first entry
511                        represents an inequality: '=','>','<','>=','<='
512        perfect -- (Boolean)
513        planar -- (Boolean)
514        radius -- (Integer) The desired radius.
515                  (List) Format: [<String>,<Integer>] WHERE the first entry represents
516                         an inequality: '=','>','<','>=','<='
517        regular -- (Boolean)
518        spectrum -- (String) The desired spectrum.  (Ordered highest to lowest,
519                             delimited by ', ' and rounded to 6 decimal places).
520        vertex_connectivity -- (Integer) The desired vertex connectivity.
521                               (List) Format: [<String>,<Integer>] WHERE the first
522                                      entry represents an inequality:
523                                      '=','>','<','>=','<='
524        vertex_transitive -- (Boolean)
525    """
526    def __init__(self, database, display=['graph6'], graph6=None, num_vertices=None, \
527                    num_edges=None, num_cycles=None, num_hamiltonian_cycles=None, \
528                    eulerian=None, planar=None, perfect=None, lovasz_number=None, \
529                    complement_graph6=None, aut_grp_size=None, num_orbits=None, \
530                    num_fixed_points=None, vertex_transitive=None, edge_transitive=None, \
531                    degree_sequence=None, min_degree=None, max_degree=None, \
532                    average_degree=None, degrees_sd=None, regular=None, \
533                    vertex_connectivity=None, edge_connectivity=None, num_components=None, \
534                    girth=None, radius=None, diameter=None, clique_number=None, \
535                    independence_number=None, num_cut_vertices=None, \
536                    min_vertex_cover_size=None, num_spanning_trees=None, \
537                    induced_subgraphs=None, spectrum=None, min_eigenvalue=None, \
538                    max_eigenvalue=None, eigenvalues_sd=None, energy=None):
539       
540        SQLQuery.__init__(self,database)
541       
542        # need multiple replace
543        from sage.misc.multireplace import multiple_replace
544        with_table = {'aut_grp_size': 'aut_grp.aut_grp_size',
545             'average_degree': 'degrees.average_degree',
546             'clique_number': 'misc.clique_number',
547             'complement_graph6': 'graph_data.complement_graph6',
548             'degree_sequence': 'degrees.degree_sequence',
549             'degrees_sd': 'degrees.degrees_sd',
550             'diameter': 'misc.diameter',
551             'edge_connectivity': 'misc.edge_connectivity',
552             'edge_transitive': 'aut_grp.edge_transitive',
553             'eigenvalues_sd': 'spectrum.eigenvalues_sd',
554             'energy': 'spectrum.energy',
555             'eulerian': 'graph_data.eulerian',
556             'girth': 'misc.girth',
557             'graph6': 'graph_data.graph6',
558             'independence_number': 'misc.independence_number',
559             'induced_subgraphs': 'misc.induced_subgraphs',
560             'lovasz_number': 'graph_data.lovasz_number',
561             'max_degree': 'degrees.max_degree',
562             'max_eigenvalue': 'spectrum.max_eigenvalue',
563             'min_degree': 'degrees.min_degree',
564             'min_eigenvalue': 'spectrum.min_eigenvalue',
565             'min_vertex_cover_size': 'misc.min_vertex_cover_size',
566             'num_components': 'misc.num_components',
567             'num_cut_vertices': 'misc.num_cut_vertices',
568             'num_cycles': 'graph_data.num_cycles',
569             'num_edges': 'graph_data.num_edges',
570             'num_fixed_points': 'aut_grp.num_fixed_points',
571             'num_hamiltonian_cycles': 'graph_data.num_hamiltonian_cycles',
572             'num_orbits': 'aut_grp.num_orbits',
573             'num_spanning_trees': 'misc.num_spanning_trees',
574             'num_vertices': 'graph_data.num_vertices',
575             'perfect': 'graph_data.perfect',
576             'planar': 'graph_data.planar',
577             'radius': 'misc.radius',
578             'regular': 'degrees.regular',
579             'spectrum': 'spectrum.spectrum',
580             'vertex_connectivity': 'misc.vertex_connectivity',
581             'vertex_transitive': 'aut_grp.vertex_transitive'}
582       
583        # TODO : BEING SILLY HERE...
584        for i in range(len(display)):
585            display[i] = multiple_replace(with_table,'%s'%display[i])
586       
587        query = __query_string__(graph6=graph6, num_vertices=num_vertices, num_edges=num_edges, num_cycles=num_cycles, num_hamiltonian_cycles=num_hamiltonian_cycles, eulerian=eulerian, planar=planar, perfect=perfect, lovasz_number=lovasz_number, complement_graph6=complement_graph6, aut_grp_size=aut_grp_size, num_orbits=num_orbits, num_fixed_points=num_fixed_points, vertex_transitive=vertex_transitive, edge_transitive=edge_transitive, degree_sequence=degree_sequence, min_degree=min_degree, max_degree=max_degree, average_degree=average_degree, degrees_sd=degrees_sd, regular=regular, vertex_connectivity=vertex_connectivity, edge_connectivity=edge_connectivity, num_components=num_components, girth=girth, radius=radius, diameter=diameter, clique_number=clique_number, independence_number=independence_number, num_cut_vertices=num_cut_vertices, min_vertex_cover_size=min_vertex_cover_size, num_spanning_trees=num_spanning_trees, induced_subgraphs=induced_subgraphs, spectrum=spectrum, min_eigenvalue=min_eigenvalue, max_eigenvalue=max_eigenvalue, eigenvalues_sd=eigenvalues_sd, energy=energy)
588        print query
589        query = re.sub('SELECT graph_data.graph6','SELECT %s'%(', '.join(display)),query )
590       
591        self.__query_string__ = query
592       
593class GraphDatabase(GenericSQLDatabase):
594    r"""
595    Graph Database
596
597    INFO:
598
599        This class interfaces with the sqlite database graphs.db.  It is an
600        immutable database that inherits from SQLDatabase (see
601        sage.databases.database.py).  The display functions and get_list create
602        their own queries, but it is also possible to query the database by
603        constructing either a GenericSQLQuery or a SQLQuery.
604       
605        The database contains all unlabeled graphs with 7 or fewer nodes. 
606        This class will also interface with the optional database package
607        containing all unlabeled graphs with 8 or fewer nodes. 
608        The database(s) consists of five tables, and has the structure given
609        by the skeleton:
610        \begin{verbatim}
611        {'aut_grp': {'aut_grp_size': {'index': True,
612                                        'primary_key': False,
613                                        'sql': 'INTEGER'},
614                      'edge_transitive': {'index': True,
615                                           'primary_key': False,
616                                           'sql': 'BOOLEAN'},
617                      'graph_id': {'index': False,
618                                    'primary_key': False,
619                                    'sql': 'INTEGER'},
620                      'num_fixed_points': {'index': True,
621                                            'primary_key': False,
622                                            'sql': 'INTEGER'},
623                      'num_orbits': {'index': True,
624                                      'primary_key': False,
625                                      'sql': 'INTEGER'},
626                      'vertex_transitive': {'index': True,
627                                             'primary_key': False,
628                                             'sql': 'BOOLEAN'}},
629         'degrees': {'average_degree': {'index': True,
630                                          'primary_key': False,
631                                          'sql': 'REAL'},
632                      'degree_sequence': {'index': False,
633                                           'primary_key': False,
634                                           'sql': 'INTEGER'},
635                      'degrees_sd': {'index': True,
636                                      'primary_key': False,
637                                      'sql': 'REAL'},
638                      'graph_id': {'index': False,
639                                    'primary_key': False,
640                                    'sql': 'INTEGER'},
641                      'max_degree': {'index': True,
642                                      'primary_key': False,
643                                      'sql': 'INTEGER'},
644                      'min_degree': {'index': True,
645                                      'primary_key': False,
646                                      'sql': 'INTEGER'},
647                      'regular': {'index': True,
648                                   'primary_key': False,
649                                   'sql': 'BOOLEAN'}},
650         'graph_data': {'complement_graph6': {'index': True,
651                                                'primary_key': False,
652                                                'sql': 'TEXT'},
653                         'eulerian': {'index': True,
654                                       'primary_key': False,
655                                       'sql': 'BOOLEAN'},
656                         'graph6': {'index': True,
657                                     'primary_key': False,
658                                     'sql': 'TEXT'},
659                         'graph_id': {'index': True,
660                                       'primary_key': False,
661                                       'sql': 'INTEGER'},
662                         'lovasz_number': {'index': True,
663                                            'primary_key': False,
664                                            'sql': 'REAL'},
665                         'num_cycles': {'index': True,
666                                         'primary_key': False,
667                                         'sql': 'INTEGER'},
668                         'num_edges': {'index': True,
669                                        'primary_key': False,
670                                        'sql': 'INTEGER'},
671                         'num_hamiltonian_cycles': {'index': True,
672                                                     'primary_key': False,
673                                                     'sql': 'INTEGER'},
674                         'num_vertices': {'index': True,
675                                           'primary_key': False,
676                                           'sql': 'INTEGER'},
677                         'perfect': {'index': True,
678                                      'primary_key': False,
679                                      'sql': 'BOOLEAN'},
680                         'planar': {'index': True,
681                                     'primary_key': False,
682                                     'sql': 'BOOLEAN'}},
683         'misc': {'clique_number': {'index': True,
684                                      'primary_key': False,
685                                      'sql': 'INTEGER'},
686                   'diameter': {'index': True,
687                                 'primary_key': False,
688                                 'sql': 'INTEGER'},
689                   'edge_connectivity': {'index': True,
690                                          'primary_key': False,
691                                          'sql': 'BOOLEAN'},
692                   'girth': {'index': True, 'primary_key': False, 'sql': 'INTEGER'},
693                   'graph_id': {'index': False,
694                                 'primary_key': False,
695                                 'sql': 'INTEGER'},
696                   'independence_number': {'index': True,
697                                            'primary_key': False,
698                                            'sql': 'INTEGER'},
699                   'induced_subgraphs': {'index': True,
700                                          'primary_key': False,
701                                          'sql': 'TEXT'},
702                   'min_vertex_cover_size': {'index': True,
703                                              'primary_key': False,
704                                              'sql': 'INTEGER'},
705                   'num_components': {'index': True,
706                                       'primary_key': False,
707                                       'sql': 'INTEGER'},
708                   'num_cut_vertices': {'index': True,
709                                         'primary_key': False,
710                                         'sql': 'INTEGER'},
711                   'num_spanning_trees': {'index': True,
712                                           'primary_key': False,
713                                           'sql': 'INTEGER'},
714                   'radius': {'index': True,
715                               'primary_key': False,
716                               'sql': 'INTEGER'},
717                   'vertex_connectivity': {'index': True,
718                                            'primary_key': False,
719                                            'sql': 'BOOLEAN'}},
720         'spectrum': {'eigenvalues_sd': {'index': True,
721                                           'primary_key': False,
722                                           'sql': 'REAL'},
723                       'energy': {'index': True,
724                                   'primary_key': False,
725                                   'sql': 'REAL'},
726                       'graph_id': {'index': False,
727                                     'primary_key': False,
728                                     'sql': 'INTEGER'},
729                       'max_eigenvalue': {'index': True,
730                                           'primary_key': False,
731                                           'sql': 'REAL'},
732                       'min_eigenvalue': {'index': True,
733                                           'primary_key': False,
734                                           'sql': 'REAL'},
735                       'spectrum': {'index': False,
736                                     'primary_key': False,
737                                     'sql': 'TEXT'}}}
738        \end{verbatim}
739
740        The automatically generated queries will search graph_data
741        automatically, and the other tables will be searched as necessary. 
742        (Display functions require that all tablesbe searched).
743       
744    USE:
745        The tables are associated by the unique primary key graph_id (int).
746       
747        For the query generating functions (display functions and get_list),
748        the query parameter allows the user to input any GenericSQLQuery
749        associated with this database.  Otherwise, the user can enter
750        the individual parameters and the sqlite call will be generated by
751        the function.
752
753        The properties currently used as search parameters are:
754        \begin{verbatim}
755            Table: aut_grp
756                - aut_grp_size (The size of the automorphism group - Integer)
757                - edge_transitive (Boolean)
758                - num_fixed_points (Integer)
759                - num_orbits (Integer)
760                - vertex_transitive (Boolean)
761            Table: degrees
762                - average_degree (Real)
763                - degree_sequence (Integer)
764                - degrees_sd (Standard Deviation of degrees - Real)
765                - max_degree (Integer)
766                - min_degree (Integer)
767                - regular (Boolean)
768            Table: graph_data
769                - complement_graph6 (graph6 canonical label of the complement
770                  graph - String)
771                - eulerian (Boolean)
772                - graph6 (canonical label - String)
773                - lovasz_number (Real)
774                - num_cycles (Integer)
775                - num_edges (Integer)
776                - num_hamiltonian_cycles (Integer)
777                - num_vertices (Integer)
778                - perfect (Boolean)
779                - planar (Boolean)
780            Table: misc
781                - clique_number (Integer)
782                - diameter (Real)
783                - edge_connectivity (Integer)
784                - girth (Integer)
785                - independence_number (Integer)
786                - induced_subgraphs (canonical label, use regexp - String)
787                - min_vertex_cover_size (Integer)
788                - num_components (Integer)
789                - num_cut_vertices (Integer)
790                - num_spanning_trees (Integer)
791                - radius (Real)
792                - vertex_connectivity (Integer)
793            Table: spectrum
794                - eigenvalues_sd (Standard Deviation of eigenvalues - Real)
795                - energy (Real)
796                - max_eigenvalue (Real)
797                - min_eigenvalue (Real)
798                - spectrum (String)
799        \end{verbatim}
800           
801    VISUALIZATION:
802
803        Beyond the typical show function, there are three options for displaying
804        the results of a query.  When running the notebook, each of these functions
805        displays an image of the graph and it's (canonical label) graph6 string
806        in an html results table.
807       
808        \begin{verbatim}
809            - display_all (displays all the database properties in the results
810              table). 
811            - display_tables (displays all the properties in the database tables
812              that are listed by the user).
813            - display_properties (displays all the individual properties
814              specified by the user).
815        \end{verbatim}
816       
817    REFERENCES:
818        -- Data provided by Jason Grout (Brigham Young University).
819           [Online] Available: http://math.byu.edu/~grout/graphs/
820       
821    """
822   
823    def __init__(self):
824        GenericSQLDatabase.__init__(self,dblocation)
825
826    def display_all(self, query=None, layout='circular', graph6=None, num_vertices=None, \
827                    num_edges=None, num_cycles=None, num_hamiltonian_cycles=None, \
828                    eulerian=None, planar=None, perfect=None, lovasz_number=None, \
829                    complement_graph6=None, aut_grp_size=None, num_orbits=None, \
830                    num_fixed_points=None, vertex_transitive=None, edge_transitive=None, \
831                    degree_sequence=None, min_degree=None, max_degree=None, \
832                    average_degree=None, degrees_sd=None, regular=None, \
833                    vertex_connectivity=None, edge_connectivity=None, num_components=None, \
834                    girth=None, radius=None, diameter=None, clique_number=None, \
835                    independence_number=None, num_cut_vertices=None, \
836                    min_vertex_cover_size=None, num_spanning_trees=None, \
837                    induced_subgraphs=None, spectrum=None, min_eigenvalue=None, \
838                    max_eigenvalue=None, eigenvalues_sd=None, energy=None):
839        r"""
840        Displays the results of a query in a table, including all stored
841        properties and an image for each graph.
842       
843        INPUT:
844            query -- (GenericSQLQuery) A sqlite query for graphs.db (See examples below).
845            layout -- (String) The layout option for the graph image.  Options include:
846                               'circular' -- plots the graph with vertices evenly
847                                             distributed on a circle
848                               'spring' -- uses the traditional spring layout
849            aut_grp_size -- (Integer) The desired size of the automorphism group.
850                            (List) Format: [<String>,<Integer>] WHERE the first
851                                           entry represents an inequality:
852                                           '=','>','<','>=','<='
853            average_degree -- (Real) The desired average degree.
854                              (List) Format: [<String>,<Real>] WHERE the first
855                                             entry represents an inequality:
856                                             '=','>','<','>=','<='
857            clique_number -- (Integer) The desired clique number.
858                             (List) Format: [<String>,<Integer>] WHERE the first
859                                            entry represents an inequality:
860                                            '=','>','<','>=','<='
861            complement_graph6 -- (String) A graph6 string isomorphic to the
862                                          desired complement graph.
863                                 (List) A list of graph6 strings.  Will search
864                                        for graphs with complement isomorphic to
865                                        any string in the list.
866            degree_sequence -- (Integer) The desired sequence of degrees.
867                                         (Ordered highest to lowest).
868            degrees_sd -- (Real) The desired standard deviation of degrees.
869                          (List) Format: [<String>,<Real>] WHERE the first
870                                         entry represents an inequality:
871                                         '=','>','<','>=','<='
872            diameter -- (Real) The desired diameter.
873                        (List) Format: [<String>,<Real>] WHERE the first
874                                        entry represents an inequality:
875                                        '=','>','<','>=','<='
876            edge_connectivity -- (Integer) The desired edge connectivity.
877                                 (List) Format: [<String>,<Integer>] WHERE the first
878                                        entry represents an inequality:
879                                        '=','>','<','>=','<='
880            edge_transitive -- (Boolean)
881            eigenvalues_sd -- (Real) The desired standard deviation of eigenvalues.
882                              (List) Format: [<String>,<Real>] WHERE the first
883                                     entry represents an inequality:
884                                     '=','>','<','>=','<='
885            energy -- (Real) The desired energy.
886                      (List) Format: [<String>,<Real>] WHERE the first entry
887                                     represents an inequality: '=','>','<','>=','<='
888            eulerian -- (Boolean)
889            girth -- (Integer) The desired girth.
890                     (List) Format: [<String>,<Integer>] WHERE the first entry
891                                    represents an inequality: '=','>','<','>=','<='
892            graph6 -- (String) A graph6 string isomorphic to the desired graph.
893                      (List) A list of graph6 strings.  Will search for graphs
894                             isomorphic to any string in the list.
895            independence_number -- (Integer) The desired independence number.
896                                   (List) Format: [<String>,<Integer>] WHERE the
897                                   first entry represents an inequality:
898                                   '=','>','<','>=','<='
899            induced_subgraphs -- (String) graph6 string isomorphic to desired subgraph.
900                                 (List) Format options:
901                                        1. ['one_of',<String>,...,<String>]
902                                           Will search for graphs containing a subgraph
903                                           isomorphic to any of the graph6 strings in
904                                           the list.
905                                        2. ['all_of',<String>,...,<String>]
906                                           Will search for graphs containing a subgraph
907                                           isomorphic to each of the graph6 strings in
908                                           the list.
909            lovasz_number -- (Real) The desired lovasz number.
910                             (List) Format: [<String>,<Real>] WHERE the first entry
911                                    represents an inequality: '=','>','<','>=','<='
912            max_degree -- (Integer) The desired maximum degree.
913                          (List) Format: [<String>,<Integer>] WHERE the first entry
914                                 represents an inequality: '=','>','<','>=','<='
915            max_eigenvalue -- (Real) The desired maximum eigenvalue.
916                              (List) Format: [<String>,<Real>] WHERE the first entry
917                                     represents an inequality: '=','>','<','>=','<='
918            min_degree -- (Integer) The desired minimum degree.
919                          (List) Format: [<String>,<Integer>] WHERE the first entry
920                                 represents an inequality: '=','>','<','>=','<='
921            min_eigenvalue -- (Real) The desired minimum eigenvalue.
922                              (List) Format: [<String>,<Real>] WHERE the first entry
923                                     represents an inequality: '=','>','<','>=','<='
924            min_vertex_cover_size -- (Integer) The desired minimum vertex cover size.
925                                     (List) Format: [<String>,<Integer>] WHERE the
926                                            first entry represents an inequality:
927                                            '=','>','<','>=','<='
928            num_components -- (Integer) The desired number of components.
929                              (List) Format: [<String>,<Integer>] WHERE the first
930                                     entry represents an inequality:
931                                     '=','>','<','>=','<='
932            num_cut_vertices -- (Integer) The desired number of cut vertices.
933                                (List) Format: [<String>,<Integer>] WHERE the first
934                                     entry represents an inequality:
935                                     '=','>','<','>=','<='
936            num_cycles -- (Integer) The desired number of cycles.
937                          (List) Format: [<String>,<Integer>] WHERE the first entry
938                                 represents an inequality: '=','>','<','>=','<='
939            num_edges -- (Integer) The desired number of edges.
940                         (List) Format: [<String>,<Integer>] WHERE the first entry
941                                represents an inequality: '=','>','<','>=','<='
942            num_fixed_points -- (Integer) The desired number of fixed points.
943                                (List) Format: [<String>,<Integer>] WHERE the first
944                                       entry represents an inequality:
945                                       '=','>','<','>=','<='
946            num_hamiltonian_cycles -- (Integer) The desired number of hamiltonian cycles.
947                                      (List) Format: [<String>,<Integer>] WHERE the first
948                                             entry represents an inequality:
949                                             '=','>','<','>=','<='
950            num_orbits -- (Integer) The desired number of orbits.
951                          (List) Format: [<String>,<Integer>] WHERE the first entry
952                                 represents an inequality: '=','>','<','>=','<='
953            num_spanning_trees -- (Integer) The desired number of spanning trees.
954                                  (List) Format: [<String>,<Integer>] WHERE the first
955                                         entry represents an inequality:
956                                         '=','>','<','>=','<='
957            num_vertices -- (Integer) The desired number of vertices.
958                            (List) Format: [<String>,<Integer>] WHERE the first entry
959                            represents an inequality: '=','>','<','>=','<='
960            perfect -- (Boolean)
961            planar -- (Boolean)
962            radius -- (Integer) The desired radius.
963                      (List) Format: [<String>,<Integer>] WHERE the first entry represents
964                             an inequality: '=','>','<','>=','<='
965            regular -- (Boolean)
966            spectrum -- (String) The desired spectrum.  (Ordered highest to lowest,
967                                 delimited by ', ' and rounded to 6 decimal places).
968            vertex_connectivity -- (Integer) The desired vertex connectivity.
969                                   (List) Format: [<String>,<Integer>] WHERE the first
970                                          entry represents an inequality:
971                                          '=','>','<','>=','<='
972            vertex_transitive -- (Boolean)
973           
974        EXAMPLES:
975        TODO
976        The basics:
977            sage.: graphs_query.display_all(num_vertices=5,lovasz_number=3.0,\
978            ...                             girth=4,radius=2,diameter=3)
979            sage.: graphs_query.display_all(layout='spring',num_hamiltonian_cycles=2,\
980            ...                             regular=True,perfect=False)
981            sage.: graphs_query.display_all(layout='spring',degree_sequence=433211)
982           
983        Compare results:
984            sage: (Graph('EAMw')).is_isomorphic(Graph('E@NW'))
985            False
986           
987        Using Inequalities:
988            sage.: graphs_query.display_all(layout='circular', min_eigenvalue=['=',-1], \
989            ...                             eigenvalues_sd=['<=',1], energy=['>',5])
990           
991        The query string:
992            sage.: graphs_query.display_all(layout='spring', \
993            ...             query='SELECT graph_data.graph6 \
994            ...             FROM graph_data WHERE num_vertices<=4 \
995            ...             and num_edges>3')
996            sage.: graphs_query.display_all(query='SELECT graph_data.graph6 FROM graph_data \
997            ...             INNER JOIN degrees on graph_data.graph_id=degrees.graph_id \
998            ...             WHERE num_vertices>6 and eulerian=1 and regular=0 and planar=1 \
999            ...             and num_cycles<=2')
1000            sage.: graphs_query.display_all(query="SELECT graph_data.graph6 \
1001            ...             FROM graph_data INNER JOIN misc on \
1002            ...             misc.graph_id=graph_data.graph_id WHERE \
1003            ...             misc.induced_subgraphs regexp '.*E~~w.*'")
1004        """
1005        from sage.plot.plot import plot
1006       
1007        if ( query is None):
1008            param = None
1009            query = __query_string__(graph6=graph6, num_vertices=num_vertices, num_edges=num_edges, num_cycles=num_cycles, num_hamiltonian_cycles=num_hamiltonian_cycles, eulerian=eulerian, planar=planar, perfect=perfect, lovasz_number=lovasz_number, complement_graph6=complement_graph6, aut_grp_size=aut_grp_size, num_orbits=num_orbits, num_fixed_points=num_fixed_points, vertex_transitive=vertex_transitive, edge_transitive=edge_transitive, degree_sequence=degree_sequence, min_degree=min_degree, max_degree=max_degree, average_degree=average_degree, degrees_sd=degrees_sd, regular=regular, vertex_connectivity=vertex_connectivity, edge_connectivity=edge_connectivity, num_components=num_components, girth=girth, radius=radius, diameter=diameter, clique_number=clique_number, independence_number=independence_number, num_cut_vertices=num_cut_vertices, min_vertex_cover_size=min_vertex_cover_size, num_spanning_trees=num_spanning_trees, induced_subgraphs=induced_subgraphs, spectrum=spectrum, min_eigenvalue=min_eigenvalue, max_eigenvalue=max_eigenvalue, eigenvalues_sd=eigenvalues_sd, energy=energy)
1010            query = re.sub('INNER JOIN .* WHERE','INNER JOIN aut_grp on aut_grp.graph_id=graph_data.graph_id INNER JOIN degrees on degrees.graph_id=graph_data.graph_id INNER JOIN misc on misc.graph_id=graph_data.graph_id INNER JOIN spectrum on spectrum.graph_id=graph_data.graph_id WHERE',query)
1011            query = re.sub('FROM graph_data WHERE','FROM graph_data INNER JOIN aut_grp on aut_grp.graph_id=graph_data.graph_id INNER JOIN degrees on degrees.graph_id=graph_data.graph_id INNER JOIN misc on misc.graph_id=graph_data.graph_id INNER JOIN spectrum on spectrum.graph_id=graph_data.graph_id WHERE',query)
1012            query = re.sub('SELECT graph_data.graph6','SELECT graph_data.graph6,graph_data.num_vertices,degrees.regular,aut_grp.aut_grp_size,graph_data.num_edges,degrees.min_degree,aut_grp.num_orbits,graph_data.num_cycles,degrees.max_degree,aut_grp.num_fixed_points,graph_data.num_hamiltonian_cycles,degrees.average_degree,aut_grp.vertex_transitive,graph_data.eulerian,degrees.degrees_sd,aut_grp.edge_transitive,graph_data.planar,degrees.degree_sequence,misc.vertex_connectivity,graph_data.perfect,spectrum.min_eigenvalue,misc.edge_connectivity,graph_data.lovasz_number,misc.girth,spectrum.max_eigenvalue,misc.num_cut_vertices,misc.independence_number,misc.radius,spectrum.eigenvalues_sd,misc.min_vertex_cover_size,misc.clique_number,misc.diameter,spectrum.energy,misc.num_spanning_trees,misc.num_components,graph_data.complement_graph6,spectrum.spectrum,misc.induced_subgraphs',query)
1013        else:
1014            # Deal only with the string:
1015            param = query.__param_tuple__
1016            query = query.__query_string__
1017
1018        cur = (self.__connection__).cursor()           
1019        if param is None:
1020            a = cur.execute(query)
1021            b = a.fetchall()
1022        else:
1023            tup = []
1024            # make it a tuple of strings:
1025            for i in range (len(param)):
1026                tup.append(str(param[i]))
1027            exe = cur.execute(query, tuple(tup))
1028            b = exe.fetchall() 
1029
1030        graph6list = []
1031        for i in range (len(b)):
1032            graph6 = str(b[i][0])
1033            g = graph.Graph('%s'%graph6)
1034            # The following line is time consuming and should not stay:
1035            graph6list.append(g.graph6_string())
1036            p = g.plot(layout=layout, vertex_size=30, vertex_labels=False, graph_border=False)
1037            p.save('%s.png'%i, figsize=[1,1])
1038       
1039        print "<html>"
1040        print '<table bgcolor=lightgrey cellpadding=0>'
1041       
1042        from sage.misc.multireplace import multiple_replace
1043        to_bool = {'0':"False", '1':"True"}
1044       
1045        for i in range(len(b)):
1046            eul = multiple_replace(to_bool,'%s'%b[i][13])
1047            reg = multiple_replace(to_bool,'%s'%b[i][2])
1048            plan = multiple_replace(to_bool,'%s'%b[i][16])
1049            perf = multiple_replace(to_bool,'%s'%b[i][19])
1050            vtran = multiple_replace(to_bool,'%s'%b[i][12])
1051            etran = multiple_replace(to_bool,'%s'%b[i][15])
1052           
1053            print '<tr><td bgcolor=white align=center rowspan="7"><img src="cell://%s.png"><br>%s</td>'%(i,graph6list[i])
1054            print '<td bgcolor=white align=left><font color=black> Vertices: %s </font></td>'%(b[i][1])
1055            print '<td bgcolor=white align=left><font color=black> Regular: %s </font></td>'%reg
1056            print '<td bgcolor=white align=left><font color=black> Aut Group Size: %s </font></td>'%(b[i][3])
1057           
1058            print '</tr><tr><td bgcolor=white align=left><font color=black> Edges: %s </font></td>'%(b[i][4])
1059            print '<td bgcolor=white align=left><font color=black> Min Degree: %s </font></td>'%(b[i][5])
1060            print '<td bgcolor=white align=left><font color=black> Orbits: %s </font></td>'%(b[i][6])
1061           
1062            print '</tr><tr><td bgcolor=white align=left><font color=black> Cycles: %s </font></td>'%(b[i][7])
1063            print '<td bgcolor=white align=left><font color=black> Max Degree: %s </font></td>'%(b[i][8])
1064            print '<td bgcolor=white align=left><font color=black> Fixed Points: %s </font></td>'%(b[i][9])
1065           
1066            print '</tr><tr><td bgcolor=white align=left><font color=black> Hamiltonian Cycles: %s </font></td>'%(b[i][10])
1067            print '<td bgcolor=white align=left><font color=black> Average Degree: %s </font></td>'%(b[i][11])
1068            print '<td bgcolor=white align=left><font color=black> Vertex Transitive: %s </font></td>'%vtran
1069           
1070            print '</tr><tr><td bgcolor=white align=left><font color=black> Eulerian: %s </font></td>'%eul
1071            print '<td bgcolor=white align=left><font color=black> Degree SD: %s </font></td>'%(b[i][14])
1072            print '<td bgcolor=white align=left><font color=black> Edge Transitive: %s </font></td>'%etran
1073           
1074            print '</tr><tr><td bgcolor=white align=left><font color=black> Planar: %s </font></td>'%plan
1075            print '<td bgcolor=white align=left><font color=black> Degree Sequence: %s </font></td>'%(b[i][17])
1076            print '<td bgcolor=white align=left><font color=black> Vertex Connectivity: %s </font></td>'%(b[i][18])
1077           
1078            print '</tr><tr><td bgcolor=white align=left><font color=black> Perfect: %s </font></td>'%perf
1079            print '<td bgcolor=white align=left><font color=black> Min Eigenvalue: %s </font></td>'%(b[i][20])
1080            print '<td bgcolor=white align=left><font color=black> Edge Connectivity: %s </font></td>'%(b[i][21])
1081           
1082            print '</tr><tr><td bgcolor=white align=left><font color=black> Lovasz Number: %s </font></td>'%(b[i][22])
1083            print '<td bgcolor=white align=left><font color=black> Girth: %s </font></td>'%(b[i][23])
1084            print '<td bgcolor=white align=left><font color=black> Max Eigenvalue: %s </font></td>'%(b[i][24])
1085            print '<td bgcolor=white align=left><font color=black> Cut Vertices: %s </font></td>'%(b[i][25])
1086           
1087            print '</tr><tr><td bgcolor=white align=left><font color=black> Independence Number: %s </font></td>'%(b[i][26])
1088            print '<td bgcolor=white align=left><font color=black> Radius: %s </font></td>'%(b[i][27])
1089            print '<td bgcolor=white align=left><font color=black> Eigenvalues SD: %s </font></td>'%(b[i][28])
1090            print '<td bgcolor=white align=left><font color=black> Min Vertex Cover Size: %s </font></td>'%(b[i][29])
1091           
1092            print '</tr><tr><td bgcolor=white align=left><font color=black> Clique Number: %s </font></td>'%(b[i][30])
1093            print '<td bgcolor=white align=left><font color=black> Diameter: %s </font></td>'%(b[i][31])
1094            print '<td bgcolor=white align=left><font color=black> Energy: %s </font></td>'%(b[i][32])
1095            print '<td bgcolor=white align=left><font color=black> Spanning Trees: %s </font></td>'%(b[i][33])
1096           
1097            print '</tr><tr><td bgcolor=white align=left><font color=black> Components: %s </font></td>'%(b[i][34])
1098            print '<td bgcolor=white align=left><font color=black> Complement: %s </font></td>'%(b[i][35])
1099            print '<td bgcolor=white align=left colspan="2"><font color=black> Spectrum: %s </font></td>'%(b[i][36])
1100           
1101            print '</tr><tr><td bgcolor=white align=left colspan="4"><font color=black> Induced Subgraphs: %s </font></td></tr>'%(b[i][37])
1102           
1103            if ( i != len(b)-1 ): print '<tr><td bgcolor=lightblue colspan="4" height="3"></td></tr>'
1104                       
1105        print "</table></html>"
1106        return
1107
1108    def display_tables(self, tables=None, layout='circular', query=None, graph6=None, \
1109                        num_vertices=None, num_edges=None, num_cycles=None, \
1110                        num_hamiltonian_cycles=None, eulerian=None, planar=None, perfect=None, \
1111                        lovasz_number=None, complement_graph6=None, aut_grp_size=None, \
1112                        num_orbits=None, num_fixed_points=None, vertex_transitive=None, \
1113                        edge_transitive=None, degree_sequence=None, min_degree=None, \
1114                        max_degree=None, average_degree=None, degrees_sd=None, regular=None, \
1115                        vertex_connectivity=None, edge_connectivity=None, num_components=None, \
1116                        girth=None, radius=None, diameter=None, clique_number=None, \
1117                        independence_number=None, num_cut_vertices=None, \
1118                        min_vertex_cover_size=None, num_spanning_trees=None, \
1119                        induced_subgraphs=None, spectrum=None, min_eigenvalue=None, \
1120                        max_eigenvalue=None, eigenvalues_sd=None, energy=None):
1121        r"""
1122        Displays the results of a query in a table, including all stored
1123        properties FROM the specified database tables and an image for each graph.
1124       
1125        INPUT:
1126            query -- (GenericSQLQuery) A sqlite query for graphs.db (See examples below).
1127            tables -- (List) A list of strings with the exact name (as the
1128                             database tables) of the tables of properties to
1129                             display with the results.  Database table names are:
1130                             'aut_grp','degrees','graph_data','misc','spectrum'
1131            layout -- (String) The layout option for the graph image.  Options include:
1132                               'circular' -- plots the graph with vertices evenly
1133                                             distributed on a circle
1134                               'spring' -- uses the traditional spring layout           
1135            aut_grp_size -- (Integer) The desired size of the automorphism group.
1136                            (List) Format: [<String>,<Integer>] WHERE the first
1137                                           entry represents an inequality:
1138                                           '=','>','<','>=','<='
1139            average_degree -- (Real) The desired average degree.
1140                              (List) Format: [<String>,<Real>] WHERE the first
1141                                             entry represents an inequality:
1142                                             '=','>','<','>=','<='
1143            clique_number -- (Integer) The desired clique number.
1144                             (List) Format: [<String>,<Integer>] WHERE the first
1145                                            entry represents an inequality:
1146                                            '=','>','<','>=','<='
1147            complement_graph6 -- (String) A graph6 string isomorphic to the
1148                                          desired complement graph.
1149                                 (List) A list of graph6 strings.  Will search
1150                                        for graphs with complement isomorphic to
1151                                        any string in the list.
1152            degree_sequence -- (Integer) The desired sequence of degrees.
1153                                         (Ordered highest to lowest).
1154            degrees_sd -- (Real) The desired standard deviation of degrees.
1155                          (List) Format: [<String>,<Real>] WHERE the first
1156                                         entry represents an inequality:
1157                                         '=','>','<','>=','<='
1158            diameter -- (Real) The desired diameter.
1159                        (List) Format: [<String>,<Real>] WHERE the first
1160                                        entry represents an inequality:
1161                                        '=','>','<','>=','<='
1162            edge_connectivity -- (Integer) The desired edge connectivity.
1163                                 (List) Format: [<String>,<Integer>] WHERE the first
1164                                        entry represents an inequality:
1165                                        '=','>','<','>=','<='
1166            edge_transitive -- (Boolean)
1167            eigenvalues_sd -- (Real) The desired standard deviation of eigenvalues.
1168                              (List) Format: [<String>,<Real>] WHERE the first
1169                                     entry represents an inequality:
1170                                     '=','>','<','>=','<='
1171            energy -- (Real) The desired energy.
1172                      (List) Format: [<String>,<Real>] WHERE the first entry
1173                                     represents an inequality: '=','>','<','>=','<='
1174            eulerian -- (Boolean)
1175            girth -- (Integer) The desired girth.
1176                     (List) Format: [<String>,<Integer>] WHERE the first entry
1177                                    represents an inequality: '=','>','<','>=','<='
1178            graph6 -- (String) A graph6 string isomorphic to the desired graph.
1179                      (List) A list of graph6 strings.  Will search for graphs
1180                             isomorphic to any string in the list.
1181            independence_number -- (Integer) The desired independence number.
1182                                   (List) Format: [<String>,<Integer>] WHERE the
1183                                   first entry represents an inequality:
1184                                   '=','>','<','>=','<='
1185            induced_subgraphs -- (String) graph6 string isomorphic to desired subgraph.
1186                                 (List) Format options:
1187                                        1. ['one_of',<String>,...,<String>]
1188                                           Will search for graphs containing a subgraph
1189                                           isomorphic to any of the graph6 strings in
1190                                           the list.
1191                                        2. ['all_of',<String>,...,<String>]
1192                                           Will search for graphs containing a subgraph
1193                                           isomorphic to each of the graph6 strings in
1194                                           the list.
1195            lovasz_number -- (Real) The desired lovasz number.
1196                             (List) Format: [<String>,<Real>] WHERE the first entry
1197                                    represents an inequality: '=','>','<','>=','<='
1198            max_degree -- (Integer) The desired maximum degree.
1199                          (List) Format: [<String>,<Integer>] WHERE the first entry
1200                                 represents an inequality: '=','>','<','>=','<='
1201            max_eigenvalue -- (Real) The desired maximum eigenvalue.
1202                              (List) Format: [<String>,<Real>] WHERE the first entry
1203                                     represents an inequality: '=','>','<','>=','<='
1204            min_degree -- (Integer) The desired minimum degree.
1205                          (List) Format: [<String>,<Integer>] WHERE the first entry
1206                                 represents an inequality: '=','>','<','>=','<='
1207            min_eigenvalue -- (Real) The desired minimum eigenvalue.
1208                              (List) Format: [<String>,<Real>] WHERE the first entry
1209                                     represents an inequality: '=','>','<','>=','<='
1210            min_vertex_cover_size -- (Integer) The desired minimum vertex cover size.
1211                                     (List) Format: [<String>,<Integer>] WHERE the
1212                                            first entry represents an inequality:
1213                                            '=','>','<','>=','<='
1214            num_components -- (Integer) The desired number of components.
1215                              (List) Format: [<String>,<Integer>] WHERE the first
1216                                     entry represents an inequality:
1217                                     '=','>','<','>=','<='
1218            num_cut_vertices -- (Integer) The desired number of cut vertices.
1219                                (List) Format: [<String>,<Integer>] WHERE the first
1220                                     entry represents an inequality:
1221                                     '=','>','<','>=','<='
1222            num_cycles -- (Integer) The desired number of cycles.
1223                          (List) Format: [<String>,<Integer>] WHERE the first entry
1224                                 represents an inequality: '=','>','<','>=','<='
1225            num_edges -- (Integer) The desired number of edges.
1226                         (List) Format: [<String>,<Integer>] WHERE the first entry
1227                                represents an inequality: '=','>','<','>=','<='
1228            num_fixed_points -- (Integer) The desired number of fixed points.
1229                                (List) Format: [<String>,<Integer>] WHERE the first
1230                                       entry represents an inequality:
1231                                       '=','>','<','>=','<='
1232            num_hamiltonian_cycles -- (Integer) The desired number of hamiltonian cycles.
1233                                      (List) Format: [<String>,<Integer>] WHERE the first
1234                                             entry represents an inequality:
1235                                             '=','>','<','>=','<='
1236            num_orbits -- (Integer) The desired number of orbits.
1237                          (List) Format: [<String>,<Integer>] WHERE the first entry
1238                                 represents an inequality: '=','>','<','>=','<='
1239            num_spanning_trees -- (Integer) The desired number of spanning trees.
1240                                  (List) Format: [<String>,<Integer>] WHERE the first
1241                                         entry represents an inequality:
1242                                         '=','>','<','>=','<='
1243            num_vertices -- (Integer) The desired number of vertices.
1244                            (List) Format: [<String>,<Integer>] WHERE the first entry
1245                            represents an inequality: '=','>','<','>=','<='
1246            perfect -- (Boolean)
1247            planar -- (Boolean)
1248            radius -- (Integer) The desired radius.
1249                      (List) Format: [<String>,<Integer>] WHERE the first entry represents
1250                             an inequality: '=','>','<','>=','<='
1251            regular -- (Boolean)
1252            spectrum -- (String) The desired spectrum.  (Ordered highest to lowest,
1253                                 delimited by ', ' and rounded to 6 decimal places).
1254            vertex_connectivity -- (Integer) The desired vertex connectivity.
1255                                   (List) Format: [<String>,<Integer>] WHERE the first
1256                                          entry represents an inequality:
1257                                          '=','>','<','>=','<='
1258            vertex_transitive -- (Boolean)
1259                       
1260        EXAMPLES:
1261        TODO
1262        The basics:
1263            sage.: graphs_query.display_tables(tables=['graph_data','misc'], num_vertices=5,\
1264            ...                             lovasz_number=3.0, girth=4, radius=2, diameter=3)
1265            sage.: graphs_query.display_tables(tables=['degrees','spectrum','aut_grp','misc'], \
1266            ...                             layout='spring', num_hamiltonian_cycles=2,\
1267            ...                             regular=True, perfect=False)
1268            sage.: graphs_query.display_tables(tables=['degrees'],layout='spring',\
1269            ...                                degree_sequence=433211)
1270           
1271        Using Inequalities:
1272            sage.: graphs_query.display_tables(tables=['spectrum'], layout='circular', \
1273            ...                             min_eigenvalue=['=',-1], eigenvalues_sd=['<=',1], \
1274            ...                             energy=['>',5])
1275           
1276        The query string:
1277            sage.: graphs_query.display_tables(tables=['graph_data'], layout='spring', \
1278            ...             query='SELECT graph_data.graph6 \
1279            ...             FROM graph_data WHERE num_vertices<=4 \
1280            ...             and num_edges>3')
1281            sage.: graphs_query.display_tables(query='SELECT graph_data.graph6 FROM graph_data \
1282            ...             INNER JOIN degrees on graph_data.graph_id=degrees.graph_id \
1283            ...             WHERE num_vertices>6 and eulerian=1 and regular=0 and planar=1 \
1284            ...             and num_cycles<=2', tables=['graph_data','degrees'])
1285            sage.: graphs_query.display_tables(query="SELECT graph_data.graph6 \
1286            ...             FROM graph_data INNER JOIN misc on \
1287            ...             misc.graph_id=graph_data.graph_id WHERE \
1288            ...             misc.induced_subgraphs regexp '.*E~~w.*'", \
1289            ...             tables=['graph_data','misc','spectrum','degrees','aut_grp'])
1290        """
1291        from sage.plot.plot import plot
1292       
1293        if ( query is None):
1294            param = None
1295            query = __query_string__(graph6=graph6, num_vertices=num_vertices, num_edges=num_edges, num_cycles=num_cycles, num_hamiltonian_cycles=num_hamiltonian_cycles, eulerian=eulerian, planar=planar, perfect=perfect, lovasz_number=lovasz_number, complement_graph6=complement_graph6, aut_grp_size=aut_grp_size, num_orbits=num_orbits, num_fixed_points=num_fixed_points, vertex_transitive=vertex_transitive, edge_transitive=edge_transitive, degree_sequence=degree_sequence, min_degree=min_degree, max_degree=max_degree, average_degree=average_degree, degrees_sd=degrees_sd, regular=regular, vertex_connectivity=vertex_connectivity, edge_connectivity=edge_connectivity, num_components=num_components, girth=girth, radius=radius, diameter=diameter, clique_number=clique_number, independence_number=independence_number, num_cut_vertices=num_cut_vertices, min_vertex_cover_size=min_vertex_cover_size, num_spanning_trees=num_spanning_trees, induced_subgraphs=induced_subgraphs, spectrum=spectrum, min_eigenvalue=min_eigenvalue, max_eigenvalue=max_eigenvalue, eigenvalues_sd=eigenvalues_sd, energy=energy)
1296            query = re.sub('INNER JOIN .* WHERE','INNER JOIN aut_grp on aut_grp.graph_id=graph_data.graph_id INNER JOIN degrees on degrees.graph_id=graph_data.graph_id INNER JOIN misc on misc.graph_id=graph_data.graph_id INNER JOIN spectrum on spectrum.graph_id=graph_data.graph_id WHERE',query)
1297            query = re.sub('FROM graph_data WHERE','FROM graph_data INNER JOIN aut_grp on aut_grp.graph_id=graph_data.graph_id INNER JOIN degrees on degrees.graph_id=graph_data.graph_id INNER JOIN misc on misc.graph_id=graph_data.graph_id INNER JOIN spectrum on spectrum.graph_id=graph_data.graph_id WHERE',query)
1298            query = re.sub('SELECT graph_data.graph6','SELECT graph_data.graph6,graph_data.num_vertices,degrees.regular,aut_grp.aut_grp_size,graph_data.num_edges,degrees.min_degree,aut_grp.num_orbits,graph_data.num_cycles,degrees.max_degree,aut_grp.num_fixed_points,graph_data.num_hamiltonian_cycles,degrees.average_degree,aut_grp.vertex_transitive,graph_data.eulerian,degrees.degrees_sd,aut_grp.edge_transitive,graph_data.planar,degrees.degree_sequence,misc.vertex_connectivity,graph_data.perfect,spectrum.min_eigenvalue,misc.edge_connectivity,graph_data.lovasz_number,misc.girth,spectrum.max_eigenvalue,misc.num_cut_vertices,misc.independence_number,misc.radius,spectrum.eigenvalues_sd,misc.min_vertex_cover_size,misc.clique_number,misc.diameter,spectrum.energy,misc.num_spanning_trees,misc.num_components,graph_data.complement_graph6,spectrum.spectrum,misc.induced_subgraphs',query)
1299        else:
1300            # Deal only with the string:
1301            param = query.__param_tuple__
1302            query = query.__query_string__
1303
1304        cur = (self.__connection__).cursor()           
1305        if param is None:
1306            a = cur.execute(query)
1307            b = a.fetchall()
1308        else:
1309            tup = []
1310            # make it a tuple of strings:
1311            for i in range (len(param)):
1312                tup.append(str(param[i]))
1313            exe = cur.execute(query, tuple(tup))
1314            b = exe.fetchall() 
1315
1316        graph6list = []
1317        for i in range (len(b)):
1318            graph6 = str(b[i][0])
1319            g = graph.Graph('%s'%graph6)
1320            # The following line is time consuming and should not stay:
1321            graph6list.append(g.graph6_string())
1322            p = g.plot(layout=layout, vertex_size=30, vertex_labels=False, graph_border=False)
1323            p.save('%s.png'%i, figsize=[1,1])
1324       
1325        print "<html>"
1326        print '<table bgcolor=lightgrey cellpadding=0>'
1327       
1328        rows = 0
1329        for j in range(len(tables)):
1330            if ( tables[j] == 'misc' ): rows += 5
1331            elif ( tables[j] == 'graph_data' ): rows += 3
1332            elif ( tables[j] == 'aut_grp' or tables[j] == 'degrees' or tables[j] == 'spectrum' ): rows += 2
1333       
1334        from sage.misc.multireplace import multiple_replace
1335        to_bool = {'0':"False", '1':"True"}
1336               
1337        for i in range(len(b)):
1338            eul = multiple_replace(to_bool,'%s'%b[i][13])
1339            reg = multiple_replace(to_bool,'%s'%b[i][2])
1340            plan = multiple_replace(to_bool,'%s'%b[i][16])
1341            perf = multiple_replace(to_bool,'%s'%b[i][19])
1342            vtran = multiple_replace(to_bool,'%s'%b[i][12])
1343            etran = multiple_replace(to_bool,'%s'%b[i][15])
1344       
1345            print '<tr><td bgcolor=white align=center rowspan="%d"><img src="cell://%s.png"><br>%s</td>'%(rows,i,graph6list[i])
1346            top_row = True
1347            for j in range (len(tables)):
1348                if ( tables[j] == 'aut_grp' ):
1349                    if ( not top_row ): print '<tr>'
1350                    print '<td bgcolor=white align=left><font color=black> Aut Group Size: %s </font></td>'%(b[i][3])
1351                    print '<td bgcolor=white align=left><font color=black> Orbits: %s </font></td>'%(b[i][6])
1352                    print '<td bgcolor=white align=left><font color=black> Fixed Points: %s </font></td>'%(b[i][9])
1353                    print '</tr><tr>'
1354                    print '<td bgcolor=white align=left><font color=black> Vertex Transitive: %s </font></td>'%vtran
1355                    print '<td bgcolor=white align=left><font color=black> Edge Transitive: %s </font></td><td bgcolor=white></td></tr>'%etran
1356                    top_row = False
1357                elif ( tables[j] == 'degrees' ):
1358                    if ( not top_row ): print '<tr>'
1359                    print '<td bgcolor=white align=left><font color=black> Regular: %s </font></td>'%reg
1360                    print '<td bgcolor=white align=left><font color=black> Min Degree: %s </font></td>'%(b[i][5])
1361                    print '<td bgcolor=white align=left><font color=black> Average Degree: %s </font></td>'%(b[i][11])
1362                    print '</tr><tr>'
1363                    print '<td bgcolor=white align=left><font color=black> Degree Sequence: %s </font></td>'%(b[i][17])
1364                    print '<td bgcolor=white align=left><font color=black> Max Degree: %s </font></td>'%(b[i][8])
1365                    print '<td bgcolor=white align=left><font color=black> Degree SD: %s </font></td></tr>'%(b[i][14])
1366                    top_row = False
1367                elif ( tables[j] == 'graph_data' ):
1368                    if ( not top_row ): print '<tr>'
1369                    print '<td bgcolor=white align=left><font color=black> Vertices: %s </font></td>'%(b[i][1])
1370                    print '<td bgcolor=white align=left><font color=black> Hamiltonian Cycles: %s </font></td>'%(b[i][10])
1371                    print '<td bgcolor=white align=left><font color=black> Eulerian: %s </font></td>'%eul
1372                    print '</tr><tr>'
1373                    print '<td bgcolor=white align=left><font color=black> Edges: %s </font></td>'%(b[i][4])
1374                    print '<td bgcolor=white align=left><font color=black> Lovasz Number: %s </font></td>'%(b[i][22])
1375                    print '<td bgcolor=white align=left><font color=black> Planar: %s </font></td>'%plan
1376                    print '</tr><tr>'
1377                    print '<td bgcolor=white align=left><font color=black> Cycles: %s </font></td>'%(b[i][7])
1378                    print '<td bgcolor=white align=left><font color=black> Complement: %s </font></td>'%(b[i][35])
1379                    print '<td bgcolor=white align=left><font color=black> Perfect: %s </font></td></tr>'%perf
1380                    top_row = False
1381                elif ( tables[j] == 'misc' ):
1382                    if ( top_row == False ): print '<tr>'
1383                    print '<td bgcolor=white align=left><font color=black> Girth: %s </font></td>'%(b[i][23])
1384                    print '<td bgcolor=white align=left><font color=black> Clique Number: %s </font></td>'%(b[i][30])
1385                    print '<td bgcolor=white align=left><font color=black> Cut Vertices: %s </font></td>'%(b[i][25])
1386                    print '</tr><tr>'
1387                    print '<td bgcolor=white align=left><font color=black> Radius: %s </font></td>'%(b[i][27])
1388                    print '<td bgcolor=white align=left><font color=black> Independence Number: %s </font></td>'%(b[i][26])
1389                    print '<td bgcolor=white align=left><font color=black> Min Vertex Cover Size: %s </font></td>'%(b[i][29])
1390                    print '</tr><tr>'
1391                    print '<td bgcolor=white align=left><font color=black> Diameter: %s </font></td>'%(b[i][31])
1392                    print '<td bgcolor=white align=left><font color=black> Vertex Connectivity: %s </font></td>'%(b[i][18])
1393                    print '<td bgcolor=white align=left><font color=black> Spanning Trees: %s </font></td>'%(b[i][33])
1394                    print '</tr><tr>'
1395                    print '<td bgcolor=white align=left><font color=black> Components: %s </font></td>'%(b[i][34])
1396                    print '<td bgcolor=white align=left><font color=black> Edge Connectivity: %s </font></td><td bgcolor=white></td>'%(b[i][21])
1397                    print '</tr><tr><td bgcolor=white align=left colspan="3"><font color=black> Induced Subgraphs: %s </font></td></tr>'%(b[i][37])
1398                    top_row = False
1399                if ( tables[j] == 'spectrum' ):
1400                    if ( not top_row ): print '<tr>'
1401                    print '<td bgcolor=white align=left><font color=black> Min Eigenvalue: %s </font></td>'%(b[i][20])
1402                    print '<td bgcolor=white align=left><font color=black> Max Eigenvalue: %s </font></td>'%(b[i][24])
1403                    print '<td bgcolor=white align=left><font color=black> Eigenvalues SD: %s </font></td>'%(b[i][28])
1404                    print '</tr><tr>'
1405                    print '<td bgcolor=white align=left><font color=black> Energy: %s </font></td>'%(b[i][32])
1406                    print '<td bgcolor=white align=left colspan="2"><font color=black> Spectrum: %s </font></td></tr>'%(b[i][36])
1407                    top_row = False
1408           
1409            if ( i != len(b)-1 ): print '<tr><td bgcolor=lightblue colspan="4" height="3"></td></tr>'
1410                       
1411        print "</table></html>"
1412        return
1413       
1414    def display_properties(self, properties=None, layout='circular', query=None, graph6=None, \
1415                           num_vertices=None, num_edges=None, num_cycles=None, num_hamiltonian_cycles=None, \
1416                           eulerian=None, planar=None, perfect=None, lovasz_number=None, \
1417                           complement_graph6=None, aut_grp_size=None, num_orbits=None, \
1418                           num_fixed_points=None, vertex_transitive=None, edge_transitive=None, \
1419                           degree_sequence=None, min_degree=None, max_degree=None, \
1420                           average_degree=None, degrees_sd=None, regular=None, \
1421                           vertex_connectivity=None, edge_connectivity=None, \
1422                           num_components=None, girth=None, radius=None, diameter=None, \
1423                           clique_number=None, independence_number=None, num_cut_vertices=None, \
1424                           min_vertex_cover_size=None, num_spanning_trees=None, \
1425                           induced_subgraphs=None, spectrum=None, min_eigenvalue=None, \
1426                           max_eigenvalue=None, eigenvalues_sd=None, energy=None):
1427        r"""
1428        Displays the results of a query in a table, including all specified
1429        properties and an image for each graph.
1430       
1431        INPUT:
1432            query -- (GenericSQLQuery) A sqlite query for graphs.db (See examples below).
1433            properties -- (List) A list of strings that are the exact name (as
1434                                 the following parameters) of the properties to
1435                                 display with the results.
1436            layout -- (String) The layout option for the graph image.  Options include:
1437                               'circular' -- plots the graph with vertices evenly
1438                                             distributed on a circle
1439                               'spring' -- uses the traditional spring layout
1440            aut_grp_size -- (Integer) The desired size of the automorphism group.
1441                            (List) Format: [<String>,<Integer>] WHERE the first
1442                                           entry represents an inequality:
1443                                           '=','>','<','>=','<='
1444            average_degree -- (Real) The desired average degree.
1445                              (List) Format: [<String>,<Real>] WHERE the first
1446                                             entry represents an inequality:
1447                                             '=','>','<','>=','<='
1448            clique_number -- (Integer) The desired clique number.
1449                             (List) Format: [<String>,<Integer>] WHERE the first
1450                                            entry represents an inequality:
1451                                            '=','>','<','>=','<='
1452            complement_graph6 -- (String) A graph6 string isomorphic to the
1453                                          desired complement graph.
1454                                 (List) A list of graph6 strings.  Will search
1455                                        for graphs with complement isomorphic to
1456                                        any string in the list.
1457            degree_sequence -- (Integer) The desired sequence of degrees.
1458                                         (Ordered highest to lowest).
1459            degrees_sd -- (Real) The desired standard deviation of degrees.
1460                          (List) Format: [<String>,<Real>] WHERE the first
1461                                         entry represents an inequality:
1462                                         '=','>','<','>=','<='
1463            diameter -- (Real) The desired diameter.
1464                        (List) Format: [<String>,<Real>] WHERE the first
1465                                        entry represents an inequality:
1466                                        '=','>','<','>=','<='
1467            edge_connectivity -- (Integer) The desired edge connectivity.
1468                                 (List) Format: [<String>,<Integer>] WHERE the first
1469                                        entry represents an inequality:
1470                                        '=','>','<','>=','<='
1471            edge_transitive -- (Boolean)
1472            eigenvalues_sd -- (Real) The desired standard deviation of eigenvalues.
1473                              (List) Format: [<String>,<Real>] WHERE the first
1474                                     entry represents an inequality:
1475                                     '=','>','<','>=','<='
1476            energy -- (Real) The desired energy.
1477                      (List) Format: [<String>,<Real>] WHERE the first entry
1478                                     represents an inequality: '=','>','<','>=','<='
1479            eulerian -- (Boolean)
1480            girth -- (Integer) The desired girth.
1481                     (List) Format: [<String>,<Integer>] WHERE the first entry
1482                                    represents an inequality: '=','>','<','>=','<='
1483            graph6 -- (String) A graph6 string isomorphic to the desired graph.
1484                      (List) A list of graph6 strings.  Will search for graphs
1485                             isomorphic to any string in the list.
1486            independence_number -- (Integer) The desired independence number.
1487                                   (List) Format: [<String>,<Integer>] WHERE the
1488                                   first entry represents an inequality:
1489                                   '=','>','<','>=','<='
1490            induced_subgraphs -- (String) graph6 string isomorphic to desired subgraph.
1491                                 (List) Format options:
1492                                        1. ['one_of',<String>,...,<String>]
1493                                           Will search for graphs containing a subgraph
1494                                           isomorphic to any of the graph6 strings in
1495                                           the list.
1496                                        2. ['all_of',<String>,...,<String>]
1497                                           Will search for graphs containing a subgraph
1498                                           isomorphic to each of the graph6 strings in
1499                                           the list.
1500            lovasz_number -- (Real) The desired lovasz number.
1501                             (List) Format: [<String>,<Real>] WHERE the first entry
1502                                    represents an inequality: '=','>','<','>=','<='
1503            max_degree -- (Integer) The desired maximum degree.
1504                          (List) Format: [<String>,<Integer>] WHERE the first entry
1505                                 represents an inequality: '=','>','<','>=','<='
1506            max_eigenvalue -- (Real) The desired maximum eigenvalue.
1507                              (List) Format: [<String>,<Real>] WHERE the first entry
1508                                     represents an inequality: '=','>','<','>=','<='
1509            min_degree -- (Integer) The desired minimum degree.
1510                          (List) Format: [<String>,<Integer>] WHERE the first entry
1511                                 represents an inequality: '=','>','<','>=','<='
1512            min_eigenvalue -- (Real) The desired minimum eigenvalue.
1513                              (List) Format: [<String>,<Real>] WHERE the first entry
1514                                     represents an inequality: '=','>','<','>=','<='
1515            min_vertex_cover_size -- (Integer) The desired minimum vertex cover size.
1516                                     (List) Format: [<String>,<Integer>] WHERE the
1517                                            first entry represents an inequality:
1518                                            '=','>','<','>=','<='
1519            num_components -- (Integer) The desired number of components.
1520                              (List) Format: [<String>,<Integer>] WHERE the first
1521                                     entry represents an inequality:
1522                                     '=','>','<','>=','<='
1523            num_cut_vertices -- (Integer) The desired number of cut vertices.
1524                                (List) Format: [<String>,<Integer>] WHERE the first
1525                                     entry represents an inequality:
1526                                     '=','>','<','>=','<='
1527            num_cycles -- (Integer) The desired number of cycles.
1528                          (List) Format: [<String>,<Integer>] WHERE the first entry
1529                                 represents an inequality: '=','>','<','>=','<='
1530            num_edges -- (Integer) The desired number of edges.
1531                         (List) Format: [<String>,<Integer>] WHERE the first entry
1532                                represents an inequality: '=','>','<','>=','<='
1533            num_fixed_points -- (Integer) The desired number of fixed points.
1534                                (List) Format: [<String>,<Integer>] WHERE the first
1535                                       entry represents an inequality:
1536                                       '=','>','<','>=','<='
1537            num_hamiltonian_cycles -- (Integer) The desired number of hamiltonian cycles.
1538                                      (List) Format: [<String>,<Integer>] WHERE the first
1539                                             entry represents an inequality:
1540                                             '=','>','<','>=','<='
1541            num_orbits -- (Integer) The desired number of orbits.
1542                          (List) Format: [<String>,<Integer>] WHERE the first entry
1543                                 represents an inequality: '=','>','<','>=','<='
1544            num_spanning_trees -- (Integer) The desired number of spanning trees.
1545                                  (List) Format: [<String>,<Integer>] WHERE the first
1546                                         entry represents an inequality:
1547                                         '=','>','<','>=','<='
1548            num_vertices -- (Integer) The desired number of vertices.
1549                            (List) Format: [<String>,<Integer>] WHERE the first entry
1550                            represents an inequality: '=','>','<','>=','<='
1551            perfect -- (Boolean)
1552            planar -- (Boolean)
1553            radius -- (Integer) The desired radius.
1554                      (List) Format: [<String>,<Integer>] WHERE the first entry represents
1555                             an inequality: '=','>','<','>=','<='
1556            regular -- (Boolean)
1557            spectrum -- (String) The desired spectrum.  (Ordered highest to lowest,
1558                                 delimited by ', ' and rounded to 6 decimal places).
1559            vertex_connectivity -- (Integer) The desired vertex connectivity.
1560                                   (List) Format: [<String>,<Integer>] WHERE the first
1561                                          entry represents an inequality:
1562                                          '=','>','<','>=','<='
1563            vertex_transitive -- (Boolean)
1564           
1565        EXAMPLES:
1566        TODO
1567        The basics:
1568            sage.: graphs_query.display_properties(properties=['num_vertices','lovasz_number',\
1569            ...                             'girth','radius','diameter'], num_vertices=5,\
1570            ...                             lovasz_number=3.0, girth=4, radius=2, diameter=3)
1571            sage.: graphs_query.display_properties(properties=['num_hamiltonian_cycles','regular',\
1572            ...                             'perfect','num_cycles','num_edges','spectrum'], \
1573            ...                             layout='spring', num_hamiltonian_cycles=2,\
1574            ...                             regular=True, perfect=False)
1575            sage.: graphs_query.display_properties(properties=['min_degree','max_degree',\
1576            ...                                'degrees_sd','average_degree','regular',\
1577            ...                                'induced_subgraphs'],layout='spring',\
1578            ...                                degree_sequence=433211)
1579           
1580        Using Inequalities:
1581            sage.: graphs_query.display_properties(properties=['energy','spectrum','eigenvalues_sd',\
1582            ...                             'complement_graph6'], layout='circular', \
1583            ...                             min_eigenvalue=['=',-1], eigenvalues_sd=['<=',1], \
1584            ...                             energy=['>',5])
1585           
1586        The query string:
1587            sage.: graphs_query.display_properties(properties=['eulerian','perfect','planar','regular',\
1588            ...             'edge_transitive','vertex_transitive','num_cycles','degree_sequence',\
1589            ...             'induced_subgraphs','num_vertices','max_degree'], layout='spring', \
1590            ...             query='SELECT graph_data.graph6 \
1591            ...             FROM graph_data WHERE num_vertices<=4 \
1592            ...             and num_edges>3')
1593            sage.: graphs_query.display_properties(query='SELECT graph_data.graph6 FROM graph_data \
1594            ...             INNER JOIN degrees on graph_data.graph_id=degrees.graph_id \
1595            ...             WHERE num_vertices>6 and eulerian=1 and regular=0 and planar=1 \
1596            ...             and num_cycles<=2', properties=['clique_number','independence_number'])
1597            sage.: graphs_query.display_properties(query="SELECT graph_data.graph6 \
1598            ...             FROM graph_data INNER JOIN misc on \
1599            ...             misc.graph_id=graph_data.graph_id WHERE \
1600            ...             misc.induced_subgraphs regexp '.*E~~w.*'", \
1601            ...             properties=['induced_subgraphs'])
1602        """
1603        from sage.plot.plot import plot
1604
1605        if ( query is None):
1606            param = None
1607            query = __query_string__(graph6=graph6, num_vertices=num_vertices, num_edges=num_edges, num_cycles=num_cycles, num_hamiltonian_cycles=num_hamiltonian_cycles, eulerian=eulerian, planar=planar, perfect=perfect, lovasz_number=lovasz_number, complement_graph6=complement_graph6, aut_grp_size=aut_grp_size, num_orbits=num_orbits, num_fixed_points=num_fixed_points, vertex_transitive=vertex_transitive, edge_transitive=edge_transitive, degree_sequence=degree_sequence, min_degree=min_degree, max_degree=max_degree, average_degree=average_degree, degrees_sd=degrees_sd, regular=regular, vertex_connectivity=vertex_connectivity, edge_connectivity=edge_connectivity, num_components=num_components, girth=girth, radius=radius, diameter=diameter, clique_number=clique_number, independence_number=independence_number, num_cut_vertices=num_cut_vertices, min_vertex_cover_size=min_vertex_cover_size, num_spanning_trees=num_spanning_trees, induced_subgraphs=induced_subgraphs, spectrum=spectrum, min_eigenvalue=min_eigenvalue, max_eigenvalue=max_eigenvalue, eigenvalues_sd=eigenvalues_sd, energy=energy)
1608            query = re.sub('INNER JOIN .* WHERE','INNER JOIN aut_grp on aut_grp.graph_id=graph_data.graph_id INNER JOIN degrees on degrees.graph_id=graph_data.graph_id INNER JOIN misc on misc.graph_id=graph_data.graph_id INNER JOIN spectrum on spectrum.graph_id=graph_data.graph_id WHERE',query)
1609            query = re.sub('FROM graph_data WHERE','FROM graph_data INNER JOIN aut_grp on aut_grp.graph_id=graph_data.graph_id INNER JOIN degrees on degrees.graph_id=graph_data.graph_id INNER JOIN misc on misc.graph_id=graph_data.graph_id INNER JOIN spectrum on spectrum.graph_id=graph_data.graph_id WHERE',query)
1610            query = re.sub('SELECT graph_data.graph6','SELECT graph_data.graph6,graph_data.num_vertices,degrees.regular,aut_grp.aut_grp_size,graph_data.num_edges,degrees.min_degree,aut_grp.num_orbits,graph_data.num_cycles,degrees.max_degree,aut_grp.num_fixed_points,graph_data.num_hamiltonian_cycles,degrees.average_degree,aut_grp.vertex_transitive,graph_data.eulerian,degrees.degrees_sd,aut_grp.edge_transitive,graph_data.planar,degrees.degree_sequence,misc.vertex_connectivity,graph_data.perfect,spectrum.min_eigenvalue,misc.edge_connectivity,graph_data.lovasz_number,misc.girth,spectrum.max_eigenvalue,misc.num_cut_vertices,misc.independence_number,misc.radius,spectrum.eigenvalues_sd,misc.min_vertex_cover_size,misc.clique_number,misc.diameter,spectrum.energy,misc.num_spanning_trees,misc.num_components,graph_data.complement_graph6,spectrum.spectrum,misc.induced_subgraphs',query)
1611        else:
1612            # Deal only with the string:
1613            param = query.__param_tuple__
1614            query = query.__query_string__           
1615
1616        cur = (self.__connection__).cursor()           
1617        if param is None:
1618            a = cur.execute(query)
1619            b = a.fetchall()
1620        else:
1621            tup = []
1622            # make it a tuple of strings:
1623            for i in range (len(param)):
1624                tup.append(str(param[i]))
1625            exe = cur.execute(query, tuple(tup))
1626            b = exe.fetchall() 
1627
1628        graph6list = []
1629        for i in range (len(b)):
1630            graph6 = str(b[i][0])
1631            g = graph.Graph('%s'%graph6)
1632            # The following line is time consuming and should not stay:
1633            graph6list.append(g.graph6_string())
1634            p = g.plot(layout=layout, vertex_size=30, vertex_labels=False, graph_border=False)
1635            p.save('%s.png'%i, figsize=[1,1])
1636       
1637        cells = len(properties)
1638        rows = 0       
1639        max_cols = 0
1640       
1641        for i in range(len(properties)):
1642            if ( properties[i] == 'spectrum' or properties[i] == 'induced_subgraphs' ):
1643                properties.append(properties[i])
1644                properties[i] = 'skip'
1645                cells -= 1
1646                rows += 1
1647            elif (max_cols < 3):
1648                max_cols += 1
1649               
1650        if (max_cols == 0): max_cols = 1
1651        rows += (cells)/3
1652        if ( cells%3 != 0 ): rows += 1
1653
1654        print "<html>"
1655        print '<table bgcolor=lightgrey cellpadding=0>'
1656       
1657        from sage.misc.multireplace import multiple_replace
1658        to_bool = {'0':"False", '1':"True"}
1659
1660        for i in range(len(b)):
1661            eul = multiple_replace(to_bool,'%s'%b[i][13])
1662            reg = multiple_replace(to_bool,'%s'%b[i][2])
1663            plan = multiple_replace(to_bool,'%s'%b[i][16])
1664            perf = multiple_replace(to_bool,'%s'%b[i][19])
1665            vtran = multiple_replace(to_bool,'%s'%b[i][12])
1666            etran = multiple_replace(to_bool,'%s'%b[i][15])
1667
1668            print '<tr><td bgcolor=white align=center rowspan="%d"><img src="cell://%s.png"><br>%s</td>'%(rows,i,graph6list[i])
1669                       
1670            top_row = True
1671            index = 0
1672            j = 0
1673            while ( j < rows ):
1674                if ( not top_row ): print '<tr>'
1675                top_row = False
1676                if ( properties[index] == 'spectrum' ): 
1677                    print '<td bgcolor=white align=left colspan="%d"><font color=black> Spectrum: %s </font></td></tr>'%(max_cols,b[i][36])
1678                    index += 1
1679                    j += 1
1680                elif ( properties[index] == 'induced_subgraphs' ): 
1681                    print '<td bgcolor=white align=left colspan="%d"><font color=black> Induced Subgraphs: %s </font></td></tr>'%(max_cols,b[i][37])
1682                    index += 1
1683                    j += 1
1684                else:
1685                    for k in range (max_cols):
1686                        while ( properties[index] == 'skip' ): index += 1
1687                        if ( properties[index] == 'spectrum' or properties[index] == 'induced_subgraphs' ):
1688                            if ( k == 0 ):
1689                                j -= 1
1690                                top_row = True
1691                                break   
1692                            else:
1693                                for m in range (max_cols-k):
1694                                    print '<td bgcolor=white></td>'
1695                                break
1696                        print '<td bgcolor=white align=left><font color=black> '
1697                        if ( properties[index] == 'num_vertices' ): print 'Vertices: %s '%b[i][1]
1698                        elif ( properties[index] == 'regular' ): print 'Regular: %s '%reg
1699                        elif ( properties[index] == 'aut_grp_size' ): print 'Aut Group Size: %s '%(b[i][3])
1700                        elif ( properties[index] == 'num_edges' ): print 'Edges: %s '%(b[i][4])
1701                        elif ( properties[index] == 'min_degree' ): print 'Min Degree: %s '%(b[i][5])
1702                        elif ( properties[index] == 'num_orbits' ): print 'Orbits: %s '%(b[i][6])
1703                        elif ( properties[index] == 'num_cycles' ): print 'Cycles: %s '%(b[i][7])
1704                        elif ( properties[index] == 'max_degree' ): print 'Max Degree: %s '%(b[i][8])
1705                        elif ( properties[index] == 'num_fixed_points' ): print 'Fixed Points: %s '%(b[i][9])
1706                        elif ( properties[index] == 'num_hamiltonian_cycles' ): print 'Hamiltonian Cycles: %s '%(b[i][10])
1707                        elif ( properties[index] == 'average_degree' ): print 'Average Degree: %s '%(b[i][11])
1708                        elif ( properties[index] == 'vertex_transitive' ): print 'Vertex Transitive: %s '%vtran
1709                        elif ( properties[index] == 'eulerian' ): print 'Eulerian: %s '%eul
1710                        elif ( properties[index] == 'degrees_sd' ): print 'Degree SD: %s '%(b[i][14])
1711                        elif ( properties[index] == 'edge_transitive' ): print 'Edge Transitive: %s '%etran
1712                        elif ( properties[index] == 'planar' ): print 'Planar: %s '%plan
1713                        elif ( properties[index] == 'degree_sequence' ): print 'Degree Sequence: %s '%(b[i][17])
1714                        elif ( properties[index] == 'vertex_connectivity' ): print 'Vertex Connectivity: %s '%(b[i][18])
1715                        elif ( properties[index] == 'perfect' ): print 'Perfect: %s '%perf
1716                        elif ( properties[index] == 'min_eigenvalue' ): print 'Min Eigenvalue: %s '%(b[i][20])
1717                        elif ( properties[index] == 'edge_connectivity' ): print 'Edge Connectivity: %s '%(b[i][21])
1718                        elif ( properties[index] == 'lovasz_number' ): print 'Lovasz Number: %s '%(b[i][22])
1719                        elif ( properties[index] == 'girth' ): print 'Girth: %s '%(b[i][23])
1720                        elif ( properties[index] == 'max_eigenvalue' ): print 'Max Eigenvalue: %s '%(b[i][24])
1721                        elif ( properties[index] == 'num_cut_vertices' ): print 'Cut Vertices: %s '%(b[i][25])
1722                        elif ( properties[index] == 'independence_number' ): print 'Independence Number: %s '%(b[i][26])
1723                        elif ( properties[index] == 'radius' ): print 'Radius: %s '%(b[i][27])
1724                        elif ( properties[index] == 'eigenvalues_sd' ): print 'Eigenvalues SD: %s '%(b[i][28])
1725                        elif ( properties[index] == 'min_vertex_cover_size' ): print 'Min Vertex Cover Size: %s '%(b[i][29])
1726                        elif ( properties[index] == 'clique_number' ): print 'Clique Number: %s '%(b[i][30])
1727                        elif ( properties[index] == 'diameter' ): print 'Diameter: %s '%(b[i][31])
1728                        elif ( properties[index] == 'energy' ): print 'Energy: %s '%(b[i][32])
1729                        elif ( properties[index] == 'num_spanning_trees' ): print 'Spanning Trees: %s '%(b[i][33])
1730                        elif ( properties[index] == 'num_components' ): print 'Components: %s '%(b[i][34])
1731                        elif ( properties[index] == 'complement_graph6' ): print 'Complement: %s '%(b[i][35])
1732                        print '</font></td>'
1733                        index += 1
1734                        if ( index >= len(properties) ): 
1735                            if ( k != max_cols-1 ):
1736                                for m in range (max_cols-k):
1737                                    print '<td bgcolor=white></td>'
1738                            break
1739                    if ( not top_row ): 
1740                        print '</tr>'
1741                        top_row = False
1742                    j += 1
1743           
1744            if ( i != len(b)-1 ): print '<tr><td bgcolor=lightblue colspan="%d" height="3"></td></tr>'%(max_cols+1)
1745                       
1746        print "</table></html>"
1747        return
1748       
1749    def get_list(self, query=None, graph6=None, num_vertices=None, num_edges=None, \
1750                 num_cycles=None, num_hamiltonian_cycles=None, eulerian=None, planar=None, \
1751                 perfect=None, lovasz_number=None, complement_graph6=None, aut_grp_size=None, \
1752                 num_orbits=None, num_fixed_points=None, vertex_transitive=None, \
1753                 edge_transitive=None, degree_sequence=None, min_degree=None, \
1754                 max_degree=None, average_degree=None, degrees_sd=None, regular=None, \
1755                 vertex_connectivity=None, edge_connectivity=None, num_components=None, \
1756                 girth=None, radius=None, diameter=None, clique_number=None, \
1757                 independence_number=None, num_cut_vertices=None, min_vertex_cover_size=None, \
1758                 num_spanning_trees=None, induced_subgraphs=None, spectrum=None, \
1759                 min_eigenvalue=None, max_eigenvalue=None, eigenvalues_sd=None, energy=None):
1760        r"""
1761        Returns a list of SAGE graphs according to provided parameters.
1762
1763        INPUT:
1764            query -- (GenericSQLQuery) A sqlite query for graphs.db (See examples below).
1765            aut_grp_size -- (Integer) The desired size of the automorphism group.
1766                            (List) Format: [<String>,<Integer>] WHERE the first
1767                                           entry represents an inequality:
1768                                           '=','>','<','>=','<='
1769            average_degree -- (Real) The desired average degree.
1770                              (List) Format: [<String>,<Real>] WHERE the first
1771                                             entry represents an inequality:
1772                                             '=','>','<','>=','<='
1773            clique_number -- (Integer) The desired clique number.
1774                             (List) Format: [<String>,<Integer>] WHERE the first
1775                                            entry represents an inequality:
1776                                            '=','>','<','>=','<='
1777            complement_graph6 -- (String) A graph6 string isomorphic to the
1778                                          desired complement graph.
1779                                 (List) A list of graph6 strings.  Will search
1780                                        for graphs with complement isomorphic to
1781                                        any string in the list.
1782            degree_sequence -- (Integer) The desired sequence of degrees.
1783                                         (Ordered highest to lowest).
1784            degrees_sd -- (Real) The desired standard deviation of degrees.
1785                          (List) Format: [<String>,<Real>] WHERE the first
1786                                         entry represents an inequality:
1787                                         '=','>','<','>=','<='
1788            diameter -- (Real) The desired diameter.
1789                        (List) Format: [<String>,<Real>] WHERE the first
1790                                        entry represents an inequality:
1791                                        '=','>','<','>=','<='
1792            edge_connectivity -- (Integer) The desired edge connectivity.
1793                                 (List) Format: [<String>,<Integer>] WHERE the first
1794                                        entry represents an inequality:
1795                                        '=','>','<','>=','<='
1796            edge_transitive -- (Boolean)
1797            eigenvalues_sd -- (Real) The desired standard deviation of eigenvalues.
1798                              (List) Format: [<String>,<Real>] WHERE the first
1799                                     entry represents an inequality:
1800                                     '=','>','<','>=','<='
1801            energy -- (Real) The desired energy.
1802                      (List) Format: [<String>,<Real>] WHERE the first entry
1803                                     represents an inequality: '=','>','<','>=','<='
1804            eulerian -- (Boolean)
1805            girth -- (Integer) The desired girth.
1806                     (List) Format: [<String>,<Integer>] WHERE the first entry
1807                                    represents an inequality: '=','>','<','>=','<='
1808            graph6 -- (String) A graph6 string isomorphic to the desired graph.
1809                      (List) A list of graph6 strings.  Will search for graphs
1810                             isomorphic to any string in the list.
1811            independence_number -- (Integer) The desired independence number.
1812                                   (List) Format: [<String>,<Integer>] WHERE the
1813                                   first entry represents an inequality:
1814                                   '=','>','<','>=','<='
1815            induced_subgraphs -- (String) graph6 string isomorphic to desired subgraph.
1816                                 (List) Format options:
1817                                        1. ['one_of',<String>,...,<String>]
1818                                           Will search for graphs containing a subgraph
1819                                           isomorphic to any of the graph6 strings in
1820                                           the list.
1821                                        2. ['all_of',<String>,...,<String>]
1822                                           Will search for graphs containing a subgraph
1823                                           isomorphic to each of the graph6 strings in
1824                                           the list.
1825            lovasz_number -- (Real) The desired lovasz number.
1826                             (List) Format: [<String>,<Real>] WHERE the first entry
1827                                    represents an inequality: '=','>','<','>=','<='
1828            max_degree -- (Integer) The desired maximum degree.
1829                          (List) Format: [<String>,<Integer>] WHERE the first entry
1830                                 represents an inequality: '=','>','<','>=','<='
1831            max_eigenvalue -- (Real) The desired maximum eigenvalue.
1832                              (List) Format: [<String>,<Real>] WHERE the first entry
1833                                     represents an inequality: '=','>','<','>=','<='
1834            min_degree -- (Integer) The desired minimum degree.
1835                          (List) Format: [<String>,<Integer>] WHERE the first entry
1836                                 represents an inequality: '=','>','<','>=','<='
1837            min_eigenvalue -- (Real) The desired minimum eigenvalue.
1838                              (List) Format: [<String>,<Real>] WHERE the first entry
1839                                     represents an inequality: '=','>','<','>=','<='
1840            min_vertex_cover_size -- (Integer) The desired minimum vertex cover size.
1841                                     (List) Format: [<String>,<Integer>] WHERE the
1842                                            first entry represents an inequality:
1843                                            '=','>','<','>=','<='
1844            num_components -- (Integer) The desired number of components.
1845                              (List) Format: [<String>,<Integer>] WHERE the first
1846                                     entry represents an inequality:
1847                                     '=','>','<','>=','<='
1848            num_cut_vertices -- (Integer) The desired number of cut vertices.
1849                                (List) Format: [<String>,<Integer>] WHERE the first
1850                                     entry represents an inequality:
1851                                     '=','>','<','>=','<='
1852            num_cycles -- (Integer) The desired number of cycles.
1853                          (List) Format: [<String>,<Integer>] WHERE the first entry
1854                                 represents an inequality: '=','>','<','>=','<='
1855            num_edges -- (Integer) The desired number of edges.
1856                         (List) Format: [<String>,<Integer>] WHERE the first entry
1857                                represents an inequality: '=','>','<','>=','<='
1858            num_fixed_points -- (Integer) The desired number of fixed points.
1859                                (List) Format: [<String>,<Integer>] WHERE the first
1860                                       entry represents an inequality:
1861                                       '=','>','<','>=','<='
1862            num_hamiltonian_cycles -- (Integer) The desired number of hamiltonian cycles.
1863                                      (List) Format: [<String>,<Integer>] WHERE the first
1864                                             entry represents an inequality:
1865                                             '=','>','<','>=','<='
1866            num_orbits -- (Integer) The desired number of orbits.
1867                          (List) Format: [<String>,<Integer>] WHERE the first entry
1868                                 represents an inequality: '=','>','<','>=','<='
1869            num_spanning_trees -- (Integer) The desired number of spanning trees.
1870                                  (List) Format: [<String>,<Integer>] WHERE the first
1871                                         entry represents an inequality:
1872                                         '=','>','<','>=','<='
1873            num_vertices -- (Integer) The desired number of vertices.
1874                            (List) Format: [<String>,<Integer>] WHERE the first entry
1875                            represents an inequality: '=','>','<','>=','<='
1876            perfect -- (Boolean)
1877            planar -- (Boolean)
1878            radius -- (Integer) The desired radius.
1879                      (List) Format: [<String>,<Integer>] WHERE the first entry represents
1880                             an inequality: '=','>','<','>=','<='
1881            regular -- (Boolean)
1882            spectrum -- (String) The desired spectrum.  (Ordered highest to lowest,
1883                                 delimited by ', ' and rounded to 6 decimal places).
1884            vertex_connectivity -- (Integer) The desired vertex connectivity.
1885                                   (List) Format: [<String>,<Integer>] WHERE the first
1886                                          entry represents an inequality:
1887                                          '=','>','<','>=','<='
1888            vertex_transitive -- (Boolean)
1889           
1890        EXAMPLES:
1891        TODO
1892            sage: g = graphs_query.get_list(num_vertices=5,lovasz_number=3.0,\
1893            ...                         girth=4,radius=2,diameter=3)
1894            ...
1895            sage: len(g)
1896            1
1897            sage.: g[0].show(layout='circular',figsize=[2,2],vertex_size=0,graph_border=True)
1898            sage: g = graphs_query.get_list(degree_sequence=433211)
1899            sage: graphs_list.to_graph6(g)
1900            'E@NW\nEAMw\n'
1901            sage: g = graphs_query.get_list(min_eigenvalue=['=',-1], \
1902            ...              eigenvalues_sd=['<=',1], energy=['>',5])
1903            ...
1904            sage: len(g) == graphs_query.number_of(min_eigenvalue=['=',-1], \
1905            ...              eigenvalues_sd=['<=',1], energy=['>',5])
1906            True
1907            sage: g = graphs_query.get_list(num_hamiltonian_cycles=2,regular=True,perfect=False)
1908            sage.: graphs_list.show_graphs(g)
1909            sage: g = graphs_query.get_list(query='SELECT graph_data.graph6 \
1910            ...             FROM graph_data WHERE num_vertices<=4 \
1911            ...             and num_edges>3')
1912            ...
1913            sage: graphs_list.to_graph6(g)
1914            'CN\nC]\nC^\nC~\n'
1915            sage: g = graphs_query.get_list(query='SELECT graph_data.graph6 FROM graph_data \
1916            ...             INNER JOIN degrees on graph_data.graph_id=degrees.graph_id \
1917            ...             WHERE num_vertices>6 and eulerian=1 and regular=0 and planar=1 \
1918            ...             and num_cycles<=2')
1919            ...
1920            sage: for i in range(len(g)):
1921            ...    if (g[i].is_isomorphic(Graph('FJ?GW'))):
1922            ...        print g[i].graph6_string()
1923            F@LAG
1924            sage: (Graph('FJ?GW')).is_isomorphic(Graph('F@LAG'))
1925            True
1926            sage.: g = graphs_query.get_list(query="SELECT graph_data.graph6 \   
1927            ...             FROM graph_data INNER JOIN misc on \                 
1928            ...             misc.graph_id=graph_data.graph_id WHERE \
1929            ...             misc.induced_subgraphs regexp '.*E~~w.*'")
1930            ...
1931            sage.: graphs_list.show_graphs(g)
1932            sage.: graphs_list.to_graph6(g)
1933            'E~~w\nFJ\\zw\nFJ\\~w\nFJ^~w\nFJ~~w\nFN~~w\nF^~~w\nF~~~w\n'
1934        """
1935        if ( query is None ):
1936            param = None
1937            query = __query_string__(graph6=graph6, num_vertices=num_vertices, num_edges=num_edges, num_cycles=num_cycles, num_hamiltonian_cycles=num_hamiltonian_cycles, eulerian=eulerian, planar=planar, perfect=perfect, lovasz_number=lovasz_number, complement_graph6=complement_graph6, aut_grp_size=aut_grp_size, num_orbits=num_orbits, num_fixed_points=num_fixed_points, vertex_transitive=vertex_transitive, edge_transitive=edge_transitive, degree_sequence=degree_sequence, min_degree=min_degree, max_degree=max_degree, average_degree=average_degree, degrees_sd=degrees_sd, regular=regular, vertex_connectivity=vertex_connectivity, edge_connectivity=edge_connectivity, num_components=num_components, girth=girth, radius=radius, diameter=diameter, clique_number=clique_number, independence_number=independence_number, num_cut_vertices=num_cut_vertices, min_vertex_cover_size=min_vertex_cover_size, num_spanning_trees=num_spanning_trees, induced_subgraphs=induced_subgraphs, spectrum=spectrum, min_eigenvalue=min_eigenvalue, max_eigenvalue=max_eigenvalue, eigenvalues_sd=eigenvalues_sd, energy=energy)       
1938        else:
1939            param = query.__param_tuple__
1940            query = query.__query_string__
1941
1942        cur = (self.__connection__).cursor()           
1943        if param is None:
1944            a = cur.execute(query)
1945            b = a.fetchall()
1946        else:
1947            tup = []
1948            # make it a tuple of strings:
1949            for i in range (len(param)):
1950                tup.append(str(param[i]))
1951            exe = cur.execute(query, tuple(tup))
1952            b = exe.fetchall()   
1953       
1954        glist = []
1955        for i in range (len(b)):
1956            glist.append(graph.Graph(str(b[i][0])))
1957        return glist
1958
1959    def number_of(self, query=None, graph6=None, num_vertices=None, num_edges=None, \
1960                  num_cycles=None, num_hamiltonian_cycles=None, eulerian=None, planar=None, \
1961                  perfect=None, lovasz_number=None, complement_graph6=None, aut_grp_size=None, \
1962                  num_orbits=None, num_fixed_points=None, vertex_transitive=None, \
1963                  edge_transitive=None, degree_sequence=None, min_degree=None, max_degree=None, \
1964                  average_degree=None, degrees_sd=None, regular=None, vertex_connectivity=None, \
1965                  edge_connectivity=None, num_components=None, girth=None, radius=None, \
1966                  diameter=None, clique_number=None, independence_number=None, \
1967                  num_cut_vertices=None, min_vertex_cover_size=None, num_spanning_trees=None, \
1968                  induced_subgraphs=None, spectrum=None, min_eigenvalue=None, max_eigenvalue=None, \
1969                  eigenvalues_sd=None, energy=None):
1970        r"""
1971        Returns the integer that represents the number of unlabeled graphs with 7 or
1972        fewer vertices that meet the provided search parameters.
1973       
1974        INPUT:
1975            query -- (GenericSQLQuery) A sqlite query for graphs.db (See examples below).
1976            aut_grp_size -- (Integer) The desired size of the automorphism group.
1977                            (List) Format: [<String>,<Integer>] WHERE the first
1978                                           entry represents an inequality:
1979                                           '=','>','<','>=','<='
1980            average_degree -- (Real) The desired average degree.
1981                              (List) Format: [<String>,<Real>] WHERE the first
1982                                             entry represents an inequality:
1983                                             '=','>','<','>=','<='
1984            clique_number -- (Integer) The desired clique number.
1985                             (List) Format: [<String>,<Integer>] WHERE the first
1986                                            entry represents an inequality:
1987                                            '=','>','<','>=','<='
1988            complement_graph6 -- (String) A graph6 string isomorphic to the
1989                                          desired complement graph.
1990                                 (List) A list of graph6 strings.  Will search
1991                                        for graphs with complement isomorphic to
1992                                        any string in the list.
1993            degree_sequence -- (Integer) The desired sequence of degrees.
1994                                         (Ordered highest to lowest).
1995            degrees_sd -- (Real) The desired standard deviation of degrees.
1996                          (List) Format: [<String>,<Real>] WHERE the first
1997                                         entry represents an inequality:
1998                                         '=','>','<','>=','<='
1999            diameter -- (Real) The desired diameter.
2000                        (List) Format: [<String>,<Real>] WHERE the first
2001                                        entry represents an inequality:
2002                                        '=','>','<','>=','<='
2003            edge_connectivity -- (Integer) The desired edge connectivity.
2004                                 (List) Format: [<String>,<Integer>] WHERE the first
2005                                        entry represents an inequality:
2006                                        '=','>','<','>=','<='
2007            edge_transitive -- (Boolean)
2008            eigenvalues_sd -- (Real) The desired standard deviation of eigenvalues.
2009                              (List) Format: [<String>,<Real>] WHERE the first
2010                                     entry represents an inequality:
2011                                     '=','>','<','>=','<='
2012            energy -- (Real) The desired energy.
2013                      (List) Format: [<String>,<Real>] WHERE the first entry
2014                                     represents an inequality: '=','>','<','>=','<='
2015            eulerian -- (Boolean)
2016            girth -- (Integer) The desired girth.
2017                     (List) Format: [<String>,<Integer>] WHERE the first entry
2018                                    represents an inequality: '=','>','<','>=','<='
2019            graph6 -- (String) A graph6 string isomorphic to the desired graph.
2020                      (List) A list of graph6 strings.  Will search for graphs
2021                             isomorphic to any string in the list.
2022            independence_number -- (Integer) The desired independence number.
2023                                   (List) Format: [<String>,<Integer>] WHERE the
2024                                   first entry represents an inequality:
2025                                   '=','>','<','>=','<='
2026            induced_subgraphs -- (String) graph6 string isomorphic to desired subgraph.
2027                                 (List) Format options:
2028                                        1. ['one_of',<String>,...,<String>]
2029                                           Will search for graphs containing a subgraph
2030                                           isomorphic to any of the graph6 strings in
2031                                           the list.
2032                                        2. ['all_of',<String>,...,<String>]
2033                                           Will search for graphs containing a subgraph
2034                                           isomorphic to each of the graph6 strings in
2035                                           the list.
2036            lovasz_number -- (Real) The desired lovasz number.
2037                             (List) Format: [<String>,<Real>] WHERE the first entry
2038                                    represents an inequality: '=','>','<','>=','<='
2039            max_degree -- (Integer) The desired maximum degree.
2040                          (List) Format: [<String>,<Integer>] WHERE the first entry
2041                                 represents an inequality: '=','>','<','>=','<='
2042            max_eigenvalue -- (Real) The desired maximum eigenvalue.
2043                              (List) Format: [<String>,<Real>] WHERE the first entry
2044                                     represents an inequality: '=','>','<','>=','<='
2045            min_degree -- (Integer) The desired minimum degree.
2046                          (List) Format: [<String>,<Integer>] WHERE the first entry
2047                                 represents an inequality: '=','>','<','>=','<='
2048            min_eigenvalue -- (Real) The desired minimum eigenvalue.
2049                              (List) Format: [<String>,<Real>] WHERE the first entry
2050                                     represents an inequality: '=','>','<','>=','<='
2051            min_vertex_cover_size -- (Integer) The desired minimum vertex cover size.
2052                                     (List) Format: [<String>,<Integer>] WHERE the
2053                                            first entry represents an inequality:
2054                                            '=','>','<','>=','<='
2055            num_components -- (Integer) The desired number of components.
2056                              (List) Format: [<String>,<Integer>] WHERE the first
2057                                     entry represents an inequality:
2058                                     '=','>','<','>=','<='
2059            num_cut_vertices -- (Integer) The desired number of cut vertices.
2060                                (List) Format: [<String>,<Integer>] WHERE the first
2061                                     entry represents an inequality:
2062                                     '=','>','<','>=','<='
2063            num_cycles -- (Integer) The desired number of cycles.
2064                          (List) Format: [<String>,<Integer>] WHERE the first entry
2065                                 represents an inequality: '=','>','<','>=','<='
2066            num_edges -- (Integer) The desired number of edges.
2067                         (List) Format: [<String>,<Integer>] WHERE the first entry
2068                                represents an inequality: '=','>','<','>=','<='
2069            num_fixed_points -- (Integer) The desired number of fixed points.
2070                                (List) Format: [<String>,<Integer>] WHERE the first
2071                                       entry represents an inequality:
2072                                       '=','>','<','>=','<='
2073            num_hamiltonian_cycles -- (Integer) The desired number of hamiltonian cycles.
2074                                      (List) Format: [<String>,<Integer>] WHERE the first
2075                                             entry represents an inequality:
2076                                             '=','>','<','>=','<='
2077            num_orbits -- (Integer) The desired number of orbits.
2078                          (List) Format: [<String>,<Integer>] WHERE the first entry
2079                                 represents an inequality: '=','>','<','>=','<='
2080            num_spanning_trees -- (Integer) The desired number of spanning trees.
2081                                  (List) Format: [<String>,<Integer>] WHERE the first
2082                                         entry represents an inequality:
2083                                         '=','>','<','>=','<='
2084            num_vertices -- (Integer) The desired number of vertices.
2085                            (List) Format: [<String>,<Integer>] WHERE the first entry
2086                            represents an inequality: '=','>','<','>=','<='
2087            perfect -- (Boolean)
2088            planar -- (Boolean)
2089            radius -- (Integer) The desired radius.
2090                      (List) Format: [<String>,<Integer>] WHERE the first entry represents
2091                             an inequality: '=','>','<','>=','<='
2092            regular -- (Boolean)
2093            spectrum -- (String) The desired spectrum.  (Ordered highest to lowest,
2094                                 delimited by ', ' and rounded to 6 decimal places).
2095            vertex_connectivity -- (Integer) The desired vertex connectivity.
2096                                   (List) Format: [<String>,<Integer>] WHERE the first
2097                                          entry represents an inequality:
2098                                          '=','>','<','>=','<='
2099            vertex_transitive -- (Boolean)
2100           
2101        EXAMPLES:
2102        TODO
2103            sage: graphs_query.number_of()
2104            1252
2105            sage: g = graphs_query.get_list(num_vertices=5,lovasz_number=3.0,\
2106            ...                         girth=4,radius=2,diameter=3)
2107            ...
2108            sage: h = graphs_query.number_of(num_vertices=5,lovasz_number=3.0,\
2109            ...                         girth=4,radius=2,diameter=3)
2110            ...
2111            sage: h == len(g)
2112            True
2113            sage: for i in range(8)[1:]:
2114            ...    g = graphs_query.number_of(num_vertices=i)
2115            ...    h = graphs_query.get_list(num_vertices=i)
2116            ...    print g == len(h)
2117            True
2118            True
2119            True
2120            True
2121            True
2122            True
2123            True
2124            sage: graphs_query.number_of(degree_sequence=433211)
2125            2
2126            sage: graphs_query.number_of(min_eigenvalue=['=',-1], \
2127            ...              eigenvalues_sd=['<=',1], energy=['>',5])
2128            2
2129            sage: graphs_query.number_of(num_hamiltonian_cycles=2,regular=True,perfect=False)
2130            2
2131            sage: graphs_query.number_of(query='SELECT graph_data.graph6 \
2132            ...             FROM graph_data WHERE num_vertices<=4 \
2133            ...             and num_edges>3')
2134            4
2135            sage: graphs_query.number_of(query='SELECT graph_data.graph6 FROM graph_data \
2136            ...             INNER JOIN degrees on graph_data.graph_id=degrees.graph_id \
2137            ...             WHERE num_vertices>6 and eulerian=1 and regular=0 and planar=1 \
2138            ...             and num_cycles<=2')
2139            9
2140            sage: graphs_query.number_of(num_vertices=7,num_cycles=['>',2])
2141            913
2142            sage: a = graphs_query.number_of(num_hamiltonian_cycles=['>=',2])
2143            sage: b = graphs_query.number_of(num_hamiltonian_cycles=['<',2])
2144            sage: c = graphs_query.number_of(num_hamiltonian_cycles=['<=',40])
2145            sage: d = graphs_query.number_of(num_hamiltonian_cycles=['>',40])
2146            sage: a-d == c-b
2147            True
2148            sage: q = 'SELECT graph_data.graph6 FROM graph_data WHERE \
2149            ...                      num_hamiltonian_cycles=2 or '
2150            ...
2151            sage: for i in range(40)[3:]:
2152            ...    q += 'num_hamiltonian_cycles=%d or '%i
2153            ...
2154            sage: q += 'num_hamiltonian_cycles=40'
2155            sage: long = graphs_query.number_of(query=q)
2156            sage: short = graphs_query.number_of(query='SELECT graph_data.graph6 \
2157            ...                      FROM graph_data WHERE num_hamiltonian_cycles>=2 \
2158            ...                      and num_hamiltonian_cycles<=40')
2159            ...
2160            sage: long == short == a-d
2161            True
2162            sage: graphs_query.number_of(query="SELECT graph_data.graph6 \
2163            ...             FROM graph_data INNER JOIN misc on \
2164            ...             misc.graph_id=graph_data.graph_id WHERE \
2165            ...             misc.induced_subgraphs regexp '.*E~~w.*'")
2166            8
2167        """
2168        glist = self.get_list(query=query, graph6=graph6, num_vertices=num_vertices, num_edges=num_edges, num_cycles=num_cycles, num_hamiltonian_cycles=num_hamiltonian_cycles, eulerian=eulerian, planar=planar, perfect=perfect, lovasz_number=lovasz_number, complement_graph6=complement_graph6, aut_grp_size=aut_grp_size, num_orbits=num_orbits, num_fixed_points=num_fixed_points, vertex_transitive=vertex_transitive, edge_transitive=edge_transitive, degree_sequence=degree_sequence, min_degree=min_degree, max_degree=max_degree, average_degree=average_degree, degrees_sd=degrees_sd, regular=regular, vertex_connectivity=vertex_connectivity, edge_connectivity=edge_connectivity, num_components=num_components, girth=girth, radius=radius, diameter=diameter, clique_number=clique_number, independence_number=independence_number, num_cut_vertices=num_cut_vertices, min_vertex_cover_size=min_vertex_cover_size, num_spanning_trees=num_spanning_trees, induced_subgraphs=induced_subgraphs, spectrum=spectrum, min_eigenvalue=min_eigenvalue, max_eigenvalue=max_eigenvalue, eigenvalues_sd=eigenvalues_sd, energy=energy)
2169        return len(glist)
2170       
2171def __query_string__(graph6=None, num_vertices=None, num_edges=None, num_cycles=None, num_hamiltonian_cycles=None, eulerian=None, planar=None, perfect=None, lovasz_number=None, complement_graph6=None, aut_grp_size=None, num_orbits=None, num_fixed_points=None, vertex_transitive=None, edge_transitive=None, degree_sequence=None, min_degree=None, max_degree=None, average_degree=None, degrees_sd=None, regular=None, vertex_connectivity=None, edge_connectivity=None, num_components=None, girth=None, radius=None, diameter=None, clique_number=None, independence_number=None, num_cut_vertices=None, min_vertex_cover_size=None, num_spanning_trees=None, induced_subgraphs=None, spectrum=None, min_eigenvalue=None, max_eigenvalue=None, eigenvalues_sd=None, energy=None):
2172    """
2173    Creates the query string for sqlite database.  Will query graph_data automatically,
2174    and any other tables as necessary.
2175   
2176    Applies parameters specified in get_list, number_of and display functions.
2177    """
2178    query = 'SELECT graph_data.graph6 FROM graph_data WHERE graph_data.graph_id=graph_data.graph_id and'
2179    if ( aut_grp_size != None or num_orbits != None or num_fixed_points != None or vertex_transitive != None or edge_transitive != None ):
2180        query = __aut_grp_string__(query=query, aut_grp_size=aut_grp_size, num_orbits=num_orbits, num_fixed_points=num_fixed_points, vertex_transitive=vertex_transitive, edge_transitive=edge_transitive)
2181    if ( degree_sequence != None or min_degree != None or max_degree != None or average_degree != None or degrees_sd != None or regular != None ):
2182        query = __degrees_string__(query=query, degree_sequence=degree_sequence, min_degree=min_degree, max_degree=max_degree, average_degree=average_degree, degrees_sd=degrees_sd, regular=regular)
2183    if ( vertex_connectivity != None or edge_connectivity != None or num_components != None or girth != None or radius != None or diameter != None or clique_number != None or independence_number != None or num_cut_vertices != None or min_vertex_cover_size != None or num_spanning_trees != None or induced_subgraphs != None ):
2184        query = __misc_string__(query=query, vertex_connectivity=vertex_connectivity, edge_connectivity=edge_connectivity, num_components=num_components, girth=girth, radius=radius, diameter=diameter, clique_number=clique_number, independence_number=independence_number, num_cut_vertices=num_cut_vertices, min_vertex_cover_size=min_vertex_cover_size, num_spanning_trees=num_spanning_trees, subgraph=induced_subgraphs)
2185    if ( spectrum != None or min_eigenvalue != None or max_eigenvalue != None or eigenvalues_sd != None or energy != None ):
2186        query = __spectrum_string__(query=query, spectrum=spectrum, min_eigenvalue=min_eigenvalue, max_eigenvalue=max_eigenvalue, eigenvalues_sd=eigenvalues_sd, energy=energy)
2187    if ( graph6 != None ):
2188        if (str(type(graph6)) == "<type 'list'>"):
2189            # only one_of
2190            for i in range (len(graph6)):
2191                graph6[i] = ((graph.Graph(graph6[i])).canonical_label()).graph6_string()
2192            query += ' ('
2193            for i in range (len(graph6)-1):
2194                query += "graph_data.graph6='%s' or "%graph6[i]
2195            query += "graph_data.graph6='%s') and"%graph6[len(graph6)-1]
2196        else:
2197            graph6 = ((graph.Graph(graph6)).canonical_label()).graph6_string()
2198            query += " graph_data.graph6='%s' and"%graph6
2199    if ( num_vertices != None ):
2200        if (str(type(num_vertices)) == "<type 'list'>"):
2201            query += ' graph_data.num_vertices%s%d and'%(num_vertices[0], num_vertices[1])
2202        else:
2203            query += ' graph_data.num_vertices=%d and'%num_vertices
2204    if ( num_edges != None ):
2205        if (str(type(num_edges)) == "<type 'list'>"):
2206            query += ' graph_data.num_edges%s%d and'%(num_edges[0], num_edges[1])
2207        else:
2208            query += ' graph_data.num_edges=%d and'%num_edges
2209    if ( num_cycles != None ):
2210        if (str(type(num_cycles)) == "<type 'list'>"):
2211            query += ' graph_data.num_cycles%s%d and'%(num_cycles[0], num_cycles[1])
2212        else:
2213            query += ' graph_data.num_cycles=%d and'%num_cycles
2214    if ( num_hamiltonian_cycles != None ):
2215        if (str(type(num_hamiltonian_cycles)) == "<type 'list'>"):
2216            query += ' graph_data.num_hamiltonian_cycles%s%d and'%(num_hamiltonian_cycles[0], num_hamiltonian_cycles[1])
2217        else:
2218            query += ' graph_data.num_hamiltonian_cycles=%d and'%num_hamiltonian_cycles
2219    if ( eulerian != None ):
2220        query += ' graph_data.eulerian=%d and'%eulerian
2221    if ( planar != None ):
2222        query += ' graph_data.planar=%d and'%planar
2223    if ( perfect != None ):
2224        query += ' graph_data.perfect=%d and'%perfect
2225    if ( lovasz_number != None ):
2226        if (str(type(lovasz_number)) == "<type 'list'>"):
2227            query += ' graph_data.lovasz_number%s%d and'%(lovasz_number[0], lovasz_number[1])
2228        else:
2229            query += ' graph_data.lovasz_number=%d and'%lovasz_number
2230    if ( complement_graph6 != None ):
2231        if (str(type(complement_graph6)) == "<type 'list'>"):
2232            # only one_of
2233            for i in range (len(complement_graph6)):
2234                complement_graph6[i] = ((graph.Graph(complement_graph6[i])).canonical_label()).graph6_string()
2235            query += ' ('
2236            for i in range (len(complement_graph6)-1):
2237                query += "graph_data.complement_graph6='%s' or "%complement_graph6[i]
2238            query += "graph_data.complement_graph6='%s') and"%complement_graph6[len(complement_graph6)-1]
2239        else:
2240            complement_graph6 = ((graph.Graph(complement_graph6)).canonical_label()).graph6_string()
2241            query += " graph_data.complement_graph6='%s' and"%complement_graph6
2242
2243    clean_query = re.sub(' and$','',query)
2244    return clean_query
2245       
2246def __aut_grp_string__(query=None, aut_grp_size=None, num_orbits=None, num_fixed_points=None, vertex_transitive=None, edge_transitive=None):
2247    """
2248    Appends necessary info to query string to search for graphs with
2249    properties specified in the aut_grp database table.
2250   
2251    Applies parameters specified in get_list, number_of and display functions.
2252    """
2253    r = query.split('WHERE',1)
2254    s = 'INNER JOIN aut_grp on aut_grp.graph_id=graph_data.graph_id WHERE'
2255    query = s.join(r)
2256   
2257    if ( aut_grp_size is not None ):
2258        if (str(type(aut_grp_size)) == "<type 'list'>"):
2259            query += ' aut_grp.aut_grp_size%s%d and'%(aut_grp_size[0], aut_grp_size[1])
2260        else:
2261            query += ' aut_grp.aut_grp_size=%d and'%aut_grp_size
2262    if ( num_orbits is not None ):
2263        if (str(type(num_orbits)) == "<type 'list'>"):
2264            query += ' aut_grp.num_orbits%s%d and'%(num_orbits[0], num_orbits[1])
2265        else:
2266            query += ' aut_grp.num_orbits=%d and'%num_orbits
2267    if ( num_fixed_points is not None ):
2268        if (str(type(num_fixed_points)) == "<type 'list'>"):
2269            query += ' aut_grp.num_fixed_points%s%d and'%(num_fixed_points[0], num_fixed_points[1])
2270        else:
2271            query += ' aut_grp.num_fixed_points=%d and'%num_fixed_points
2272    if ( vertex_transitive is not None ):
2273        query += ' aut_grp.vertex_transitive=%d and'%vertex_transitive
2274    if ( edge_transitive is not None ):
2275        query += ' aut_grp.edge_transitive=%d and'%edge_transitive
2276       
2277    return query
2278
2279def __degrees_string__(query=None, degree_sequence=None, min_degree=None, max_degree=None, average_degree=None, degrees_sd=None, regular=None):
2280    """
2281    Appends necessary info to query string to search for graphs with
2282    properties specified in the degrees database table.
2283   
2284    Applies parameters specified in get_list, number_of and display functions.
2285    """
2286    r = query.split('WHERE',1)
2287    s = 'INNER JOIN degrees on degrees.graph_id=graph_data.graph_id WHERE'
2288    query = s.join(r)
2289   
2290    if ( degree_sequence is not None ):
2291        query += ' degrees.degree_sequence=%d and'%degree_sequence
2292    if ( min_degree is not None ):
2293        if (str(type(min_degree)) == "<type 'list'>"):
2294            query += ' degrees.min_degree%s%d and'%(min_degree[0], min_degree[1])
2295        else:
2296            query += ' degrees.min_degree=%d and'%min_degree
2297    if ( max_degree is not None ):
2298        if (str(type(max_degree)) == "<type 'list'>"):
2299            query += ' degrees.max_degree%s%d and'%(max_degree[0], max_degree[1])
2300        else:
2301            query += ' degrees.max_degree=%d and'%max_degree
2302    if ( average_degree is not None ):
2303        if (str(type(average_degree)) == "<type 'list'>"):
2304            query += ' degrees.average_degree%s%s and'%(average_degree[0], average_degree[1])
2305        else:
2306            query += ' degrees.average_degree=%s and'%average_degree
2307    if ( degrees_sd is not None ):
2308        if (str(type(degrees_sd)) == "<type 'list'>"):
2309            query += ' degrees.degrees_sd%s%s and'%(degrees_sd[0], degrees_sd[1])
2310        else:
2311            query += ' degrees.degrees_sd=%s and'%degrees_sd
2312    if ( regular is not None ):
2313        query += ' degrees.regular=%d and'%regular
2314       
2315    return query
2316
2317def __misc_string__(query=None, vertex_connectivity=None, edge_connectivity=None, num_components=None, girth=None, radius=None, diameter=None, clique_number=None, independence_number=None, num_cut_vertices=None, min_vertex_cover_size=None, num_spanning_trees=None, subgraph=None):
2318    """
2319    Appends necessary info to query string to search for graphs with
2320    properties specified in the misc database table.
2321   
2322    Applies parameters specified in get_list, number_of and display functions.
2323    """
2324    r = query.split('WHERE',1)
2325    s = 'INNER JOIN misc on misc.graph_id=graph_data.graph_id WHERE'
2326    query = s.join(r)
2327   
2328    if (subgraph is not None):
2329        from sage.misc.multireplace import multiple_replace
2330        clean = {'[': '[[]', ']': '[]]', '?': '[?]', '{': '[{]', '}': '[}]', '^': '[^]', '|': '[|]'}
2331        if (str(type(subgraph)) == "<type 'list'>"):
2332            for i in range (len(subgraph))[1:]:
2333                subgraph[i] = ((graph.Graph(subgraph[i])).canonical_label()).graph6_string()
2334                subgraph[i] = multiple_replace(clean,subgraph[i])
2335                subgraph[i] = subgraph[i].replace('\\',"\\\\")
2336            if (subgraph[0] == 'one_of'):
2337                query += ' ('
2338                for i in range (len(subgraph)-1)[1:]:
2339                    query += "misc.induced_subgraphs regexp '.*%s.*' or "%subgraph[i]
2340                query += "misc.induced_subgraphs regexp '.*%s.*') and"%subgraph[len(subgraph)-1]
2341            elif (subgraph[0] == 'all_of'):
2342                for i in range (len(subgraph))[1:]:
2343                    query += " misc.induced_subgraphs regexp '.*%s.*' and"%subgraph[i]
2344        else:
2345            subgraph = ((graph.Graph(subgraph)).canonical_label()).graph6_string()
2346            subgraph = multiple_replace(clean,subgraph)
2347            subgraph = subgraph.replace('\\',"\\\\")
2348            query += " misc.induced_subgraphs regexp '.*%s.*' and"%subgraph
2349               
2350    if (vertex_connectivity is not None):
2351        if (str(type(vertex_connectivity)) == "<type 'list'>"):
2352            query += ' misc.vertex_connectivity%s%d and'%(vertex_connectivity[0], vertex_connectivity[1])
2353        else:
2354            query += ' misc.vertex_connectivity=%d and'%vertex_connectivity
2355    if (edge_connectivity is not None):
2356        if (str(type(edge_connectivity)) == "<type 'list'>"):
2357            query += ' misc.edge_connectivity%s%d and'%(edge_connectivity[0], edge_connectivity[1])
2358        else:
2359            query += ' misc.edge_connectivity=%d and'%edge_connectivity
2360    if (num_components is not None):
2361        if (str(type(num_components)) == "<type 'list'>"):
2362            query += ' misc.num_components%s%d and'%(num_components[0], num_components[1])
2363        else:
2364            query += ' misc.num_components=%d and'%num_components
2365    if (girth is not None):
2366        if (str(type(girth)) == "<type 'list'>"):
2367            query += ' misc.girth%s%d and'%(girth[0], girth[1])
2368        else:
2369            query += ' misc.girth=%d and'%girth
2370    if (radius is not None):
2371        if (str(type(radius)) == "<type 'list'>"):
2372            query += ' misc.radius%s%d and'%(radius[0], radius[1])
2373        else:
2374            query += ' misc.radius=%d and'%radius
2375    if (diameter is not None):
2376        if (str(type(diameter)) == "<type 'list'>"):
2377            query += ' misc.diameter%s%d and'%(diameter[0], diameter[1])
2378        else:
2379            query += ' misc.diameter=%d and'%diameter
2380    if (clique_number is not None):
2381        if (str(type(clique_number)) == "<type 'list'>"):
2382            query += ' misc.clique_number%s%d and'%(clique_number[0], clique_number[1])
2383        else:
2384            query += ' misc.clique_number=%d and'%clique_number
2385    if (independence_number is not None):
2386        if (str(type(independence_number)) == "<type 'list'>"):
2387            query += ' misc.independence_number%s%d and'%(independence_number[0], independence_number[1])
2388        else:
2389            query += ' misc.independence_number=%d and'%independence_number
2390    if (num_cut_vertices is not None):
2391        if (str(type(num_cut_vertices)) == "<type 'list'>"):
2392            query += ' misc.num_cut_vertices%s%d and'%(num_cut_vertices[0], num_cut_vertices[1])
2393        else:
2394            query += ' misc.num_cut_vertices=%d and'%num_cut_vertices
2395    if (min_vertex_cover_size is not None):
2396        if (str(type(min_vertex_cover_size)) == "<type 'list'>"):
2397            query += ' misc.min_vertex_cover_size%s%d and'%(min_vertex_cover_size[0], min_vertex_cover_size[1])
2398        else:
2399            query += ' misc.min_vertex_cover_size=%d and'%min_vertex_cover_size
2400    if (num_spanning_trees is not None):
2401        if (str(type(num_spanning_trees)) == "<type 'list'>"):
2402            query += ' misc.num_spanning_trees%s%d and'%(num_spanning_trees[0], num_spanning_trees[1])
2403        else:
2404            query += ' misc.num_spanning_trees=%d and'%num_spanning_trees
2405       
2406    return query
2407
2408def __spectrum_string__(query=None, spectrum=None, min_eigenvalue=None, max_eigenvalue=None, eigenvalues_sd=None, energy=None):
2409    """
2410    Appends necessary info to query string to search for graphs with
2411    properties specified in the spectrum database table.
2412   
2413    Applies parameters specified in get_list, number_of and display functions.
2414    """
2415    r = query.split('WHERE',1)
2416    s = 'INNER JOIN spectrum on spectrum.graph_id=graph_data.graph_id WHERE'
2417    query = s.join(r)
2418   
2419    if ( spectrum is not None ):
2420        query += ' spectrum.spectrum=%s and'%spectrum
2421    if ( min_eigenvalue is not None ):
2422        if (str(type(min_eigenvalue)) == "<type 'list'>"):
2423            query += ' spectrum.min_eigenvalue%s%s and'%(min_eigenvalue[0], min_eigenvalue[1])
2424        else:
2425            query += ' spectrum.min_eigenvalue=%s and'%min_eigenvalue
2426    if ( max_eigenvalue is not None ):
2427        if (str(type(max_eigenvalue)) == "<type 'list'>"):
2428            query += ' spectrum.max_eigenvalue%s%s and'%(max_eigenvalue[0], max_eigenvalue[1])
2429        else:
2430            query += ' spectrum.max_eigenvalue=%s and'%max_eigenvalue
2431    if ( eigenvalues_sd is not None ):
2432        if (str(type(eigenvalues_sd)) == "<type 'list'>"):
2433            query += ' spectrum.eigenvalues_sd%s%s and'%(eigenvalues_sd[0], eigenvalues_sd[1])
2434        else:
2435            query += ' spectrum.eigenvalues_sd=%s and'%eigenvalues_sd
2436    if ( energy is not None ):
2437        if (str(type(energy)) == "<type 'list'>"):
2438            query += ' spectrum.energy%s%s and'%(energy[0], energy[1])
2439        else:
2440            query += ' spectrum.energy=%s and'%energy
2441       
2442    return query
Note: See TracBrowser for help on using the repository browser.