普林斯顿算法课 纲要及笔记 Princeton University Algorithms

5/25/2023 新建了这篇笔记(呜呜呜不知道什么时候才能开始写下一个字)

6/13/2023 完成了Union-Find(并查集)的视频观看。

6/22/2023 完成了第一次大作业Percolation。6/20就提交了92分的不考虑backwash的版本但是直到今天才完成100分的版本…backwash并不好想但是也不难想,还是自己太菜了。

6/23/2023 今天看完了Analysis of Algorithm的前两个视频……(Analysis of Algorithms Introduction和Mathematical Models)。由于微积分没学好并看不太懂后面的算法分析部分。后面似乎用到了Euler-Maclaurin公式?哭死。估计这部分要等重学微积分后重新回顾了……不过该说不说,内容还是很精彩的……

6/26/2023 今天完成了Analysis of Algorithm剩下的部分。这部分简单提到了一些Theory of Algorithm的内容,介绍了upper bound和lower bound等一系列内容。最后的memory usage的笔记如下:

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.

注意Object的内部有数组的时候需要加一个reference的memory 8 bytes。另外要注意Object的大小需要是8的整数倍。

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

6/27/2023 今天看完了Module 2的Stacks这个视频。里面基本上讲了用LinkedList和Array两种方法实现Stack。

6/29/2023 完成了Module 2 Stacks and Queues的视频部分。

7/23/2023 (这几天好累啊呜呜)完成了Week2 Stacks and Queues的Assignment。这个Assignment是关于手动实现一个Deque和一个RandomizedQueue的。这个作业虽然没那么难,但是也有不少需要注意的点。首先,对于操作的时间和内存都有一定的要求。对于Deque,要注意List上只有一个Node时执行删除的处理。另外,要注意删除节点后同时移除该节点的引用,从而避免loitering。

对于RandomizedQueue,需要注意resize arraym,同样也要让不再使用的array指向null。这个作业还涉及到了Iterator的写法。

最后,作业有个bonus涉及到了Reservoir Sampling。我把这个算法的描述和代码贴在下面(来自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  截至今天的进度是看完了Elementray Sort中的Insertion Sort. Selection Sort是找最小的插(交换)到序列的位置中,而Insertion Sort是与当前元素的前一个元素进行比较并且交换。

9/4/2023 完成了Week2的所有内容。觉得非常有意思的内容是Convex Hull(闭包)。利用极坐标系的角度排序,以及特定的检测两条线段是顺逆时针关系的ccw算法,可以很轻松地计算出数个点的Convex Hull。同时排序算法的效率是Convex Hull算法的效率的主要因素。真喜欢这门课举的实际生活的例子啊。可以让学生很容易明白这些算法究竟有什么用,能够解决什么实际生活中的问题。

猜你喜欢

转载自blog.csdn.net/zjy997/article/details/130878932