Omnimaga

General Discussion => Other Discussions => Math and Science => Topic started by: harold on March 05, 2013, 02:39:18 pm

Title: Interesting data structures and algorithm you may not have heard about
Post by: harold on March 05, 2013, 02:39:18 pm
I'll start out with some stuff I actually use and may not be widely known: ("widely known" is stuff such as red-black trees, array-based heaps, circular buffers, hash tables, linked lists, you name it - first year CS material)
Let's get this going, I'm sure we can all learn something new and interesting from this thread if people start adding stuff to it.
Title: Re: Interesting data structures and algorithm you may not have heard about
Post by: Scipi on March 05, 2013, 02:45:26 pm
A lot of these I've never heard about.

I will have to take a look into understanding these and implementing them in a program or two :D

/me doesn't know any data structures or algorithms off the top of his head)
Title: Re: Interesting data structures and algorithm you may not have heard about
Post by: harold on March 06, 2013, 04:25:58 am
Come on guys, post some more :)
Title: Re: Interesting data structures and algorithm you may not have heard about
Post by: calc84maniac on March 06, 2013, 12:58:06 pm
Binary Indexed Tree (http://en.wikipedia.org/wiki/Binary_indexed_tree), a data structure which represents an array that can both be modified in O(log n) time and retrieve the sum of any range of elements in O(log n) time. In addition, the storage requirements are no more than that of an array of the elements.

The alternatives to this method are simply keeping an array of the elements, which is O(1) for modification and O(n) for range sum, or keeping an array of the cumulative sum, which is O(n) for modification and O(1) for range sum. Binary indexed tree is the best of both methods at O(log n) for each operation, and it can be implemented in a very small amount of code.

Seriously small:
Code: [Select]
// Note that indices start at 1

// Get sum from elements 1 to k
int get_prefix_sum(int k) {
   int sum=0;
   for(; k; k-=(k&-k))
      sum += array[k-1];
   return sum;
}

// Add n to element k
void add_to_element(int k, int n) {
   for(; k<=array_size; k+=(k&-k))
      array[k-1] += n;
}

Edit: Also, here are some related helper functions.
Spoiler For Spoiler:
Code: [Select]
// Sum from element a to b
int range_sum(int a, int b) {
   return get_prefix_sum(b) - get_prefix_sum(a-1);
}

// Get element k
int get_element(int k) {
   return range_sum(k,k);
}

// Set element k to value n
void set_element(int k, int n) {
   add_to_element(k, n - get_element(k));
}
Title: Re: Interesting data structures and algorithm you may not have heard about
Post by: harold on March 08, 2013, 04:34:48 am
Very good, Binary Indexed Trees are awesome, simple but powerful :)

Here's some more stuff I find interesting:
Title: Re: Interesting data structures and algorithm you may not have heard about
Post by: AngelFish on March 08, 2013, 01:04:30 pm


More of an idea than a specific algorithm, but Secure Multi-party Computation (http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.11.2201&rep=rep1&type=pdf). Quite possibly one of my favorite pieces.

Blum Blum Shub (no free links, sorry). It's a cryptographic random number generator of the form: xn+1 = xn2 mod M, where M is the product of two large primes. Each iteration can produce a max of two bits (!) of random numbers, which can be done via the following: (parity(xn)*(xn & 0x01))*(parity(xn+1)*(xn+1 & 0x01)). Alternatively, you can just take the straight parity or the least significant bit of the output and get one bit per iteration.
Title: Re: Interesting data structures and algorithm you may not have heard about
Post by: Scipi on March 08, 2013, 01:41:55 pm
Well, what would you know, I do have something to add here. :D

This is something I just found today and am trying out.


Edit:

This article might be more relevant http://en.wikipedia.org/wiki/Artificial_neural_network (http://en.wikipedia.org/wiki/Artificial_neural_network)
Title: Re: Interesting data structures and algorithm you may not have heard about
Post by: harold on March 30, 2013, 02:56:41 pm