1 Chapter 13 Priority Queues. 2 Chap.13 Contents 13.1 Introduction 13.2 The PurePriorityQueue Interface 13.3 Implementations of the PurePriorityQueue.

Slides:



Advertisements
Similar presentations
FCU, Department of ECE, IC Design Research Lab. TEL: # 4945 Pre-SIm , Post-Sim.
Advertisements

1 生物計算期末作業 暨南大學資訊工程系 2003/05/13. 2 compare f1 f2  只比較兩個檔案 f1 與 f2 ,比完後將結果輸出。 compare directory  以兩兩比對的方式,比對一個目錄下所有檔案的相 似程度。  將相似度很高的檔案做成報表輸出,報表中至少要.
Divide-and-Conquer. 什麼是 divide-and-conquer ? Divide 就是把問題分割 Conquer 則是把答案結合起來.
6-1 指標簡介 6-2 指標與陣列 6-3 動態配置記憶體 6-4 本章綜合練習
: Arrange the Numbers ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11481: Arrange the Numbers 解題者:李重儀 解題日期: 2008 年 9 月 13 日 題意: 將數列 {1,2,3, …,N}
布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
Introduction to Java Programming Lecture 13 Classes I OO Programming.
建立使用案例敘述 --Use Case Narrative
第七章 抽樣與抽樣分配 蒐集統計資料最常見的方式是抽查。這 牽涉到兩個問題: 抽出的樣本是否具有代表性?是否能反應出母體的特徵?
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
1 Q10276: Hanoi Tower Troubles Again! 星級 : ★★★ 題組: Online-judge.uva.es PROBLEM SET Volume CII 題號: Q10276: Hanoi Tower Troubles Again! 解題者:薛祖淵 解題日期: 2006.
Instructor: Ching-Chi Lin 林清池 助理教授
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章.
序列分析工具:MDDLogo 謝勝任 林宗慶 指導教授:李宗夷 教授.
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
指導教授:陳淑媛 學生:李宗叡 李卿輔.  利用下列三種方法 (Edge Detection 、 Local Binary Pattern 、 Structured Local Edge Pattern) 來判斷是否為場景變換,以方便使用者來 找出所要的片段。
1.1 線性方程式系統簡介 1.2 高斯消去法與高斯-喬登消去法 1.3 線性方程式系統的應用(-Skip-)
Review of Chapter 3 - 已學過的 rules( 回顧 )- 朝陽科技大學 資訊管理系 李麗華 教授.
建立靜態資料成員 關鍵字: static 靜態資料成員不屬於特定物件,而是該類物件 所共同擁有的,可做為類別範圍裡的靜態變數。
Graph V(G 1 )={0, 1, 2, 3, 4, 5, 6, 7, 8, 9} E(G 1 )={(0, 2), (0, 3), (1, 4), (2, 3), (2, 5), (2, 6), (3, 6), (3, 7), (4, 7), (5, 6), (5,
:New Land ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11871: New Land 解題者:施博修 解題日期: 2011 年 6 月 8 日 題意:國王有一個懶兒子,為了勞動兒子,他想了一個 辦法,令他在某天早上開始走路,直到太陽下山前,靠.
: OPENING DOORS ? 題組: Problem Set Archive with Online Judge 題號: 10606: OPENING DOORS 解題者:侯沛彣 解題日期: 2006 年 6 月 11 日 題意: - 某間學校有 N 個學生,每個學生都有自己的衣物櫃.
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
JAVA 程式設計與資料結構 第十四章 Linked List. Introduction Linked List 的結構就是將物件排成一列, 有點像是 Array ,但是我們卻無法直接經 由 index 得到其中的物件 在 Linked List 中,每一個點我們稱之為 node ,第一個 node.
程式註解說明. 2 程式註解格式 塊狀註解 對檔案、 class 、 method 、資料結構、一段程式 …. 等程式區塊 做說明。 第一行的開頭必需為 “/*” 且沒有其他文字,最後一行的開頭 必需以 “*/” 做為結束,在中間每一行的開頭都必需是一個 “*” 。 單行註解 佔據一整行的說明。 以.
8.1 何謂高度平衡二元搜尋樹 8.2 高度平衡二元搜尋樹的加入 8.3 高度平衡二元搜尋樹的刪除
: The Playboy Chimp ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10611: The Playboy Chimp 解題者:蔡昇宇 解題日期: 2010 年 2 月 28 日 題意:給一已排序的數列 S( 升冪.
具備人臉追蹤與辨識功能的一個 智慧型數位監視系統 系統架構 在巡邏模式中 ,攝影機會左右來回巡視,並 利用動態膚色偵測得知是否有移動膚色物體, 若有移動的膚色物體則進入到追蹤模式,反之 則繼續巡視。
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
1 Chapter 8 Stacks and Queues. 2 This is a stack of books.
第二章 供給與需求 中興大學會計學系 授課老師:簡立賢.
: Happy Number ★ ? 題組: Problem Set Archive with Online Judge 題號: 10591: Happy Number 解題者:陳瀅文 解題日期: 2006 年 6 月 6 日 題意:判斷一個正整數 N 是否為 Happy Number.
: Fast and Easy Data Compressor ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10043: Fast and Easy Data Compressor 解題者:葉貫中 解題日期: 2007 年 3.
Chapter 2 Getting Started Insertion Sort: 能有效率地排序小數字的演算法 範例 :
第十章 再論結構. 指標與結構的關係 /* File name: ex10-1a.c */ #include int main() { struct student { char *name; int score; }; struct student st= {"Brian", 97}; struct.
: Multisets and Sequences ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11023: Multisets and Sequences 解題者:葉貫中 解題日期: 2007 年 4 月 24 日 題意:在這個題目中,我們要定義.
:Nuts for nuts..Nuts for nuts.. ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10944:Nuts for nuts.. 解題者:楊家豪 解題日期: 2006 年 2 月 題意: 給定兩個正整數 x,y.
資料結構實習-一 參數傳遞.
Deletion in MIN-MAX Heaps Delete min element Delete max element.
Lecture 7 Sorting in Linear Time. Sorting in Linear Time2 7.1 Lower bounds for sorting 本節探討排序所耗用的時間複雜度下限。 任何一個以比較為基礎排序的演算法,排序 n 個元 素時至少耗用 Ω(nlogn) 次比較。
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
4 堆疊與佇列 4.1 前言 四種基本的資料結構 (可儲存資料的容器) 陣列 (Array)、串列(List): 最基本
函式 Function Part.2 東海大學物理系‧資訊教育 施奇廷. 遞迴( Recursion ) 函式可以「呼叫自己」,這種動作稱為 「遞迴」 此程式的執行結果相當於陷入無窮迴圈, 無法停止(只能按 Ctrl-C ) 這給我們一個暗示:函式的遞迴呼叫可以 達到部分迴圈的效果.
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
資料結構實習-二.
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
Review of Chapter 9 張啟中. Inheritance Hierarchy Min PQ Max PQ Min Heap Mergeable Min PQ DeapMin-Max Min-LeftistMin-SkewMinFHeap MinBHeap DEPQMax Heap Symmetric.
-Antidifferentiation- Chapter 6 朝陽科技大學 資訊管理系 李麗華 教授.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
資料結構實習-十 Binary Tree Traversal.
Chapter 10 m-way 搜尋樹與B-Tree
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
中序轉後序 藉由由左向右掃瞄中序運算式產生後序運算式,遇到 運算元就直接輸出,遇到運算符號則先存入堆疊,將 優先權較高者輸出。 範例: a + b * c TokenStack [0] [1] [2] topoutput aa ++0a b+0ab *+ *1ab c+ *1abc eosabc*+
: Problem E Antimatter Ray Clearcutting ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11008: Problem E Antimatter Ray Clearcutting 解題者:林王智瑞.
Teacher : Ing-Jer Huang TA : Chien-Hung Chen 2015/6/30 Course Embedded Systems : Principles and Implementations Weekly Preview Question CH7.1~CH /12/26.
資料結構實習-六.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
: SAM I AM ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11419: SAM I AM 解題者:李重儀 解題日期: 2008 年 9 月 11 日 題意: 簡單的說,就是一個長方形的廟裡面有敵人,然 後可以橫的方向開砲或縱向開砲,每次開砲可以.
What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;
數字系統與資料表示法 教師: 陳炯勳 數系轉換 r進制數字 稱為 base r或 radix r 有r個計數符號,計數順序逢r歸零(進位) A n A n - 1 ‥‥A 2 A 1 A 0 ﹒A -1 A -2 ‥‥A -m 其中A n 及A.
Introduction to Java Programming Lecture 16 Super and Sub Classes.
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
著作權所有 © 旗標出版股份有限公司 第 14 章 製作信封、標籤. 本章提要 製作單一信封 製作單一郵寄標籤.
幼兒行為觀察與記錄 第八章 事件取樣法.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
Data Structures -3 rd test- 2015/06/01 授課教授:李錫智. Question 1 Suppose we use an array to implement a sorted list. Please answer the following questions.
6.1.3 Graph representation.
1. What is it? It is a queue that access elements according to their importance value. Eg. A person with broken back should be treated before a person.
6.1.3 Graph representation.
Presentation transcript:

1 Chapter 13 Priority Queues

2 Chap.13 Contents 13.1 Introduction 13.2 The PurePriorityQueue Interface 13.3 Implementations of the PurePriorityQueue Interface The Heap Implementation of the PurePriorityQueue Interface 13.4 Application: Huffman Codes

Introduction A priority queue (PQ) is an interface (property) in which access or deletion is of: the highest-priority element, according to some method of assigning priorities to elements.

4

5

6

The PurePriorityQueue Interface public interface PriorityQueue { /** * size 回傳 PurePriorityQueue 中元素的個數 */ int size ( )

8 /* isEmpty 看 PurePriorityQueue * 是否沒有元素. * * return true – if PurePriorityQueue * 沒有元素 ; * otherwise, return false ; */ boolean isEmpty ( )

9 /** * add 加元素到 PurePriorityQueue. * * The worstTime(n) is O(n). * element – 要插入 PurePriorityQueue 的元素 */ void add (E element)

10 /* getMin * 回傳 PurePriorityQueue 中的最高優先權元素 * * The worstTime(n) is O (1). * PurePriorityQueue 的最高優先權元素 * NoSuchElementException – * if PurePriorityQueue 是空的 */ E getMin ( )

11 /* removeMin * 從 PurePriorityQueue 移除具有最高優先權的元素 * * The worstTime(n) is O (log n). * 被移除的元素 * NoSuchElementException – * if PurePriorityQueue 是空的 */ E removeMin ( )

Implementations of The PurePriorityQueue Interface 有三種 data structures 可實 作之,分別是 : 1) linked list 2) tree set 3) heap

1. Use Linked List 13

14 public class LinkedPQ impelments PurePriorityQueue { LinkedList list ; Comparator comparator ;

15 public LinkedPQ() {list = new LinkedList ( ) ; comparator = null ; } public LinkedPQ (Comparator comp) {this() ; comparator = comp ;} public int size() { return list.size() ; } public E getMin() { return list.getFirst() ; } public E removeMin(){ return list.removeFirst() ;}

16

17 public void add (E element){ if /* 空或 element 優先權比最後的大 */ (list.isEmpty( ) || compare (element, list.get (list.size( ) – 1)) >= 0) /* 直接 add 此 element */ list.add (element) ; else {/* 找適當位置 add 使優先權由小到大 */ ListIterator itr = list.listIterator( ); while (itr.hasNext() && compare (element, itr.next( )) >= 0) ; /* 倒退一位, 再 add*/ itr.previous( ) ; itr.add (element) ; }} //end of add WorstTime (n) is linear in n.

18

19 2. Use Tree Set

20 public class TreeSetPQ implements PriorityQueue { TreeSet set; Comparator comparator; // the 2 constructors, size, isEmpty and compare // methods 與 LinkedPQ class 的類似 public TreeSetPQ (Comparator c) { comparator = c ; set = new TreeSet (c); } // one-parameter constructor

21 public void add (E element) {set.add (element);} public E getMin ( ) {return set.first( );} public E removeMin ( ) {E temp = set.first( ); set.remove (set.first( )); return temp;} } // end of class TreeSetPQ For these three methods, worst time (n) is logarithmic in n.

3. Use Heap 22

23

24 Heap [Collins English Dictionary] A collection of articles or mass of material gathered together in one place. 這定義用於 compiler, OS. Ex: Heap storage ( 堆 ) vs. stack storage ( 疊 ) in main memory.

25 但是, Data Structure 中的 Heap , 有不同的定義 : Heap 是一棵 tree , 且任何一個節點比 所有 descendants ( 後代 ) 都小

26

27

28

29 heap 不是 binary search tree! 因為對 root 而言 binary search tree 是 左小右大 heap 則是上 (root) 小 下 ( 左右 ) 大 這叫 min heap (minimal element at root) 另有 max heap (maximal element at root) 則是下 ( 左右 ) 小 上 (root) 大

30

31

32 下列的學生分數程式,創出 Heap 結構 , 並執行 Heap 動作 : add 和 removeMin 請輸入學生姓名及 GPA 或 ***( 結束 )”; Mary 4.0 (red indicates user input) John 3.5 *** 系統輸出如下 John 3.5 Mary 4.0

33 public static void main(String[] args){ final String PROMPT= “ 請輸入學生姓名及 GPA 或 ***( 結束 )”; final String RESULTS =“\n 學生姓名及 GPA 如下 ” ; String line ; Heap heap = new Heap ( ) ; BufferedReader keyboardReader = new BufferedReader (new InputStreamReader(System.in) ) ; try{ while(true) {System.out.print (PROMPT) ; line = keyboardReader.readLine() ; if (line.equals(“***”)) break ; heap.add (new Student(line) );} //while system.out.println (RESULTS); while(!heap.isEmpty()) System.out.println (heap.removeMin() ) ; }//try catch(Exception e){ System.out.println(e) ;} } // end of main

34 import java.util.* ; public class Student implements Comparable { protected String name ; protected double gpa ; /** Student 從特定的 String s 初始化 Student object. * s – String 初始化 Student object. NullPointerException, NoSuchElementException, * NumberFormatException */ public Student (String s){ StringTokenizer tokens = new StringTokenizer(s) ; name = tokens.nextToken() ; gpa = Double.parseDouble( tokens.nextToken() ) ; } // constructor public String toString() {return name+“ “+ gpa;} } // end of class Student

The Heap Implementation of the PurePriorityQueue Interface

36

37

38

39

40

41

42

43

44 Protected void percolateUp(){ // 設定 child 為 最後一個 node (size -1) int child = size -1; int parent; Object temp ; while(child > 0){ parent = (child-1)/2; // 如果 parent <= child 不用做了 if(compare(heap[parent],heap[child] <= 0) break; // 否則 swap parent 與 child temp=heap[parent];heap[parent]=heap[child];heap[child]=temp; /*child 向上走一步 */ child = parent; }//end while } //end of percolateUp

45

46

47

48

49

50 Protected void percolateDown(int start){ // parent 為 tree 的 root (index 0 ) // child 為 root 的左子樹 int parent = start, child = 2*parent + 1; Object temp ; while(child < size){ // 如 child 有右兄弟 且 右兄弟較小 則設定 child 為右兄弟 if(child < size – 1 && compare(heap[child],heap[child+1])> 0) child ++; // 如 parent <= child 不用再比了 if(compare(heap[parent],heap[child] <= 0) break; // 否則 swap parent 與 child temp = heap[child]; heap[child] = heap[parent];heap[parent] = temp; // child 向下走一步 parent = child; child = 2 * parent +1; }//while } // end of percolateDown

51

實際儲存於 array: index: 0 1 2

Application: Huffman Codes

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

The Huffman Encoding Project

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112