Skip to content

Improve PNC algorithm #40364

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

Merged
merged 14 commits into from
Aug 2, 2025
Merged

Improve PNC algorithm #40364

merged 14 commits into from
Aug 2, 2025

Conversation

kappybar
Copy link
Contributor

@kappybar kappybar commented Jul 2, 2025

Improve the implementation of PNC algorithm.
See comments in #40284.

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

#40284

@kappybar
Copy link
Contributor Author

kappybar commented Jul 2, 2025

I am wondering whether there can be more improvement of this implementation of PNC algorithm.

when getting a path in G \ path[:dev_idx] when path is not simple, I do not care green vertices (from green vertices, reachable to the target vertex by a path with residual weight 0, so shortest path is calculated effectively). To realize this, it will be better to implement dijkstra function tailoring to this case. (But I am not sure how the current shortest path function implemented. The current one may already be good. I will have to see the implementation.) I am not quite sure this improves the current implementation, but I think its worth trying. How do you think about it?

@dcoudert

Copy link

github-actions bot commented Jul 2, 2025

Documentation preview for this PR (built with commit 53d8308; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@dcoudert
Copy link
Contributor

dcoudert commented Jul 2, 2025

Some ideas to be tested:

  • instead of using shortest_paths from boost_graph, you could use G._backend.bidirectional_dijkstra_special. I don't know if it is faster, but at least it avoids to copy the graph to another format at each call.
  • Relabel the graph so that vertices are named from 0 to n-1. This way, instead of using a dictionary ancestor_idx_dict, you could use a list or C array or C++ vector. You will off course have to relabel a path before yielding it. To do so, you can use
cdef list int_to_vertex = list(G)
cdef dict vertex_to_int = {u: i for i, u in enumerate(int_to_vertex)}
G.relabel{perm=vertex_to_int, inplace=True)
cdef int id_source = vertex_to_int[source]
cdef int id_target = vertex_to_int[target]
  • The idea of modifying Dijkstra to stop when reaching a green vertex is interesting, but as you said, using the reduced weights should already speed up computation. So I'm not sure we of the benefit of yet another implementation of Dijkstra...

@dcoudert dcoudert added c: graph theory gsoc: 2025 Tag for GSoC2025 issues/PRs labels Jul 2, 2025
@kappybar
Copy link
Contributor Author

kappybar commented Jul 9, 2025

I have done the second, the fourth improvements. It became slightly faster. I have already done the first one.
The remaining (possible) improvement is the thrid one. Now, I don't have time to implement yet another dijkstra, but I hope I can do it in a week and a few days.

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 light be other possible improvements, but I'm not sure which ones yet.

@dcoudert
Copy link
Contributor

Let me know if you plan to do more on this PR or if you prefer to use a follow up PR for other changes.

@kappybar
Copy link
Contributor Author

Sorry for the inconvenience. I will plan to do more on this PR.

@kappybar
Copy link
Contributor Author

kappybar commented Jul 13, 2025

I implemented the shortest path computation for a set of target vertices (not one target vertex) and use it to compute shortest path to green vertices in PNC algorithm. It became faster than previous one in my laptop (150-160ms to 90-100ms for graphs.Grid2dGraph(5, 5)).

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.

a few suggestions to improve method shortest_path_to_set:

  • prefer source and targets to x and y_set
  • Ensure that exclude_vertices is turned to a set when not empty to speed up tests
  • add tests for cases when the source is in exclude_vertices or in targets
  • is the method working well if a target is in exclude_vertices ?
  • is it faster to use a priority queue or a pairing heap (this data structure has decrease key operation) ?
  • when iterating over edges, prefer label to l to avoid confusion with 1

@kappybar
Copy link
Contributor Author

kappybar commented Jul 14, 2025

is it faster to use a priority queue or a pairing heap (this data structure has decrease key operation)

They are not much different. Either one is ok.

@dcoudert
Copy link
Contributor

you should rebase on the last beta to better highlight the changes done in this PR.

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.

Shouldn't we raise an error when no path can be found ?

@dcoudert
Copy link
Contributor

I agree with the addition of the shortest path to set method. This might be useful to others.
However, the drawback for our case is the need for extracting the set of green vertices. The advantage of coloring vertices is to explore a smaller portion of the graph when calling Dijkstra. It may happen that the identification of the set of green vertices dominates the search.
An alternative is to design a specific shortest path method that calls ancestor_idx_func, and so colors the smallest possible portion of the graph. There is a tradeoff to find here.

@kappybar
Copy link
Contributor Author

I agree with you. Coloring only necessary portions of graphs will be faster. I try to implement it.

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.

is it faster in your experiments ?

@kappybar
Copy link
Contributor Author

kappybar commented Jul 17, 2025

is it faster in your experiments ?

It is a little faster, though I tested only one instance.

D = graphs.Grid2dGraph(5, 5).relabel(inplace=False).to_directed()
%time len(list(pnc(D, 0, 24)))

will take about 90ms now, but previous one takes about 110ms. And execution time becomes stable (the previous one sometimes takes 150ms), but I don't know why.

I would like to also know how execution time changed in your environment.

@dcoudert
Copy link
Contributor

on a linux computer I get

sage: D = graphs.Grid2dGraph(5, 5).relabel(inplace=False).to_directed()
sage: %time len(list(pnc(D, 0, 24)))
CPU times: user 159 ms, sys: 6 µs, total: 159 ms
Wall time: 159 ms
8512

on a Mac M1 I get

sage: %time len(list(pnc(D, 0, 24)))
CPU times: user 232 ms, sys: 1.02 ms, total: 233 ms
Wall time: 232 ms
8512

both are faster than what I get before.

@kappybar
Copy link
Contributor Author

@dcoudert
I think this implementation of PNC algorithm became faster than before.
Is it ok to finish this PR?

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.

I agree that it's faster than before. Thanks.

LGTM.

vbraun pushed a commit to vbraun/sage that referenced this pull request Jul 26, 2025
sagemathgh-40364: Improve PNC algorithm
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Improve the implementation of PNC algorithm.
See comments in sagemath#40284.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->

sagemath#40284
    
URL: sagemath#40364
Reported by: Yuta Inoue
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Jul 26, 2025
sagemathgh-40364: Improve PNC algorithm
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Improve the implementation of PNC algorithm.
See comments in sagemath#40284.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->

sagemath#40284
    
URL: sagemath#40364
Reported by: Yuta Inoue
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Jul 27, 2025
sagemathgh-40364: Improve PNC algorithm
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Improve the implementation of PNC algorithm.
See comments in sagemath#40284.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->

sagemath#40284
    
URL: sagemath#40364
Reported by: Yuta Inoue
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Jul 28, 2025
sagemathgh-40364: Improve PNC algorithm
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Improve the implementation of PNC algorithm.
See comments in sagemath#40284.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->

sagemath#40284
    
URL: sagemath#40364
Reported by: Yuta Inoue
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Jul 29, 2025
sagemathgh-40364: Improve PNC algorithm
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Improve the implementation of PNC algorithm.
See comments in sagemath#40284.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->

sagemath#40284
    
URL: sagemath#40364
Reported by: Yuta Inoue
Reviewer(s): David Coudert
@kappybar kappybar mentioned this pull request Jul 30, 2025
5 tasks
@vbraun vbraun merged commit b9cbfdd into sagemath:develop Aug 2, 2025
22 of 24 checks passed
vbraun pushed a commit to vbraun/sage that referenced this pull request Aug 10, 2025
sagemathgh-40510: Improve Feng, PNC algorithm
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

+ Fix several bugs of PNC algorithm.
+ Improve implementation of Feng algorithm.
+ Add PNC as a supported algorithm in shortest_simple_paths

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
sagemath#40364
    
URL: sagemath#40510
Reported by: Yuta Inoue
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Aug 12, 2025
sagemathgh-40510: Improve Feng, PNC algorithm
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

+ Fix several bugs of PNC algorithm.
+ Improve implementation of Feng algorithm.
+ Add PNC as a supported algorithm in shortest_simple_paths

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
sagemath#40364
    
URL: sagemath#40510
Reported by: Yuta Inoue
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Aug 13, 2025
sagemathgh-40510: Improve Feng, PNC algorithm
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

+ Fix several bugs of PNC algorithm.
+ Improve implementation of Feng algorithm.
+ Add PNC as a supported algorithm in shortest_simple_paths

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
sagemath#40364
    
URL: sagemath#40510
Reported by: Yuta Inoue
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Aug 14, 2025
sagemathgh-40510: Improve Feng, PNC algorithm
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

+ Fix several bugs of PNC algorithm.
+ Improve implementation of Feng algorithm.
+ Add PNC as a supported algorithm in shortest_simple_paths

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
sagemath#40364
    
URL: sagemath#40510
Reported by: Yuta Inoue
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Aug 15, 2025
sagemathgh-40510: Improve Feng, PNC algorithm
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

+ Fix several bugs of PNC algorithm.
+ Improve implementation of Feng algorithm.
+ Add PNC as a supported algorithm in shortest_simple_paths

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
sagemath#40364
    
URL: sagemath#40510
Reported by: Yuta Inoue
Reviewer(s): David Coudert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: graph theory gsoc: 2025 Tag for GSoC2025 issues/PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants