# Tag Info

128

There's a textbook waiting to be written at some point, with the working title Data Structures, Algorithms, and Tradeoffs. Almost every algorithm or data structure which you're likely to learn at the undergraduate level has some feature which makes it better for some applications than others. Let's take sorting as an example, since everyone is familiar with ...

105

Consider the set of keys $K=\{0,1,...,100\}$ and a hash table where the number of buckets is $m=12$. Since $3$ is a factor of $12$, the keys that are multiples of $3$ will be hashed to buckets that are multiples of $3$: Keys $\{0,12,24,36,...\}$ will be hashed to bucket $0$. Keys $\{3,15,27,39,...\}$ will be hashed to bucket $3$. Keys $\{6,18,30,42,...\}$ ...

84

Heap just guarantees that elements on higher levels are greater (for max-heap) or smaller (for min-heap) than elements on lower levels, whereas BST guarantees order (from "left" to "right"). If you want sorted elements, go with BST.by Dante is not a geek Heap is better at findMin/findMax (O(1)), while BST is good at all finds (O(logN)). Insert is O(logN)...

52

Aside from the fact that there are myriads of cost measures (running time, memory usage, cache misses, branch mispredictions, implementation complexity, feasibility of verification...) on myriads of machine models (TM, RAM, PRAM,...), average-vs-worst-case as well as amortization considerations to weigh against each other, there are often also functional ...

44

Answer why was the data considered to be a discrete mathematical entity rather than a continuous one This was not a choice; it is theoretically and practically impossible to represent continuous, concrete values in a digital computer, or actually in any kind of calculation. Note that "discrete" does not mean "integer" or something like that. "discrete" ...

44

Fixed-size queues are often implemented using what some people call circular buffers. If you remove the protection against it being full, you get the desired behaviour. Of course, no actual pushing will happen in the array -- that would be too expensive -- but it will look like it from the outside.

44

Sure. Certainly. Here's how to reconcile your discomfort. When we analyze the running time of algorithms, we do it with respect to a particular model of computation. The model of computation specifies things like the time it takes to perform each basic operation (is an array lookup $O(\log n)$ time or $O(1)$ time?). The running time of the algorithm ...

43

Summary Type BST (*) Heap Insert average log(n) 1 Insert worst log(n) log(n) or n (***) Find any worst log(n) n Find max worst 1 (**) 1 Create worst n log(n) n Delete worst log(n) log(n) All average times on this table are the same as their worst times except for Insert. *: everywhere in ...

37

Both binary search trees and binary heaps are tree-based data structures. Heaps require the nodes to have a priority over their children. In a max heap, each node's children must be less than itself. This is the opposite for a min heap: Binary search trees (BST) follow a specific ordering (pre-order, in-order, post-order) among sibling nodes. The tree must ...

33

What you described is Voronoi diagram. Here is an excerpt from Wikipedia. In the simplest case, shown in the first picture, we are given a finite set of points ${p_1, \cdots, p_n}$ in the Euclidean plane. In this case each site $p_k$ is simply a point, and its corresponding Voronoi cell $R_k$ consists of every point in the Euclidean plane whose distance to $... 31 I found this post very helpful. To see the difference between Patricia tries and radix trees, it is important to understand: The notion of radix, since Patricia tries are radix trees with radix equal to 2. The way keys are treated: as streams of bits. Keys are compared$r$bits at a time, where$2^r$is the radix of the trie. Suppose that we insert the ... 30 Computers represent a piece of data as a finite number of bits (zeros and ones) and the set of all finite bit strings is discrete. You can only work with, say, real numbers if you find some finite representation for them. For example, you can say "this data corresponds to the number$\pi$", but you cannot store all digits of$\pi$in a computer. Hence, ... 28 As far as I'm concerned, null, nil, none and nothing are common names for the same concept: a value which represents the “absence of a value”, and which is present in many different types (called nullable types). This value is typically used where a value is normally present, but may be omitted, for example an optional parameter. Different programming ... 28 You can achieve constant amortized time per operation by keeping a dynamically-sized array$A$(using the doubling/halving technique). To insert an element append it at the end. To implement remove_random() generate a random index$k$between$1$and$n$, swap$A[k]$with$A[n]$and delete (and return)$A[n]$. If you want a non-amortized worst-case bound on ... 26 In many algorithms we don't need to check whether two vertices are adjacent, like in search algorithms, DFS, BFS, Dijkstra's, and many other algorithms. In the cases where we only need to enumerate the neighborhoods, a list/vector/array far outperforms typical set structures. Python's set uses a hashtable underneath, which is both much slower to iterate ... 24 Positive result: persistence does not cost too much. One can show that every data structure can be made fully persistent with at most a$O(\lg n)$slowdown. Proof: You can take an array and make it persistent using standard data structures (e.g., a balanced binary tree; see the end of this answer for a bit more detail). This incurs a$O(\lg n)$slowdown: ... 24 By cheating, and doing two passes at the same time, in parallel. But I do not know whether the recruiters will like this. Can be done on a single linked list, with a nice trick. Two pointers travel over the list, one with double speed. When the fast one reaches the end, the other one is half-way. 24$\small \texttt{find-min}$(resp.$\small \texttt{find-max}$),$\small \texttt{delete-min}$(resp.$\small \texttt{delete-max}$) and$\small \texttt{insert}$are the three most important operations of a min-heap (resp. max-heap), and they usually have complexity of$\small \mathcal{O}(1)$,$\small \mathcal{O}(\log n)$and$\small \mathcal{O}(\log n)$... 23 First of all note that sparse means that you have very few edges, and dense means many edges, or almost complete graph. In a complete graph you have$n(n-1)/2$edges, where$n$is the number of nodes. Now, when we use matrix representation we allocate$n\times n$matrix to store node-connectivity information, e.g.,$M[i][j] = 1$if there is edge between ... 20 A Fenwick tree is a binary tree used to efficiently handle cumulative frequencies or sums in an array. Without loss of generality we shall examine a 16-element array. Imagine a binary tree imposed on top of the array. Furthermore, label all the left edges in this tree with a "0" and all the right edges with a "1". We get something like this: This is the so ... 17 Algorithms for isomorphism problems such as graph isomorphism rely heavily on group theory. An unusual example of group theory applied to computer science is the famous proof of Barrington's theorem, which uses the nonsolvability of the symmetric group$S_5$to show equality of two complexity classes that superficially have nothing whatsoever to do with ... 16 Create a buffer of size$2k$. Read in$2k$elements from the array. Use a linear-time selection algorithm to partition the buffer so that the$k$smallest elements are first; this takes$O(k)$time. Now read in another$k$items from your array into the buffer, replacing the$k$largest items in the buffer, partition the buffer as before, and repeat. This ... 16 Here is an array: A[0] = Alice A[1] = Bob A[2] = Charlie Here 0,1,2 are indices. Now suppose that we want to know which index contains a given word. Then we use a dictionary: D[Alice] = 0 D[Bob] = 1 D[Charlie] = 2 This is an inverted index (according to your Wikipedia quote). The word index has different meaning in different contexts: Technical books ... 15 Here's a simpler logical proof. Last leaf is$n^{th}$index. Its parent is at index$\lfloor{n/2}\rfloor$and similarly, there is no element such that its parent is ($\lfloor n/2 +1\rfloor)^{th}$element. Thus leaves are indexed from$\lfloor{n/2}\rfloor$+1 to n. Hence, total number of leaves = n-$\lfloor(n/2)\rfloor$=$\lceil(n/2)\rceil$. 15 The entries of a hash table are stored in an array. However, you have misunderstood the application of the modulo operator to the hash values. If the hash table is stored in an array of size$n$, then the hash function is computed modulo$n$, regardless of how many items are currently stored in the table. So, in your example, if you were storing ... 15 Well, that is basically what all data structures boil down to. Data with connections. The nodes are all artificial - they don't actually exist physically. This is where the binary part comes in. You should create a few data structures in C++ and check out where your objects land in memory. It can be very interesting to learn about how the data is laid out ... 15 My solution is similar to j_random_hacker's but uses only a single hash set. I would create a hash set of strings. For each string in the input, add to the set$k\$ strings. In each of these strings replace one of the letters with a special character, not found in any of the strings. While you add them, check that they are not already in the set. If they are ...

15

Since you clearly don't care about the order of elements changing, I think the simplest approach is to use a resizable array (like C++'s std::vector or Java's java.util.ArrayList). When you remove an element, if it's not the last element, you just move the last element to take its place. That gives amortized-constant-time add and constant-time remove_random. ...

14

Hash tables can only tell you if an element is present or not. Here are somethings you can do with a binary tree that you can't do wiht a hash table. sorted traversal of the tree find the next closest element find all elements less than or greater than a certain value See this wikipedia article on K-d trees for an example of a real world data structure ...

14

To quote from the answer to “Traversals from the root in AVL trees and Red Black Trees” question For some kinds of binary search trees, including red-black trees but not AVL trees, the "fixes" to the tree can fairly easily be predicted on the way down and performed during a single top-down pass, making the second pass unnecessary. Such insertion ...

Only top voted, non community-wiki answers of a minimum length are eligible