Self learning of Princeton University's Algorithms course in Coursera.
Instructors: Robert Sedgewick, Kevin Wayne
Textbook: Algorithms, 4th
Coursera:
Interview Questions are optional, you should write it if you want a job. Every week has an assignment which requires content of that week and previous weeks.
Week | Lecture | Textbook | Practice Quiz | Programming Assignments |
---|---|---|---|---|
1 | Course Introduction | 1.1, 1.2 | Project 0: Hello, World | |
Union-Find | 1.5 | Union-Find | Project 1: Percolation | |
Analysis of Algorithms | 1.4 | Analysis of Algorithms | ||
2 | Stacks and Queues | 1.3 | Stacks and Queues | Project 2: Queues |
Elementary Sorts | 2.1 | Elementary Sorts | ||
3 | Mergesort | 2.2 | Mergesort | Project 3: Collinear |
Quicksort | 2.3 | Quicksort | ||
4 | Priority Queues | 2.4 | Priority Queues | Project 4: 8 Puzzle |
Elementary Symbol Tables | 3.1, 3.2 | Elementary Symbol Tables | ||
5 | Balanced Search Trees | 3.3 | Balanced Search Trees | |
Geometric Applications of BSTs | Project 5: Kd-Trees | |||
6 | Hash Tables | 3.4 | Hash Tables | |
Symbol Table Applications |
Week | Lecture | Textbook | Practice Quiz | Programming Assignments |
---|---|---|---|---|
1 | Course Introduction | 1.1, 1.2 | ||
Undirected Graphs | 4.1 | Undirected Graphs | ||
Directed Graphs | 4.2 | Directed Graphs | Project 6: WordNet | |
2 |
Q: Why each edge counted twice in count self-loops ?
public static int numberOfSelfLoops(Graph G)
{
int count = 0;
for (int v = 0; v < G.V(); v++)
for (int w : G.adj(v))
if (v == w) count++;
return count/2; // each edge counted twice
}
A: This depends on the Graph API implementation. Self-loops are inserted twice (since v == w).
public void addEdge(int v, int w)
{
adj[v].add(w);
adj[w].add(v);
}
Implementations of class HelloWorld and class HelloGoodbye are uncessary import algs4.jar.
Thus, CLI use javac and java instead of javac-algs4 and java-algs4.
Note: Knuth’s method, when reading ith word which i is start from 1, not 0.
In class Percolation, method open() should not open site repeated times. If you get a more larger value, check your open() method whether it open site multiply times and count the repeated times.
For RandomizedQueue.java's deque operation, I swap random item with tail item.
For client part, command below:
java Permutation 3 < distinct.txt
should be:
java-algs4 Permutation 3 < distinct.txt
General:
- For Comparable's compareTo() and Comparator's compare(), if not all operators are integers, don't use subtraction and then cast to int (which will cause small real number (positive or negative) to be zero).
- Don't mutate argument array's content.
FastCollinearPoints:
- Build a copy of array (since sort is inplace) for correct interation.
- Make use of stability of sort to avoid invalid segments.
- If your number of collinear segments is less, consider all points are collinear.
Board:
- Recommend use 2D array instead 1D array to implemnet.
- twin() method must return same Board every time.
- Skip blank (since it is not tile). Check that you skip blank in isGoal(), hamming() and manhattan() methods.
Solver:
- Key problem is to implemnet a class about search node in game tree.
- You can use stack to store solution for correct move order.
Note: If you want to understand A* algorithm, UC Berkeley's cs188 is a good reference.
I only get 97 scores at this project since (my nearest() method).
- insert() and contains() are similar as implementation in BST (consider level).
- range() just like iterate implementation.
- nearest() only coniders rectangle(s) which would contains closer point (consider using RectHW's distanceSquaredTo() (or distanceTo()) method).