From 1a8561d3b9a30f01e582902e571680f3973890e0 Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Mon, 30 Oct 2023 10:53:05 +0000 Subject: [PATCH 01/17] Created functions to generate random k-tree and partial k-trees --- src/sage/graphs/generators/random.py | 110 +++++++++++++++++++++++++++ src/sage/graphs/graph_generators.py | 4 + 2 files changed, 114 insertions(+) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index e0868b36e21..1135b1be8a3 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1459,6 +1459,116 @@ def RandomTreePowerlaw(n, gamma=3, tries=1000, seed=None): return False +def RandomKTree(n, k, seed=None): + r""" + Returns a random k-tree on `n` nodes numbered `0` through `n-1`. + + ALGORITHM: + + The algorithm first generates a complete graph on `k+1` vertices. Vertices are + subsequently generated by randomly choosing one of the existing cliques in the graph, + and creating a new clique by replacing one of the vertices in the selected clique with + a newly created one. + + INPUT: + + - ``n`` -- number of vertices in the k-tree + + - ``k`` -- within a clique each vertex is connected to `k` vertices. `k` also corresponds + to the treewidth of the k-tree + + - ``seed`` -- a ``random.Random`` seed or a Python ``int`` for the random + number generator (default: ``None``) + + TESTS:: + sage: g=graphs.RandomKTree(50,5) + sage: g.size() + 235 + sage: len(g.vertices(sort=False)) + 50 + sage: g.treewidth() + 5 + + EXAMPLES:: + + sage: G = graphs.RandomKTree(50,5) + sage: G.treewidth() + 5 + sage: G.show() + """ + + from sage.graphs.generators.basic import CompleteGraph + + if (n g.size(): + raise ValueError("x must be less than the number of nodes in the K-Tree with `n` nodes.") + + for i in range(x): + randomEdge = g.random_edge() + g.delete_edge(randomEdge) + + return g + + def RandomRegular(d, n, seed=None): r""" Return a random `d`-regular graph on `n` vertices, or ``False`` on failure. diff --git a/src/sage/graphs/graph_generators.py b/src/sage/graphs/graph_generators.py index b7b5d350250..05c92d7e841 100644 --- a/src/sage/graphs/graph_generators.py +++ b/src/sage/graphs/graph_generators.py @@ -361,6 +361,8 @@ def wrap_name(x): "RandomHolmeKim", "RandomChordalGraph", "RandomIntervalGraph", + "RandomKTree", + "RandomPartialKTree", "RandomLobster", "RandomNewmanWattsStrogatz", "RandomRegular", @@ -2755,6 +2757,8 @@ def quadrangulations(self, order, minimum_degree=None, minimum_connectivity=None RandomNewmanWattsStrogatz = staticmethod(random.RandomNewmanWattsStrogatz) RandomRegular = staticmethod(random.RandomRegular) RandomShell = staticmethod(random.RandomShell) + RandomKTree = staticmethod(random.RandomKTree) + RandomPartialKTree = staticmethod(random.RandomPartialKTree) RandomToleranceGraph = staticmethod(random.RandomToleranceGraph) RandomTreePowerlaw = staticmethod(random.RandomTreePowerlaw) RandomTree = staticmethod(random.RandomTree) From 8bec89684a09e07385cbb58bf20238fbfd9207b6 Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Wed, 1 Nov 2023 07:58:09 +0000 Subject: [PATCH 02/17] addressed comments on commit #1e8bca1b9f1b14591f9d48a0420da18be739a790 --- src/sage/graphs/generators/random.py | 63 +++++++++++++++------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index 1135b1be8a3..be2a6197cd2 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1461,85 +1461,92 @@ def RandomTreePowerlaw(n, gamma=3, tries=1000, seed=None): def RandomKTree(n, k, seed=None): r""" - Returns a random k-tree on `n` nodes numbered `0` through `n-1`. + Return a random `k`-tree on `n` nodes numbered `0` through `n-1`. ALGORITHM: - The algorithm first generates a complete graph on `k+1` vertices. Vertices are - subsequently generated by randomly choosing one of the existing cliques in the graph, - and creating a new clique by replacing one of the vertices in the selected clique with - a newly created one. + The algorithm first generates a complete graph on `k+1` vertices. Vertices + are subsequently generated by randomly choosing one of the existing cliques + in the graph, and creating a new clique by replacing one of the vertices in + the selected clique with a newly created one. INPUT: - ``n`` -- number of vertices in the k-tree - - ``k`` -- within a clique each vertex is connected to `k` vertices. `k` also corresponds - to the treewidth of the k-tree + - ``k`` -- within a clique each vertex is connected to `k` vertices. `k` + also corresponds to the treewidth of the k-tree - ``seed`` -- a ``random.Random`` seed or a Python ``int`` for the random number generator (default: ``None``) TESTS:: + sage: g=graphs.RandomKTree(50,5) sage: g.size() 235 - sage: len(g.vertices(sort=False)) + sage: g.order() 50 sage: g.treewidth() 5 - EXAMPLES:: + EXAMPLES: - sage: G = graphs.RandomKTree(50,5) + A (random) `k`-tree has tree width `k`:: + + sage: G = graphs.RandomKTree(50, 5) sage: G.treewidth() 5 - sage: G.show() - """ + sage: G.show() # not tested + """ + if n < k + 1: + raise ValueError("n must be greater than k + 1") + + if seed is not None: + set_random_seed(seed) from sage.graphs.generators.basic import CompleteGraph - - if (n g.size(): - raise ValueError("x must be less than the number of nodes in the K-Tree with `n` nodes.") + raise ValueError("x must be less than the number of edges in the K-Tree with `n` nodes") for i in range(x): randomEdge = g.random_edge() From fd163296b844d887f2ecbdf9db9a8dd5954fcc42 Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Thu, 2 Nov 2023 08:48:24 +0000 Subject: [PATCH 03/17] address comments on commit #578a6abb8b121e7543654319d53f685e565268bc --- src/sage/graphs/generators/random.py | 49 ++++++++++++++-------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index be2a6197cd2..9578495d8a2 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1465,17 +1465,17 @@ def RandomKTree(n, k, seed=None): ALGORITHM: - The algorithm first generates a complete graph on `k+1` vertices. Vertices - are subsequently generated by randomly choosing one of the existing cliques - in the graph, and creating a new clique by replacing one of the vertices in - the selected clique with a newly created one. + The algorithm first generates a complete graph on `k + 1` vertices. + Vertices are subsequently generated by randomly choosing one of the + existing cliques in the graph, and creating a new clique by replacing + one of the vertices in the selected clique with a newly created one. INPUT: - - ``n`` -- number of vertices in the k-tree + - ``n`` -- number of vertices in the `k`-tree - - ``k`` -- within a clique each vertex is connected to `k` vertices. `k` - also corresponds to the treewidth of the k-tree + - ``k`` -- within a clique each vertex is connected to `k` vertices. `k` + also corresponds to the treewidth of the `k`-tree - ``seed`` -- a ``random.Random`` seed or a Python ``int`` for the random number generator (default: ``None``) @@ -1489,10 +1489,10 @@ def RandomKTree(n, k, seed=None): 50 sage: g.treewidth() 5 - + EXAMPLES: - A (random) `k`-tree has tree width `k`:: + A (random) `k`-tree has treewidth `k`:: sage: G = graphs.RandomKTree(50, 5) sage: G.treewidth() @@ -1500,14 +1500,12 @@ def RandomKTree(n, k, seed=None): sage: G.show() # not tested """ if n < k + 1: - raise ValueError("n must be greater than k + 1") + raise ValueError("n must be greater than `k + 1`") if seed is not None: set_random_seed(seed) - from sage.graphs.generators.basic import CompleteGraph - - g = Graph() + g = Graph(name=f"Random {k}-tree of order {n}") g.add_clique(list(range(k + 1))) cliques = [list(range(k+1))] @@ -1529,16 +1527,16 @@ def RandomPartialKTree(n, k, x, seed=None): Return a random partial `k`-tree on `n` nodes. A partial `k`-tree is defined as a subgraph of a `k`-tree. This can also be - described as a graph with treewidth at most k. + described as a graph with treewidth at most `k`. INPUT: - - ``n`` -- number of vertices in the k-tree + - ``n`` -- number of vertices in the `k`-tree - ``k`` -- within a clique each vertex is connected to `k` vertices. `k` - also corresponds to the treewidth of the k-tree + also corresponds to the treewidth of the `k`-tree - - ``x`` -- how many edges are deleted from the k-tree + - ``x`` -- how many edges are deleted from the `k`-tree - ``seed`` -- a ``random.Random`` seed or a Python ``int`` for the random number generator (default: ``None``) @@ -1555,7 +1553,7 @@ def RandomPartialKTree(n, k, x, seed=None): EXAMPLES: - sage: G = graphs.RandomKTree(50,5) + sage: G = graphs.RandomPartialKTree(50,5,5) sage: G.treewidth() 5 sage: G.show() @@ -1566,13 +1564,16 @@ def RandomPartialKTree(n, k, x, seed=None): g = RandomKTree(n,k,seed) # Check that x doesn't delete too many edges - if x > g.size(): - raise ValueError("x must be less than the number of edges in the K-Tree with `n` nodes") - - for i in range(x): - randomEdge = g.random_edge() - g.delete_edge(randomEdge) + # This formula calculates how many edges are in `k`-tree with `n` nodes + if x > ((n*(n - 1)) / 2) + (n - k - 1) * k: + raise ValueError("x must be less than the number of edges in the `k`-tree with `n` nodes") + + from sage.misc.prandom import shuffle + edges = list(g.edges()) + # Deletes x random edges from the graph + shuffle(edges) + g.delete_edges(edges[:x]) return g From d1c76e869a244275f0281a67a6e41a8d048516f8 Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Mon, 6 Nov 2023 10:05:14 +0000 Subject: [PATCH 04/17] fixed k-tree edge count formula --- src/sage/graphs/generators/random.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index 9578495d8a2..9c5f857a0be 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1500,7 +1500,7 @@ def RandomKTree(n, k, seed=None): sage: G.show() # not tested """ if n < k + 1: - raise ValueError("n must be greater than `k + 1`") + raise ValueError("n must be greater than k + 1") if seed is not None: set_random_seed(seed) @@ -1565,7 +1565,7 @@ def RandomPartialKTree(n, k, x, seed=None): # Check that x doesn't delete too many edges # This formula calculates how many edges are in `k`-tree with `n` nodes - if x > ((n*(n - 1)) / 2) + (n - k - 1) * k: + if x > (k^2 + k) / 2 + (n - k - 1) * k: raise ValueError("x must be less than the number of edges in the `k`-tree with `n` nodes") from sage.misc.prandom import shuffle From ff9d03745a54d4245b91d130d1383a0ccfb01cfd Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Sat, 11 Nov 2023 09:34:36 +0000 Subject: [PATCH 05/17] Update code style --- src/sage/graphs/generators/random.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index 9c5f857a0be..a55a0b0f875 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1497,7 +1497,7 @@ def RandomKTree(n, k, seed=None): sage: G = graphs.RandomKTree(50, 5) sage: G.treewidth() 5 - sage: G.show() # not tested + sage: G.show() # not tested """ if n < k + 1: raise ValueError("n must be greater than k + 1") @@ -1556,12 +1556,12 @@ def RandomPartialKTree(n, k, x, seed=None): sage: G = graphs.RandomPartialKTree(50,5,5) sage: G.treewidth() 5 - sage: G.show() + sage: G.show() # not tested """ if seed is not None: set_random_seed(seed) - g = RandomKTree(n,k,seed) + g = RandomKTree(n, k, seed) # Check that x doesn't delete too many edges # This formula calculates how many edges are in `k`-tree with `n` nodes From ae86ffd383b5fea33cfb716886c79ab696b485e6 Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Thu, 1 Feb 2024 02:46:56 +0000 Subject: [PATCH 06/17] fixed to make style compliant --- src/sage/graphs/generators/random.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index a55a0b0f875..7efa030449d 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1490,7 +1490,7 @@ def RandomKTree(n, k, seed=None): sage: g.treewidth() 5 - EXAMPLES: + EXAMPLES:: A (random) `k`-tree has treewidth `k`:: @@ -1531,12 +1531,12 @@ def RandomPartialKTree(n, k, x, seed=None): INPUT: - - ``n`` -- number of vertices in the `k`-tree + - ``n`` -- number of vertices in the `k`-tree - - ``k`` -- within a clique each vertex is connected to `k` vertices. `k` + - ``k`` -- within a clique each vertex is connected to `k` vertices. `k` also corresponds to the treewidth of the `k`-tree - - ``x`` -- how many edges are deleted from the `k`-tree + - ``x`` -- how many edges are deleted from the `k`-tree - ``seed`` -- a ``random.Random`` seed or a Python ``int`` for the random number generator (default: ``None``) @@ -1551,7 +1551,7 @@ def RandomPartialKTree(n, k, x, seed=None): sage: g.treewidth() 5 - EXAMPLES: + EXAMPLES:: sage: G = graphs.RandomPartialKTree(50,5,5) sage: G.treewidth() From 858381f1da224cb471917202dd46e9425f4346ab Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Tue, 6 Feb 2024 05:28:29 +0000 Subject: [PATCH 07/17] fix linting --- src/sage/graphs/generators/random.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index 7efa030449d..adb0074315e 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1489,7 +1489,7 @@ def RandomKTree(n, k, seed=None): 50 sage: g.treewidth() 5 - + EXAMPLES:: A (random) `k`-tree has treewidth `k`:: @@ -1497,8 +1497,8 @@ def RandomKTree(n, k, seed=None): sage: G = graphs.RandomKTree(50, 5) sage: G.treewidth() 5 - sage: G.show() # not tested - """ + sage: G.show() # not tested + """ if n < k + 1: raise ValueError("n must be greater than k + 1") @@ -1507,24 +1507,24 @@ def RandomKTree(n, k, seed=None): g = Graph(name=f"Random {k}-tree of order {n}") g.add_clique(list(range(k + 1))) - + cliques = [list(range(k+1))] # Randomly choose a row, and copy 1 of the cliques - # One of those vertices is then replaced with a new vertex + # One of those vertices is then replaced with a new vertex for newVertex in range(k + 1, n): copiedClique = cliques[randint(0, len(cliques)-1)].copy() - copiedClique[randint(0, k)] = newVertex + copiedClique[randint(0, k)] = newVertex cliques.append(copiedClique) for u in copiedClique: if u != newVertex: g.add_edge(u, newVertex) - return g + return g def RandomPartialKTree(n, k, x, seed=None): r""" - Return a random partial `k`-tree on `n` nodes. + Return a random partial `k`-tree on `n` nodes. A partial `k`-tree is defined as a subgraph of a `k`-tree. This can also be described as a graph with treewidth at most `k`. @@ -1565,7 +1565,7 @@ def RandomPartialKTree(n, k, x, seed=None): # Check that x doesn't delete too many edges # This formula calculates how many edges are in `k`-tree with `n` nodes - if x > (k^2 + k) / 2 + (n - k - 1) * k: + if x > (k ^ 2 + k) / 2 + (n - k - 1) * k: raise ValueError("x must be less than the number of edges in the `k`-tree with `n` nodes") from sage.misc.prandom import shuffle From c390cc130a9d32a16e90e9575706e11c274b310f Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Wed, 7 Feb 2024 11:49:42 +0000 Subject: [PATCH 08/17] Updating examples --- src/sage/graphs/generators/random.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index adb0074315e..e723fb9e6ef 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1543,7 +1543,7 @@ def RandomPartialKTree(n, k, x, seed=None): TESTS:: - sage: g=graphs.RandomPartialKTree(50,5,5) + sage: g=graphs.RandomPartialKTree(50,5,2) sage: g.order() 50 sage: g.size() @@ -1553,7 +1553,7 @@ def RandomPartialKTree(n, k, x, seed=None): EXAMPLES:: - sage: G = graphs.RandomPartialKTree(50,5,5) + sage: G = graphs.RandomPartialKTree(50,5,2) sage: G.treewidth() 5 sage: G.show() # not tested From 074a178da90910f069490ba879f1514fc2ea46bc Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Thu, 8 Feb 2024 09:06:52 +0000 Subject: [PATCH 09/17] fix test --- src/sage/graphs/generators/random.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index e723fb9e6ef..27437f1d8ba 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1492,8 +1492,6 @@ def RandomKTree(n, k, seed=None): EXAMPLES:: - A (random) `k`-tree has treewidth `k`:: - sage: G = graphs.RandomKTree(50, 5) sage: G.treewidth() 5 @@ -1547,7 +1545,7 @@ def RandomPartialKTree(n, k, x, seed=None): sage: g.order() 50 sage: g.size() - 230 + 233 sage: g.treewidth() 5 From 0cfb682e349c31dfc4718b6d6fc65b98cd2b220e Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Fri, 9 Feb 2024 08:08:07 +0000 Subject: [PATCH 10/17] Added error checking cases --- src/sage/graphs/generators/random.py | 42 +++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index 27437f1d8ba..dbf4cd1f20b 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1497,8 +1497,19 @@ def RandomKTree(n, k, seed=None): 5 sage: G.show() # not tested """ + if n < 0: + raise ValueError("n must not be negative") + + if k < 0: + raise ValueError("k must not be negative") + + # A graph with treewidth 0 has no edges + if k == 0: + g = Graph(n, name=f"Random 0-tree of order {n}") + return g + if n < k + 1: - raise ValueError("n must be greater than k + 1") + raise ValueError("n must be greater than k") if seed is not None: set_random_seed(seed) @@ -1556,22 +1567,45 @@ def RandomPartialKTree(n, k, x, seed=None): 5 sage: G.show() # not tested """ + if n < 0: + raise ValueError("n must not be negative") + + if k < 0: + raise ValueError("k must not be negative") + + # A graph with treewidth 0 has no edges + if k == 0: + g = Graph(n - k, name=f"Random Partial 0-tree of order {n - x}") + return g + + if n < k + 1: + raise ValueError("n must be greater than k") + if seed is not None: set_random_seed(seed) - g = RandomKTree(n, k, seed) + # This formula calculates how many edges are in a `k`-tree with `n` nodes + edgesInKTree = (k ^ 2 + k) / 2 + (n - k - 1) * k # Check that x doesn't delete too many edges - # This formula calculates how many edges are in `k`-tree with `n` nodes - if x > (k ^ 2 + k) / 2 + (n - k - 1) * k: + if x > edgesInKTree: raise ValueError("x must be less than the number of edges in the `k`-tree with `n` nodes") + # The graph will have no edges + if x == edgesInKTree: + g = Graph(0, name=f"Random Partial {k}-tree of order 0") + return g + + g = RandomKTree(n, k, seed) + from sage.misc.prandom import shuffle edges = list(g.edges()) # Deletes x random edges from the graph shuffle(edges) g.delete_edges(edges[:x]) + + g.name(f"Random Partial {k}-tree of order {n - x}") return g From 30e5bb6e641f0d252732c51463b493fdde49fdce Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Sat, 10 Feb 2024 00:52:43 +0000 Subject: [PATCH 11/17] fix incorrect disconnected graphs --- src/sage/graphs/generators/random.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index dbf4cd1f20b..9202819aec8 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1575,7 +1575,7 @@ def RandomPartialKTree(n, k, x, seed=None): # A graph with treewidth 0 has no edges if k == 0: - g = Graph(n - k, name=f"Random Partial 0-tree of order {n - x}") + g = Graph(n, name=f"Random Partial 0-tree of order {n}") return g if n < k + 1: @@ -1593,7 +1593,7 @@ def RandomPartialKTree(n, k, x, seed=None): # The graph will have no edges if x == edgesInKTree: - g = Graph(0, name=f"Random Partial {k}-tree of order 0") + g = Graph(n, name=f"Random Partial {k}-tree of order {n}") return g g = RandomKTree(n, k, seed) @@ -1604,8 +1604,8 @@ def RandomPartialKTree(n, k, x, seed=None): # Deletes x random edges from the graph shuffle(edges) g.delete_edges(edges[:x]) - - g.name(f"Random Partial {k}-tree of order {n - x}") + + g.name(f"Random Partial {k}-tree of order {n}") return g From 911e1c794139c5a4af27f36efb5d2b121a957ec5 Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Mon, 12 Feb 2024 04:10:45 +0000 Subject: [PATCH 12/17] Changed k-tree graph names --- src/sage/graphs/generators/random.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index 9202819aec8..cd5819d126c 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1505,7 +1505,7 @@ def RandomKTree(n, k, seed=None): # A graph with treewidth 0 has no edges if k == 0: - g = Graph(n, name=f"Random 0-tree of order {n}") + g = Graph(n, name=f"Random 0-tree") return g if n < k + 1: @@ -1514,7 +1514,7 @@ def RandomKTree(n, k, seed=None): if seed is not None: set_random_seed(seed) - g = Graph(name=f"Random {k}-tree of order {n}") + g = Graph(name=f"Random {k}-tree") g.add_clique(list(range(k + 1))) cliques = [list(range(k+1))] @@ -1575,7 +1575,7 @@ def RandomPartialKTree(n, k, x, seed=None): # A graph with treewidth 0 has no edges if k == 0: - g = Graph(n, name=f"Random Partial 0-tree of order {n}") + g = Graph(n, name=f"Random partial 0-tree") return g if n < k + 1: @@ -1593,7 +1593,7 @@ def RandomPartialKTree(n, k, x, seed=None): # The graph will have no edges if x == edgesInKTree: - g = Graph(n, name=f"Random Partial {k}-tree of order {n}") + g = Graph(n, name=f"Random partial {k}-tree") return g g = RandomKTree(n, k, seed) @@ -1605,7 +1605,7 @@ def RandomPartialKTree(n, k, x, seed=None): shuffle(edges) g.delete_edges(edges[:x]) - g.name(f"Random Partial {k}-tree of order {n}") + g.name(f"Random partial {k}-tree") return g From 7769f1cb808cc90187d06914e970b4ae878105d9 Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Tue, 13 Feb 2024 07:44:21 +0000 Subject: [PATCH 13/17] Increase test coverage --- src/sage/graphs/generators/random.py | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index cd5819d126c..9215a1051be 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1489,6 +1489,21 @@ def RandomKTree(n, k, seed=None): 50 sage: g.treewidth() 5 + sage: graphs.RandomKTree(-5, 5) + Traceback (most recent call last): + ... + ValueError: n must not be negative + sage: graphs.RandomKTree(5, -5) + Traceback (most recent call last): + ... + ValueError: k must not be negative + sage: graphs.RandomKTree(2, 5, 1) + Traceback (most recent call last): + ... + ValueError: n must be greater than k + sage: G = graphs.RandomKTree(50, 0) + sage: G.treewidth() + 0 EXAMPLES:: @@ -1559,6 +1574,30 @@ def RandomPartialKTree(n, k, x, seed=None): 233 sage: g.treewidth() 5 + sage: graphs.RandomPartialKTree(-5, 5, 2) + Traceback (most recent call last): + ... + ValueError: n must not be negative + sage: graphs.RandomPartialKTree(5, -5, 2) + Traceback (most recent call last): + ... + ValueError: k must not be negative + sage: G = graphs.RandomPartialKTree(2, 5, 2, 1) + Traceback (most recent call last): + ... + ValueError: n must be greater than k + sage: G = graphs.RandomPartialKTree(5, 2, 100) + Traceback (most recent call last): + ... + ValueError: x must be less than the number of edges in the `k`-tree with `n` nodes + sage: G = graphs.RandomPartialKTree(50, 0, 0) + sage: G.treewidth() + 0 + sage: G = graphs.RandomPartialKTree(5, 2, 7) + sage: G.treewidth() + 0 + sage :g.size() + 0 EXAMPLES:: From f1342ec6e9773a760b9fa7b72e7208c97cbec49c Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Thu, 15 Feb 2024 04:53:10 +0000 Subject: [PATCH 14/17] remove unnecessary paramters from tests --- src/sage/graphs/generators/random.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index 9215a1051be..ec1d9b675e2 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1497,7 +1497,7 @@ def RandomKTree(n, k, seed=None): Traceback (most recent call last): ... ValueError: k must not be negative - sage: graphs.RandomKTree(2, 5, 1) + sage: graphs.RandomKTree(2, 5) Traceback (most recent call last): ... ValueError: n must be greater than k @@ -1582,7 +1582,7 @@ def RandomPartialKTree(n, k, x, seed=None): Traceback (most recent call last): ... ValueError: k must not be negative - sage: G = graphs.RandomPartialKTree(2, 5, 2, 1) + sage: G = graphs.RandomPartialKTree(2, 5, 2) Traceback (most recent call last): ... ValueError: n must be greater than k From b4dff90fc5bbf68cc3a787d0b0e7e0e780a031a0 Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Thu, 15 Feb 2024 08:59:20 +0000 Subject: [PATCH 15/17] fixed typo --- src/sage/graphs/generators/random.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index ec1d9b675e2..e2e1e80c024 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1596,7 +1596,7 @@ def RandomPartialKTree(n, k, x, seed=None): sage: G = graphs.RandomPartialKTree(5, 2, 7) sage: G.treewidth() 0 - sage :g.size() + sage : g.size() 0 EXAMPLES:: From 79d0bddcc27f956c0e8d68ace03f9c42ba9301ac Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Thu, 15 Feb 2024 09:03:37 +0000 Subject: [PATCH 16/17] fix typo --- src/sage/graphs/generators/random.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index e2e1e80c024..e41ee7af4ec 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1596,7 +1596,7 @@ def RandomPartialKTree(n, k, x, seed=None): sage: G = graphs.RandomPartialKTree(5, 2, 7) sage: G.treewidth() 0 - sage : g.size() + sage: g.size() 0 EXAMPLES:: From faa737bd0adce9aecccb82b5e14af1e9afa45d4f Mon Sep 17 00:00:00 2001 From: Damian Basso Date: Thu, 15 Feb 2024 09:37:25 +0000 Subject: [PATCH 17/17] fix test --- src/sage/graphs/generators/random.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/graphs/generators/random.py b/src/sage/graphs/generators/random.py index e41ee7af4ec..fd439b98e9a 100644 --- a/src/sage/graphs/generators/random.py +++ b/src/sage/graphs/generators/random.py @@ -1596,7 +1596,7 @@ def RandomPartialKTree(n, k, x, seed=None): sage: G = graphs.RandomPartialKTree(5, 2, 7) sage: G.treewidth() 0 - sage: g.size() + sage: G.size() 0 EXAMPLES::