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

Convexity algorithm #97

Merged
merged 18 commits into from
Jul 31, 2023
Merged

Convexity algorithm #97

merged 18 commits into from
Jul 31, 2023

Conversation

lmondada
Copy link
Contributor

@lmondada lmondada commented Jul 28, 2023

This builds on top of #96, so it will be easier to review that one first!

@lmondada lmondada requested a review from aborgna-q July 28, 2023 07:33
Copy link
Collaborator

@aborgna-q aborgna-q left a comment

Choose a reason for hiding this comment

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

Great!

Do you think you could copy one of the tests as a benchmark?


/// A pre-computed datastructure for fast convexity checking.
///
/// TODO: implement for graph traits?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Outdated comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, thanks!

Comment on lines 17 to 18
P, // in the past
F, // in the future
Copy link
Collaborator

Choose a reason for hiding this comment

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

Using the full words as variant names would make the code more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

Comment on lines 30 to 31
// A temporary datastructure used during `is_convex`
causal: Vec<Causal>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this used to avoid allocations on each is_node_convex call? If not sure it's that necessary, specially since we'd only need to allocate max_ind - min_ind elems on each call (which hopefully is smaller than n).

Also, a bitvec would be better (or a CausalFlags wrapper to keep the nice Causal interface).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd rather leave this option (because in my usecase I really expect a lot of calls to this), but I can offer a third option, which would be to pass this as an optional argument to is_convex. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh but that would require to make Causal and CausalVec public, which I'm not too keen on.

Copy link
Collaborator

@aborgna-q aborgna-q Jul 28, 2023

Choose a reason for hiding this comment

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

Makes sense. Just leave it as is, we can change it in the future.
(but check the bitvec option, Vec<bool> is quite inefficient).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now using bitvec 😊

// The nodes in topological order
topsort_nodes: Vec<NodeIndex>,
// The index of a node in the topological order (the inverse of topsort_nodes)
topsort_ind: BTreeMap<NodeIndex, usize>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

The indices should be dense on a full graph toposort, so an UnmanagedMap would do better here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@lmondada
Copy link
Contributor Author

Comments addressed!

src/algorithms/convex.rs Outdated Show resolved Hide resolved
/// Create a new ConvexChecker.
pub fn new(graph: G) -> Self {
let mut topsort_nodes = Vec::with_capacity(graph.node_count());
while topsort_nodes.len() < graph.node_count() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't you traverse graph.nodes_iter() once, get nodes with no inputs, and run toposort from there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, sorry I'm getting tired. I mixed up two separate concerns: the only thing I needed to take care of is the case where there are inputs put they are not connected.

@lmondada
Copy link
Contributor Author

Good thing you're checking!

@lmondada lmondada merged commit 19bc312 into main Jul 31, 2023
@lmondada lmondada deleted the feature/convex branch July 31, 2023 05:31
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.

2 participants