Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1

2 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 with minor wounds, even though he arrives later. Printer queue: less page should be selected to print first. But this does not happen in real life., 2

3 Priority Normally, small value is regarded as more important. If two item have equal priority, we may allow the item that exists longer in the queue to be more important. To compare elements, we use Comparable interface or Comparator Interface. 3

4 Comparator Interface Has method: compare(Object o1, Object o2) Has to be the same class or one is the subclass of the other. Return negative value if o1 is less than o2, positive value if o1 is more than o2, and 0 otherwise. 4

5 The interface for heap // Postcondition: return the number of items in this priority queue. // Postcondition: return true if this priority queue does not store any item, otherwise return false. // Postcondition: element is added to priority queue. 5

6 // Precondition: must not be an empty queue, otherwise it throws NoSuchElementException. // Postcondition: return the most important object from this PriorityQueue. // Precondition: must not be an empty queue, otherwise it throws NoSuchElementException. // Postcondition: remove and return the most important object from this PriorityQueue. 6

7 Many implementation choices exist Normal queue: no priority then? Array of queue (each queue for each priority): too much space to prepare if there are lots of possible priority. 7

8 Use ArrayList Easy to put members in order right away. Takes time to find a position to insert though. Many array elements have to be shifted after insertion or deletion. getMin is constant time because we can return the first array element right away. Add and remove take linear time, which is rather slow. We want better time. 8

9 Use LinkedList getMin and removeMin take constant time bacause we can return the front of the list right away. Add takes linear time, still need to search the position to add, but does not need element shifting. Remove takes constant time because we can just update pointers. 9

10 Use Comparable by default Linked List priority queue 10

11 public LinkedPQ (Comparator comp) { this(); comparator = comp; } // constructor with Comparator parameter public int size( ) { return list.size( ); } // method size public Object getMin ( ) { return list.getFirst( ); } // method getMin List methods in this chapter comes from LinkedList class in Java. 11

12 public Object removeMin( ) { return list.removeFirst( ); } // method removeFirst 12

13 why? 13

14 1542 3 Wanna add this When loop fails itr.next() returns 4 and it moves. itr.add(element): The element is inserted immediately before the next element that would be returned by next, if any. So using itr.add(element) will insert in the wrong place. Therefore we need to call previous(). 14

15 protected int compare (Object elem1, Object elem2) { return (comparator==null ? ((Comparable)elem1).compareTo(elem2) : comparator.compare (elem1, elem2)); } // method compare If comparator = null, return elem1.compareTo (elem2) ; Otherwise, return comparator.compare (elem1, elem2) ; 15

16 An implementation using set Real structure is a tree. Use comparator too. 16

17 public void add (Object element) { set.add (element); } // method add public Object getMin ( ) { return set.first( ); } // method getMin public Object removeMin ( ) { Object temp = set.first( ); set.remove (set.first( )); return temp; } // method removeMin worstTime(n) for all these methods = O(log n). 17

18 Implement using real Heap public class Heap implements PriorityQueue { 18

19 Complete Binary tree 19

20 Heap is a complete binary tree It is either an empty tree, or A tree that has its minimum value in its root. (this kind of heap is called minHeap) Left and right subtree have to be heaps too. Heap is not a binary search tree! 20

21 Smallest value is at the root for every subtree. 21

22 26 40 31 48 50 85 36 107 48 55 57 88 Complete binary tree can be put into array It is easy to find child, parent. This is actually the tree from the previous page. 22

23 First, how to use Heap (smallest GPA is most important.) 23

24 24

25 25

26 // constructor receives string as input. toString should be implemented. // Postcondition: return integer that 0, depending on student’s GPA. 26

27 Let us see the class for heap. Actual implementation is array. 27

28 Index of a node’s child 28

29 Index of a node’s parent 29

30 Method of a Heap 30

31 Resize array, this is O(n) //add element to the back. // Postcondition: element is added to the heap. // worst case time = O(n) // average time = constant The real operation. Depends on avarage time of percolateUp 31

32 Let us add 30 30 32

33 Percolate up is Swapping 30 with its parent until parent has less value. We will get a heap when we finish. 33

34 Let us follow the movement. 34

35 We finally get a heap at the end of all swaps. 35

36 protected void percolateUp() { int parent; int child = size-1; Object temp; while (child>0) { parent = (child-1)/2; if(compare(heap[parent],heap[child]) <=0) break; temp = heap[parent]; heap[parent] = heap[child]; heap[child] = temp; child = parent; } // while } // method percolateUp Depend on the height of the tree. Worst case happens when we need to swap up to the root. // Postcondition: move the last element up the tree. The worst time is O(log n). The average time is constant. 36

37 Average time -percolateUp Let the new value be around the middle of existing values. Half array elements have greater value. This is a complete binary tree, therefore this half are leaves. Therefore we need to swap only once to get our value to the right place. This is why it is said to take a constant time. 37

38 public Object getMin() { if (size == 0) throw new NoSuchElementException( “ Empty ” ); return heap[0]; } // method getMin 38

39 removeMin() – Be careful! 4 6 5 8 4 6 8 5 The tree may no longer be a complete binary tree. 39

40 //put the last element into the root. reduce size to prevent access of the array’s back // Precondition: must not be an empty queue or it throws NoSuchElementException. // Postcondition: remove the most important value from this queue. This object is returned as pur answer. // worst time = O(log n). Don’t need to do the exact swap. 40

41 107 48 when removeMin( ) is called, 48 will swap place with 26. Size will be reduced by 1. after that, percolateDown will be called. 26 30 31 32 50 85 36 41

42 PercolateDown is Swapping 48 with its smallest child. We do it until no more swap can be done. We will get a legal heap in the end. 42

43 43

44 44

45 Now we finally get our heap. 45

46 protected void percolateDown(int start) { int parent = start; int child = 2*parent+1; Object temp; while (child<size) { if(child 0) child++; if (compare(heap[parent],heap[child]) <=0) break; temp = heap[child]; heap[child] = heap[parent]; heap[parent] = temp; parent = child; child= 2*parent+1; } // while } // method percolateDown If right child is smaller, change our “child”. Initially, let “child” be the left child. Child can’t be the last element because there will be no right child to compare. // Postcondition: heap arranged by moving the top element down the tree. Worst and average time is O(log n). 46

47 Try this and print the heap content. See if it is what you expected. 47

48 48

49 Let ’ s look Let ’ s say we want to compress file without losing any information Let M be the data that we want to compress. Let its size be 100000 characters. M contains only character a to character e. Let E be M after compression. 49

50 Normally, 1 character needs 16 bit to store. 100000 character needs 100000x16 bits. It is useful to know how to reduce the number of bits while maintaining the same information. 50

51 Try this The number of bits reduce to 300,000 51

52 Can it be reduced even more? The number of bits now <=300,000 But 001010 can mean either Aababa or cbda. Rather ambiguous. 52

53 How do we prevent ambiguity use prefix-free encoding. By creating a binary tree Left branch is 0. Right branch is 1. Character is at the leaves. 53

54 Therefore, there is no two characters on the same path (from root) a = 010 b = 11 c = 00 d = 10 e = 011 54

55 alternatives? Which scheme is best? Ans: frequently appear character should have small number of bits. 55

56 Create Huffman tree This is a binary tree of codes. but it is built using the frequency of characters in the message. Characters with high frequency will take less bits. 56

57 let us use the following example. A has 5000 B has 10000 c has 20000 d has 31000 e has 34000 57

58 Put pair (character, frequency) into Priority Q Priority Q that we use, we will have the lowest frequency on top. Each node of our priority Q has left, right, parent of its own. In our example, the initial queue is (a:5000) (b: 10000) (c: 20000) (d: 31000) (e: 34000) Now let us build huffman tree. 58

59 Remove the two items with smallest frequency from the queue. The first one becomes the left branch, the second one becomes the right branch. The sum of their frequency becomes the root. a b 15000 Then we put the result back into the priority queue. The queue will now be ( :15000) (c: 20000) (d: 31000) (e: 34000) 0 1 A tree here 59

60 Remove the two front elements and do the same again. a b 15000 c 35000 0 1 0 1 When we put this tree back into the queue, we get -> (d: 31000) (e: 34000) ( : 35000) 60

61 Remove the two front elements and do the same again. d e 65000 0 1 This becomes a new tree. When we put this tree back into the queue, we get -> ( : 35000) ( : 65000) 61

62 We do the same again. priority Q will become (: 100000) a b 15000 c 100000 0 1 0 1 d e 65000 0 1 35000 0 1 62

63 Try creating a tree with this example. 63

64 Huffman class example Change input to huffman code. See next page. Sample code here is from Collin’s Java Collection Framework book. You don’t have to remember it. Just take it as an idea. 64

65 65

66 Array of leaves For quick access 66

67 Example a node for ‘c’ This is one entry Anything that is not a leaf is not in leafEntries. 67

68 // Postcondition: ‘ This ’ Huffman object is initialized. // Postcondition: file (identified by s) is dealt with. // Postcondition: priority queue is created from input file. worst case = O(n) 68

69 // Postcondition: build huffman tree. worst case = constant // Postcondition: create huffman code of each item. // worst case time is constant. // Postcondition: Huffman codes and all data are saved. worst case time = O (n). 69

70 Fields in the Huffman class Gui priority queue file reader file writer input file name (saved to allow file opening more than once.) Calculate frequency Encode data boolean telling us which file we are reading. 70

71 71

72 Method processInput Create priority Q. Build Huffman tree. Create code. save code and the real message into file. 72

73 Method createPQ Create leafEntries ใ update freq field of each Entry After that, create priority queue using freq. 73

74 Method createHuffmanTree Loop until the size of priority queue is 1. Create the tree. 74

75 Method calculateHuffmanCodes Loop on leafEntries For each Entry: There is a variable which is empty string. Look at parent and then fill in the number indicating parent in front of the string, until the root is reached. 75

76 Example code building Starting with this tree. See next page. 76

77 Code of f f has its int = = 102, so we set leafEntries[102].code = “ 1010 ” ; Loop is reversing 77

78 Method saveToFile Loop on leafEntries Send character and its code to a file. Then read the input file Code of input is created and saved into the file. 78

79 See all the code here 79

80 Try building a huffman tree from this and reveal the message. 80


Download ppt "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."

Similar presentations


Ads by Google