1 / 58

Array 和朋友 們

下午的 office hour 改成 4-5 點. Array 和朋友 們. Michael Tsai 2010/10/01. 作業 1 開始寫了沒 ?. 作業 1 題目有少數更動 . 請上課程網站下載. 今日菜單. 複習上次教過 ( 以及沒教完 ) 的部分 陣列 陣列的朋友 們 多項式 字串 矩陣 都是 中文 Slides 底色是白的 ( 比較好印 ) 希望大家可以醒著久一點. Asymptotic notation 有什麼用 ?. 知道當 input” 大小 ” 改變的時候 , 執行速度有什麼改變 ? “ 粗略 ” 的比較

Download Presentation

Array 和朋友 們

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 下午的office hour 改成4-5點 Array和朋友們 Michael Tsai 2010/10/01 作業1開始寫了沒? 作業1題目有少數更動. 請上課程網站下載.

  2. 今日菜單 • 複習上次教過(以及沒教完)的部分 • 陣列 • 陣列的朋友們 • 多項式 • 字串 • 矩陣 • 都是中文 • Slides底色是白的 (比較好印) • 希望大家可以醒著久一點

  3. Asymptotic notation有什麼用? • 知道當input”大小”改變的時候, 執行速度有什麼改變? • “粗略”的比較 • a, bn, c都”比較不重要” • bn, c都”成長得”比較慢 • 不同的a差別不是很大 • 讓我們看看不同的”order”長的速度差別有多大…

  4. 1秒10億指令的機器的執行速度

  5. 兜基??

  6. n很大的時候右邊比左邊好 但是n會很大嗎? 有時候不會 如果n永遠小於?? 結論:要看n大小 (實際上寫程式的時候適用) 哪一個好…

  7. Definition [Big “oh”]: if and only if there exist positive constants and such that for all . “f of n is big oh of g of n” “=“ is “is” not “equal” Asymptotic notation – Big OH

  8. Definition [Omega]: if and only if there exist positive constants and such that for all . “f of n is omega of g of n” Asymptotic notation – Omega

  9. Definition [Theta]: if and only if there exist positive constants , and such that for all . “f of n is theta of g of n” Asymptotic notation – Theta Theta Platform GMC Terrain

  10. 用圖表示 • Big Oh • 紅色 • Omega • 藍色 • Theta • 紅色藍色都要

  11. Example for all

  12. (1) Example for all

  13. Example for all

  14. Theorem: If , then . • Proof: , for all . So, 有用的Theorem (別睡著)

  15. 分析演算法 • 複習: • 程式花的時間: • Compile time • Run (execution) time • 程式花的空間: • Fixed space requirement • Variable space requirement • 實用上, 大多使用Big Oh • 表示最糟的狀況也不會比Big Oh裡面的order糟

  16. 那麼, 怎麼知道程式的時間複雜度? • 看每個statement執行幾次, 畫表來計算 • 今天會看到不少例子 • 有時候複雜度跟input大小不一定有關係 • 這時候就會取average case, worst case, best case • 實際測量程式執行時間(作業有) 最實際

  17. 再看一次簡單的例子

  18. 接下來, 讓我們看點有趣的~~~ 2010/03/26 在Carnegie Mellon University的Lab中努力焊板子

  19. Array 是什麼東東 • 帶狀的記憶體, 連續的, 一格一格的 • 每一格存不同的資料 • 每一格有個編號 • 我們可以把它看成是一個 index和value的關聯或者對應 • 很像是電腦記憶體的結構, 所以常常被拿來當作基本元素 2 0 1 … ㄅ ㄐ ㄖ ㄊ ㄟ ㄌ

  20. Array支援那些動作? • 創造一個array • 讀取某格子的值 • 寫入某格子的值 • 毀掉一個array?

  21. <複習時間> C語言 0 1 2 index: 8-11 0-3 4-7 address: • int list[3]; • 降子的話, list[i]是去記憶體哪邊拿值? • 這邊: list + i* sizeof (int) • 那這樣呢? *(list+i) • 會拿到同樣一個值 • C會看list是哪種type的pointer • 然後自動把i乘以sizeof(type) 1 4 5 list

  22. <複習時間> C語言 最好多一個參數表示array的大小 void change_array(int a[], int b) { a[0]=10; b=10; } int main() { int foo[2]; int bar=0; int i; foo[0]=0; change_array(foo, bar); printf("foo[0]=%d bar=%d\n", foo[0], bar); return 0; • } 答案: foo[0]=10 bar=0 bar:“call-by-value” foo:“call-by-value”, 但是foo是address

  23. <複習時間> C語言 • 如果事先不知道資料大小怎麼辦? • 開太大浪費, 開太小不能用 • 知道了再開 • 請愛用 malloc. • Example: • 我要開一個大小n的陣列 list(注意n是變數喔) • int * list; • list=(int*)malloc(sizeof(int)*n);

  24. <複習時間> C語言 • calloc: 跟malloc差不多 不過會把內容先都清成0 • realloc: 之前用malloc拿的空間, 可以用realloc變大變小 • free: 不用了, 把之前拿來的空間還給系統 • 怎麼用? 請自己看man page or help

  25. 那麼二維的陣列呢? • C是這樣玩 -如果我要宣告 int x[3][5]: • 比如說, 怎麼拿到x[1][3]的值? [3] [0] [1] [2] [4] x[0] x[1] x[2] 指標們 放資料的地方

  26. 那麼三維的陣列呢? • 請一位同學來畫, 並解釋運作的方式

  27. 有沒有別的方法? • 指標一直指來指去好麻煩  • 用一個一維陣列表示多維陣列好不好? • 例子: 假設我要宣告 int list[3][5] • 請一位同學來畫圖:D • 怎麼宣告? (要幾個元素?) • 怎麼拿值? (我要index=(i,j)的值) • 二維: “Row major” v.s. “column major”

  28. <複習時間> C語言 趴兔 Part II • 什麼是Structure? • 例子: struct { char name[10]; int age; float salary; } person; struct person pa; pa.age=25;

  29. <複習時間> C語言 趴兔 Part II • 怎麼比較兩個structure variable一不一樣? • struct person a, b; • if (!strcmp(a.name, b.name) && • a.name==b.name && ….) • ANSI C 可以讓你直接比  • if (a==b) • 也可以直接assign • a=b;

  30. <複習時間> C語言 趴兔 Part II • 什麼是enum? • 什麼是union? • typedef幹嘛用的? • 例子: typedefstructsexType{ enumtagField {female, male} sex; union { int children; int beard; }u; } sexTypesi; si.sex=male; si.u.beard=0;

  31. 朋友一: 多項式 • 問題: 我想要把多項式存起來,方便做加法(可能還有別的動作) • 多項式: • 提示: array是它的朋友 • 這樣存方不方便作加法? • 有沒有什麼壞處? 3 0 1 2 n index代表指數 … value 存係數

  32. 很鬆的多項式 • 如果是 要怎麼存? • 浪費了999個格子 T_T • 有沒有什麼更好的方法? 3 0 1 2 1000 index代表指數 … value 存係數

  33. 很鬆的多項式 存係數 存指數 startA finishA startB finishB • 用兩個變數(start, finish)存多項式的開始與結束的index • 照指數大排到小

  34. 那麼, 加法怎麼運作呢? 存係數 • 題目: 多項式A和多項式B乘起來的結果要放到C(新的) • 先想一想 … 存指數 startC, finishC finishB startA finishA startB

  35. 舉個例子好了 多項式A 多項式B 多項式C Time complexity = O(??)

  36. 接下來看一下程式 • Program 2.6 on page 70 • Time complexity=???? • 有沒有什麼缺點??

  37. 朋友二: 字串 • C語言的字串是怎麼表示的? (Representation) • char foo[]=“data “; • 看起來像這樣子 • 字串可以做什麼動作? • 超多的! • 把B字串附在A字串後面 strcat • 比較兩個字串是不是完全一樣 strcmp • 還有什麼?… • (<複習時間>: 回去自己看一下string.h裡面有哪些function)

  38. <動腦時間> • 題目: 把t字串插入s字串位置為i的地方, 結果放到s • t字串: amobile • s字串: uto • i=1 • 那麼結果應為automobile

  39. 先看一個爛方法  • 方法: • 開一個新的temp 字串 • 把i之前的a字串copy過去temp • 然後把b字串copy過去temp • 然後把i之後的a字串copy過去temp • 然後把整個temp字串copy到b • 為什麼不好? • Space • Time • 請同學們想出一個好一點的方法

  40. <動腦時間> 趴兔 Part II • 題目: 在s字串裡面找p字串第一次出現的位置, 如果有的話output出p字串在s字串裡面開始的位置. 如果沒有的話output -1. • 例子: • s字串: abcbabcabca • p字串: abca. • output: 4

  41. 先看一個爛方法  • 方法: • 比對s字串裡面從第一個字元開始是不是跟p字串一樣 • 比對s字串裡面從第二個字源開始是不是跟p字串一樣 • 一直比到s字串的最後一個字元或者找到為止 • 為什麼不好? • Time complexity = O(??)

  42. 大師們的好方法 • KMP: Knuth, Morris, and Pratt • 可以達到O(n+m). • 意思就是說, 每個在p和s的字元都只需要處理一次 • order來講不可能更好了 • 這個神奇的方法, 大意是什麼呢? • 如果p裡面有重複出現的pattern的話, 要記得 • 如果比對到一個不對的字元, 就退到前一次出現同樣pattern的下一個字源繼續開始比較就好了

  43. 例子 • p=“abcabcacab” • s=“abcabcabcacab”

  44. Failure function • 定義: • 例子:

  45. Failure function • 另外一個定義(recursive)

  46. Failure function • 那麼, 算failure function的time complexity=O(??) • 答案: O(strlen(pat))

  47. 有了failure function以後要怎麼用呢? • 舉個例子

  48. Knuth–Morris–Pratt algorithm • 看一下程式, 看能不能理解 • O(n+m) • 每個字元處理過一遍, order無法更好的演算法 • 利用failure function記得, 現在這邊最近幾個字元的pattern是不是前面一開始也出現過 • 如果比對錯了, 就回到前一個字元的failure function的位置的下一個字元開始比對

  49. 朋友三: 矩陣 • 怎麼存起來? • 用二維陣列, a[行][列]來存 • 加法乘法都很容易

  50. 很鬆的矩陣 • 浪費很多空間在存0 • 有沒有什麼節省空間的方法可以存?

More Related