cse 326 data structures lecture 3 mind your priority qs
play

CSE326:DataStructures Lecture#3 MindyourPriorityQs BartNiswonger - PDF document

CSE326:DataStructures Lecture#3 MindyourPriorityQs BartNiswonger SummerQuarter2001 TodaysOutline TheFirstQuiz! ThingsBartDidntGettoonWednesday


  1. CSE�326:�Data�Structures Lecture�#3 Mind�your�Priority�Qs Bart�Niswonger Summer�Quarter�2001 Today’s�Outline • The�First�Quiz!� • Things�Bart�Didn’t�Get�to�on�Wednesday� (Priority�Queues�&�Heaps) • Extra�heap�operations • d-Heaps 1

  2. Back�to�Queues • Some�applications – ordering�CPU�jobs – simulating�events – picking�the�next�search�site • Problems? – short�jobs� should�go�first – earliest�(simulated�time)�events� should�go�first – most�promising�sites� should�be�searched�first Priority�Queue�ADT • Priority�Queue�operations – create F(7) E(5) – destroy deleteMin insert G(9) D(100) A(4) C(3) – insert B(6) – deleteMin – is_empty • Priority�Queue�property:�for�two�elements�in� the�queue,� x and� y ,�if� x has�a�lower�priority� value than� y ,� x will�be�deleted�before� y 2

  3. Applications�of�the�Priority�Q • Hold�jobs�for�a�printer�in�order�of�length • Store�packets�on�network�routers�in� order�of�urgency • Simulate�events • Select�symbols�for�compression • Sort�numbers • Anything� greedy Naïve�Priority�Q�Data�Structures • Unsorted�list: – insert: – deleteMin: • Sorted�list: – insert: – deleteMin: 3

  4. Binary�Search�Tree�Priority�Q� Data�Structure�(that’s�a�mouthful) 8 insert: 5 11 deleteMin: 2 6 10 12 4 7 9 14 13 Binary�Heap�Priority�Q�Data� Structure • Heap-order�property – parent’s�key�is�less�than� 2 children’s�keys – result:�minimum�is� always�at�the�top 4 5 • Structure�property – complete�tree�with�fringe� 7 6 10 8 nodes�packed�to�the�left – result:�depth�is�always� O(log�n);�next�open� 11 9 12 14 20 location�always�known 4

  5. Nifty�Storage�Trick 1 2 • Calculations: – child: 2 3 4 5 – parent: 4 7 5 6 7 6 10 8 – root: 8 9 11 9 12 14 20 – next�free: 12 10 11 0 1 2 3 4 5 6 7 8 9 10 11 12 12 2 4 5 7 6 10 8 11 9 12 14 20 DeleteMin pqueue.deleteMin() 2 2 ? 4 5 4 5 7 6 10 8 7 6 10 8 11 9 12 14 20 11 9 12 14 20 5

  6. Percolate�Down ? 4 4 5 ? 5 7 6 10 8 7 6 10 8 11 9 12 14 20 11 9 12 14 20 4 4 6 5 6 5 7 ? 10 8 7 12 10 8 11 9 12 14 20 11 9 20 14 20 Finally… 4 6 5 7 12 10 8 11 9 20 14 6

  7. DeleteMin Code int�percolateDown(int�hole,�Object�val)�{ while�(�2�*�hole�<=�size�)�{ Object�deleteMin()�{ left�=�2�*�hole;� assert(!isEmpty()); right�=�left�+�1; returnVal =�Heap[1]; if�(�right�<=�size�&&� Heap[right]�<�Heap[left]) size--; target�=�right; newPos�=� else percolateDown(1, target�=�left; Heap[size+1]); if�(�Heap[target]�<�val )�{ Heap[newPos]�=� Heap[hole]�=�Heap[target]; Heap[size�+�1]; hole�=�target; } return�returnVal; else } break; } runtime: return�hole; } Insert pqueue.insert(3) 2 2 4 5 4 5 7 6 10 8 7 6 10 8 11 9 12 14 20 11 9 12 14 20 ? 7

  8. Percolate�Up 2 2 4 5 4 5 3 7 6 10 8 7 6 ? 8 3 11 9 12 14 20 ? 11 9 12 14 20 10 2 2 3 4 ? 4 3 7 6 5 8 7 6 5 8 11 9 12 14 20 10 11 9 12 14 20 10 Insert�Code void�insert(Object�o)�{ int�percolateUp(int�hole,� Object�val)�{ assert(!isFull()); while�(hole�>�1�&& size++; val�<�Heap[hole/2]) newPos�= Heap[hole]�=�Heap[hole/2]; hole�/=�2; percolateUp(size,o); } Heap[newPos]�=�o; return�hole; } } runtime: 8

  9. Changing�Priorities • In�many�applications�the�priority�of�an�object� in�a�priority�queue�may�change�over�time – if�a�job�has�been�sitting�in�the�printer�queue�for�a� long�time�increase�its�priority – unix�“renice” • Must�have�some�(separate)�way�of�find�the� position�in�the�queue�of�the�object�to�change� ( e.g. a�hash�table) Other�Priority�Queue� Operations • decreaseKey� – given�the�position�of�an�object�in�the�queue,� reduce�its�priority�value • increaseKey – given�the�position�of�an�an�object�in�the�queue,� increase�its�priority�value • remove – given�the�position�of�an�object�in�the�queue,� remove�it • buildHeap – given�a�set�of�items,�build�a�heap 9

  10. DecreaseKey,�IncreaseKey,� and�Remove void�decreaseKey(int obj)�{ void�remove(int�obj)�{ assert(size�>= obj); assert(size�>=�obj); percolateUp(obj,� temp�=�Heap[obj]; NEG_INF_VAL); deleteMin(); newPos�= percolateUp(obj,�temp); } Heap[newPos]�=�temp; } void�increaseKey(int�obj)�{ assert(size�>=�obj); temp�=�Heap[obj]; newPos�=�percolateDown(obj,�temp); Heap[newPos]�=�temp; } BuildHeap Floyd’s�Method.�Thank�you,�Floyd. 12 5 11 3 10 6 9 4 8 1 7 2 pretend�it’s�a�heap�and�fix�the�heap-order�property! 12 5 11 3 10 6 9 4 8 1 7 2 10

  11. Build(this)Heap 12 12 5 11 5 11 3 10 2 9 3 1 2 9 4 8 1 7 6 4 8 10 7 6 12 12 5 2 1 2 3 1 6 9 3 5 6 9 4 8 10 7 11 4 8 10 7 11 Finally…� 1 3 2 4 5 6 9 12 8 10 7 11 runtime: 11

  12. Thinking�about�Heaps • Observations – finding�a�child/parent�index�is�a�multiply/divide�by� two – operations�jump�widely�through�the�heap – each�operation�looks�at�only�two�new�nodes – inserts�are�at�least�as�common�as�deleteMins • Realities – division�and�multiplication�by�powers�of�two�are� fast – looking�at�one�new�piece�of�data�sucks�in�a�cache� line – with� huge data�sets,�disk�accesses�dominate Solution:�d-Heaps • Each�node�has� d children 1 • Still�representable�by� 3 7 2 array • Good�choices�for� d : 4 8 5 12 11 10 6 9 – optimize�performance�based�on� #�of�inserts/removes – choose�a�power�of�two�for� 12 1 3 7 2 4 8 5 121110 6 9 efficiency – fit�one�set�of�children�in�a�cache� line – fit�one�set�of�children�on�a� memory�page/disk�block 12

  13. One�More�Operation • Merge�two�heaps.�Ideas? To�Do • Read�chapter�6�in�the�book • Have�teams • Do�project�1 • Ask�questions! 13

  14. Coming�Up • Mergeable�heaps • Dictionary�ADT�and�Self-Balancing� Trees • Unix�Development�Tutorial�(Tuesday) • First�project�due�(next�Wednesday) 14

Recommend


More recommend