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

Conversation

saatvikraoIITGN
Copy link
Contributor

@saatvikraoIITGN saatvikraoIITGN commented Oct 30, 2023

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

  • The title is concise, informative, and self-explanatory.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion (if applicable).
  • I have created tests covering the changes.
  • I have updated the documentation accordingly.

⌛ Dependencies

#36582 : To implement power of graph

@dcoudert
Copy link
Contributor

It would be better to implement method power in generic_graph.py to address both directed and undirected graphs, and to use builtin fonctions .

For instance,

other = self.copy()
for u in self:
    for v in self.breadth_first_search(u, distance=k):
        other.add_edge(u, v)

One difficulty is to deal with (di)graphs with loops and/or multiple edges.

@dcoudert
Copy link
Contributor

For your information, we also have method distance_graph that returns the graph with only edges between vertices at distance exactly k in the original graph.

@saatvikraoIITGN
Copy link
Contributor Author

It would be better to implement method power in generic_graph.py to address both directed and undirected graphs, and to use builtin fonctions .

For instance,

other = self.copy()
for u in self:
    for v in self.breadth_first_search(u, distance=k):
        other.add_edge(u, v)

One difficulty is to deal with (di)graphs with loops and/or multiple edges.

Sir the code suggested by your works fine with digraphs with loops and multiple edges

I will raise the PR with the changes

Copy link
Contributor

@dcoudert dcoudert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The coding style must be improved (see how it is done in other methods) and some care is needed for graphs with multiple edges.

src/sage/graphs/generic_graph.py Outdated Show resolved Hide resolved
src/sage/graphs/generic_graph.py Outdated Show resolved Hide resolved
src/sage/graphs/generic_graph.py Outdated Show resolved Hide resolved
src/sage/graphs/generic_graph.py Outdated Show resolved Hide resolved
src/sage/graphs/generic_graph.py Outdated Show resolved Hide resolved
src/sage/graphs/generic_graph.py Outdated Show resolved Hide resolved
@saatvikraoIITGN
Copy link
Contributor Author

@dcoudert does the code seem fine?

Copy link
Contributor

@dcoudert dcoudert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Check and correct the doctest errors reported by the CI
  2. Although it is currently possible to import DiGraph from sage.graphs.graph, please don't do that.
  3. Check and document the expected result when k < 0, k = 0, k=1, k=+oo
  4. Respect the 80 columns mode in comments and doctests, as done in other methods.
  5. Add empty line after INPUT: and OUTPUT: as done in other methods.
  6. Set the name of the graph before returning it.
  7. Add a TESTS:: block and check the behavior of the method in corner cases, when the graph has multiple edges

src/sage/graphs/generic_graph.py Outdated Show resolved Hide resolved
@saatvikraoIITGN
Copy link
Contributor Author

@dcoudert What do you mean by Set the name of the graph before returning it.

@dcoudert
Copy link
Contributor

@dcoudert What do you mean by Set the name of the graph before returning it.

sage: G = graphs.PetersenGraph()
sage: G
Petersen graph: Graph on 10 vertices
sage: G.name("a cool name")   # Here we change the name
sage: G
a cool name: Graph on 10 vertices
sage: G = Graph(name="My empty graph")    # Here we set the name of a new graph
sage: G
My empty graph: Graph on 0 vertices

@saatvikraoIITGN
Copy link
Contributor Author

@dcoudert What do you mean by Set the name of the graph before returning it.

sage: G = graphs.PetersenGraph()
sage: G
Petersen graph: Graph on 10 vertices
sage: G.name("a cool name")   # Here we change the name
sage: G
a cool name: Graph on 10 vertices
sage: G = Graph(name="My empty graph")    # Here we set the name of a new graph
sage: G
My empty graph: Graph on 0 vertices

For eg.: in the following test

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)```

We have to name the graph, `G`, before `PG = G.power(k)`?

@dcoudert
Copy link
Contributor

We have to name the graph, G, before PG = G.power(k)?

No, there is no need to name the graph here.

I'm saying that you can set the name inside the method, as is done for instance in method complement

sage: G = graphs.PetersenGraph()
sage: H = G.complement()
sage: H
complement(Petersen graph): Graph on 10 vertices
sage: H.name()
'complement(Petersen graph)'

src/sage/graphs/generic_graph.py Outdated Show resolved Hide resolved
src/sage/graphs/generic_graph.py Outdated Show resolved Hide resolved
src/sage/graphs/generic_graph.py Outdated Show resolved Hide resolved
Copy link

Documentation preview for this PR (built with commit e0dc49d; changes) is ready! 🎉

Copy link
Contributor

@dcoudert dcoudert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

vbraun pushed a commit to vbraun/sage that referenced this pull request Dec 4, 2023
…hods

    
### 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 sagemath#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

sagemath#36582 : To implement power of graph
    
URL: sagemath#36584
Reported by: saatvikraoIITGN
Reviewer(s): David Coudert, saatvikraoIITGN
@vbraun vbraun merged commit caa1068 into sagemath:develop Dec 6, 2023
19 checks passed
@mkoeppe mkoeppe added this to the sage-10.3 milestone Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement a function to find the power of graph
4 participants