Algorithms I: Queues Assignment (Princeton University)
This repository contains the solution for the Queues programming assignment, part of Princeton University's Algorithms, Part I course available on Coursera. The assignment involves implementing generic Deque and Randomized Queue data structures using linked lists and resizing arrays, respectively, along with a client program.
Assignment Goal
The core task is to implement two fundamental data structures:
Deque.java: A double-ended queue supporting adding/removing items from both front and back in constant time (using a doubly-linked list).
RandomizedQueue.java: A queue where removal and sampling operations select a random item, implemented with constant amortized time operations (using a resizing array).
Both structures must be generic and implement the Iterable interface.
A client program, Permutation.java, uses the RandomizedQueue to read strings from standard input and print exactly k of them uniformly at random.
For the full details, problem description, and API requirements, please see the Official Queues Assignment Specification.
Repository Structure
/src: Contains the Java source code:
Deque.java: Implements the generic double-ended queue using a linked list.
RandomizedQueue.java: Implements the generic randomized queue using a resizing array.
Permutation.java: Client program to print k random items from input.
/inputs: Contains sample input text files for testing Permutation.java (e.g., distinct.txt, duplicates.txt).
Core Concepts
- Deque: Implemented using a doubly-linked list to achieve constant worst-case time for all operations.
- RandomizedQueue: Implemented using a resizing array to achieve constant amortized time for queue operations. Randomness is achieved using
StdRandom.
- Generics: Both data structures use
<Item> to allow storing objects of any type.
- Iterators: Both data structures provide
Iterator implementations. The RandomizedQueue iterator returns items in a uniformly random, independent order.
- Reservoir Sampling: The
Permutation.java client implements this technique to select k items uniformly at random from an input stream of unknown size, using space proportional only to k.
How to Compile and Run the Permutation Client
These commands assume you are running them from the root directory of this repository.
Compile:
Run Permutation:
- Execute the
Permutation client, providing the number k as a command-line argument and piping input from a file.
# On Linux/macOS (replace ~/algs4/algs4.jar with your actual path)
java -cp ~/algs4/algs4.jar:src Permutation <k> < inputs-permutation/<input-file.txt>
# On Windows (replace C:\Users\username\algs4\algs4.jar with your actual path)
java -cp C:\Users\username\algs4\algs4.jar;src Permutation <k> < inputs-permutation/<input-file.txt>
- Replace
<k> with the desired number of items to print (e.g., 3).
- Replace
<input-file.txt> with an actual filename from the /inputs-permutation directory (e.g., distinct.txt).
- This command tells
java where to find the compiled classes (src) and the library (algs4.jar), which program to run (Permutation), passes k as a command-line argument (args[0]), and redirects the content of the input file to standard input.
Expected output
~/Desktop/queues> cat distinct.txt
A B C D E F G H I
~/Desktop/queues> java Permutation 3 < distinct.txt
C
G
A
~/Desktop/queues> java Permutation 3 < distinct.txt
E
F
G
Extra Credit Requirement Met
The Permutation.java client successfully implements the Reservoir Sampling algorithm using a RandomizedQueue. This fulfills the assignment's extra credit challenge by ensuring that the client uses memory proportional only to k (the number of items to print), regardless of the total number of items N in the input stream.
- Correctness: 49/49 tests passed
- Memory: 134/133 tests passed
- Timing: 193/193 tests passed
- Aggregate score: 100.08%