Princeton University Algorithms course outline and notes

Created this new note on 5/25/2023 (I don’t know when I can start writing a word)

6/13/2023 Completed the video viewing of Union-Find.

6/22/2023 Completed the first major job Percolation. I submitted a 92-point version without considering backwash on 6/20, but I didn’t finish the 100-point version until today...backwash is not easy to think about, but it’s not difficult to think about. I’m still too good at it.

6/23/2023 Today I watched the first two videos of Analysis of Algorithm... (Analysis of Algorithms Introduction and Mathematical Models). Because I didn't learn calculus well, I couldn't quite understand the algorithm analysis part behind. It seems that the Euler-Maclaurin formula is used later? Cry to death. I guess I'll have to review this part again after I relearn calculus ... But whatever I have to say, the content is still very exciting...

6/26/2023 Completed the remaining part of Analysis of Algorithm today. This part briefly mentions some contents of Theory of Algorithm, and introduces a series of contents such as upper bound and lower bound. The final memory usage notes are as follows:

boolean 1 byte 1 char 2 int 4 float 4 long 8 double 8

char[] 2N + 24 (24 for some overhead)

int[] 4N + 24

double[] 8N + 24

char[][] ~2MN

int[][] ~4MN

double[][] ~8MN
 

Object overhead. 16 bytes.   Reference. 8 bytes. 

Padding: Each object uses a multiple of 8 bytes.

Note that when there is an array inside the Object, a reference memory of 8 bytes needs to be added. Also note that the size of Object needs to be an integer multiple of 8.

最后:Big Theta classify algorithms.   Big Oh develop upper bound.  Big Omega develop lower bounds.

6/27/2023 Today I watched the video of Module 2’s Stacks. It basically talks about using LinkedList and Array to implement Stack.

6/29/2023 Completed the video portion of Module 2 Stacks and Queues.

7/23/2023 (I’m so tired these days) Completed the Assignment of Week2 Stacks and Queues. This Assignment is about manually implementing a Deque and a RandomizedQueue. Although this assignment is not that difficult, there are still many points that need to be paid attention to. First of all, there are certain requirements for operation time and memory. For Deque, please note that deletion is performed when there is only one Node on the List. In addition, be careful to remove the reference to the node after deleting it to avoid loitering.

For RandomizedQueue, you need to pay attention to resize arraym, and also make the array that is no longer used point to null. This assignment also involves how to write Iterator.

Finally, the assignment has a bonus involving Reservoir Sampling. I've posted the description and code of this algorithm below (from ChatGPT):

  1. Create a reservoir array of size k and populate it with the first k items of the input.

  2. For each item from k+1 through n:

    • Generate a random number j between 0 and the index i of the current item (inclusive).
    • If j is less than k, replace the jth element in the reservoir array with the ith item from the input.

This program randomly selects k items from an array stream of size n.

public class ReservoirSampling {
    
    // A function to randomly select k items from stream[0..n-1]
    static void selectKItems(int stream[], int n, int k)
    {
        int i;   // index for elements in stream[]
      
        // reservoir[] is the output array. Initialize it with
        // first k elements from stream[]
        int reservoir[] = new int[k];
        for (i = 0; i < k; i++)
            reservoir[i] = stream[i];
      
        Random r = new Random();
      
        // Iterate from the (k+1)th element to nth element
        for (; i < n; i++)
        {
            // Pick a random index from 0 to i.
            int j = r.nextInt(i + 1);
      
            // If the randomly picked index is smaller than k,
            // then replace the element present at the index
            // with new element from stream
            if(j < k)
                reservoir[j] = stream[i];
        }
      
    }

9/1/2023 The progress as of today is to finish reading Insertion Sort in Elementray Sort. Selection Sort is to find the smallest insertion (exchange) into the position of the sequence, and Insertion Sort is to compare and exchange with the previous element of the current element. .

9/4/2023 Completed all content of Week2. The content that I find very interesting is Convex Hull (closure). Using the angle sorting of the polar coordinate system and the specific ccw algorithm that detects the clockwise relationship between two line segments, the Convex Hull of several points can be easily calculated. The efficiency of the simultaneous sorting algorithm is the main factor in the efficiency of the Convex Hull algorithm. I really like the real life examples given in this course. It allows students to easily understand what these algorithms are used for and what real-life problems they can solve.

Guess you like

Origin blog.csdn.net/zjy997/article/details/130878932