From 5b1acb4e94c6df404ef43b19e889dc0551afa94b Mon Sep 17 00:00:00 2001 From: kekeandzeyu Date: Thu, 17 Oct 2024 13:53:19 +0800 Subject: [PATCH] convert markdown to xml format --- Writerside/hi.tree | 6 +- .../topics/Data-Structures-and-Algorithms.md | 556 ------------ .../Data-Structures-and-Algorithms.topic | 688 +++++++++++++++ Writerside/topics/Database-System.md | 753 ---------------- Writerside/topics/Database-System.topic | 816 ++++++++++++++++++ Writerside/topics/Operating-System.md | 71 -- Writerside/topics/Operating-System.topic | 91 ++ Writerside/topics/Python-Programming.topic | 2 +- 8 files changed, 1599 insertions(+), 1384 deletions(-) delete mode 100644 Writerside/topics/Data-Structures-and-Algorithms.md create mode 100644 Writerside/topics/Data-Structures-and-Algorithms.topic delete mode 100644 Writerside/topics/Database-System.md create mode 100644 Writerside/topics/Database-System.topic delete mode 100644 Writerside/topics/Operating-System.md create mode 100644 Writerside/topics/Operating-System.topic diff --git a/Writerside/hi.tree b/Writerside/hi.tree index 97a37ef..c454cb0 100644 --- a/Writerside/hi.tree +++ b/Writerside/hi.tree @@ -9,14 +9,14 @@ - + - - + + diff --git a/Writerside/topics/Data-Structures-and-Algorithms.md b/Writerside/topics/Data-Structures-and-Algorithms.md deleted file mode 100644 index 48f3f3e..0000000 --- a/Writerside/topics/Data-Structures-and-Algorithms.md +++ /dev/null @@ -1,556 +0,0 @@ - - -# Data Structures and Algorithms - - - -

In the following sections, we will explore more about Data -Structures and Algorithms.

- -

In the meantime, these topics will also serve as an introductory -part to Java Programming.

- -## 1 Data Structures and Algorithms Overview - -### 1.1 Data Storage & Logical Structures - -#### 1.1.1 Data Storage Structures - - -
  • -

    Sequential Storage Structures:

    - -
  • -

    Linear list

    -
  • -
  • -

    Array

    -
  • -
  • -

    Vector

    -
  • -
    - -
  • -

    Linked Storage Structure

    - -
  • -

    Linked list

    -
  • - - -
  • -

    Index Storage Structure

    - -
  • -

    B-Tree/B+-Tree

    -
  • - - -
  • -

    Hashing Storage Structure

    - -
  • -

    Hash table

    -
  • - - - - -#### 1.1.2 Data Logical Structures - - -
  • -

    Set

    -
  • -
  • -

    Linear

    -
  • -
  • -

    Tree

    -
  • -
  • -

    Graph

    -
  • -
    - -### 1.2 Mathematical Models - -

    Simplifications:

    - - -
  • -

    Cost Model: Use some -basic operations as a proxy for running time.

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    OperationFrequency
    Variable declarationN + 2
    Assignment statementN + 2
    Less than compare\frac {(N + 1)(N + 2)} {2}
    Equal to compare\frac {N(N - 1)} {2}
    Array accessN(N - 1)
    Increment\frac {N(N - 1)} {2} to N(N - 1)
    - -
  • - -
  • - -
  • -

    Estimate running time (or memory) as a function of input - size N

    -
  • -
  • -

    Ignore lower order terms.

    - -
  • -

    When N is large, terms are negligible.

    -
  • -
  • -

    When N is small, we don't care.

    -
  • -
    - - - - - -### 1.3 Order-of-Growth Classifications {id = "Growth"} - -#### 1.3.1 Common Classifications - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Order of growthNameTypical code FrameworkDecriptionExampleT(2N)/T(N)
    1Constanta = b + c;statementadd two numbers1
    \log NLogarithmic - while (N > 1) - {N = N / 2; ...} - divide in halfbinary search- 1
    NLinear - for (int i = 0; i < N; i++) - {...} - loopfind the maximum2
    N \log NLinearithmaticsee mergesort lecturedivide and conquermergesort- 2
    N ^ {2}Quadratic - for (int i = 0; i < N; i++) - for (int j = 0; j < N; j++) - {...} - double loopcheck all pairs4
    N ^ {3}Cubic - for (int i = 0; i < N; i++) - for (int j = 0; j < N; j++) - for (int k = 0; k < N; k++) - {...} - triple loopcheck all triples8
    2 ^ {N}Exponentialsee combinatorial search lectureexhaustive searchcheck all subsetsT(N)
    - -#### 1.3.2 Binary Search - -

    Property: Binary search -uses at most \leq 1 + \log N to search in a sorted array -of size N.

    - -

    Proof:

    - -

    T (N) = number of compares to binary search in a sorted -subarray of size \leq N.

    - - -T(N) \leq T\left(\frac{N}{2}\right) + 1, N > 1, T(1) = 1 - - - -\begin{align*} -T(N) &\leq T\left(\frac{N}{2}\right) + 1 \\ -&\leq T\left(\frac{N}{4}\right) + 1 + 1 \\ -&\leq T\left(\frac{N}{8}\right) + 1 + 1 + 1 \\ -&... \\ -&\leq T\left(\frac{N}{N}\right) + 1 + 1 + ... + 1 \\ -&= 1 + \log N -\end{align*} - - - - -

    Compare key against middle entry.

    -
    - -

    Too small, go left.

    -
    - -

    Too big, go right.

    -
    - -

    Equal, found.

    -
    -
    - - - - -public static int binarySearch(int[] a, int key) { - int lo = 0; - int hi = a.length - 1; - while (lo <= hi) { - int mid = lo + (hi - lo) / 2; - if (key < a[mid]) { - hi = mid - 1; - } else if (key > a[mid]) { - lo = mid + 1; - } else { - return mid; - } - } - return -1; -} - - - - -#include <vector> -\/ -int binarySearch(std::vector<int> arr, int x) { - int l = 0, r = arr.size() - 1; - while (l <= r) { - int m = l + (r - l) / 2; - if (arr[m] == x) { - return m; - } - if (arr[m] < x) { - l = m + 1; - } else { - r = m - 1; - } - } - return -1; -} - - - - -def binary_search(arr, x): - l, r = 0, len(arr) - 1 - while l <= r: - m = l + (r - l) // 2 - if arr[m] == x: - return m - if arr[m] < x: - l = m + 1 - else: - r = m - 1 - return -1 - - - - -#### 1.3.3 3-Sum - -

    Leetcode 15: 3Sum

    - -

    Description:

    - -

    Given an integer array nums, return all the triplets -[nums[i], nums[j], nums[k]] such that -i != j, i != k, and j != k, -and nums[i] + nums[j] + nums[k] == 0.

    - - - -

    Sort the N (distinct) numbers.

    -
    - -

    For each pair of numbers a[i] and a[j], - binary search for - -(a[i] + a[j]).

    -
    -
    - -

    Analysis: Order of growth is -N^{2} \log N

    - - -
  • -

    Step 1: N^{2} with insertion sort.

    -
  • -
  • -

    Step 2: N^{2} \log N with binary search.

    -
  • -
    - -

    Better Algorithm: Sorting & -Two pointers => O(N^{2})

    - - - - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -\/ -public class Solution { - public Solution() { - } -\/ - public List<List<Integer>> threeSum(int[] nums) { - int n = nums.length; - Arrays.sort(nums); - List<List<Integer>> answer = new ArrayList<>(); -\/ - for (int first = 0; first < n; ++first) { - if (first > 0 && nums[first] == nums[first - 1]) { - continue; - } - int third = n - 1; - int target = -nums[first]; - for (int second = first + 1; second < n; ++second) { - if (second > first + 1 && nums[second] == nums[second - 1]) { - continue; - } - while (second < third && nums[second] + nums[third] > target) { - --third; - } -\/ - if (second == third) { - break; - } - if (nums[second] + nums[third] == target) { - List<Integer> list = new ArrayList<>(); - list.add(nums[first]); - list.add(nums[second]); - list.add(nums[third]); - answer.add(list); - } - } - } - return answer; - } -} - - - - -#include <vector> -#include <algorithm> -\/ -std::vector<std::vector<int>> threeSum(std::vector<int>& nums) { -const int n = static_cast<int>(nums.size()); -std::sort(nums.begin(), nums.end()); -\/ - std::vector<std::vector<int>> answer; -\/ - for (int first = 0; first < n; first++) { - if (first > 0 && nums[first] == nums[first - 1]) { - continue; - } -\/ - int third = n - 1; - const int target = -nums[first]; -\/ - for (int second = first + 1; second < n; second++) { - if (second > first + 1 && nums[second] == nums[second - 1]) { - continue; - } - while (second < third && nums[second] + nums[third] > target) { - --third; - } -\/ - if (second == third) { - break; - } - if (nums[second] + nums[third] == target) { - answer.emplace_back(std::vector<int>{nums[first], nums[second], nums[third]}); - } - } - } - return answer; -} - - - - -from typing import List -\/ -\/ -def threeSum(nums: List[int]) -> List[List[int]]: - n = len(nums) - nums.sort() - ans = list() -\/ - for first in range(n): - if first > 0 and nums[first] == nums[first - 1]: - continue - third = n - 1 - target = -nums[first] - for second in range(first + 1, n): - if second > first + 1 and nums[second] == nums[second - 1]: - continue - while second < third and nums[second] + nums[third] > target: - third -= 1 - if second == third: - break - if nums[second] + nums[third] == target: - ans.append([nums[first], nums[second], nums[third]]) -\/ - return ans - - - - -### 1.4 Theory of Algorithms - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NotionFormal DefinitionProvidesShorthand forExampleUsed to
    Big Theta - R(N) \in \Theta (f(N)) - -

    means there are positive constants k_1 and - k_2 such that

    - - k_1 f(N) \leq R(N) \leq k_2 f(N) - -
    asymptotic order of growth\Theta (N ^ {2}) - \begin{align*} - &\frac {N ^ {2}}{2} \\ - &10 N^{2} \\ - &5 N^{2} + 22N \log N + 3N \\ - &\text {...} \\ - \end{align*} - Classify Algorithms
    Big Oh - R(N) \in O(f(N)) - -

    means there is a positive constant k_2 such that

    - - R(N) \leq k_2 f(N) -
    \Theta (N ^ {2}) and smallerO(N ^ {2}) - \begin{align*} - &10 N ^ {2} \\ - &100 N \\ - &22 N \log N + 3 N \\ - &\text {...} \\ - \end{align*} - Develop Upper Bounds
    Big Omega - R(N) \in \Omega (f(N)) - -

    means there is a positive constant k_1 such that

    - - k_1 f(N) \leq R(N) -
    \Theta (N ^ {2}) and larger\Omega (N ^ {2}) - \begin{align*} - &\frac {N ^ {2}}{2} \\ - &N ^ {5} \\ - &N ^ {3} + 22 N \log N + 3 N \\ - &\text {...} \\ - \end{align*} - Develop Lower Bounds
    \ No newline at end of file diff --git a/Writerside/topics/Data-Structures-and-Algorithms.topic b/Writerside/topics/Data-Structures-and-Algorithms.topic new file mode 100644 index 0000000..0614146 --- /dev/null +++ b/Writerside/topics/Data-Structures-and-Algorithms.topic @@ -0,0 +1,688 @@ + + + + + + + Data Structures and Algorithms + + +

    In the following sections, we will explore more about Data + Structures and Algorithms.

    +

    In the meantime, these topics will also serve as an introductory + part to Java Programming.

    + + + + +
  • +

    + Sequential Storage Structures: +

    + +
  • +

    Linear list

    +
  • +
  • +

    Array

    +
  • +
  • +

    Vector

    +
  • +
    + +
  • +

    + Linked Storage Structure +

    + +
  • +

    Linked list

    +
  • + + +
  • +

    + Index Storage Structure +

    + +
  • +

    B-Tree/B+-Tree

    +
  • + + +
  • +

    + Hashing Storage Structure +

    + +
  • +

    Hash table

    +
  • + + + +
    + + +
  • +

    + Set +

    +
  • +
  • +

    + Linear +

    +
  • +
  • +

    + Tree +

    +
  • +
  • +

    + Graph +

    +
  • +
    +
    +
    + +

    + Simplifications: +

    + +
  • +

    + Cost Model: + Use some + basic operations as a proxy for running time. +

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OperationFrequency
    Variable declaration + N + 2 +
    Assignment statement + N + 2 +
    Less than compare + \frac {(N + 1)(N + 2)} {2} +
    Equal to compare + \frac {N(N - 1)} {2} +
    Array access + N(N - 1) +
    Increment + \frac {N(N - 1)} {2} + to + N(N - 1) +
    +
  • +
  • + +
  • +

    Estimate running time (or memory) as a function of input + size + N +

    +
  • +
  • +

    Ignore lower order terms.

    + +
  • +

    When + N + is large, terms are negligible. +

    +
  • +
  • +

    When + N + is small, we don't care. +

    +
  • +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Order of growthNameTypical code FrameworkDecriptionExampleT(2N)/T(N)
    + 1 + Constant + a = b + c; + statementadd two numbers + 1 +
    + \log N + Logarithmic + + while (N > 1) + {N = N / 2; ...} + + divide in halfbinary search + - 1 +
    + N + Linear + + for (int i = 0; i < N; i++) + {...} + + loopfind the maximum2
    + N \log N + Linearithmaticsee mergesort lecturedivide and conquermergesort + - 2 +
    + N ^ {2} + Quadratic + + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + {...} + + double loopcheck all pairs + 4 +
    + N ^ {3} + Cubic + + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + for (int k = 0; k < N; k++) + {...} + + triple loopcheck all triples8
    + 2 ^ {N} + Exponentialsee combinatorial search lectureexhaustive searchcheck all subsetsT(N)
    +
    + +

    + Property: + Binary search + uses at most + \leq 1 + \log N + to search in a sorted array + of size + N + . +

    +

    + Proof: +

    +

    + T (N) = + number of compares to binary search in a sorted + subarray of size + \leq N + . +

    + + T(N) \leq T\left(\frac{N}{2}\right) + 1, N > 1, T(1) = 1 + + + \begin{align*} + T(N) &\leq T\left(\frac{N}{2}\right) + 1 \\ + &\leq T\left(\frac{N}{4}\right) + 1 + 1 \\ + &\leq T\left(\frac{N}{8}\right) + 1 + 1 + 1 \\ + &... \\ + &\leq T\left(\frac{N}{N}\right) + 1 + 1 + ... + 1 \\ + &= 1 + \log N + \end{align*} + + + +

    Compare key against middle entry.

    +
    + +

    Too small, go left.

    +
    + +

    Too big, go right.

    +
    + +

    Equal, found.

    +
    +
    + + + + public static int binarySearch(int[] a, int key) { + int lo = 0; + int hi = a.length - 1; + while (lo <= hi) { + int mid = lo + (hi - lo) / 2; + if (key < a[mid]) { + hi = mid - 1; + } else if (key > a[mid]) { + lo = mid + 1; + } else { + return mid; + } + } + return -1; + } + + + + + #include <vector> + + int binarySearch(std::vector<int> arr, int x) { + int l = 0, r = arr.size() - 1; + while (l <= r) { + int m = l + (r - l) / 2; + if (arr[m] == x) { + return m; + } + if (arr[m] < x) { + l = m + 1; + } else { + r = m - 1; + } + } + return -1; + } + + + + + def binary_search(arr, x): + l, r = 0, len(arr) - 1 + while l <= r: + m = l + (r - l) // 2 + if arr[m] == x: + return m + if arr[m] < x: + l = m + 1 + else: + r = m - 1 + return -1 + + + +
    + +

    Leetcode 15: 3Sum

    +

    + Description: +

    +

    Given an integer array nums, return all the triplets + [nums[i], nums[j], nums[k]] such that + i != j, i != k, and j != k, + and nums[i] + nums[j] + nums[k] == 0.

    + + +

    Sort the + N + (distinct) numbers. +

    +
    + +

    For each pair of numbers a[i] and a[j], + binary search + for + -(a[i] + a[j]). +

    +
    +
    +

    + Analysis: + Order of growth is + N^{2} \log N +

    + +
  • +

    Step 1: + N^{2} + with insertion sort. +

    +
  • +
  • +

    Step 2: + N^{2} \log N + with binary search. +

    +
  • +
    +

    + Better Algorithm: + Sorting & + Two pointers => + O(N^{2}) +

    + + + + import java.util.ArrayList; + import java.util.Arrays; + import java.util.List; + + public class Solution { + public Solution() { + } + + public List<List<Integer>> threeSum(int[] nums) { + int n = nums.length; + Arrays.sort(nums); + List<List<Integer>> answer = new ArrayList<>(); + + for (int first = 0; first < n; ++first) { + if (first > 0 && nums[first] == nums[first - 1]) { + continue; + } + int third = n - 1; + int target = -nums[first]; + for (int second = first + 1; second < n; ++second) { + if (second > first + 1 && nums[second] == nums[second - 1]) { + continue; + } + while (second < third && nums[second] + nums[third] > target) { + --third; + } + + if (second == third) { + break; + } + if (nums[second] + nums[third] == target) { + List<Integer> list = new ArrayList<>(); + list.add(nums[first]); + list.add(nums[second]); + list.add(nums[third]); + answer.add(list); + } + } + } + return answer; + } + } + + + + + #include <vector> + #include <algorithm> + + std::vector<std::vector<int>> threeSum(std::vector<int>& nums) { + const int n = static_cast<int>(nums.size()); + std::sort(nums.begin(), nums.end()); + + std::vector<std::vector<int>> answer; + + for (int first = 0; first < n; first++) { + if (first > 0 && nums[first] == nums[first - 1]) { + continue; + } + + int third = n - 1; + const int target = -nums[first]; + + for (int second = first + 1; second < n; second++) { + if (second > first + 1 && nums[second] == nums[second - 1]) { + continue; + } + while (second < third && nums[second] + nums[third] > target) { + --third; + } + + if (second == third) { + break; + } + if (nums[second] + nums[third] == target) { + answer.emplace_back(std::vector<int>{nums[first], nums[second], nums[third]}); + } + } + } + return answer; + } + + + + + from typing import List + + + def threeSum(nums: List[int]) -> List[List[int]]: + n = len(nums) + nums.sort() + ans = list() + + for first in range(n): + if first > 0 and nums[first] == nums[first - 1]: + continue + third = n - 1 + target = -nums[first] + for second in range(first + 1, n): + if second > first + 1 and nums[second] == nums[second - 1]: + continue + while second < third and nums[second] + nums[third] > target: + third -= 1 + if second == third: + break + if nums[second] + nums[third] == target: + ans.append([nums[first], nums[second], nums[third]]) + + return ans + + + +
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NotionFormal DefinitionProvidesShorthand forExampleUsed to
    + Big Theta + + + R(N) \in \Theta (f(N)) + +

    means there are positive constants + k_1 + and + + k_2 + + such that +

    + + k_1 f(N) \leq R(N) \leq k_2 f(N) + +
    asymptotic order of growth + \Theta (N ^ {2}) + + + \begin{align*} + &\frac {N ^ {2}}{2} \\ + &10 N^{2} \\ + &5 N^{2} + 22N \log N + 3N \\ + &\text {...} \\ + \end{align*} + + Classify Algorithms
    + Big Oh + + + R(N) \in O(f(N)) + +

    means there is a positive constant + k_2 + such that +

    + + R(N) \leq k_2 f(N) + +
    + \Theta (N ^ {2}) + and smaller + + O(N ^ {2}) + + + \begin{align*} + &10 N ^ {2} \\ + &100 N \\ + &22 N \log N + 3 N \\ + &\text {...} \\ + \end{align*} + + Develop Upper Bounds
    + Big Omega + + + R(N) \in \Omega (f(N)) + +

    means there is a positive constant + k_1 + such that +

    + + k_1 f(N) \leq R(N) + +
    + \Theta (N ^ {2}) + and larger + + \Omega (N ^ {2}) + + + \begin{align*} + &\frac {N ^ {2}}{2} \\ + &N ^ {5} \\ + &N ^ {3} + 22 N \log N + 3 N \\ + &\text {...} \\ + \end{align*} + + Develop Lower Bounds
    +
    +
    + +
    \ No newline at end of file diff --git a/Writerside/topics/Database-System.md b/Writerside/topics/Database-System.md deleted file mode 100644 index 6148ae0..0000000 --- a/Writerside/topics/Database-System.md +++ /dev/null @@ -1,753 +0,0 @@ - - -# Database System - -## 1 SQL Part Ⅰ - -### 1.1 SQL Introduction - -

    SQL = Structured Query Language

    - -

    Although over 40 years old, it keeps re-emerging as the standard.

    - -

    Features:

    - - -
  • -

    Declarative!

    - -
  • Specify what you want, not - how to get it.
  • -
    - -
  • -

    Implemented widely

    - -
  • With varying levels of efficiency, completeness.
  • - - -
  • -

    Constrained

    - -
  • -

    Not targeted at Turing-complete tasks.

    -
  • - - -
  • -

    General-purpose and feature-rich -

    - -
  • -

    Many years of added features.

    -
  • -
  • -

    Extensible: Callouts to other languages, databases.

    -
  • - - - - -### 1.2 Relational Terminology - -

    Definitions:

    - - -
  • Database: Set of named -relations.
  • -
  • -

    Relation (aka Table):

    - -
  • Schema: description ( - "metadata").
  • -
  • Instance: set of data - satisfying the schema.
  • -
    - -
  • Attribute (aka Column, Field) -
  • -
  • Tuple (aka Record, Row)
  • -
  • Cardinality: Number of rows in a -table.
  • - - -Database - -

    Properties:

    - - -
  • -

    Schema is fixed, unique attribute names, atomic (aka primitive) - types.

    -
  • -
  • -

    Tables are NOT ordered, they are sets or multisets (bags).

    -
  • -
  • -

    Tables are FLAT, no nested attributes.

    -
  • -
  • -

    Tables DO NOT prescribe how they are implemented/stored on - disk, which is called physical data - independence.

    -
  • -
    - - -

    Some important notes:

    - -
  • -

    Instance must follow the schema to be relations.

    -
  • -
  • -

    Each column has to have distinct names.

    -
  • -
  • -

    Every column must use atomic type!

    -
  • -
    -
    - -### 1.3 SQL Language - -

    SQL Language:

    - - -
  • -

    Two Sublanguages:

    - -
  • -

    DDL (Data Definition Language): - Define and modify schema.

    -
  • -
  • -

    DML (Data Manipulation Language): - Queries can be written intuitively.

    -
  • -
    - -
  • -

    Relational Database Management System - (RDBMS): responsible for efficient evaluation, choose - and run algorithms for declarative queries (choice of algorithm - must not affect query answer).

    -
  • - - -

    SQL DDL Examples:

    - -

    Sailors

    - - - - - - - - - - - - - - - - - - - - - - - - - - -
    sidsnameratingage
    1Fred722
    2Jim239
    3Nancy827
    - - -CREATE TABLE Sailors ( - sid INTEGER, - sname CHAR(100), - rating INTEGER, - age FLOAT - PRIMARY KEY (sid)); - - -

    Boats

    - - - - - - - - - - - - - - - - - - - - - - -
    bidbnamecolor
    101Ninared
    102Pintablue
    103Santa Mariared
    - - -CREATE TABLE Boats ( - bid INTEGER, - bname CHAR (20), - color CHAR(10), - PRIMARY KEY (bid)); - - -

    Reserves

    - - - - - - - - - - - - - - - - - -
    sidbidday
    11029/12
    21029/13
    - - -CREATE TABLE Reserves ( - sid INTEGER, - bid INTEGER, - day DATE, - PRIMARY KEY (sid, bid, day), - FOREIGN KEY (sid) REFERENCES Sailors, - FOREIGN KEY (bid) REFERENCES Boats); - - - - -
  • -

    Primary Key column(s)

    - -
  • -

    Provides a unique “lookup key” for the relation.

    -
  • -
  • -

    Cannot have any duplicate values.

    -
  • -
  • -

    Can be made up of >1 column, e.g. (firstname, lastname). -

    -
  • -
    - -
  • -

    Foreign Key column(s)

    - -
  • -

    References a table via the primary key of that table, i.e. - references the primary key column of that table.

    -
  • -
  • -

    Need not share the name of the referenced primary key.

    -
  • - - - -
    - -### 1.4 SQL Queries - -

    Basic Single Table Queries:

    - - -SELECT [DISTINCT] <column expression list> -FROM <single table> -[WHERE <predicate>] - - - -
  • -

    Produce all tuples in the table that satisfy the predicate.

    -
  • -
  • -

    Output the expressions in the SELECT list,

    -
  • -
  • -

    Expression can be a column reference, or an arithmetic - expression over column refs.

    -
  • -
    - -

    Example:

    - - -SELECT S.name, S.gpa, S.age*2 AS a2 -FROM Students [AS] S -WHERE S.dept = 'CS' -ORDER BY S.gpa DESC, S.name ASC, a2; -LIMIT 3; - - - -
  • -

    SELECT, FROM, WHERE lines:

    - -
  • -

    Return all unique (name, GPA) pairs from students.

    -
  • -
  • -

    DISTINCT specifies removal of duplicate rows before output. -

    -
  • -
  • -

    Can refer to the students table as S, this is called an - alias.

    -
  • -
    - -
  • -

    ORDER line:

    - -
  • -

    ORDER BY clause specifies output to be sorted.

    - -
  • -

    Sorts the output by GPA in descending order, then by - name in ascending order.

    -
  • -
  • -

    Also computes the value of age*2 and names it a2.

    -
  • - - -
  • -

    Obviously must refer to columns in the output.

    -

    Note the AS clause for naming output columns!

    -
  • - - -
  • -

    LIMIT line:

    - -
  • -

    Only produces the first 3 output rows.

    -
  • -
  • -

    Typically used with ORDER BY.

    - -
  • -

    Otherwise the output is non- - deterministic.

    -
  • -
  • -

    Not a "pure" declarative construct in that case – output - set depends on algorithm for query processing.

    -
  • - - - - - - -

    Aggregates:

    - - -SELECT [DISTINCT] AVG(S.gpa) -FROM Students S -WHERE S.dept = 'CS'; - - - -
  • -

    Before producing output, compute a summary (aka an aggregate) - of some arithmetic expression.

    -
  • -
  • -

    Produces 1 row of output, with one column of average in this - case.

    -
  • -
  • -

    Other aggregates: SUM, COUNT, MAX, MIN (and others).

    -
  • -
    - -

    Group By:

    - - -SELECT [DISTINCT] AVG(S.gpa), S.dept -FROM Students S -GROUP BY S.dept - - - -
  • -

    Partition table into groups with same GROUP BY column values, - can group by a list of columns.

    -
  • -
  • -

    Produce an aggregate result per group, cardinality (rows) of - output = number of distinct group values.

    -
  • -
  • -

    Note: can put grouping columns in SELECT list (in this case, - average GPA + department).

    -
  • -
    - -

    Having:

    - - -SELECT [DISTINCT] AVG(S.gpa), S.dept -FROM Students S -GROUP BY S.dept -HAVING COUNT(*) > 2 - - - -
  • -

    The HAVING predicate filters groups

    -
  • -
  • -

    HAVING is applied after grouping and aggregation

    - -
  • -

    Hence can contain anything that could go in the SELECT list -

    -
  • -
  • -

    i.e., aggs or GROUP BY columns

    -
  • -
    - -
  • -

    HAVING can only be used in aggregate queries

    -
  • -
  • -

    It's an optional clause

    -
  • - - -SQL Queries - -## 2 SQL Part Ⅱ - -### 2.1 Join Queries - -

    Join: Form cross product of the -tables, output all tuples and select specific columns.

    - -

    Example:

    - -

    Sailors

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    sidsnameratingage
    1Popeye1022
    2OliveOyl1139
    3Garfield127
    4Bob519
    - -

    Reserves

    - - - - - - - - - - - - - - - - - - - - - - -
    sidbidday
    11029/12
    21029/13
    110110/01
    - - - -SELECT S.sid, S.sname, R.bid -FROM Sailors, Reserves -WHERE Sailors.sid=Reserves.sid; - - -SELECT S.sid, S.sname, R.bid -FROM Sailors AS S, Reserves AS R -WHERE S.sid=R.sid; - - - -

    Output

    - -Join Queries - - - - - - - - - - - - - - - - - -
    sidsnamebid
    1Popeye102
    2OliveOyl102
    - -

    Self-Joins

    - - -SELECT x.sname AS sname1, - x.age AS age1, - y.sname AS sname2, - y.age AS age2 -FROM Sailors AS x, Sailors AS y -WHERE x.age > y.age; - - -

    Output

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    sname1age1sname2age2
    Popeye22Bob19
    OliveOyl39Popeye22
    OliveOyl39Garfield27
    OliveOyl39Bob19
    Garfield27Popeye22
    Garfield27Bob19
    - -### 2.2 Select & Where Advanced - -

    Arithmetic Expressions

    - - -SELECT S.age, S.age-5 AS age1, 2*S.age AS age2 -FROM Sailors AS S -WHERE S.sname = 'Popeye'; - - - -

    Comparison is "=" not "==" !!!

    -

    But SQLite allows this!

    -
    - -

    SQL Calculator

    - - -SELECT - log(1000) as three, - exp(ln(2)) as two, - cos(0) as one, - ln(2*3) = ln(2) + ln(3) as sanity; - - - - - - - - - - - - - - - -
    threetwoonesanity
    3.02.01.01
    - -

    String Comparison

    - - - -SELECT S.sname -FROM Sailors AS S -WHERE S.sname LIKE 'B_%'; - - -SELECT S.sname -FROM Sailors AS S -WHERE S.sname ~ 'B.*'; - - - -

    Boolean Logic vs. Set Operators

    - -

    Reserve a red or a -green boat

    - - - -SELECT R.sid -FROM Boats B, Reserves R -WHERE R.bid = B.bid AND (B.color = 'red' OR B.color = 'green'); - - -SELECT R.sid -FROM Boats B, Reserves R -WHERE R.bid = B.bid AND B.color = 'red'; -\/ -UNION ALL -\/ -SELECT R.sid -FROM Boats B, Reserves R -WHERE R.bid = B.bid AND B.color = 'green'; - - - -

    Reserve a red and a -green boat

    - - - --- A boat cannot be red and green at the same time -SELECT R.sid -FROM Boats B, Reserves R -WHERE R.bid = B.bid AND (B.color = 'red' AND B.color = 'green'); - - -SELECT R.sid -FROM Boats B, Reserves R -WHERE R.bid = B.bid AND B.color = 'red'; -\/ -INTERSECT -\/ -SELECT R.sid -FROM Boats B, Reserves R -WHERE R.bid = B.bid AND B.color = 'green'; - - - -

    Set Semantics

    - -

    Default (Think of each letter as being a tuple in a relation):

    - -

    R = {A, A, A, A, B, B, C, D}

    - -

    S = {A, A, B, B, B, C, E}

    - - -
  • -

    UNION: {A, B, C, D, E}

    -
  • -
  • -

    INTERSECT: {A, B, C}

    -
  • -
  • -

    EXCEPT: {D}

    -
  • -
    - -

    "ALL": Multiset Semantics

    - -

    R = {A, A, A, A, B, B, C, D} = {A(4), B(2), C(1), D(1)}

    - -

    S = {A, A, B, B, B, C, E} = {A(2), B(3), C(1), E(1)}

    - - -
  • -

    UNION ALL (sum of all cardinalities): - {A(6), B(5), C(2), D(1), E(1)}

    -
  • -
  • -

    INTERSECT ALL (min of cardinalities): - {A(min(4,2)), B(min(2,3)), C(min(1,1)), D(min(1,0)), E(min(0,1))} - = {A, A, B, B, C}

    -
  • -
  • -

    EXCEPT ALL (subtract cardinalities): - {A(4-2), B(2-3), C(1-1), D(1-0), E(0-1)} = {A, A, D}

    -
  • -
    - -### 2.3 Nested Queries - diff --git a/Writerside/topics/Database-System.topic b/Writerside/topics/Database-System.topic new file mode 100644 index 0000000..f7f0c6d --- /dev/null +++ b/Writerside/topics/Database-System.topic @@ -0,0 +1,816 @@ + + + + + + + Database System + + + +

    SQL = Structured Query Language

    +

    Although over 40 years old, it keeps re-emerging as the standard.

    +

    + Features: +

    + +
  • +

    + Declarative! +

    + +
  • Specify + what + you want, not + how + to get it. +
  • +
    + +
  • +

    + Implemented widely +

    + +
  • With varying levels of efficiency, completeness.
  • + + +
  • +

    + Constrained +

    + +
  • +

    Not targeted at Turing-complete tasks.

    +
  • + + +
  • +

    + General-purpose and feature-rich + +

    + +
  • +

    Many years of added features.

    +
  • +
  • +

    Extensible: Callouts to other languages, databases.

    +
  • + + + +
    + +

    + Definitions: +

    + +
  • + Database: + Set of named + relations. +
  • +
  • +

    + Relation (aka Table): +

    + +
  • + Schema: + description ( + "metadata"). +
  • +
  • + Instance: + set of data + satisfying the schema. +
  • +
    + +
  • + Attribute (aka Column, Field) +
  • +
  • + Tuple (aka Record, Row) +
  • +
  • + Cardinality: + Number of rows in a + table. +
  • + + Database +

    + Properties: +

    + +
  • +

    Schema is fixed, unique attribute names, atomic (aka primitive) + types.

    +
  • +
  • +

    Tables are NOT ordered, they are sets or multisets (bags).

    +
  • +
  • +

    Tables are FLAT, no nested attributes.

    +
  • +
  • +

    Tables DO NOT prescribe how they are implemented/stored on + disk, which is called + physical data + independence + + . +

    +
  • +
    + +

    Some important notes:

    + +
  • +

    Instance must follow the schema to be relations.

    +
  • +
  • +

    Each column has to have distinct names.

    +
  • +
  • +

    Every column must use atomic type!

    +
  • +
    +
    +
    + +

    + SQL Language: +

    + +
  • +

    + Two Sublanguages: +

    + +
  • +

    + DDL (Data Definition Language): + + Define and modify schema. +

    +
  • +
  • +

    + DML (Data Manipulation Language): + + Queries can be written intuitively. +

    +
  • +
    + +
  • +

    + Relational Database Management System + (RDBMS): + + responsible for efficient evaluation, choose + and run algorithms for declarative queries (choice of algorithm + must not affect query answer). +

    +
  • + +

    + SQL DDL Examples: +

    +

    Sailors

    + + + + + + + + + + + + + + + + + + + + + + + + + +
    sidsnameratingage
    1Fred722
    2Jim239
    3Nancy827
    + + CREATE TABLE Sailors ( + sid INTEGER, + sname CHAR(100), + rating INTEGER, + age FLOAT + PRIMARY KEY (sid)); + +

    Boats

    + + + + + + + + + + + + + + + + + + + + + +
    bidbnamecolor
    101Ninared
    102Pintablue
    103Santa Mariared
    + + CREATE TABLE Boats ( + bid INTEGER, + bname CHAR (20), + color CHAR(10), + PRIMARY KEY (bid)); + +

    Reserves

    + + + + + + + + + + + + + + + + +
    sidbidday
    11029/12
    21029/13
    + + CREATE TABLE Reserves ( + sid INTEGER, + bid INTEGER, + day DATE, + PRIMARY KEY (sid, bid, day), + FOREIGN KEY (sid) REFERENCES Sailors, + FOREIGN KEY (bid) REFERENCES Boats); + + + +
  • +

    + Primary Key column(s) +

    + +
  • +

    Provides a unique “lookup key” for the relation.

    +
  • +
  • +

    Cannot have any duplicate values.

    +
  • +
  • +

    Can be made up of >1 column, e.g. (firstname, lastname). +

    +
  • +
    + +
  • +

    + Foreign Key column(s) +

    + +
  • +

    References a table via the primary key of that table, i.e. + references the primary key column of that table.

    +
  • +
  • +

    Need not share the name of the referenced primary key.

    +
  • + + + +
    +
    + +

    + Basic Single Table Queries: +

    + + SELECT [DISTINCT] <column expression list> + FROM <single table> + [WHERE <predicate>] + + +
  • +

    Produce all tuples in the table that satisfy the predicate.

    +
  • +
  • +

    Output the expressions in the SELECT list,

    +
  • +
  • +

    Expression can be a column reference, or an arithmetic + expression over column refs.

    +
  • +
    +

    + Example: +

    + + SELECT S.name, S.gpa, S.age*2 AS a2 + FROM Students [AS] S + WHERE S.dept = 'CS' + ORDER BY S.gpa DESC, S.name ASC, a2; + LIMIT 3; + + +
  • +

    + SELECT, FROM, WHERE lines: +

    + +
  • +

    Return all unique (name, GPA) pairs from students.

    +
  • +
  • +

    DISTINCT specifies removal of duplicate rows before output. +

    +
  • +
  • +

    Can refer to the students table as S, this is called an + alias + . +

    +
  • +
    + +
  • +

    + ORDER line: +

    + +
  • +

    ORDER BY clause specifies output to be sorted.

    + +
  • +

    Sorts the output by GPA in descending order, then by + name in ascending order.

    +
  • +
  • +

    Also computes the value of age*2 and names it a2.

    +
  • + + +
  • +

    Obviously must refer to columns in the output.

    +

    Note the AS clause for naming output columns!

    +
  • + + +
  • +

    + LIMIT line: +

    + +
  • +

    Only produces the first 3 output rows.

    +
  • +
  • +

    Typically used with ORDER BY.

    + +
  • +

    Otherwise the output is + non- + deterministic + + . +

    +
  • +
  • +

    Not a "pure" declarative construct in that case – output + set depends on algorithm for query processing.

    +
  • + + + + + +

    + Aggregates: +

    + + SELECT [DISTINCT] AVG(S.gpa) + FROM Students S + WHERE S.dept = 'CS'; + + +
  • +

    Before producing output, compute a summary (aka an aggregate) + of some arithmetic expression.

    +
  • +
  • +

    Produces 1 row of output, with one column of average in this + case.

    +
  • +
  • +

    Other aggregates: SUM, COUNT, MAX, MIN (and others).

    +
  • +
    +

    + Group By: +

    + + SELECT [DISTINCT] AVG(S.gpa), S.dept + FROM Students S + GROUP BY S.dept + + +
  • +

    Partition table into groups with same GROUP BY column values, + can group by a list of columns.

    +
  • +
  • +

    Produce an aggregate result per group, cardinality (rows) of + output = number of distinct group values.

    +
  • +
  • +

    Note: can put grouping columns in SELECT list (in this case, + average GPA + department).

    +
  • +
    +

    + Having: +

    + + SELECT [DISTINCT] AVG(S.gpa), S.dept + FROM Students S + GROUP BY S.dept + HAVING COUNT(*) > 2 + + +
  • +

    The HAVING predicate filters groups

    +
  • +
  • +

    HAVING is applied after grouping and aggregation

    + +
  • +

    Hence can contain anything that could go in the SELECT list +

    +
  • +
  • +

    i.e., aggs or GROUP BY columns

    +
  • +
    + +
  • +

    HAVING can only be used in aggregate queries

    +
  • +
  • +

    It's an optional clause

    +
  • + + SQL Queries +
    +
    + + +

    + Join: + Form cross product of the + tables, output all tuples and select specific columns. +

    +

    + Example: +

    +

    Sailors

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    sidsnameratingage
    1Popeye1022
    2OliveOyl1139
    3Garfield127
    4Bob519
    +

    Reserves

    + + + + + + + + + + + + + + + + + + + + + +
    sidbidday
    11029/12
    21029/13
    110110/01
    + + + SELECT S.sid, S.sname, R.bid + FROM Sailors, Reserves + WHERE Sailors.sid=Reserves.sid; + + + SELECT S.sid, S.sname, R.bid + FROM Sailors AS S, Reserves AS R + WHERE S.sid=R.sid; + + +

    Output

    + Join Queries + + + + + + + + + + + + + + + + +
    sidsnamebid
    1Popeye102
    2OliveOyl102
    +

    + Self-Joins +

    + + SELECT x.sname AS sname1, + x.age AS age1, + y.sname AS sname2, + y.age AS age2 + FROM Sailors AS x, Sailors AS y + WHERE x.age > y.age; + +

    Output

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    sname1age1sname2age2
    Popeye22Bob19
    OliveOyl39Popeye22
    OliveOyl39Garfield27
    OliveOyl39Bob19
    Garfield27Popeye22
    Garfield27Bob19
    +
    + +

    + Arithmetic Expressions +

    + + SELECT S.age, S.age-5 AS age1, 2*S.age AS age2 + FROM Sailors AS S + WHERE S.sname = 'Popeye'; + + +

    Comparison is "=" not "==" !!!

    +

    + But SQLite allows this! +

    +
    +

    + SQL Calculator +

    + + SELECT + log(1000) as three, + exp(ln(2)) as two, + cos(0) as one, + ln(2*3) = ln(2) + ln(3) as sanity; + + + + + + + + + + + + + + +
    threetwoonesanity
    3.02.01.01
    +

    + String Comparison +

    + + + SELECT S.sname + FROM Sailors AS S + WHERE S.sname LIKE 'B_%'; + + + SELECT S.sname + FROM Sailors AS S + WHERE S.sname ~ 'B.*'; + + +

    + Boolean Logic vs. Set Operators +

    +

    + Reserve a red + or + a + green boat + +

    + + + SELECT R.sid + FROM Boats B, Reserves R + WHERE R.bid = B.bid AND (B.color = 'red' OR B.color = 'green'); + + + SELECT R.sid + FROM Boats B, Reserves R + WHERE R.bid = B.bid AND B.color = 'red'; + \/ + UNION ALL + \/ + SELECT R.sid + FROM Boats B, Reserves R + WHERE R.bid = B.bid AND B.color = 'green'; + + +

    + Reserve a red + and + a + green boat + +

    + + + -- A boat cannot be red and green at the same time + SELECT R.sid + FROM Boats B, Reserves R + WHERE R.bid = B.bid AND (B.color = 'red' AND B.color = 'green'); + + + SELECT R.sid + FROM Boats B, Reserves R + WHERE R.bid = B.bid AND B.color = 'red'; + \/ + INTERSECT + \/ + SELECT R.sid + FROM Boats B, Reserves R + WHERE R.bid = B.bid AND B.color = 'green'; + + +

    + Set Semantics +

    +

    Default (Think of each letter as being a tuple in a relation):

    +

    R = {A, A, A, A, B, B, C, D}

    +

    S = {A, A, B, B, B, C, E}

    + +
  • +

    + UNION: + {A, B, C, D, E} +

    +
  • +
  • +

    + INTERSECT: + {A, B, C} +

    +
  • +
  • +

    + EXCEPT: + {D} +

    +
  • +
    +

    + "ALL": Multiset Semantics +

    +

    R = {A, A, A, A, B, B, C, D} = {A(4), B(2), C(1), D(1)}

    +

    S = {A, A, B, B, B, C, E} = {A(2), B(3), C(1), E(1)}

    + +
  • +

    + UNION ALL (sum of all cardinalities): + + {A(6), B(5), C(2), D(1), E(1)} +

    +
  • +
  • +

    + INTERSECT ALL (min of cardinalities): + + {A(min(4,2)), B(min(2,3)), C(min(1,1)), D(min(1,0)), E(min(0,1))} + = {A, A, B, B, C} +

    +
  • +
  • +

    + EXCEPT ALL (subtract cardinalities): + + {A(4-2), B(2-3), C(1-1), D(1-0), E(0-1)} = {A, A, D} +

    +
  • +
    +
    + + +
    + +
    \ No newline at end of file diff --git a/Writerside/topics/Operating-System.md b/Writerside/topics/Operating-System.md deleted file mode 100644 index 23a14af..0000000 --- a/Writerside/topics/Operating-System.md +++ /dev/null @@ -1,71 +0,0 @@ - - -# Operating System - -## 1 Introduction to Operating System - -

    Definition: Special layer of -software that provides application software access to hardware resources. -

    - -Operating System - -

    What is an Operating System?

    - - -
  • -

    Illusionist: Provide -clean, easy-to-use abstractions of physical resources.

    -
  • -
  • -

    Referee: Manage -protection, isolation, and sharing of resources.

    -
  • -
  • -

    Glue: Common services.

    -
  • -
    - -### 1.1 Operating System as Illusionist - Provide clean, easy-to-use abstractions of physical resources - -Operating System as Illusionist - - -
  • -

    Application's "machine" is the -process abstraction provided by OS.

    -
  • -
  • -

    Each running program runs in its own process.

    -
  • -
  • -

    Processes provide nicer interfaces than raw hardware.

    -
  • -
    - -

    A process consists of:

    - - -
  • -

    Address Space

    -
  • -
  • -

    One or more threads of control executing in that address space

    -
  • -
  • -

    Additional system state associated with it, e.g., open files, -open sockets, etc.

    -
  • -
    - - -
  • -

    OS translates from hardware interface to application interface.

    -
  • -
  • -

    OS provides each running program with its own process.

    -
  • -
    - -### 1.2 Operating System as Referee - Manage protection, isolation, and sharing of resources - diff --git a/Writerside/topics/Operating-System.topic b/Writerside/topics/Operating-System.topic new file mode 100644 index 0000000..60e27c2 --- /dev/null +++ b/Writerside/topics/Operating-System.topic @@ -0,0 +1,91 @@ + + + + + + + Operating System + + +

    + Definition: + Special layer of + software that provides application software access to hardware resources. +

    + Operating System +

    + What is an Operating System? +

    + +
  • +

    + Illusionist: + Provide + clean, easy-to-use abstractions of physical resources. +

    +
  • +
  • +

    + Referee: + Manage + protection, isolation, and sharing of resources. +

    +
  • +
  • +

    + Glue: + Common services. +

    +
  • +
    + + Operating System as Illusionist + +
  • +

    Application's "machine" + is + the + process abstraction provided by OS. +

    +
  • +
  • +

    Each running program runs in its own process.

    +
  • +
  • +

    Processes provide nicer interfaces than raw hardware.

    +
  • +
    +

    + A process consists of: +

    + +
  • +

    Address Space

    +
  • +
  • +

    One or more threads of control executing in that address space

    +
  • +
  • +

    Additional system state associated with it, e.g., open files, + open sockets, etc.

    +
  • +
    + +
  • +

    OS translates from hardware interface to application interface.

    +
  • +
  • +

    OS provides each running program with its own process.

    +
  • +
    +
    + + +
    + +
    \ No newline at end of file diff --git a/Writerside/topics/Python-Programming.topic b/Writerside/topics/Python-Programming.topic index c2db36a..a591103 100644 --- a/Writerside/topics/Python-Programming.topic +++ b/Writerside/topics/Python-Programming.topic @@ -1088,7 +1088,7 @@ for example 1"/>

    For more information on order of growth, please refer to Data Structures and Algorithms 1.