| 1812 | |
| 1813 | def KneserGraph(self,n,k): |
| 1814 | r""" |
| 1815 | Returns the Kneser Graph with parameters `n, k`. |
| 1816 | |
| 1817 | The Kneser Graph with parameters `n,k` is the graph |
| 1818 | whose vertices are the `k`-subsets of `[0,1,\dots,n-1]`, and such |
| 1819 | that two vertices are adjacent if their corresponding sets |
| 1820 | are disjoint. |
| 1821 | |
| 1822 | For example, the Petersen Graph can be defined |
| 1823 | as the Kneser Graph with parameters `5,2`. |
| 1824 | |
| 1825 | EXAMPLE:: |
| 1826 | |
| 1827 | sage: KG=graphs.KneserGraph(5,2) |
| 1828 | sage: print KG.vertices() |
| 1829 | [{4, 5}, {1, 3}, {2, 5}, {2, 3}, {3, 4}, {3, 5}, {1, 4}, {1, 5}, {1, 2}, {2, 4}] |
| 1830 | sage: P=graphs.PetersenGraph() |
| 1831 | sage: P.is_isomorphic(KG) |
| 1832 | True |
| 1833 | |
| 1834 | TESTS:: |
| 1835 | |
| 1836 | sage: KG=graphs.KneserGraph(0,0) |
| 1837 | Traceback (most recent call last): |
| 1838 | ... |
| 1839 | ValueError: Parameter n should be a strictly positive integer |
| 1840 | sage: KG=graphs.KneserGraph(5,6) |
| 1841 | Traceback (most recent call last): |
| 1842 | ... |
| 1843 | ValueError: Parameter k should be a strictly positive integer inferior to n |
| 1844 | """ |
| 1845 | |
| 1846 | if not n>0: |
| 1847 | raise ValueError, "Parameter n should be a strictly positive integer" |
| 1848 | if not (k>0 and k<=n): |
| 1849 | raise ValueError, "Parameter k should be a strictly positive integer inferior to n" |
| 1850 | |
| 1851 | g=graph.Graph(name="Kneser graph with parameters "+str(n)+","+str(k)) |
| 1852 | from sage.combinat.subset import Subsets |
| 1853 | |
| 1854 | if k>n/2: |
| 1855 | g.add_vertices(Subsets(n,k).list()) |
| 1856 | |
| 1857 | S=Subsets(Subsets(n,k),2) |
| 1858 | l=lambda x:list(x) |
| 1859 | [g.add_edge(s,t) for [s,t] in map(l,S) if s.intersection(t).cardinality() ==0 ] |
| 1860 | return g |
| 1861 | |
| 1862 | def OddGraph(self,n): |
| 1863 | r""" |
| 1864 | Returns the Odd Graph with parameter `n`. |
| 1865 | |
| 1866 | The Odd Graph with parameter `n` is defined as the |
| 1867 | Kneser Graph with parameters `2n-1,n-1`. |
| 1868 | Equivalently, the Odd Graph is the graph whose vertices |
| 1869 | are the `n-1`-subsets of `[0,1,\dots,2(n-1)]`, and such |
| 1870 | that two vertices are adjacent if their corresponding sets |
| 1871 | are disjoint. |
| 1872 | |
| 1873 | For example, the Petersen Graph can be defined |
| 1874 | as the Odd Graph with parameter `3`. |
| 1875 | |
| 1876 | EXAMPLE:: |
| 1877 | |
| 1878 | sage: OG=graphs.OddGraph(3) |
| 1879 | sage: print OG.vertices() |
| 1880 | [{4, 5}, {1, 3}, {2, 5}, {2, 3}, {3, 4}, {3, 5}, {1, 4}, {1, 5}, {1, 2}, {2, 4}] |
| 1881 | sage: P=graphs.PetersenGraph() |
| 1882 | sage: P.is_isomorphic(OG) |
| 1883 | True |
| 1884 | |
| 1885 | TESTS:: |
| 1886 | |
| 1887 | sage: KG=graphs.OddGraph(1) |
| 1888 | Traceback (most recent call last): |
| 1889 | ... |
| 1890 | ValueError: Parameter n should be an integer strictly greater than 1 |
| 1891 | """ |
| 1892 | |
| 1893 | if not n>1: |
| 1894 | raise ValueError, "Parameter n should be an integer strictly greater than 1" |
| 1895 | return self.KneserGraph(2*n-1,n-1) |
| 1896 | |