Skip to content

Conversation

@starovoid
Copy link
Collaborator

This PR aims to bring uniformity to the documentation of algorithms.

Now the doc comments will have the following structure:

Main description...
...
...

# Arguments
...

# Returns
...

# Complexity
* Time complexity: ...
* Space complexity: ...

# Examples

For example, the spfa doc comment might look like:

/// \[Generic\] Compute shortest paths from node `source` to all other.
///
/// Using the [Shortest Path Faster Algorithm][spfa].
/// Compute shortest distances from node `source` to all other.
///
/// Compute shortest paths lengths in a weighted graph with positive or negative edge weights,
/// but with no negative cycles.
///
/// ## Arguments
/// * `graph`: weighted graph.
/// * `source`: the source vertex, for which we calculate the lengths of the shortest paths to all the others.
/// * `edge_cost`: closure that returns cost of a particular edge
///
/// ## Returns
/// * `Err`: if graph contains negative cycle.
/// * `Ok`: a pair of a vector of shortest distances and a vector
///   of predecessors of each vertex along the shortest path.
///
/// ## Complexity
/// * Time complexity: **O(|V||E|)**, but it's generally assumed that in the average case it is **O(|E|)**
/// * Space complexity: **O(|V|)**
///
/// |V| is the number of nodes, |E| is the number of edges in the input graph.
///
///
/// [spfa]: https://www.geeksforgeeks.org/shortest-path-faster-algorithm/
///
/// # Example

@starovoid starovoid changed the title Unify algo docs docs: Unify algo docs Jun 4, 2025
@starovoid starovoid added this to the 0.8.2 milestone Jun 4, 2025
@RaoulLuque
Copy link
Member

RaoulLuque commented Jun 4, 2025

Wow, that's a great idea !

I also like the layout of the doc comment you are suggesting :)

While we are at it, I always wondered what the \[Generic\] is supposed to be mean?

Also, maybe in the main description at the top, there could be a bit less repetition, by adjusting the following sentence like so:
From

/// Compute shortest paths lengths in a weighted graph with positive or negative edge weights,
/// but with no negative cycles.

To

/// Works on weighted graphs with positive or negative edge weights, but with no negative cycles.

That is, not state again what the algorithm does, and instead only state the requirements for the provided graph.
Maybe we could try to keep the requirements on the graph consistent somehow. I am not sure if it makes sense to make an entire bulletpoint (#) out of it, but instead adjusting the structure to be like so for example:

Main description...
...
...
Requirements for the provided graph

# Arguments
...

I think we could also reuse your layout/structure and add it to a PR template to make sure other contributions also stick to this layout 👍

At last, if you'd like, I would really like to review the changes :)

@starovoid
Copy link
Collaborator Author

starovoid commented Jun 4, 2025

At last, if you'd like, I would really like to review the changes :)

I'm glad to hear that! Moreover, I need help on several points:

  • Find out the space complexity of the greedy_feedback_arc_set algorithm
  • Make sure that ford_fulkerson is actually Edmonds-Karp (or not) and document the time and space complexity
  • Document all estimates regarding isomorphism algorithms. In fact, it looks so grand that I'm thinking of leaving it for the next release.

This list will probably be updated in the near future :)


While we are at it, I always wondered what the [Generic] is supposed to be mean?

This means that the algorithm accepts an arbitrary graph type (satisfying the trait bounds) as input. I suggest to ignore this historical artifact for now.


Also, maybe in the main description at the top, there could be a bit less repetition, by adjusting the following sentence like so...

Please suggest all such changes through the review mechanism, so it will be easier and faster to accept them ;)


I think we could also reuse your layout/structure and add it to a PR template to make sure other contributions also stick to this layout

As for that, the contributing guide has been in need of updating for a long time, I will raise this issue among the maintainers a little later.

@RaoulLuque
Copy link
Member

I'm glad to hear that! Moreover, I need help on several points:

* [ ]  Find out the space complexity of the `greedy_feedback_arc_set` algorithm

I'll look into that 👍

* [ ]  Make sure that `ford_fulkerson` is actually Edmonds-Karp (or not) and document the time and space complexity

I can also look into that 👍

* [ ]  Document all estimates regarding isomorphism algorithms. In fact, it looks so grand that I'm thinking of leaving it for the next release.

I am not very familiar with this class of algorithms, so I'm not sure I'll have enough time to look into this in the near future.

This means that the algorithm accepts an arbitrary graph type (satisfying the trait bounds) as input. I suggest to ignore this historical artifact for now.

Ah, I see. Because I find it somewhat confusing since one might think that the algorithm is generic in the sense that it can be used for arbitrary graphs. That is, something like directed/undirected or just arbitrary (cycles / no cycles). Because that is not true for spfa although there is the generic tag.
I then wonder, if there is a reason to keep the generic tag? Because the rustdocs and compiler should tell users if the algorithm accepts arbitrary graphs or not (and which Traits should be implemented) and adding such a tag might be confusing as I described above.

Please suggest all such changes through the review mechanism, so it will be easier and faster to accept them ;)

Alright I'll do that :) Should I already review the changes or wait until your PR is not in draft mode anymore :)?

As for that, the contributing guide has been in need of updating for a long time, I will raise this issue among the maintainers a little later.

Alright 👍

@starovoid
Copy link
Collaborator Author

Should I already review the changes or wait until your PR is not in draft mode anymore

You can do it right now💪

Copy link
Member

@RaoulLuque RaoulLuque left a comment

Choose a reason for hiding this comment

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

I only had enough time to get to bridges.rs (alphabetically) but these are some suggestions which contain some general ones as well :)

@starovoid starovoid requested a review from RaoulLuque June 5, 2025 11:30
@starovoid
Copy link
Collaborator Author

Hey @RaoulLuque, I think I've finished the work, except for the points mentioned above. I suggest we leave the isomorphism algorithms for later and wait for your review :)

@starovoid starovoid marked this pull request as ready for review June 5, 2025 11:39
@RaoulLuque
Copy link
Member

RaoulLuque commented Jun 5, 2025

Very nice :) I'll try to look into it later today and also give my opinion on:

Find out the space complexity of the greedy_feedback_arc_set algorithm

What is your opinion on removing the [Generic] Tags on the algorithms with the arguments I gave above?

@starovoid
Copy link
Collaborator Author

What is your opinion on removing the [Generic] Tags on the algorithms with the arguments I gave above?

At the moment, there are still several algorithms that accept specific graph types. You can suggest a change in wording for them, and then nothing will stop us from deleting [Generic] labels.

@RaoulLuque
Copy link
Member

I see. The thing is, that I am not sure users will understand the [Generic] tag as meaning that. But I do see the point that it would be good to point out the difference to users between algs which work on generic graphs (fulfilling only some trait bounds) and algs that only work on specific graphs 🤔 I'll think about that then and maybe open a PR/Issue in the future about it :)

@RaoulLuque
Copy link
Member

RaoulLuque commented Jun 5, 2025

Regarding this:

* [ ]  Find out the space complexity of the `greedy_feedback_arc_set` algorithm

I would say the runtime is in O(|V| + |E|) and the space complexity is the same.

@starovoid
Copy link
Collaborator Author

I would say the runtime is in O(|V| + |E|) and the space complexity is the same.

Is there a proof for this estimate for the execution time? The author of reference paper gives O(|E|).

Copy link
Member

@RaoulLuque RaoulLuque left a comment

Choose a reason for hiding this comment

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

This is how far I got for now (min_spanning_tree.rs - alphaetically) :)
I'm not sure I'll be able to finish the review today. Until when would you need/like to have it?

@RaoulLuque
Copy link
Member

Is there a proof for this estimate for the execution time? The author of reference paper gives O(|E|).

Well, the + |V| term just makes sure that if there are not edges, the bound is still correct. I mean, we compute a good_node_sequence. And to actually compute the sequence, we will have to somehow iterate over each node at least once. So that's where I would imagine the + |V| term comes from.

Co-authored-by: Raoul Luqué <125205120+RaoulLuque@users.noreply.github.com>
@starovoid
Copy link
Collaborator Author

I'm not sure I'll be able to finish the review today. Until when would you need/like to have it?

There's no need to rush 👌, it's better to fix all the inaccuracies before release.

Copy link
Member

@RaoulLuque RaoulLuque left a comment

Choose a reason for hiding this comment

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

Alright, I am done now :) Thanks a lot again for the changes, seems like a great addition to make the petgraph more consistent and approachable for users 🌟

starovoid and others added 3 commits June 6, 2025 12:29
@starovoid starovoid added this pull request to the merge queue Jun 6, 2025
Merged via the queue into master with commit eeb98c4 Jun 6, 2025
10 checks passed
@starovoid starovoid deleted the unify-algo-docs branch June 6, 2025 10:15
@github-actions github-actions bot mentioned this pull request Jun 6, 2025
github-merge-queue bot pushed a commit that referenced this pull request Jun 6, 2025
## 🤖 New release

* `petgraph`: 0.8.1 -> 0.8.2 (✓ API compatible changes)

<details><summary><i><b>Changelog</b></i></summary><p>

<blockquote>

##
[0.8.2](https://github.com/petgraph/petgraph/compare/petgraph@v0.8.1...petgraph@v0.8.2)
- 2025-06-06

### Bug Fixes

- Ford Fulkerson sometimes Panics on StableGraphs
([#793](#793))
- Run Maximal Cliques Quickcheck only on Digraphs which are symmetrical
([#800](#800))
- Run Steiner Tree Quickcheck on the connected components to properly
support disconnected graphs
([#801](#801))
- Quickcheck random01 function only outputs 0
([#798](#798))

### Documentation

- Specify that Acyclic::try_udpate_edge may add an edge
([#770](#770))
- Update remove_node doc comment in graphmap.rs
([#663](#663))
- Add examples to minimum spanning tree functions
([#808](#808))
- Minimal typo fix in comments
([#803](#803))
- Update docs.rs ([#807](#807))
- Add note about `StableGraph::edge_indices` behaviour
([#812](#812))
- Clarification of references to nodes and V (refresh #358)
([#814](#814))
- Fix link and mention Dfs and Bfs as special case in examples
([#816](#816))
- Unify algo docs
([#815](#815))

### New Features

- *(parser)* allow parsing graphs from Dot/Graphviz files
([#653](#653))
- Implement `DataMap` for `GraphMap` graphs
([#776](#776))
- Add Johnson's algorithm
([#741](#741))
- Add algorithm to find bridge edges
([#590](#590))

### Performance

- Reuse queue allocation in `maximum_matching` main loop
([#817](#817))

### Refactor

- Fix new clippy warnings
([#791](#791))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: starovoid <prototyperailgun@gmail.com>
AnarchistHoneybun added a commit to AnarchistHoneybun/petgraph-contrib that referenced this pull request Jun 14, 2025
github-merge-queue bot pushed a commit that referenced this pull request Jun 16, 2025
I think the auxiliary space and time complexity as recently updated in
#815, are not quite right for Bron-Kerbosch.

For one, the auxiliary space complexity cannot be right. Since, we
return a `Vec` with `HashSet`, the auxiliary space has to be at least
equal to the number of maximal cliques in the graph. [Moon and Moser
showed](https://link.springer.com/article/10.1007/BF02760024) that this
can be up to `3 ^ (|V| / 3)` many. Thus, I suggest introducing `k` as a
variable to denote the number of maximal cliques in the graph.

The time complexity might also be able to be written in terms of `k`,
however this would probably also depend on this implementation of
Bron-Kerbosch with pivoting. Instead one could write something like:

```
/// * Time complexity: **O(p(|V|)k)**
```
For some polynomial `p`, which makes clear that the runtime is not
always exponential in the number of vertices, but is merely dominated by
the number of maximal cliques in the graph.
RaoulLuque added a commit to RaoulLuque/petgraph that referenced this pull request Jun 18, 2025
This PR aims to bring uniformity to the documentation of algorithms.

Now the doc comments will have the following structure:

```markdown
Main description...
...
...

# Arguments
...

# Returns
...

# Complexity
* Time complexity: ...
* Space complexity: ...

# Examples

```

For example, the `spfa` doc comment might look like:
```markdown
/// \[Generic\] Compute shortest paths from node `source` to all other.
///
/// Using the [Shortest Path Faster Algorithm][spfa].
/// Compute shortest distances from node `source` to all other.
///
/// Compute shortest paths lengths in a weighted graph with positive or negative edge weights,
/// but with no negative cycles.
///
/// ## Arguments
/// * `graph`: weighted graph.
/// * `source`: the source vertex, for which we calculate the lengths of the shortest paths to all the others.
/// * `edge_cost`: closure that returns cost of a particular edge
///
/// ## Returns
/// * `Err`: if graph contains negative cycle.
/// * `Ok`: a pair of a vector of shortest distances and a vector
///   of predecessors of each vertex along the shortest path.
///
/// ## Complexity
/// * Time complexity: **O(|V||E|)**, but it's generally assumed that in the average case it is **O(|E|)**
/// * Space complexity: **O(|V|)**
///
/// |V| is the number of nodes, |E| is the number of edges in the input graph.
///
///
/// [spfa]: https://www.geeksforgeeks.org/shortest-path-faster-algorithm/
///
/// # Example
```

---------

Co-authored-by: Raoul Luqué <125205120+RaoulLuque@users.noreply.github.com>
RaoulLuque pushed a commit to RaoulLuque/petgraph that referenced this pull request Jun 18, 2025
## 🤖 New release

* `petgraph`: 0.8.1 -> 0.8.2 (✓ API compatible changes)

<details><summary><i><b>Changelog</b></i></summary><p>

<blockquote>

##
[0.8.2](https://github.com/petgraph/petgraph/compare/petgraph@v0.8.1...petgraph@v0.8.2)
- 2025-06-06

### Bug Fixes

- Ford Fulkerson sometimes Panics on StableGraphs
([petgraph#793](petgraph#793))
- Run Maximal Cliques Quickcheck only on Digraphs which are symmetrical
([petgraph#800](petgraph#800))
- Run Steiner Tree Quickcheck on the connected components to properly
support disconnected graphs
([petgraph#801](petgraph#801))
- Quickcheck random01 function only outputs 0
([petgraph#798](petgraph#798))

### Documentation

- Specify that Acyclic::try_udpate_edge may add an edge
([petgraph#770](petgraph#770))
- Update remove_node doc comment in graphmap.rs
([petgraph#663](petgraph#663))
- Add examples to minimum spanning tree functions
([petgraph#808](petgraph#808))
- Minimal typo fix in comments
([petgraph#803](petgraph#803))
- Update docs.rs ([petgraph#807](petgraph#807))
- Add note about `StableGraph::edge_indices` behaviour
([petgraph#812](petgraph#812))
- Clarification of references to nodes and V (refresh petgraph#358)
([petgraph#814](petgraph#814))
- Fix link and mention Dfs and Bfs as special case in examples
([petgraph#816](petgraph#816))
- Unify algo docs
([petgraph#815](petgraph#815))

### New Features

- *(parser)* allow parsing graphs from Dot/Graphviz files
([petgraph#653](petgraph#653))
- Implement `DataMap` for `GraphMap` graphs
([petgraph#776](petgraph#776))
- Add Johnson's algorithm
([petgraph#741](petgraph#741))
- Add algorithm to find bridge edges
([petgraph#590](petgraph#590))

### Performance

- Reuse queue allocation in `maximum_matching` main loop
([petgraph#817](petgraph#817))

### Refactor

- Fix new clippy warnings
([petgraph#791](petgraph#791))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: starovoid <prototyperailgun@gmail.com>
RaoulLuque added a commit to RaoulLuque/petgraph that referenced this pull request Jun 18, 2025
…graph#825)

I think the auxiliary space and time complexity as recently updated in
petgraph#815, are not quite right for Bron-Kerbosch.

For one, the auxiliary space complexity cannot be right. Since, we
return a `Vec` with `HashSet`, the auxiliary space has to be at least
equal to the number of maximal cliques in the graph. [Moon and Moser
showed](https://link.springer.com/article/10.1007/BF02760024) that this
can be up to `3 ^ (|V| / 3)` many. Thus, I suggest introducing `k` as a
variable to denote the number of maximal cliques in the graph.

The time complexity might also be able to be written in terms of `k`,
however this would probably also depend on this implementation of
Bron-Kerbosch with pivoting. Instead one could write something like:

```
/// * Time complexity: **O(p(|V|)k)**
```
For some polynomial `p`, which makes clear that the runtime is not
always exponential in the number of vertices, but is merely dominated by
the number of maximal cliques in the graph.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants