rust-unofficial / patterns

A catalogue of Rust design patterns, anti-patterns and idioms
https://rust-unofficial.github.io/patterns/
Mozilla Public License 2.0
8.15k stars 374 forks source link

Graphs #202

Open simonsan opened 3 years ago

simonsan commented 3 years ago

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 linear in n.

Originally posted by @fade2black in https://github.com/rust-unofficial/patterns/issues/68#issuecomment-760524218

simonsan commented 3 years ago

Closed #68 to focus on discussing the topic here, may be reopened later to use it as a basis for a general Graphs intro.md, not sure.