Skip to content

Add UndirectedGraph and DirectedGraph data structures #7460

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 4 commits into from
Apr 16, 2024

Conversation

MaxDesiatov
Copy link
Contributor

@MaxDesiatov MaxDesiatov commented Apr 15, 2024

DirectedGraph can be used to represent the existing modules graph in the PackageGraph module hopefully with better performance due to reduced number of allocations. UndirectedGraph could represent a new linkage graph concept, which would help with breaking dependency cycles for certain packages with products that don't have a linkage cycle.

These can be used to represent the existing modules graph in the `PackageGraph` module, or a new linkage graph concept, which would help with breaking dependency cycles for certain packages.
@MaxDesiatov MaxDesiatov added no functional change No user-visible functional changes included dependency resolution labels Apr 15, 2024
@MaxDesiatov MaxDesiatov changed the title Add LinkageGraph and DirectedGraph data structures Add UndirectedGraph and DirectedGraph data structures Apr 15, 2024
@MaxDesiatov MaxDesiatov marked this pull request as ready for review April 15, 2024 15:34
@MaxDesiatov MaxDesiatov requested a review from xedin April 15, 2024 15:34
@MaxDesiatov
Copy link
Contributor Author

@swift-ci test

@MaxDesiatov MaxDesiatov self-assigned this Apr 15, 2024
@MaxDesiatov
Copy link
Contributor Author

@swift-ci test

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test windows

/// edge exists.
///
/// See https://en.wikipedia.org/wiki/Adjacency_matrix for more details.
struct AdjacencyMatrix {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the difference in adjacency list vs matrix between the directed and undirected graphs?

Copy link
Contributor Author

@MaxDesiatov MaxDesiatov Apr 16, 2024

Choose a reason for hiding this comment

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

I started with an adjacency list in a directed graph implementation, but then proceeded to an undirected graph where adjacency matrix seemed obviously more performant when adding edges: just flipping two bits in the matrix instead of adding potential array (re-)allocations with the list.

I'll add benchmarks to get a confirmation for that. I'm unsure though if either is a clear win in all cases, adjacency matrix may be fast at adding edges, but not the fastest for different kinds of graphs analysis and lookups. Keeping both implementations for now for future reference seemed like a sensible idea given that it's an NFC PR anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm unsure though if either is a clear win in all cases

Yeah, really depends on what type of graphs we have and what the main operation on them is. If they're sparse (which I would assume is the case) and we mainly want to loop over edges (rather than check if there is an edge), lists are likely better. Happy for you to keep both for now if you want.

@MaxDesiatov MaxDesiatov merged commit d811faf into main Apr 16, 2024
@MaxDesiatov MaxDesiatov deleted the maxd/graph-structures branch April 16, 2024 21:02
furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
…7460)

`DirectedGraph` can be used to represent the existing modules graph in the `PackageGraph` module hopefully with better performance due to reduced number of allocations. `UndirectedGraph` could represent a new linkage graph concept, which would help with breaking dependency cycles for certain packages with products that don't have a linkage cycle.

I started with an adjacency list in a directed graph implementation, but then proceeded to an undirected graph where adjacency matrix seemed obviously more performant when adding edges: just flipping two bits in the matrix instead of adding potential array (re-)allocations with the list.

I'm unsure though if either is a clear win in all cases, adjacency matrix may be fast at adding edges, but not the fastest for different kinds of graphs analysis and lookups. Keeping both implementations for future reference.
furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
…7460)

`DirectedGraph` can be used to represent the existing modules graph in the `PackageGraph` module hopefully with better performance due to reduced number of allocations. `UndirectedGraph` could represent a new linkage graph concept, which would help with breaking dependency cycles for certain packages with products that don't have a linkage cycle.

I started with an adjacency list in a directed graph implementation, but then proceeded to an undirected graph where adjacency matrix seemed obviously more performant when adding edges: just flipping two bits in the matrix instead of adding potential array (re-)allocations with the list.

I'm unsure though if either is a clear win in all cases, adjacency matrix may be fast at adding edges, but not the fastest for different kinds of graphs analysis and lookups. Keeping both implementations for future reference.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependency resolution no functional change No user-visible functional changes included
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants