Earlier quoted context omitted.
Storing the graph structure in a BST is only useful if your graph is very sparse and you need to have fast lookup for checking specific edges (say, given two nodes, find the cost for the edge between them). If your graph is dense, using a an adjacency matrix is simpler and will be faster most of the time. If you don't need to query specifific edges and all you need to do is iterate over the edges for given vertices t…
if my graph is dense then would using a adjacency matrix take like O(V^2) space complexity ? Anyway I was just suggesting this because I use it in practice. Just wanted to know the cons of it if any.
Big-O Algorithm Complexity Cheat Sheet
121–130 of 136 posts
Re: Big-O Algorithm Complexity Cheat Sheet
#122Earlier quoted context omitted.
For most graph algorithms it doesn't matter in which order you traverse neighbours, you just need to visit them all.
As an aside, trees (Red-Black trees for std::map in libstdc++) have terrible locality, and thus cache behavior. In general, for reasonable n, it's even going to be better to have a vector > in which you literally push to each vector (for amortized O(1) each time) when you find an adjacency. In the case of dense graphs, yes, adjacency matrices will be even better, since you're going to pay the size cost anyway, and yo…
Just because std::set and map are completely awful doesn't mean you should completely give up on trees.
Re: Big-O Algorithm Complexity Cheat Sheet
#123Re: Big-O Algorithm Complexity Cheat Sheet
#124Re: Big-O Algorithm Complexity Cheat Sheet
#125If you want to visualise big O runtime, you draw it on a log-log scale. The linear gradient on the log-log plot is the factor. i.e. if its at 45 degrees its O(n), if its at a gradient of 2:1 its O(n^2). Handy fact to work out your big O without having to do the tedious math! (See http://jcsites.juniata.edu/faculty/kruse/cs2/ch12a.htm )
A log-log plot is useful for polynomials, but has nothing to do with big O notation in general.
Re: Big-O Algorithm Complexity Cheat Sheet
#126I think the graph at the end is the most useful thing. Really helps understanding the complexity in relative terms.
Adding tree stuff would be cool (especially for search which is often implemented as either a tree or graph version)
Re: Big-O Algorithm Complexity Cheat Sheet
#127Earlier quoted context omitted.
Quicksort is O(k*n log n) though since it does O(n log n) comparisons and a comparison is going to be O(k), so radix sort is still asymptotically faster.
See response to Retric: https://news.ycombinator.com/item?id=5656534 A comparison isn't always O(k). It's O(k) for strings. It's O(1) for machine-word integers. It can be much more than O(k) for user-defined types, eg. it can be O(k^3) if you have to multiply a matrix out and take the determinant of the product.
So the fact is that quicksort is a factor O(log n) slower than radix sort. The flip side is that quicksort is more generally applicable to any comparison function whereas radix sort only works for lexicographic sorts. In almost all cases that is exactly what you want, but for example I'm not aware of any way to efficiently sort rational numbers with radix sort.
Re: Big-O Algorithm Complexity Cheat Sheet
#128Earlier quoted context omitted.
> You can also make quicksort worst-case complexity to be nlogn. Quicksort in the worst take can take O(n^2) time, not O(nlogn).
You can use a randomized selection algorithm to find the median in linear time, and if you use the median as a pivot you will never get worst case n^2 behavior. This is not used in practice because the probability of getting worst case behavior is extremely slim if you do some clever, and cheap, tricks.
Re: Big-O Algorithm Complexity Cheat Sheet
#129Earlier quoted context omitted.
Storing the graph structure in a BST is only useful if your graph is very sparse and you need to have fast lookup for checking specific edges (say, given two nodes, find the cost for the edge between them). If your graph is dense, using a an adjacency matrix is simpler and will be faster most of the time. If you don't need to query specifific edges and all you need to do is iterate over the edges for given vertices t…
if my graph is dense then would using a adjacency matrix take like O(V^2) space complexity ? Anyway I was just suggesting this because I use it in practice. Just wanted to know the cons of it if any.
Re: Big-O Algorithm Complexity Cheat Sheet
#130Earlier quoted context omitted.
Radixsort isn't linear ( https://en.wikipedia.org/wiki/Radix_sort ). I have actually published research that said Radixsort was linear, only to have it later explained to me that it is not linear, for a subtle an hard to remember reason involving number theory.
It's O(k * n) where k is the number of digits. It takes log-b(N) digits to represent N distinct integers in base-b, so O(k * N) reduces to O(N log N) when there's no bound on the range of keys. It's still pretty useful for sorting data where you know the keys are small integers (say, less than a machine word).
If k ∈ Θ(log N), then O(Nk) becomes O(N log N), which is asymptotically no better than any optimal comparison based sort.
However, if k ∈ o(log N), then we get an asymptotically better algorithm.