Skip to content

Commit caa1068

Browse files
author
Release Manager
committed
gh-36584: implemented power of graph function under basic methods ### Description This PR introduces a new function, `power_of_graph`, to compute the kth power of an undirected, unweighted graph efficiently using the shortest distances method. The proposed function leverages Breadth-First Search (BFS) to calculate the power graph, providing a practical and scalable solution. ### Why is this change required? The change is required to address the need for efficiently calculating the kth power of a graph, a fundamental operation in graph theory. The PR aims to incorporate this feature into the SageMath library. Fixes #36582 ### Checklist - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion (if applicable). - [x] I have created tests covering the changes. - [x] I have updated the documentation accordingly. ### ⌛ Dependencies #36582 : To implement power of graph URL: #36584 Reported by: saatvikraoIITGN Reviewer(s): David Coudert, saatvikraoIITGN
2 parents 8801f6e + e0dc49d commit caa1068

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

src/sage/graphs/generic_graph.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15785,6 +15785,95 @@ def distance_all_pairs(self, by_weight=False, algorithm=None,
1578515785
weight_function=weight_function,
1578615786
check_weight=check_weight)[0]
1578715787

15788+
def power(self, k):
15789+
r"""
15790+
Return the `k`-th power graph of ``self``.
15791+
15792+
In the `k`-th power graph of a graph `G`, there is an edge between
15793+
any pair of vertices at distance at most `k` in `G`, where the
15794+
distance is considered in the unweighted graph. In a directed graph,
15795+
there is an arc from a vertex `u` to a vertex `v` if there is a path
15796+
of length at most `k` in `G` from `u` to `v`.
15797+
15798+
INPUT:
15799+
15800+
- ``k`` -- integer; the maximum path length for considering edges in
15801+
the power graph.
15802+
15803+
OUTPUT:
15804+
15805+
- The kth power graph based on shortest distances between nodes.
15806+
15807+
EXAMPLES:
15808+
15809+
Testing on undirected graphs::
15810+
15811+
sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)])
15812+
sage: PG = G.power(2)
15813+
sage: PG.edges(sort=True, labels=False)
15814+
[(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (2, 5), (3, 4), (4, 5)]
15815+
15816+
Testing on directed graphs::
15817+
15818+
sage: G = DiGraph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)])
15819+
sage: PG = G.power(3)
15820+
sage: PG.edges(sort=True, labels=False)
15821+
[(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)]
15822+
15823+
TESTS:
15824+
15825+
Testing when k < 0::
15826+
15827+
sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)])
15828+
sage: PG = G.power(-2)
15829+
Traceback (most recent call last):
15830+
...
15831+
ValueError: distance must be a non-negative integer, not -2
15832+
15833+
Testing when k = 0::
15834+
15835+
sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)])
15836+
sage: PG = G.power(0)
15837+
sage: PG.edges(sort=True, labels=False)
15838+
[]
15839+
15840+
Testing when k = 1::
15841+
15842+
sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)])
15843+
sage: PG = G.power(1)
15844+
sage: PG.edges(sort=True, labels=False)
15845+
[(0, 1), (0, 3), (1, 2), (2, 3), (2, 4), (4, 5)]
15846+
15847+
Testing when k = Infinity::
15848+
15849+
sage: G = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)])
15850+
sage: PG = G.power(Infinity)
15851+
Traceback (most recent call last):
15852+
...
15853+
ValueError: distance must be a non-negative integer, not +Infinity
15854+
15855+
Testing on graph with multiple edges::
15856+
15857+
sage: G = DiGraph([(0, 1), (0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5)], multiedges=True)
15858+
sage: PG = G.power(3)
15859+
sage: PG.edges(sort=True, labels=False)
15860+
[(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)]
15861+
"""
15862+
from sage.graphs.digraph import DiGraph
15863+
from sage.graphs.graph import Graph
15864+
15865+
power_of_graph = DiGraph() if self.is_directed() else Graph()
15866+
15867+
for u in self:
15868+
for v in self.breadth_first_search(u, distance=k):
15869+
if u != v:
15870+
power_of_graph.add_edge(u, v)
15871+
15872+
if self.name():
15873+
power_of_graph.name("power({})".format(self.name()))
15874+
15875+
return power_of_graph
15876+
1578815877
def girth(self, certificate=False):
1578915878
"""
1579015879
Return the girth of the graph.

0 commit comments

Comments
 (0)