Professional Documents
Culture Documents
補充講義
大綱
n Double-ended priority queue implementations
Ø Min-Max Heap
Ø Deap
Ø SMMH
Ø Deap
Ø SMMH
WEPL=4∗2+4∗4+4∗5+4∗7+3∗9+3∗10+1∗15=144.
HUFFMAN ALGORITHM – FIND THE SMALLEST WEPL
HUFFMAN CODING IS OPTIMAL PREFIX CODE AND VARIABLE LENGTH
Ø Dijkstra’s algorithm for shortest path length from one single source to other destinations
Ø 最多節點數目= 2h-1
𝒎𝒉 &𝟏
Ø The maximum number of nodes = ∑𝒉𝒊"𝟏(𝒎𝒊&𝟏 ) =
𝒎&𝟏
[𝒎]𝒉&𝟏
&𝟏
Ø The minimum number of nodes = 1+𝟐 ∗ 𝟐
[𝒎]
𝟐
&𝟏
[𝒎]𝒉&𝟏
Ø The minimum number of keys =𝟐 ∗ -1
𝟐
B TREE OF ORDER 3 (2-3 TREE) B TREE ORDER 4 IS ALSO NAMED AS 2-3-4 TREE
INSERT X IN B TREE ORDER M
n Step 1. Search for the X. since X does not exist in the tree, this will lead to an external
node(null), so put x into the parent of the external node
n Step 2. Check the node
Ø If it is not overflow, then exits
Ø If it is overflow, then do split action. After split, for its parent node, go back to step 2
n Split action
Ø Pick up the [m/2] order key: k in the overflow node, move k into its parent node, and then split the
left and right child for the remaining keys
Insert 55
EXAMPLE
n Insert 37, 5, 18, and 12
DELETE X IN A B TREE OF ORDER M
1) Search for the X, find the node containing X
2) Delete x from this node
3) check node.
Ø if the number of keys in the node >= [m/2]-1 ([ ]: 上限),表示no underflow, then exits.
Ø Otherwise (underflow)
l Try Rotation firstly. If can, do Rotation, then exits.
l If it cannot do Rotation, then do Combine(or Merge) processing. After combination, for the parent of the node,
go to step (3) for checking whether its underflow.
n Rotation processing
Ø 檢查 left, right sibling node, 檢視其key 數目是否 > [m/2]-1, 如果左(右)sibling 符合,則將左(右)兄弟中的最
大(小)key 往parent node置放,且將parent 中的對應key 往下放到自己節點中。如此即完成
n Combination processing
Ø 從parent node中下拉一個key, 且與此node及其左(右)兄弟node中所有的keys 合併成一個新子點。此時parent
node 會少一個key. 故須對parent 檢查是否underflow
B TREE OF ORDER 3, DELETE 58, 55, AND 40
B+ TREE OF ORDER M
n 支援ISAM (index sequential access method)實施
n 分為兩大層
Ø Index Level: 採用 B tree of order m 結構。純粹作為索引用途,不存放資料
Ø Data blocks level: 用來存放資料之用,且各個data blocks 之間以Link list 方式串連。而data block 內的資料
數,可以依據 B tree 的定義或是自行制訂,不一定要與B tree 規則一致。依據題目規定而執行。
EXAMPLE Root
B+ tree of order 5 13 17 24 30
2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39*
Root
After inserting “8”
17
5 13 24 30
2* 3* 5* 7* 8* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39*
EXAMPLE Root
17
5 13 24 30
2* 3* 5* 7* 8* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39*
17
5 13 27 30
2* 3* 5* 7* 8* 14* 16* 22* 24* 27* 29* 33* 34* 38* 39*
EXAMPLE Root
17
5 13 27 30
2* 3* 5* 7* 8* 14* 16* 22* 24* 27* 29* 33* 34* 38* 39*
Root
5 13 17 30
5. For each node, all path from the node to descendant leaves
contain the same number of black nodes
• All path from the node have the same black height
因為RB tree is balanced BST, so the search/insert/delete/find-min(max)/find kth item operations are O(logn) time
INSERT X IN THE RB TREE (TOP-DOWN APPROACH)
n STEPS
搜尋 X 的適當插入位置
檢查有無連續的紅色節點(也就是X以及X的父點是否皆為紅色),如果是,則需執行Rotation 調整
最後,檢查ROOT 是否為黑。如果為紅色,則一律改成黑色。
n 差別
Ø 中間鍵值往上拉,標示黑色。小左大右兩個子點標紅色。
EXAMPLE: INSERT 15
補充: [ALGORITHM] BOTTOM-UP APPROACH FOR INSERTING X
1. 插入Z,找出適當插入位置,且Z標示紅色
2. 執行
RB TREE 高度最多 2*LOG(N+1)
n the number of black nodes on any simple path from, but not including, a node x down to a leaf the black-
height of the node, denoted bh(x)
CONT.
補充
另一種版本說法
EXAMPLE OF A RED-BLACK TREE
7 bh = 2
3 18 bh = 2
null null
bh = 1 10 22 h=4
null
bh = 1 8 11 26
null null null null null null
bh = 0
5. All simple paths from any node x, excluding
x, to a descendant leaf have the same
number of black nodes = black-height(x).
HEIGHT OF A RED-BLACK TREE
n 則一個BST 的搜尋總成本為
𝟏 𝟐𝒏
n 所以在 棵不同的BST 中,搜尋總成本最小者稱之為OBST
𝒏(𝟏 𝒏
Ø OBST 可能>=1棵
EXAMPLE
公式推導
TABLE RESULT
Q: Draw the OBST ?
OBST TIME=O(N3)
補充 OBST
n [CLRS algorithm版本]
Ø 失敗搜尋的成本定義與資結版本不同,演算法版本中,失敗結點的level 值沒有減一,即 qi * (失敗結點的level值)
n 有些版本只給予內部節點的加權值,沒有給予失敗結點加權值
n 另外,Tij 定義有的是代表 Ai,…Aj 之內部節點之OBST,並非Ai+1, …Aj , 所以公式要重新推導 一下。
資料結構
H.Y.
SEARCH AND SORT
大綱
n Search
Ø Linear Search
Ø Binary Search
n Sort
Ø 名詞解釋
l Internal Sort and External Sort
l Stable and Unstable sorting method
l Sorting in-place
Ø 初等排序
l Insertion, Selection, Bubble, Shell sort
大綱
Ø 高等排序
l Quick Sort, Merge Sort, Heap Sort
Ø 排序可以達多快?(在限定使用Comparison-base skill情況下):
𝛀(𝒏𝒍𝒐𝒈𝒏)
Ø 線性時間排序(Linear-Time sorting methods)
l Radix Sort, Buckets Sort, Counting Sort
n 演算法補充
Ø Find min&Max
Ø Selection problem (select the ith smallest item
among unsorted array with n items)
LINEAR (SEQUENTIAL) SEARCH
n 特色:
Ø 資料搜尋之前不需事先經過排序
Ø 如果小於中間位置資料值,則在左半部進行二分搜尋
Ø 如果大於中間位置資料值,則在右半部進行二分搜尋
n Sorting In-Place
n A sorting algorithm sorts in place if only a constant number of
elements of the input array are ever stored outside the array
n An in-place algorithm is an algorithm that does not need an extra
space and produces an output in the same memory that contains
the data by transforming the input ‘in-place’. However, a small
constant extra space used for variables is allowed.
n 高等以及初等排序方法中,只有Merge Sort 不是 sorting in –place, 其餘都
是
n Linear-time sorting methods 也不是 sorting in-place
初等排序方法
nInsertion sort
nSelection sort
nBubble Sort
nShell Sort
INSERTION SORT
1. void insert(int arr[], int r, int i) 1. void insertionSort(int arr[], int n)
2. { 2. {
3. arr[-1]=r; 3. for (int i=1; i<n; i++)
4. int j=i; 4. insert(arr, arr[i], i-1);
5. while (r<arr[j]) 5.
6. }
6. {
7. arr[j+1]=arr[j];
8. j--;
9. }
10. arr[j+1]=r;
11.}
INSERTION SORT 分析
if (i != min)
swap(&arr[min], &arr[i]);
}
}
SELECTION SORT ANALYSIS
空間複雜度 O(1)
Unstable
適用於大型紀錄 因為每一回合頂
排序 多SWAP 一次
1. void bubbleSort(int arr[], int n)
BUBBLE SORT
2. {
3. int i, j, flag;
4. for (i = 0; i < n-1; i++)
5. {
6. flag=0;
7. for (j = 0; j < n-i-1; j++)
8. if (arr[j] > arr[j+1])
9. {
10. swap(&arr[j], &arr[j+1]);
11. flag=1;
12. }
13. if (flag==0) break; // no swap happen in this phase
14. }
15. }
BUBBLE SORT 分析
nQuick sort
nMerge Sort
nHeap sort
QUICK SORT
n 平均狀況下,執行時間最快的排序方法
n 觀念
Ø 選擇一個pivot key (the leftmost or the rightmost key)
Ø 兩邊都排序好,則整個資料也就排序好
n Partition 方法
Ø 資料結構版:採用 [Hoare] partition, use the leftmost key as pk
Ø 法二:middle of three
l 假設array 的index 為left to right,
l Middle =(left+right)/2;
n Decision tree for sorting comparison behavior (see the following diagram)
3個資料(K1, K2, K3)的排序比較之DECISION TREE
證明
MERGE SORT
n External sorting 常用方法之一
n 基本術語
Ø Run: 排序好的資料片段
Ø Run 長度:Run 中資料個數
n 分為兩種版本:
Ø Iterative version
Ø Recursive version : 採取divide-and-conquer approach
Time: O(n-l+1)
EXAMPLE: TWO-WAY MERGE SORT (ITERATIVE VERSION)
EXAMPLE: RECURSIVE MERGE SORT
RECURSIVE MERGE SORT ALGORITHM
MERGE SORT 分析
時間複雜度 Best case Worst case Average case
O(nlogn) O(nlogn) O(nlogn)
T(n)=2T(n/2)+cn
空間複雜度 O(n)
Stable
NOT sorting in-place
SELECTION TREE
n 目的:協助k-way merge (K個runs 合併成一個run) 加速合併過程
n 傳統方法:
Ø 要準備k 個指標變數,每一次最多進行(k-1)次比較,從k 個runs 中找出最小值,輸出到新的run, 最多進行(n-1)回合,
所以,花費O(n*k) 的時間合併k 個runs成一個run.
n 使用selection tree
Ø 建樹:花費 O(K)的時間
Ø Total: O(k)+O(n*logk)
Ø Loser tree
Winner
HEAP SORT
HEAP SORT (CONT.)
EXAMPLE
HEAP SORT 分析
時間複雜度 Best case Worst case Average case
O(nlogn) O(nlogn) O(nlogn)
空間複雜度 O(1)
Unstable
資料結構版本 演算法版本
LSD Radix Sort Radix Sort
MSD Radix Sort Bucket Sort
Counting Sort Counting sort
n 由最低位數值到最高位數值,執行 d 回合,每回合執行下列動作
Ø 分派:依據各資料的位數值,將資料分派到對應的桶子中
Pass1: 個位數
Pass2: 十位數
Pass3: 百位數
LSD RADIX SORT 分析
An important property of counting sort is that it is stable: numbers with the same
value appear in the output array in the same order as they do in the input array.
Counting sort’s stability is important for another reason: counting sort is often used as a
subroutine in radix sort.
SELECTION PROBLEM
n Find min&max
n Select ith smallese item among unsorted n data