單詞

binary tree二叉樹
recurrences遞歸
closed forms解析解
recursion遞歸
iteration迭代
closed form ( non-recursively)將遞歸展開
asymptotic 漸近

Algorithms: for solving problems, transforming data.

  • A sequence of well-defined, computer-implementable instructions for solving a problem
  • Algorithms operate on data.

Data structures: for storing data; arranging data in a way that suits an
algorithm.
1)Linear data structures: stacks and queues
2)Trees and graphs
3)Dictionaries
? Storing data
? Organizing data so that it can be used efficiently

image.png

Brute Force Algorithms
? Straightforward problem solving approach, usually based directly on the problem’s statement.
? Exhaustive search for solutions is a prime example.
Selection sort/String matching/Closest pair/Exhaustive search for combinatorial solutions/Graph traversal
? Simple, easy to program, widely applicable.
? Standard approach for small tasks.
? Reasonable algorithms for some problems.
? But: Generally inefficient—does not scale well.
? Use brute force for prototyping, or when it is known that input remains small.

in-place if it does not require additional memory except, perhaps, for a few units of memory是否使用多余的空間
stable if it preserves the relative order of elements with identical keys 對相同的元素是否保持原有的空間順序
input-insensitive if its running time is fairly independent of input properties other than size是否其運行時間只與輸入值的大小有關(guān)

Recursion
? A very natural approach when the data structure is recursive (e.g. lists, trees)
? But also examples of naturally recursive array processing algorithms
? Next week we’ll express depth first graph traversal recursively (the natural way);
? Although our recursive algorithm is short and elegant, it is not the most efficient way of solving the problem.
? Its running time grows exponentially as you grow the input amount.
? More efficient solutions can be developed using memoing or dynamic programming(around Week 10).

Decrease-and-Conquer by-a-Constant
? In this approach, the size of the problem is reduced by some constant in each iteration of the algorithm.

  • Insertion Sort
  • topological sorting
  1. Decrease-and-Conquer by-a-Factor
  • Decrease-by-a-constant-factor
    binary search
  • Decrease-by-a-variable-factor
    interpolation search

Divide and Conquer
The divide-and-conquer strategy tries to make the most of this idea:
1.Divide the given problem instance into smaller instances.
2.Solve the smaller instances recursively.
3.Combine the smaller solutions to solve the original instance.
The Master Theorem/Mergesort/ Quicksort/ Tree traversal/ Closest Pair revisited

heap and priority queue-13
The heap is a very useful data structure for priority queues, used in many algorithms.
A heap is a complete binary tree which satisfies the heap condition:
– Each child has a priority (key) which is not greater than its parents.子數(shù)小于等于父max-heap
? A priority queue is a set (or pool) of elements.
? An element is injected into the priority queue together with a priority (often the key
value itself) and elements are ejected according to priority.
? We think of the heap as a partially ordered binary tree.
? Since it can be easily maintained as a complete tree, the standard implementation
uses an array to represent the tree.

  • H[1]是最大的數(shù)宏侍,eject的時間復雜度是O(1)+重新存儲heap的時間
  • heap的高是log2(n)的向下取整,樹的高是log2(n)的向下取整杨名,空樹高為-1.
  • heap的parents節(jié)點為1 到n/2向下取整

As an abstract data type, the priority queue supports the following operations on a “pool”
of elements (ordered by some linear order):
– find an item with maximal priority
– insert a new item with associated priority
– test whether a priority queue is empty
– eject the largest element.
? Other operations may be relevant, for example:
– replace the maximal item with some new item
– construct a priority queue from a list of items
– join two priority queue.

Transform-and-Conquer is a group of design techniques that:
– Transform: Modify the problem to a more amenable form, and then
– Conquer: Solve it using known efficient algorithms.
1)Instance simplification
Finding the median找到中位數(shù)
Uniqueness checking唯一性檢查
Finding the mode找到眾數(shù)
2)Representational change
3)Problem reduction

A binary search tree, or BST, is a binary tree that stores elements in all internal
nodes, with each sin-tree satisfying the BST property:
Let the root be r; then each element in the left subtree is smaller that r and each
element in the right sub-tree is larger than r. (For simplicity, we assume that all
keys are different.)

AVL tree
? Named after Adelson-Velsky and Landis
? Recall that we defined the height of the empty tree as -1.
? For a binary (sub-) tree, let the balance factor be the difference between the height of
its left sub-tree and that of its right sub-tree
.
? An AVL tree is a BST in which the balance factor is -1, 0, or 1, for every sub-tree.

Regaining balance can achieved with one or two simple, local transformtions, socalled rotations.
It is always the lowest unbalanced subtree that is re-balanced.

2-3 trees and 2-3-4 trees are search trees that allow more than one item to be stored
in a tree node.
? As with BSTs, a node that holds a single item has (at most) two children.
? A node that holds two items (a so-called 3-node) has (at most) three children.

Hashing is a standard way of implementing the abstract data type “dictionary”.
A good hash function uniformly distributes data items across the entire set of possible hash values.
A perfect hash function allows for constant time search, insertion, and deletion, into and from a hash table.
When using an open addressing method of collision resolution, all data records reside in a single bucket array.
? Implemented well, it makes data retrieval very fast.
? A key can be anything, as long as we can map it efficiently to a positive integer. In particular, the set k of keys needs not be bounded.
? Assume we have a table of size m (the hash table).
? The idea is to have a function h: k → {1, … , m} (the hash function) determine where records are
stored: A record with key k should be stored in location h(k).
? The address h(k) is the hash address.

In some cases different keys will be mapped to the same hash table address.
When this happens we have a collision.

Dynamic programming is an algorithm design technique that is sometimes applicable when we want to solve a recurrence relation and the recursion involves overlapping instances.
The bottom-up approach used the tabulated results, rather than solving overlapping
sub-problems repeatedly.使用表數(shù)據(jù)
Optimisation problems sometimes allow for clever dynamic programming solutions.
– the objective is to find the best possible combination: the one with the lowest cost, or higher profit, subject to some constraints.
? For dynamic programming to be useful, the optimality principle must hold:
An optimal solution to a problem is composed of optimal solutions to its subproblems.一個問題的最優(yōu)解是由它的子問題的最優(yōu)解組成的
? While not always, this principle often holds.
動態(tài)規(guī)劃:用于解決具有overlapping subproblems(memoization) 和 optimal substructure(使用局部最優(yōu)解構(gòu)造整體最優(yōu)解)屬性的問題
貪心算法:greedy choice property(是指在做每一步的選擇時不需要和以前的選擇一起考慮)和optimal substructure.
top-down通常以遞歸形式出現(xiàn)宴猾,從父問題開始崇呵,遞歸地求解子問題陨囊。top-down的求解過程通常與memoization結(jié)合,即將計算過的結(jié)果緩存在數(shù)組或者哈希表等結(jié)構(gòu)中诱建。當進入遞歸求解問題時稀并,先查看緩存中是否已有結(jié)果仅颇,如果有則直接返回緩存的結(jié)果。
bottom-up通常以循環(huán)形式出現(xiàn)碘举。bottom-up的求解過程通常與tabulation結(jié)合忘瓦,即先解最小的子問題,解決后將結(jié)果記錄在表格中(通常是一維或二維數(shù)組)引颈,解決父問題時直接查表拿到子問題的結(jié)果政冻,然后將父問題的結(jié)果也填在表中,直到把表格填滿线欲,最后填入的就是起始問題的結(jié)果明场。

貪心算法
A natural strategy to problem solving is to make decision based on what is the locally best choice.當前最優(yōu)狀態(tài)下,找下一步
In general we cannot expect locally best choices to yield globally best outcomes.
? However, there are some well-known algorithms that rely on the greedy approach, being both correct and fast.
? In other cases, for hard problems, a greedy algorithm can sometimes serve as an acceptable approximation algorithm

Transitive closure is important in all sorts of applications where we want to see of some “goal start” is reachable from some “initial state”.

From an information-theoretic point of view, most computer files contain a lot of redundancy.
? Compression is used to store files in less space.
? For text files, savings up to 50% are common.
? For binary files, savings up to 90% are common.
? Savings in space mean savings in time for files transmission.

A trie is a binary tree for search applications.
? To search for a key we look at individual bits of a key and descend to the left whenever a bit is 0, to the right whenever it is 1.
? Using a trie to determine codes means that no code will be the prefix of another!

How many different binary search trees (BSTs) with elements {1,2,3,4} are there? And how many with elements {1,2,3,4,5}?


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末李丰,一起剝皮案震驚了整個濱河市苦锨,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌趴泌,老刑警劉巖舟舒,帶你破解...
    沈念sama閱讀 212,816評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異嗜憔,居然都是意外死亡秃励,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,729評論 3 385
  • 文/潘曉璐 我一進店門吉捶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來夺鲜,“玉大人,你說我怎么就攤上這事呐舔”依” “怎么了?”我有些...
    開封第一講書人閱讀 158,300評論 0 348
  • 文/不壞的土叔 我叫張陵珊拼,是天一觀的道長食呻。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么仅胞? 我笑而不...
    開封第一講書人閱讀 56,780評論 1 285
  • 正文 為了忘掉前任每辟,我火速辦了婚禮,結(jié)果婚禮上干旧,老公的妹妹穿的比我還像新娘渠欺。我一直安慰自己,他們只是感情好莱革,可當我...
    茶點故事閱讀 65,890評論 6 385
  • 文/花漫 我一把揭開白布峻堰。 她就那樣靜靜地躺著讹开,像睡著了一般盅视。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上旦万,一...
    開封第一講書人閱讀 50,084評論 1 291
  • 那天闹击,我揣著相機與錄音,去河邊找鬼成艘。 笑死赏半,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的淆两。 我是一名探鬼主播断箫,決...
    沈念sama閱讀 39,151評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼秋冰!你這毒婦竟也來了仲义?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,912評論 0 268
  • 序言:老撾萬榮一對情侶失蹤剑勾,失蹤者是張志新(化名)和其女友劉穎埃撵,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體虽另,經(jīng)...
    沈念sama閱讀 44,355評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡暂刘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,666評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了捂刺。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谣拣。...
    茶點故事閱讀 38,809評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖族展,靈堂內(nèi)的尸體忽然破棺而出芝发,到底是詐尸還是另有隱情,我是刑警寧澤苛谷,帶...
    沈念sama閱讀 34,504評論 4 334
  • 正文 年R本政府宣布辅鲸,位于F島的核電站,受9級特大地震影響腹殿,放射性物質(zhì)發(fā)生泄漏独悴。R本人自食惡果不足惜例书,卻給世界環(huán)境...
    茶點故事閱讀 40,150評論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望刻炒。 院中可真熱鬧决采,春花似錦、人聲如沸坟奥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,882評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽爱谁。三九已至晒喷,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間访敌,已是汗流浹背凉敲。 一陣腳步聲響...
    開封第一講書人閱讀 32,121評論 1 267
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留寺旺,地道東北人爷抓。 一個月前我還...
    沈念sama閱讀 46,628評論 2 362
  • 正文 我出身青樓,卻偏偏與公主長得像阻塑,于是被迫代替她去往敵國和親蓝撇。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,724評論 2 351

推薦閱讀更多精彩內(nèi)容

  • Selection sort 如果有一沓人民幣陈莽,怎么按照面額從小到大按順序排序渤昌?答:每次從這沓人民幣中取出面額最小...
    a_tomcat閱讀 1,248評論 0 51
  • 久違的晴天,家長會传透。 家長大會開好到教室時耘沼,離放學已經(jīng)沒多少時間了。班主任說已經(jīng)安排了三個家長分享經(jīng)驗朱盐。 放學鈴聲...
    飄雪兒5閱讀 7,515評論 16 22
  • 今天感恩節(jié)哎群嗤,感謝一直在我身邊的親朋好友。感恩相遇兵琳!感恩不離不棄狂秘。 中午開了第一次的黨會,身份的轉(zhuǎn)變要...
    迷月閃星情閱讀 10,559評論 0 11
  • 可愛進取躯肌,孤獨成精者春。努力飛翔,天堂翱翔清女。戰(zhàn)爭美好钱烟,孤獨進取。膽大飛翔,成就輝煌拴袭。努力進取读第,遙望,和諧家園拥刻×鳎可愛游走...
    趙原野閱讀 2,723評論 1 1
  • 在妖界我有個名頭叫胡百曉,無論是何事般哼,只要找到胡百曉即可有解決的辦法吴汪。因為是只狐貍大家以訛傳訛叫我“傾城百曉”,...
    貓九0110閱讀 3,256評論 7 3