8792 | | |
| 8792 | |
| 8793 | A Graph is a set of vertices connected by edges |
| 8794 | (cf. http://en.wikipedia.org/wiki/Graph_(mathematics) ) |
| 8795 | |
| 8796 | One can very easily create a graph in sage by typing :: |
| 8797 | |
| 8798 | sage: g=Graph() |
| 8799 | |
| 8800 | By typing the name of the Graph, one can get some basic informations |
| 8801 | about it:: |
| 8802 | |
| 8803 | sage: g |
| 8804 | Graph on 0 vertices |
| 8805 | |
| 8806 | This graph is not very interesting as it is by default the empty graph.. But |
| 8807 | Sage contains a large collection of pre-defined graph classes that can be listed |
| 8808 | this way : |
| 8809 | |
| 8810 | * type in Sage : graphs. |
| 8811 | ( do not press "Enter", and do not forget the final "." ) |
| 8812 | * hit "tabulation" two times in a row |
| 8813 | |
| 8814 | You will see the list of methods defined in the class "graphs", all of which |
| 8815 | generate graphs you can play with ! |
| 8816 | |
| 8817 | If you want to see what they look like, begin this way :: |
| 8818 | |
| 8819 | sage: g=graphs.PetersenGraph() |
| 8820 | sage: g.plot() |
| 8821 | |
| 8822 | or:: |
| 8823 | |
| 8824 | sage: g=graphs.ChvatalGraph() |
| 8825 | sage: g.plot() |
| 8826 | |
| 8827 | If you are curious about what these graphs are, for example if you wonder what ``RandomGNP`` |
| 8828 | actually is, you but have to type:: |
| 8829 | |
| 8830 | sage: graphs.RandomGNP? |
| 8831 | |
| 8832 | Once you have defined the graph you want, you can begin to work on it by using the |
| 8833 | almost 200 functions on graphs in the Sage library ! |
| 8834 | If your graph is named ``g``, you can list these functions as previously this way |
| 8835 | |
| 8836 | * type in Sage : ``g.`` |
| 8837 | ( do not press "Enter", and do not forget the final "." ) |
| 8838 | * hit "tabulation" two times in a row |
| 8839 | |
| 8840 | As usual, you can get some information about what these functions do by typing |
| 8841 | ( if you want to know about the ``diameter()`` method ):: |
| 8842 | |
| 8843 | sage: g.diameter? |
| 8844 | |
| 8845 | If you have defined a graph ``g`` having several connected components ( = which is not |
| 8846 | connected... Type ``g.is_connected()`` to know if your graph is connected ), you can |
| 8847 | print each one of its connected components with only two lines : |
| 8848 | |
| 8849 | ( if you do not have such a graph G, here is one for free : ``g=graphs.RandomGNP(30,.05)`` ):: |
| 8850 | |
| 8851 | sage: for component in g.connected_components(): |
| 8852 | ... g.subgraph(component).plot() |
| 8853 | |
| 8854 | |
8879 | | #. A NetworkX XGraph:: |
8880 | | |
8881 | | sage: import networkx |
8882 | | sage: g = networkx.XGraph({0:[1,2,3], 2:[4]}) |
8883 | | sage: Graph(g) |
8884 | | Graph on 5 vertices |
8885 | | |
8886 | | #. A NetworkX graph:: |
8887 | | |
8888 | | sage: import networkx |
8889 | | sage: g = networkx.Graph({0:[1,2,3], 2:[4]}) |
8890 | | sage: DiGraph(g) |
8891 | | Digraph on 5 vertices |
8892 | | |
8893 | | Note that in all cases, we copy the NetworkX structure. |
8894 | | |
8895 | | :: |
8896 | | |
8897 | | sage: import networkx |
8898 | | sage: g = networkx.Graph({0:[1,2,3], 2:[4]}) |
8899 | | sage: G = Graph(g, implementation='networkx') |
8900 | | sage: H = Graph(g, implementation='networkx') |
8901 | | sage: G._backend._nxg is H._backend._nxg |
8902 | | False |
8903 | | |
8904 | | |
8905 | | |
| 9127 | #. A NetworkX XGraph:: |
| 9128 | |
| 9129 | sage: import networkx |
| 9130 | sage: g = networkx.XGraph({0:[1,2,3], 2:[4]}) |
| 9131 | sage: Graph(g) |
| 9132 | Graph on 5 vertices |
| 9133 | |
| 9134 | #. A NetworkX graph:: |
| 9135 | |
| 9136 | sage: import networkx |
| 9137 | sage: g = networkx.Graph({0:[1,2,3], 2:[4]}) |
| 9138 | sage: DiGraph(g) |
| 9139 | Digraph on 5 vertices |
| 9140 | |
| 9141 | Note that in all cases, we copy the NetworkX structure. |
| 9142 | |
| 9143 | :: |
| 9144 | |
| 9145 | sage: import networkx |
| 9146 | sage: g = networkx.Graph({0:[1,2,3], 2:[4]}) |
| 9147 | sage: G = Graph(g, implementation='networkx') |
| 9148 | sage: H = Graph(g, implementation='networkx') |
| 9149 | sage: G._backend._nxg is H._backend._nxg |
| 9150 | False |