Meow~
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Help
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# 演算法 Algorithm (CSIE-2A) ###### tags: `courses meow` `CSIE-2A` `Algorithm` `2021 Spring` :::success ### 計分 - 兩次期中考 `60%` - 從作業中出題 - 平時成績 `40%` - 演習課(討論作業,每週一次作業(可用時間 < 1week)(7~9題)) > 歷年來的經驗會被當20個人 (沒有刻意當人,單純就分數沒到) ### Book - T.H. Cormen et al., "introduction to Algorithms" 3rd ed,(The MIT Press 2009) - other reference - R. Sedgewick - A. Levitin - ... ### relative web - [UVA online judge(link)](https://onlinejudge.org/index.php) ### 相關競賽 - CPE - ITSA - PTC ::: ## $[Unit 1] Introduction - Traning for an algorithm designer - Design - Analysis - Implementation - some related topic ### What are Algorithm :::info #### Definition > blablabla ::: ### What are Problems - well-sprcified computational problem - 3 compoents - a set of `inputs` (or instances) - a set of `outputs` (or solutions, answers) - ==precise description== of relation between inputs and outputs - > inputs and outputs are all of finite lengths :::warning ### Example: Sorting Algorithm - statment - Input: A sequence of n numbers <$a_1$, $a_2$, ..., $a_3$> - Output: blablabal/not complete/ - Algorithms - Selection sort - Insertion sort - Merge sort ::: ### Problems & Modeling #### Modeling [Probelms in real world] --> [Algorithmic problems] ``` img of modeling ``` # week2 (02/25) > halting problem > 連程式會不會停都不知道了,你也沒辦法證明程式對不對 ## Some modeling example :::info ### model 1: LCS (Longest Common Subsequences) - 可用於語音辨識 (古早味) - sequence: - 可重複拿取 & 次序重要 - subsequence: - 給定一個 n 個字母的 sequence - subsequence 個數: $2^n$ > 在設計演算法的時候,可以想一個“等價”的問題,以方便自己思考[name=教授] ### model 2: Edit Distance - 在只有[insert, delete, replace, twiddle] 操作的情況下的字串相似度 ### model 3: Alignment of Two Strings - 在字串中插入 blank ::: ## Important problem types > 演算法課通常只教 離散的 > 數值問題會開在「數值分析」課,需要考慮計算誤差 - Studying Discrete object - Sorting - Searching - String Processing - Combinational problems - Graph problems - Studying Continuous objects - Geometric problems - Algebraic problems - Numerical problems # 0303 ## 演算法基本設計方式 - 觀察,直覺& (trail and error) - > 有時候你的直覺對,有時錯 - 遞迴設計法 (數學歸納法) - 轉換法(Transform-and-Conquer)(將問題轉換成自己(別人)會解的問題) - 其他:.....Brute force(xD ## Express Algorithm - Programs - Natural language - Flow charts - **pseudo codes** ## 遞迴 - Selection sort - Merge sort ### Analysis of Selection Sort - Correctness: By induction - > 相當於硬做(每個人都跟其他人比一次 $C^n_2$ - 類似概念的演算法: - Bubble sort - Heap sort - > 當第一名從缺的時候,只比有機會是第一名的人(曾被第一名打敗的) ### Analysis of Merge - Time complexity - $T(n) = T([n/2]) + T(n/2) + M(n), T(1) = 0$ - 假設 $n = 2^k$ - $T(n) = 2T(n/2) + n-1, T(1) = 0$ > 離散有一個算法可以解這種遞迴式[name=教授] > ~~可是我忘記了~~[name=87%的學生] - feature - stable - keys with same value do not exchange - not in-place - need extra space to put some number temporarily.(MergeSort吃空間) ### 遞迴的過程 - Top down(拆解) - Button up(合成) - 省略 - 當 split 為 trivial 時可省略; e.g. Merge sort ### Analysis of insertion sort - correctness: By induction - Time complexty - T(1) = 0 - T(n) = T(n-1) + f(n-1) - **Best case**: f(n-1) = 1 => T(n) = n-1 - **Wrost case**: f(n-1) = n => T(n) = n*(n+1) / 2 - **AVG**: f(n-1) about n/2 => T(n) about n^2/4 <!-- TODO: use latex --> :::danger :skull_and_crossbones: 注意: Average case: **絕對不是 (worst + best)/2** ::: :::info #### AVG 如何計算 > 算期望值 - 假設 input N 個數字 - 則總共有 N! 種組合 (因為只討論sorting所以只考慮大小排列) ::: ## Analysis of an Algorithm - Correctness - Resources (time, space, ...) consumed - Complexities (worst, avrage, best cases) - Better one exist? - lower bond - >e.g. the lower bond of compare-based sorting is `nlog(n)` - optimality(**Quicksort** is the lower bound of sorting) ### Analysis 的必要性 > 分析演算法是設計者的職責 ~~所以不關我的事~~ - 選擇適當的演算法 - 「適當」的演算法不一定是最快的(達到要求就好) ### Implementation of an Algorithm - Select a suitable data structure & codeing > Program = algorithm + data structure - Empirical(經驗) analysis - Program optimization(優化) - recursion-removal, sentinel(好像跟LOOP有關), tuning, ...) <!-- sentinel 不知道是指什麼 --> <!-- 是不是Apex那把槍 --> <!-- 我覺得 long bow 比較好用,我泡槍 sentinel 都打不到QQ --> <!--我都拿三重擊跟電能步槍 --> ### Related Topics in Computation Theory - Machine models - 假設單核,ram 無窮大 - 假設基本運算都是常數時間 - Decidable <-> Undecidable - 有無演算法可以解 - Lower bounds - Tractable <-> Intractable - 找不找的到快的演算法可解 - > A notorious one:`NP-Complete` #### How to Handle Intractable Problem > 這學期不太會講到 - Branch-and-bound - Heuristic algorithm - > Optimization Program - 能得到一個不錯的解,但不保證最佳 - Randomized algorithm - Simulated annealing - genetic algorithm (讓好的解答繁殖,突變,淘汰) - probabilistic algorithm (給出的答案有機率性,多做幾次可信性就可以提高) - Approximation algorithm (有保證就算) - Fixed-parameter algorithm - Others: - quantum, DNA, parallel, neural nets - ... <!-- - 終於把簡介講完了 --> ## Unit 2 ### Analysis of algorithm #### Issues: - Correctness - ==Time efficiency== - Space(or other resources) efficiency - Optimality > 我們會以討論時間複雜度為主,因為: > 空間通常比較好分析(宣告了哪些空間) > 時間的分析方法也可以套用在空間上 #### Case of analysis - Worst case: 從一樣是輸入長度 n 的輸入內,挑出最差的結果 - Average case: 期望值(uniform distribution) ### Notes on Big-$O$, $\Theta$, $\Omega$ > 有時候大家還是寫成 $O$ 而不是 $\Theta$ > 寫成 $T(n)=O(n)$ 但其實是 $T(n) \in O(n)$ #### The Limitations of asymptotic Notations - $O(n)$ 的執行時間一定會比 $O(n^2)$ 快嗎? - 理論上會,但是要在 $n$ > 某個 $m$ 的時候 :::info ### 一個不知道該被分在哪裡的東西 - quick sort 可以 random 選擇 pivot 來避免 worst case - merge 算起來比 quick-sort 快,但是這是沒有考慮“搬”的動作的情況下 > 別人比了才搬,merge sort 先搬再說 ::: ### 所有的符號一覽~ - $O(g(n))$ - $\Theta(g(n))$ - $\Omega(g(n))$ - $o(g(n))$ - $\omega(g(n))$ ### Properties of Asymptotic Notations - Transitivity - $\Theta(g(n))$ - Reflexivity - $\Theta(g(n))$ - Symmetry - $\Theta(g(n))$ - Transpose symmetry > 當一個函數滿足 Transitivity, Reflexivity 和 Symmetry,代表**等價**關係。 > 三個符號只有 $\Theta$ 滿足以上三個條件。 <!-- ### Master Theorem --> <!-- ### Divide and Conquer --> :::warning #### 隨堂小測驗 | question | Ans | | ------------------------------------------- | --- | | (i) $n^2 = O(n^3)$ | :O: | | (ii) $n^3 = O(n^2)$ | :X: | | (iii) $2^{n+1} = \Theta(2^n)$ | :O: | | (iv) $(n+1)! = \Theta(n!)$ | :X: | | (v) $f(n)=O(n) -> f(n)*f(n) = O(n^2)$ | :O: | | (vi) $f(n) = O(n) -> 2^{f(n)}$ | :X: | | (vii) $\lg ^{100000}n = o(n^{0.000000001})$ | :O: | | (viii) $n^{1.01} = O(n\lg n)$ | :X: | ::: ### 演算法中常見的函數 | Big-Oh form | Name | |:--------------- | ----------- | | $O(1)$ | Constant | | $O(\lg n)$ | Logarithmic | | $O(n)$ | Linear | | $O(n\lg n)$ | | | $O(n^2)$ | square | | $O(n^3)$ | cube | | $O(n^m), m ≥ 1$ | polynomial | | $O(c^n)$, c > 1 | Exponential | | $O(n!)$ | Factorial | > 只要演算法複雜度到 Exponential,就不用等了.... > 只要 input 稍微大一點,你就算不完了 > 電腦燒掉都跑不出來 --- ### The recursion-tree method $T(n) = T(n/3) + T(2n/3) + n$ ![img_recursion_tree_example](https://i.imgur.com/WiSuKN0.png) > 因此猜測 $n\lg n$ ### The substitution method 1. 先猜一的可能的答案 2. 用數學歸納法證明其之 ### Striling's Formula (or Approximation) - $n! = \sqrt{2πn}(n/e)^n(1+\Theta(1/n))$ > Q: 網路上查到的都沒有後面那個部分$(1+\Theta(1/n))$ > A: 後面的部分是為了表達 n 越大誤差越小,而且誤差在 1/n 的等級 <img src="https://i.imgur.com/H0Vsigg.png" alt="Striling's Formula" width="400"> ### Approximation by Integrals - 用積分來逼近 sigma 的東東 # 0317 ## 矩陣乘法 - 給 n*n 矩陣 - 傳統做法: $\Theta(n^3)$ - 想法:每個矩陣個分成4個 n/2 * n/2 舉證 - T(n) = 8*T(n/2) + $\Theta(n^2)$ - 還是 n^3 - 再用奇怪的方法 - T(n) = 7 * T(n/2) + $\Theta(n^2)$ - T(n) = $\Theta(n^{lg7})$ - $\Theta(n^{2.81})$ ### 長整數運算 - 兩個 n 位整數做乘法 - 傳統:$\Theta(n^2)$ - 可以 n/2 去算,然後用奇怪的分配法減少乘法 - $T(n) \, = \Theta(n^{lg3})$ - = $O(n^{1.585})$ > 這次就用招標的方式吧,想投稿就丟給助教 ### A Stock Buying Problem > STONK![](https://i.imgur.com/fG6PR73.jpg) :::danger **Exercise:** Solve the original stock problem in $O(n)$ time complexity ::: ### Priority Queue & Heap #### Some operations of priority queue - **Construct** a priority queue - **Insert** a new item - **Remove** the largest item (Extract-Max) - **Replace** the largest item with a new item (unless the new item is larger) - **Change** the priority pf an item - **Delete** an arbitrary specified item - **Join** two priority queues into one large one ### Sort' - compaired-based sort: $\Omega(nlgn)$ - other special purpose sort - counting sort - radix sort - bucket sort <!-- 484 漏了一堆 --> ## 0325 ## Dynamic Programming Example: $r_j=max_{1≤i≤j}({p_i+t_{j-i}})$ - Bottom-Up method - Time: $\Theta(n^2)$ - Space $\Theta(n)$ - Top-Down ### Matrix-Chain Multiplication :::info Given a chain of matrices <$A_1, A_2,..., A_n$>, where matrix $A_i$ has dimension $p_{i-1} * p_i$ .... ::: 不同乘法順序會導致不同的複雜度 $A_1 * A_2 * A_3 * A_4$ 13, 5, 89, 3, 34 (A1(A2(A3A4))) => 26418 (A1((A2A3)A4)) => 4055 #### Catalan Number 對於 n+1 個矩陣,括號方式的數量 # = n node 可組成的 Binary tree 數量 = n 組數字經過 stack 後的輸出種類 (push, pop 操作) = n 對括號有多少種配對方式 = n th Catalan Number = $\frac{C^{2n}_n}{n+1}$ = $\Omega(\frac{4^n}{n^{3/2}})≈O(2^n)$ 一對一對應 123 => ()()() 321 => ((())) expression tree #### Solution A1,A2... An k T1, T2 ==TODO==: 畫圖 遞迴 ### Steps of developing DP - **Characterize** the structure of a optimal solution - **Derive** a *recursive formula* - **Computing the value** of recursive formula - 寫程式建議使用 **Bottom up** 的方法 - **Construct** an optimal solution from computed information in a "**Top down**" ### Subtleties of Optimal Substructure ### Longest common subsequence > 遇到不知道怎麼解的問題,我喜歡把他變換成其他問題 <x1, x2, ..., xm> <y1, y2, ..., yn> case 1-1: xm != yn then: solve(<x1,...xm>, <y1,...,yn-1>) case 1-2: xm != yn then: solve(<x1,..., xm-1>, <y1,...,yn>) case 2-1: $x_m=y_n$, and they form a match. then: solve(<$x_1, x_2,...,x_{m-1}$>, <$y_1, y_2,..., y_{n-1}$>) + 1 case 2-2: $x_m=y_n$, and they don't form a match. We can change its original pairs to form case 2-1. then: solve(<$x_1, x_2,...,x_{m-1}$>, <$y_1, y_2,..., y_{n-1}$>) + 1 ### Optimal Bianry Search Tree - Definition: $E[$search cost $]$ 最小的 Binary Search Tree ### DP Notes > DP 基本上可以視為聰明的硬做 - DP 子問題不重複做 - DP 需要所有子問題數量不能太大 :::success ### LCS - Improvenments on time - LCS ,, LIS - The Four-Russians Speedup - improvements on space - linear space Algo ::: ## Greedy > 每次都選最好的(~~結果不一定是最好的~~) > Greedy Algorithm Problem: 被證明是可以用 greedy method 來算會是對的 ### Examples #### 辦活動問題 :::warning :slightly_smiling_face: 這題一定會出 ::: - 給定一堆活動(起始時間 ~ 結束時間),活動與活動之間時間可能會衝突,求取最多的活動可以被舉辦 - > 解:Greedy,每次取結束時間最小的 (或最晚開始,因為對稱性) - #### Knapsack Problem (小偷問題) - 找到最有價值的負重,而且背包要能夠承載 - 其他衍生問題 - 0-1 knapsack problem - Fractional knapsack problem (C/P 值最高) - Greedy - (selection problem) ##### 0-1 Knapsack Problem - Time: $O(nK)$ > :heavy_exclamation_mark: a pseudo-polynomial time algorithm > It's possible that $K>2^n$ #### Huffman #### Task-Scheduling Problem - 有 n 個工作 - 期限前做完有獎勵 - 超過期限會有懲罰 ## 4/28 ## Elementary Graph Algorithms ### Königberg Bridge Problem - Euler circuit (一筆畫問題) - Euler grpah: 可以一筆畫走完整張圖且每一條路徑都只有走過一次 ### Hamiltonian Cycle Problem - Hamiltonian cycle ? (應該是走過所有頂點吧?) - Hamiltonian graph ### Utilities Problem > 平面圖定義:邊只在頂點相交 - $\exists$ planar embedding ? (是不是平面圖) - Planar Graph ## 5/19 ### Bellman-ford algorithm - 不斷的對每一個邊做 "Relax" - 是一種 DP <- What!? - > 不能處理負迴圈 - 時間複雜度:$O(VE)$ #### Some tips when implement the algorithm - Is it necessary to do the relaxation for each edge $uv \in E$ ? - 用一個 queue 來紀錄下一次要做 relaxation 的 edge > 實做上有所改進,不過在分析上並沒有改進 > 演算法本身通常都是以精簡的方式呈現,實做時不會直接對著原本的樣子做 [name=教授] ### DAG & DP #### 以剪鋼筋為例子 可以當成找最大路徑,點為鋼筋的長度, weight 即是切之後的獲利 ```graphviz digraph { 4->3 [label=""] 4->2 [label=""] 4->1 [label="\n "] 4->0 [label="\n\n "] 3->2 [color="blue" label=""] 3->1 [color="blue" label=""] 3->0 [color="blue" label=""] 2->1 [color="green" label=""] 2->0 [color="green" label=""] 1->0 [label=""] {rank = same; 4; 3; 2; 1; 0} } ``` #### LCS

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully