Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented power of graph function under basic methods #36584

Merged
merged 17 commits into from
Dec 6, 2023
Merged
35 changes: 35 additions & 0 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -15745,6 +15745,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
saatvikraoIITGN marked this conversation as resolved.
Show resolved Hide resolved
shortest distances between nodes using BFS.

INPUT:
- graph: An undirected, unweighted graph.
saatvikraoIITGN marked this conversation as resolved.
Show resolved Hide resolved
- 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)])
saatvikraoIITGN marked this conversation as resolved.
Show resolved Hide resolved
sage: k = 2
sage: PG = G.power(k)
sage: PG.edges()
saatvikraoIITGN marked this conversation as resolved.
Show resolved Hide resolved
[(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()
saatvikraoIITGN marked this conversation as resolved.
Show resolved Hide resolved
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):
"""
Expand Down
Loading