From 88f6795ebb75f3ab2ef379d523eddbdc291b3336 Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Mon, 30 Oct 2023 14:36:53 +0530 Subject: [PATCH 01/14] implemented power of graph function under basic methods --- src/sage/graphs/graph.py | 56 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/sage/graphs/graph.py b/src/sage/graphs/graph.py index 2435bd815b7..b1e6573356d 100644 --- a/src/sage/graphs/graph.py +++ b/src/sage/graphs/graph.py @@ -4890,6 +4890,62 @@ def independent_set_of_representatives(self, family, solver=None, verbose=0, return repr + @doc_index("Basic methods") + def power_of_graph(self, k): + r""" + Compute the kth power graph of an undirected, unweighted graph based on + shortest distances between nodes using BFS. + + INPUT: + - graph: An undirected, unweighted graph. + - k: The maximum path length for considering edges in the power graph. + + OUTPUT: + - The kth power graph based on shortest distances between nodes. + + EXAMPLE: + + sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) + sage: k = 2 + sage: PG = power_graph_with_shortest_distances(G, k) + sage: PG.edges() + [(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3), (5, 4), (4, 5)] + + """ + n = self.order() + all_distances = {} # Dictionary to store shortest distances between all pairs of nodes + + from collections import deque + + # Step 1: Compute shortest distances using BFS + for u in self.vertices(): + distances = {} # Dictionary to store distances from u to all other nodes + visited = {v: False for v in self.vertices()} + queue = deque() + + visited[u] = True + distances[u] = 0 + queue.append(u) + + while queue: + v = queue.popleft() + for neighbor in self.neighbors(v): + if not visited[neighbor]: + visited[neighbor] = True + distances[neighbor] = distances[v] + 1 + queue.append(neighbor) + + all_distances[u] = distances + + # Step 2: Create the kth power graph based on distances + PG = Graph() + for u in self.vertices(): + for v in self.vertices(): + if u != v and all_distances[u].get(v, float('inf')) <= k: + PG.add_edge(u, v) + + return PG + @doc_index("Algorithmically hard stuff") def minor(self, H, solver=None, verbose=0, induced=False, *, integrality_tolerance=1e-3): r""" From fd59d5347bb3239fa5478ca1215155b8363a09ae Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Mon, 13 Nov 2023 23:09:15 +0530 Subject: [PATCH 02/14] simplified the code --- src/sage/graphs/generic_graph.py | 35 ++++++++++++++++++++ src/sage/graphs/graph.py | 56 -------------------------------- 2 files changed, 35 insertions(+), 56 deletions(-) diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index eedbc36bef3..73d7556344e 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -15726,6 +15726,41 @@ def distance_all_pairs(self, by_weight=False, algorithm=None, algorithm=algorithm, weight_function=weight_function, check_weight=check_weight)[0] + + def power(self, k): + r""" + Compute the kth power graph of an undirected, unweighted graph based on + shortest distances between nodes using BFS. + + INPUT: + - graph: An undirected, unweighted graph. + - k: The maximum path length for considering edges in the power graph. + + OUTPUT: + - The kth power graph based on shortest distances between nodes. + + EXAMPLE: + + sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) + sage: k = 2 + sage: PG = G.power(k) + sage: PG.edges() + [(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3), (5, 4), (4, 5)] + + sage: G = DiGraph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) + sage: k = 3 + sage: PG = g.power(k) + sage: PG.edges() + [(0, 1, None), (0, 2, None), (0, 3, None), (0, 4, None), (1, 0, None), (1, 2, None), (1, 3, None), (1, 4, None), (1, 5, None), (2, 0, None), (2, 1, None), (2, 3, None), (2, 4, None), (2, 5, None), (3, 0, None), (3, 1, None), (3, 2, None), (4, 5, None)] + + """ + power_of_graph = self.copy() + for u in self: + for v in self.breadth_first_search(u, distance=k): + if u != v: + power_of_graph.add_edge(u, v) + + return power_of_graph def girth(self, certificate=False): """ diff --git a/src/sage/graphs/graph.py b/src/sage/graphs/graph.py index b1e6573356d..2435bd815b7 100644 --- a/src/sage/graphs/graph.py +++ b/src/sage/graphs/graph.py @@ -4890,62 +4890,6 @@ def independent_set_of_representatives(self, family, solver=None, verbose=0, return repr - @doc_index("Basic methods") - def power_of_graph(self, k): - r""" - Compute the kth power graph of an undirected, unweighted graph based on - shortest distances between nodes using BFS. - - INPUT: - - graph: An undirected, unweighted graph. - - k: The maximum path length for considering edges in the power graph. - - OUTPUT: - - The kth power graph based on shortest distances between nodes. - - EXAMPLE: - - sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) - sage: k = 2 - sage: PG = power_graph_with_shortest_distances(G, k) - sage: PG.edges() - [(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3), (5, 4), (4, 5)] - - """ - n = self.order() - all_distances = {} # Dictionary to store shortest distances between all pairs of nodes - - from collections import deque - - # Step 1: Compute shortest distances using BFS - for u in self.vertices(): - distances = {} # Dictionary to store distances from u to all other nodes - visited = {v: False for v in self.vertices()} - queue = deque() - - visited[u] = True - distances[u] = 0 - queue.append(u) - - while queue: - v = queue.popleft() - for neighbor in self.neighbors(v): - if not visited[neighbor]: - visited[neighbor] = True - distances[neighbor] = distances[v] + 1 - queue.append(neighbor) - - all_distances[u] = distances - - # Step 2: Create the kth power graph based on distances - PG = Graph() - for u in self.vertices(): - for v in self.vertices(): - if u != v and all_distances[u].get(v, float('inf')) <= k: - PG.add_edge(u, v) - - return PG - @doc_index("Algorithmically hard stuff") def minor(self, H, solver=None, verbose=0, induced=False, *, integrality_tolerance=1e-3): r""" From 675a3efdf65b679cb381b4e3a7b41c1919877eb2 Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Thu, 16 Nov 2023 17:10:28 +0530 Subject: [PATCH 03/14] minor changes to code style --- src/sage/graphs/generic_graph.py | 40 ++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index 62dc2be2ed9..6ca953ae84a 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -15748,38 +15748,42 @@ def distance_all_pairs(self, by_weight=False, algorithm=None, def power(self, k): r""" - Compute the kth power graph of an undirected, unweighted graph based on - shortest distances between nodes using BFS. + Compute the kth power graph of an unweighted graph based on shortest + distances between nodes using BFS. INPUT: - - graph: An undirected, unweighted graph. - - k: The maximum path length for considering edges in the power graph. + - ``k`` -- integer; the maximum path length for considering edges in the power graph. OUTPUT: - The kth power graph based on shortest distances between nodes. - EXAMPLE: + EXAMPLES:: + + Testing on undirected graphs:: + + sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) + sage: k = 2 + sage: PG = G.power(k) + sage: PG.edges(sort=True, labels=False) + [(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3), (5, 4), (4, 5)] - sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) - sage: k = 2 - sage: PG = G.power(k) - sage: PG.edges() - [(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3), (5, 4), (4, 5)] + Testing on directed graphs:: - sage: G = DiGraph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) - sage: k = 3 - sage: PG = g.power(k) - sage: PG.edges() - [(0, 1, None), (0, 2, None), (0, 3, None), (0, 4, None), (1, 0, None), (1, 2, None), (1, 3, None), (1, 4, None), (1, 5, None), (2, 0, None), (2, 1, None), (2, 3, None), (2, 4, None), (2, 5, None), (3, 0, None), (3, 1, None), (3, 2, None), (4, 5, None)] + sage: G = DiGraph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) + sage: k = 3 + sage: PG = g.power(k) + sage: PG.edges(sort=True, labels=False) + [(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (1, 5), (2, 0), (2, 1), (2, 3), (2, 4), (2, 5), (3, 0), (3, 1), (3, 2), (4, 5)] """ - power_of_graph = self.copy() + power_of_graph = self.copy() + for u in self: for v in self.breadth_first_search(u, distance=k): if u != v: power_of_graph.add_edge(u, v) - - return power_of_graph + + return power_of_graph def girth(self, certificate=False): """ From 1f6892ca12dc6cf03d62f39205b01d4a8ae35584 Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Thu, 16 Nov 2023 17:45:03 +0530 Subject: [PATCH 04/14] handling multigraphs --- src/sage/graphs/generic_graph.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index 6ca953ae84a..8cabf01ee2e 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -15776,12 +15776,17 @@ def power(self, k): [(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (1, 5), (2, 0), (2, 1), (2, 3), (2, 4), (2, 5), (3, 0), (3, 1), (3, 2), (4, 5)] """ - power_of_graph = self.copy() + from sage.graphs.graph import DiGraph, Graph + + power_of_graph = DiGraph() if self.is_directed() else Graph() + + added_edges = set() for u in self: for v in self.breadth_first_search(u, distance=k): - if u != v: + if u != v and (u, v) not in added_edges and (v, u) not in added_edges: power_of_graph.add_edge(u, v) + added_edges.add((u, v)) return power_of_graph From 1a97d5751460e23c696af2a720e5671a0354b7bd Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Thu, 16 Nov 2023 17:50:47 +0530 Subject: [PATCH 05/14] minor fixes --- src/sage/graphs/generic_graph.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index 8cabf01ee2e..2ffd6dc7405 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -15780,13 +15780,10 @@ def power(self, k): power_of_graph = DiGraph() if self.is_directed() else Graph() - added_edges = set() - for u in self: for v in self.breadth_first_search(u, distance=k): - if u != v and (u, v) not in added_edges and (v, u) not in added_edges: + if u != v: power_of_graph.add_edge(u, v) - added_edges.add((u, v)) return power_of_graph From 1064a501ea2116b353d6728dedf1f8d8315a390a Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Fri, 17 Nov 2023 22:59:13 +0530 Subject: [PATCH 06/14] fixed linting error --- src/sage/graphs/generic_graph.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index 2ffd6dc7405..7b53fd4e013 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -15757,7 +15757,7 @@ def power(self, k): OUTPUT: - The kth power graph based on shortest distances between nodes. - EXAMPLES:: + EXAMPLES: Testing on undirected graphs:: From 643145f4a7be8e2e0b2da2944bdf33afc0e6e6cc Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Sat, 18 Nov 2023 22:42:51 +0530 Subject: [PATCH 07/14] fixed linting errors --- src/sage/graphs/generic_graph.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index 7b53fd4e013..c4c26918a43 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -15745,7 +15745,7 @@ def distance_all_pairs(self, by_weight=False, algorithm=None, algorithm=algorithm, weight_function=weight_function, check_weight=check_weight)[0] - + def power(self, k): r""" Compute the kth power graph of an unweighted graph based on shortest From 5656d941339a3cd7e0e1ef9dfa70c22b3427aecb Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Sat, 18 Nov 2023 22:54:50 +0530 Subject: [PATCH 08/14] fixed linting errors --- src/sage/graphs/generic_graph.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index c4c26918a43..be72d61ecae 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -15745,7 +15745,7 @@ def distance_all_pairs(self, by_weight=False, algorithm=None, algorithm=algorithm, weight_function=weight_function, check_weight=check_weight)[0] - + def power(self, k): r""" Compute the kth power graph of an unweighted graph based on shortest From ffcd2bc6c31bad84bf81e476c6357a70d999a4d1 Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Mon, 20 Nov 2023 17:58:56 +0530 Subject: [PATCH 09/14] added more doctests and improved code style --- src/sage/graphs/generic_graph.py | 55 +++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index be72d61ecae..3573b502665 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -15752,9 +15752,12 @@ def power(self, k): distances between nodes using BFS. INPUT: - - ``k`` -- integer; the maximum path length for considering edges in the power graph. + + - ``k`` -- integer; the maximum path length for considering edges in the power + graph. OUTPUT: + - The kth power graph based on shortest distances between nodes. EXAMPLES: @@ -15765,18 +15768,62 @@ def power(self, k): sage: k = 2 sage: PG = G.power(k) sage: PG.edges(sort=True, labels=False) - [(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3), (5, 4), (4, 5)] + [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (2, 5), (3, 4), (4, 5)] Testing on directed graphs:: sage: G = DiGraph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) sage: k = 3 - sage: PG = g.power(k) + sage: PG = G.power(k) sage: PG.edges(sort=True, labels=False) [(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (1, 5), (2, 0), (2, 1), (2, 3), (2, 4), (2, 5), (3, 0), (3, 1), (3, 2), (4, 5)] + TESTS: + + Testing when k < 0:: + + sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) + sage: k = -2 + sage: PG = G.power(k) + Traceback (most recent call last) + ... + ValueError: distance must be a non-negative integer, not -2 + + Testing when k = 0:: + + sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) + sage: k = 0 + sage: PG = G.power(k) + sage: PG.edges(sort=True, labels=False) + [] + + Testing when k = 1:: + + sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) + sage: k = 1 + sage: PG = G.power(k) + sage: PG.edges(sort=True, labels=False) + [(0, 1), (0, 3), (1, 2), (2, 3), (2, 4), (4, 5)] + + Testing when k = Infinity:: + + sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) + sage: k = Infinity + sage: PG = G.power(k) + Traceback (most recent call last) + ... + ValueError: distance must be a non-negative integer, not +Infinity + + Testing on graph with multiple edges:: + + sage: G = DiGraph([(0, 1), (0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) + sage: k = 3 + sage: PG = G.power(k) + sage: PG.edges(sort=True, labels=False) + [(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (1, 5), (2, 0), (2, 1), (2, 3), (2, 4), (2, 5), (3, 0), (3, 1), (3, 2), (4, 5)] """ - from sage.graphs.graph import DiGraph, Graph + from sage.graphs.digraph import DiGraph + from sage.graphs.graph import Graph power_of_graph = DiGraph() if self.is_directed() else Graph() From b4a347a694ca12d44a7539fa2bb4111bca49ca33 Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Mon, 20 Nov 2023 18:07:07 +0530 Subject: [PATCH 10/14] fixed linting errors --- src/sage/graphs/generic_graph.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index 3573b502665..564fc071d53 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -15753,8 +15753,7 @@ def power(self, k): INPUT: - - ``k`` -- integer; the maximum path length for considering edges in the power - graph. + - ``k`` -- integer; the maximum path length for considering edges in the power graph. OUTPUT: From dbb85c3ecfa830f509487acdc05437b71172c7bb Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Mon, 20 Nov 2023 23:27:54 +0530 Subject: [PATCH 11/14] fixed error in doctests --- src/sage/graphs/generic_graph.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index 564fc071d53..e6c8756e5f2 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -15753,7 +15753,8 @@ def power(self, k): INPUT: - - ``k`` -- integer; the maximum path length for considering edges in the power graph. + - ``k`` -- integer; the maximum path length for considering edges in + the power graph. OUTPUT: @@ -15784,7 +15785,7 @@ def power(self, k): sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) sage: k = -2 sage: PG = G.power(k) - Traceback (most recent call last) + Traceback (most recent call last): ... ValueError: distance must be a non-negative integer, not -2 @@ -15809,7 +15810,7 @@ def power(self, k): sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) sage: k = Infinity sage: PG = G.power(k) - Traceback (most recent call last) + Traceback (most recent call last): ... ValueError: distance must be a non-negative integer, not +Infinity From 4ec8bf97428a597897819938c56fab187dc956c3 Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Tue, 21 Nov 2023 14:40:38 +0530 Subject: [PATCH 12/14] naming the graph before returning it --- src/sage/graphs/generic_graph.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index e6c8756e5f2..b70ba295a6c 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -15832,6 +15832,9 @@ def power(self, k): if u != v: power_of_graph.add_edge(u, v) + if self.name(): + power_of_graph.name("power({})".format(self.name())) + return power_of_graph def girth(self, certificate=False): From bfd6e35102fc8bbb4f340ec74124f8053d90b06b Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Tue, 21 Nov 2023 15:39:15 +0530 Subject: [PATCH 13/14] fixed function definition and improved doctests readability --- src/sage/graphs/generic_graph.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index b70ba295a6c..fad8344923d 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -15748,8 +15748,13 @@ def distance_all_pairs(self, by_weight=False, algorithm=None, def power(self, k): r""" - Compute the kth power graph of an unweighted graph based on shortest - distances between nodes using BFS. + Return the `k`-th power graph of ``self``. + + In the `k`-th power graph of a graph `G`, there is an edge between + any pair of vertices at distance at most `k` in `G`, where the + distance is considered in the unweighted graph. In a directed graph, + there is an arc from a vertex `u` to a vertex `v` if there is a path + of length at most `k` in `G` from `u` to `v`. INPUT: @@ -15765,16 +15770,14 @@ def power(self, k): Testing on undirected graphs:: sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) - sage: k = 2 - sage: PG = G.power(k) + sage: PG = G.power(2) sage: PG.edges(sort=True, labels=False) [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (2, 5), (3, 4), (4, 5)] Testing on directed graphs:: sage: G = DiGraph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) - sage: k = 3 - sage: PG = G.power(k) + sage: PG = G.power(3) sage: PG.edges(sort=True, labels=False) [(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (1, 5), (2, 0), (2, 1), (2, 3), (2, 4), (2, 5), (3, 0), (3, 1), (3, 2), (4, 5)] @@ -15783,8 +15786,7 @@ def power(self, k): Testing when k < 0:: sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) - sage: k = -2 - sage: PG = G.power(k) + sage: PG = G.power(-2) Traceback (most recent call last): ... ValueError: distance must be a non-negative integer, not -2 @@ -15792,24 +15794,21 @@ def power(self, k): Testing when k = 0:: sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) - sage: k = 0 - sage: PG = G.power(k) + sage: PG = G.power(0) sage: PG.edges(sort=True, labels=False) [] Testing when k = 1:: sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) - sage: k = 1 - sage: PG = G.power(k) + sage: PG = G.power(1) sage: PG.edges(sort=True, labels=False) [(0, 1), (0, 3), (1, 2), (2, 3), (2, 4), (4, 5)] Testing when k = Infinity:: sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) - sage: k = Infinity - sage: PG = G.power(k) + sage: PG = G.power(Infinity) Traceback (most recent call last): ... ValueError: distance must be a non-negative integer, not +Infinity @@ -15817,8 +15816,7 @@ def power(self, k): Testing on graph with multiple edges:: sage: G = DiGraph([(0, 1), (0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) - sage: k = 3 - sage: PG = G.power(k) + sage: PG = G.power(3) sage: PG.edges(sort=True, labels=False) [(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (1, 5), (2, 0), (2, 1), (2, 3), (2, 4), (2, 5), (3, 0), (3, 1), (3, 2), (4, 5)] """ From e0dc49dc569408e2988b45359a0bf817cc396ef0 Mon Sep 17 00:00:00 2001 From: Saatvik Rao Date: Tue, 21 Nov 2023 20:54:26 +0530 Subject: [PATCH 14/14] fixed doctest on multigraph --- src/sage/graphs/generic_graph.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index fad8344923d..efddb03b1cc 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -15815,7 +15815,7 @@ def power(self, k): Testing on graph with multiple edges:: - sage: G = DiGraph([(0, 1), (0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)]) + sage: G = DiGraph([(0, 1), (0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)], multiedges=True) sage: PG = G.power(3) sage: PG.edges(sort=True, labels=False) [(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (1, 5), (2, 0), (2, 1), (2, 3), (2, 4), (2, 5), (3, 0), (3, 1), (3, 2), (4, 5)]