viernes, 26 de septiembre de 2014

Algorithms, part I (Princeton University) Week 2: Queues

The goal of this assignment was to implement elementary data structures using arrays and linked lists, and to be introduced to generics and iterators.

Deque

double-ended queue or deque (pronounced "deck") is a generalization of a stack and a queue that supports inserting and removing items from either the front or the back of the data structure.

The deque implementation had to support each deque operation in constant worst-case time and use space proportional to the number of items currently in the deque. This lead to the use of a singly linked list as the basic datastructure. But the removeLast() operation at first caused a problem: the fact that we had to keep track of the node inmediately before the last one made this operation of time O(n) since I had to iterate from the first node to that one and then be able to get the new tail node.

So the solution was to use a doubly linked list. This way, i had access to the previous node of the last node in the list without having to iterate. Needless to say that a doubly linked list is a bit more complicated than a singly one!!

 Randomized Queue

randomized queue is similar to a stack or queue, except that the item removed is chosen uniformly at random from items in the data structure.

The randomized queue implementation had to support each randomized queue operation (besides creating an iterator) in constant amortized time and use space proportional to the number of items currently in the queue. In that case the use of a resizable array was the solution.

Also, the order of two or more iterators to the same randomized queue should be mutually independent; each iterator had to maintain its own random order.

The interesting thing about resizing the array is that it has been proved that the best practice is to double the size of the array when it gets full but only downsize the array by half the current size if the array is 1/4 full at most and not 1/2. This way we avoid upsizing and downsizing the array if we perform multiples consecutive enqueue() and dequeue() operations.

Bonus: Reservoir Sampling Algorithm

Now the interesting thing about this assignment was the possibility to discover and implement the reservoir sampling algorithm (http://en.wikipedia.org/wiki/Reservoir_sampling)

The requirement was as follows:
"Write a client program Subset.java that takes a command-line integer k; reads in a sequence of N strings from standard input using StdIn.readString(); and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. You may assume that 0 ≤ k ≤ N, where N is the number of string on standard input.
(For an extra challenge, use only one Deque or RandomizedQueue object of maximum size at most k.)"

The first approach would be to store each new n-th item and then select k elements but then we need a storage capacity of N. This algorithm solves that by using only k-items storage capacity.

This algorithm is useful when we need to get a random subset of fixed k elements from a set of unknown size N.

Finally the results i obtained by the grader:

Compilation:  PASSED
Style:        FAILED
Findbugs:     No potential bugs found.
API:          PASSED

Correctness:  35/35 tests passed
Memory:       50/49 tests passed
Timing:       24/24 tests passed

Raw score: 100.20% [Correctness: 65%, Memory: 10%, Timing: 25%, Style: 0%]


No hay comentarios:

Publicar un comentario