-
Notifications
You must be signed in to change notification settings - Fork 384
Graphs Pattern #68
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Graphs Pattern #68
Conversation
```rust | ||
// A node of the graph. | ||
pub struct Node<T> { | ||
parent: Option<NodeId>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a node can have at most one parent, then the data structure is a tree, not a graph. Nodes in a graph can have more than one parent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks more like a forest, since this code allows for multiple roots 😉
This is a way to have a graph in Rust, I'm not sure if its the best way. It's also possible to use |
I made a distinction. |
Found this while going through old PRs. In my view, the intention of this patterns repo is to document situations where there is usually one "best" solution. So an overview of many different graph implementations, while valuable, would not belong here. I will close this PR. Thanks! |
Imho a |
But yeah, I know what lambda-fairy wanted to state, it's hard to tell this is a good solution or this not. My guess with the book would probably be a subchapter with graphs and then give some examples or probably better to explain the internal graph data structures as a pattern, something like that: and give input on how you would do that on not give some custom implementation. But maybe some recommended crates with already implemented data structures to use, to not reinvent the wheel. Hmm, thoughts? |
I think Graph data structure, on its own, is too wide topic to cover in a couple of pages. As a rule, complexity of algorithms on graphs directly depends their representation. What kind of representation we will choose depends on our use case. Factors to take into account:
Let me give a simple example. Say, I know my graph changes (new edges or vertex) once a day, but the graph is almost complete and in my algorithms I traverse neighbours of a single vertex not too often. I'd probably use adjacency matrix. Or another example. If we know that our graph has too few edges and we do extensive DFS then matrix is a bad choice since it would have O(n^2) complexity, while using adj list we could reduce it to linear time O(n) if say m is liner in n. |
I think as well, that it should be a complete chapter of different graph structures, would you agree? |
@simonsan I agree. We could categorise into use cases or representations, or anything else what people think. But not everything in a single page. Of course the Rust way should be put on the first place. |
I opened an issue for discussions, it's easier going forward I guess |
This is too wide and there are few patterns to solve this. But there are also many cases of graph, I think we may need a category rather than writing it like this. For example:
|
Let's use issue #202 to discuss it, I'll mark this as zombie for the future and close it for now. |
An idiomatic way to implement graphs in Rust.