Problem Description
A tree is defined as an unweighted undirected graph.
$$T = (V, E)
$$
The diameter of the tree is defined as the longest distance between all pairs of nodes.
$$
\text{diameter}(T)=max_{(u, v)\in E} \text{ }d(u, v)
$$
kch042 changed 2 months agoView mode Like Bookmark
Intuition
Suppose we are given a sorted array, say
$$A = [1, 1, 2, 4, 6, 9]$$
then we can see that all numbers in $A[0:3]$ are smaller than $4$, while all in $A[3:6]$ are equal or greater than $4$.
Thus, we can define a predicate,
$$f(x) := (x >= 4)$$
Let $B$ be the array of $f(A)$, then
$$B = [0, 0, 0, 1, 1, 1]$$
xv6
In xv6, we have per-process pagetables, and the kernel owns a pagetable kernel_pagetable.
Whenever switching to kernel mode, we will replace pagetable register w/ kernel_pagetable.
Lab
In 2020, MIT 6.S081 release a lab that modifies the per-process pagetable such that each process contains two pagetable.
pagetable: original pagetable
kpagetable: kernel pagetable with user mappings
Range Sum Query
Given an array A of size n, and m queries, where each query asks the sum of a range in A.
For example, A = [1, 2, 4, 2, 3, 4], and query = [[1, 2], [1, 4]]. The answer to the query[0] is 2+4=6, and 2+4+2+3=11 for query[1].
The naive way to do this is to sum over the range for each query, and this give O(mn) time complexity.
Prefix Sum
A better way to do this is by precomputing the prefix sum. We will make another array P, where P[i]=A[0]+A[1]+...+A[i]. Moreover, P[i] = P[i-1]+A[i], for i=1 to n-1. So we can compute P in O(n) time.
Prerequisites
python basic concept
magic method
zip
iterable, iterator, generator
Introduction
When training DL models, it is often needed to preprocess data and find a way to feed data into model. The deep learning library torch provides two utility class torch.utils.Dataset and torch.utils.DataLoader to facilitate preprocessing.
Background
When training deep learning model, we often use Nvidia gpu on the server to accelerate the training, and use docker container to avoid messing up the server.
However, there are so many problems we may come up when building the environment. One of them is cuda version compatibility.
Hierarchy
There are 3 layers of abstraction when using Nvidia gpu (cuda) to train the model.
nvidia runtime (libcudart.so, cuda-toolkit, nvcc)
nvidia drivernvidia user-mode driver (libcuda.so)
Problem
Link
Thinking
Group words by first letter.
Obviously, two words in the same group (i.e. same first letter) cannot form a valid pair.
For convenience, let's store each word by their suffix.
Problem
Suppose we want to know the ratio of supporters of some political party to the whole people, or how many supporters. It is often expensive to ask everyone and sum up the data.
Instead, we randomly ask a few people, and take the ratio in the selected people as our answer. Intuitively, the more people we ask, the more accurate our result is close to the real answer.
Problem Formulation
Let $U$ be the universe and $G$ be the target group.
Assume we are given
$|U|$
Desciption
You are given an array people where people[i] is the weight of the i-th person, and an infinite number of boats where each boat can carry a maximum weight of limit. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit.
Return the minimum number of boats to carry every given person.
For example:
people = [4, 5, 3, 1]
limit = 8
ans = 2 ((5, 3) and (4, 1))
In modern computers, we have multi-core (multi-processor) to do parallel programming, however, synchronization between the threads is very hard to achieve. To deal with these sync issues, some solutions are provided.
Basics
Hardware Support
1. Atomic instruction
Instruction that cannot be stopped while exectuting.
test and set
compare and swap
fecch and add
Overview
Link is creating another reference to a given file.
There are 2 types of link within the file system
hard link
soft link (symbolic link, or symlink)
Hard link
Usage
Idea
MCST
GCN
4 score design
GCN
A 1-step reaction prediction model.
Used in expansion and rollout phase.
In expansion phase, the branching factor is determined by the top-n accuracy
Problem
Given a linked list, detect if some cycle exists.
A naive solution is to use hash table to record while traversing the linked list, which requires $O(n)$ time and $O(n)$ space.
In the following section, we will describe an algorithm that solves this problem in $O(1)$ space.
Algorithm
The algorithm is actually a two pointer algorithm.
We maintain two pointer fast and slow while traversing the linked list.