Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Queues 4/14/2017 5:26 PM Queues Queues.

Presentasi serupa


Presentasi berjudul: "Queues 4/14/2017 5:26 PM Queues Queues."— Transcript presentasi:

1 Queues 4/14/2017 5:26 PM Queues Queues

2 The Queue ADT (§4.3) Auxiliary queue operations: Exceptions
Queues 4/14/2017 5:26 PM The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front of the queue Main queue operations: enqueue(object): inserts an element at the end of the queue object dequeue(): removes and returns the element at the front of the queue Auxiliary queue operations: object front(): returns the element at the front without removing it integer size(): returns the number of elements stored boolean isEmpty(): indicates whether no elements are stored Exceptions Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException Queues

3 Queue Example Operation Output Q enqueue(5) – (5) enqueue(3) – (5, 3)
dequeue() 5 (3) enqueue(7) – (3, 7) dequeue() 3 (7) front() 7 (7) dequeue() 7 () dequeue() “error” () isEmpty() true () enqueue(9) – (9) enqueue(7) – (9, 7) size() 2 (9, 7) enqueue(3) – (9, 7, 3) enqueue(5) – (9, 7, 3, 5) dequeue() 9 (7, 3, 5) Queues

4 Applications of Queues
Direct applications Waiting lists, bureaucracy Access to shared resources (e.g., printer) Multiprogramming Indirect applications Auxiliary data structure for algorithms Component of other data structures Queues

5 Queue ADT Operations Implementation: Application:
Enqueue, Dequeue, Peek, isEmpty Implementation: Circular Array (Weiss), Linked List, Circular Linked List (text) Application: Level-Order Tree Traversal

6 Implementing a Queue Using Vector /Array
requires estimate of maximum queue length may grow dynamically Ø = empty slots Can contain varied data/objects (not necessarily homogeneous) 28-17 212 rules! Golf #1 Ø Ø Ø front rear

7 Implementing a Queue Using Linked List
flexible, adjusts to problem size implementing a linked list nodes and references/links/pointers front 28-17 212 rules! Golf #1 Ø rear

8 Implementing a Queue Using Linked List implementing a linked list
1 2 3 4 5 6 7 8 Ø 2 -1 7 1 4 3 6 Using Linked List implementing a linked list cursor implementation Golf #1 Ø Ø front = 5 212 rules! rear = 1 28-17 freelist = 8 Ø Ø Ø

9 wrapped-around configuration
Array-based Queue Use an array of size N in a circular fashion Two variables keep track of the front and rear f index of the front element r index immediately past the rear element Array location r is kept empty normal configuration Q 1 2 r f wrapped-around configuration Q 1 2 f r Queues

10 Queue Operations We use the modulo operator (remainder of division)
Algorithm size() return (N - f + r) mod N Algorithm isEmpty() return (f = r) Q 1 2 r f Q 1 2 f r Queues

11 Queue Operations (cont.)
Algorithm enqueue(o) if size() = N  1 then throw FullQueueException else Q[r]  o r  (r + 1) mod N Operation enqueue throws an exception if the array is full This exception is implementation-dependent Q 1 2 r f Q 1 2 f r Queues

12 Queue Operations (cont.)
Algorithm dequeue() if isEmpty() then throw EmptyQueueException else o  Q[f] f  (f + 1) mod N return o Operation dequeue throws an exception if the queue is empty This exception is specified in the queue ADT Q 1 2 r f Q 1 2 f r Queues

13 Queue Interface in Java
public interface Queue { public int size(); public boolean isEmpty(); public Object front() throws EmptyQueueException; public void enqueue(Object o); public Object dequeue() throws EmptyQueueException; } Java interface corresponding to our Queue ADT Requires the definition of class EmptyQueueException No corresponding built-in Java class Queues

14 Antrian Mobil Class Mobil{ ….} Class AntrianMobil implements Queue{ Mobil []data; int N,r,f; public AntrianMobil(int n){ N = n; data = new Mobil[N]; r = 0; f = 0; } public int size(){} public boolean isEmpty(){} public Object front() throws EmptyQueueException{} public void enqueue(Object o){} public Object dequeue() throws EmptyQueueException{} Queues

15 Application: Round Robin Schedulers
Queues 4/14/2017 5:26 PM Application: Round Robin Schedulers We can implement a round robin scheduler using a queue, Q, by repeatedly performing the following steps: e = Q.dequeue() Service element e Q.enqueue(e) Class proses Input N banyaknya proses Input T time slice For I  0 to N do pi  baca proses ke-I endQ(pi) End for // RR While (not isEmptyQ()) { pEx <- deQ() } The Queue Shared Service 1 . Deque the next element 3 Enqueue the serviced element 2 Service the Queues

16 Implementasi RR Class proses { int id, timeX; proses(int id, int time){ithis.id = id; timeX = time;} } //inisi Input N - banyaknya proses Input t - time slice Buat Antrian Q For I <- 0 to N do input proseske-I enQ(proses_i) end for // RR While (! Q.isEmpty()){ proses p = deQ(); p.timeX = p.timeX – t if (p.TimeX > 0) enQ(p) Queues

17 Queue Abstract Data Type
A queue is a linear collection where the elements are added to one end and removed from the other end The processing is first in, first out (FIFO) The first element put on the queue is the first element removed from the queue Think of a line of people waiting for a bus (The British call that “queuing up”)

18 A Conceptual View of a Queue
Rear of Queue (or Tail) Front of Queue (or Head) Removing an Element Adding an Element

19 Queue Terminology We enqueue an element on a queue to add one
We dequeue an element off a queue to remove one We can also examine the first element without removing it We can determine if a queue is empty or not and how many elements it contains (its size) The L&C QueueADT interface supports the above operations and some typical class operations such as toString()

20 <<interface>>
Queue ADT Interface <<interface>> QueueADT<T> + enqueue(element : T) : void + dequeue () : T + first() : T + isEmpty () : bool + size() : int + toString() : String

21 Queue Design Considerations
Although a queue can be empty, there is no concept for it being full. An implementation must be designed to manage storage space For first and dequeue operation on an empty queue, this implementation will throw an exception Other implementations could return a value null that is equivalent to “nothing to return”

22 Queue Design Considerations
No iterator method is provided That would be inconsistent with restricting access to the first element of the queue If we need an iterator or other mechanism to access the elements in the middle or at the end of the collection, then a queue is not the appropriate data structure to use

23 The java.util.Queue Interface
The java.util.Queue interface is in the Java Collections API (extends Collection) However, it is only an interface and you must use an implementing class LinkedList is the most commonly used implementing class For a queue of type objects: Queue<type> myQueue = new LinkedList<type>();

24 The java.util.Queue Interface
The names of the methods are different Enqueue is done using: boolean offer(T element) // returns false if full Dequeue is done using either: T poll() // returns null value if empty T remove() // throws an exception if empty Peek is done using either: T peek() // returns null value if empty T element() // throws an exception if empty

25 Using Vectors You could implement a deque with java.util.Vector:
addAtFront(Object)  insertElementAt(Object, 0) addAtRear(Object item)  add(Object) getFromFront()  remove(0) getFromRear()  remove(size() – 1) Would this be a good implementation? Why or why not?

26 Circular arrays We can treat the array holding the queue elements as circular (joined at the ends) 44 55 11 22 33 myQueue: rear = 1 front = 5 Elements were added to this queue in the order 11, 22, 33, 44, 55, and will be removed in the same order Use: front = (front + 1) % myQueue.length; and: rear = (rear + 1) % myQueue.length;

27 Full and empty queues This is a problem!
If the queue were to become completely full, it would look like this: 44 55 66 77 88 11 22 33 myQueue: rear = 4 front = 5 If we were then to remove all eight elements, making the queue completely empty, it would look like this: myQueue: rear = 4 front = 5 This is a problem!

28 Full and empty queues: solutions
Solution #1: Keep an additional variable 44 55 66 77 88 11 22 33 myQueue: rear = 4 front = 5 count = 8 Solution #2: (Slightly more efficient) Keep a gap between elements: consider the queue full when it has n-1 elements 44 55 66 77 11 22 33 myQueue: rear = 3 front = 5

29 Linked-list implementation of queues
In a queue, insertions occur at one end, deletions at the other end Operations at the front of a singly-linked list (SLL) are O(1), but at the other end they are O(n) Because you have to find the last element each time BUT: there is a simple way to use a singly-linked list to implement both insertions and deletions in O(1) time You always need a pointer to the first thing in the list You can keep an additional pointer to the last thing in the list

30 SLL implementation of queues
In an SLL you can easily find the successor of a node, but not its predecessor Remember, pointers (references) are one-way If you know where the last node in a list is, it’s hard to remove that node, but it’s easy to add a node after it Hence, Use the first element in an SLL as the front of the queue Use the last element in an SLL as the rear of the queue Keep pointers to both the front and the rear of the SLL

31 Enqueueing a node To enqueue (add) a node: Find the current last node
17 Node to be enqueued 23 44 last first 97 To enqueue (add) a node: Find the current last node Change it to point to the new last node Change the last pointer in the list header

32 Dequeueing a node To dequeue (remove) a node:
44 97 23 17 last first To dequeue (remove) a node: Copy the pointer from the first node into the header

33 Queue implementation details
With an array implementation: you can have both overflow and underflow you should set deleted elements to null With a linked-list implementation: you can have underflow overflow is a global out-of-memory condition there is no reason to set deleted elements to null

34 Deques A deque is a double-ended queue
Insertions and deletions can occur at either end Implementation is similar to that for queues Deques are not heavily used You should know what a deque is, but we won’t explore them much further

35 A queue ADT Java does not provide a queue class
Here is a possible queue ADT: Queue(): the constructor boolean empty() Object enqueue(Object item): add at element at the rear Object dequeue(): remove an element from the front Object peek(): look at the front element int search(Object o): Returns the 1-based position from the front of the queue

36 A deque ADT Java does not provide a deque class
Here is a possible deque ADT: Deque(): the constructor boolean empty() Object addAtFront(Object item) Object addAtRear(Object item) Object getFromFront() Object getFromRear() Object peekAtFront() Object peekAtRear() int search(Object o): Returns the 1-based position from the front of the deque

37 Using Vectors You could implement a deque with java.util.Vector:
addAtFront(Object)  insertElementAt(Object, 0) addAtRear(Object item)  add(Object) getFromFront()  remove(0) getFromRear()  remove(size() – 1) Would this be a good implementation? Why or why not?

38 11. (wajib) Misalkan anda mendapatkan kesempatan untuk membuat sistem pelayanan pelanggan pada suatu restoran. Restoran ini mempunyai N buah meja yang dikelilingi 4 buah kursi. Karena terkenal favorit maka restoran ini selalu dipadati oleh pelanggan pada jam kerjanya. Buka setiap hari mulai jam 08 pagi sampai jam 8 sore. Setiap pelangga harus pesan tempat terlebih dahulu minimal 2 jam sebelum dia masuk. Ada tiga pilihan pemesanan : VIP, EXPRESS, BIASA. Jatah masing-masing kelas 1/3 dari tempat yang disediakan. Masing-masing pelanggan dapat memesan 0 sampai M menu yang disediakan. Proses pelayanan akan dikerjakan berdasarkan antrian per kelas. Kelas VIP, EXPRESS kemudian BIASA. Kelas VIP dilayani terlebih dahulu, kemudian Express dan terakhir kelas BIASA. Biaya akan dihitung berdasarkan item yang dipesan ditambah biaya kelas masing-masing 0.2%, 0,1 % dan 0% untuk VIP, EXPRESS dan BIASA dari biaya pelayanan. Besarnya biaya pelayanan sebesar Rp per meja,-. Jumlah biaya yang harus dibayar masing-masing pelanggan meliputi : biaya pelayanan + biaya kelas + Akumulasi biaya item + pajak (10 % dari netto Akumulasi item). Seorang pelanggan dapat memesan lebih dari dua meja dengan syarat jika meja pada kelas VIP penuh boleh manambah meja dari kelas Biasa maksimal kuota (5% dari jatah meja kelas BIASA). Jika kelas meja kelas EXPRESS dan BIASA penuh tidak boleh nambah meja dari kelas lain. Queues

39 2 (pilihan) Misalkan anda disuruh membuat program untuk menata penempatan barang pada suatu rak dengan ketentuan sebagai berikut : Setiap barang mempunyai attribut : Id, merk, jenis, harga Rak terbagi dalam dua slot (slot1 dan slot2) Slot 1 akan ditempati barang dengan tipe jenis 1 dengan harga > X. Selain itu ditempatkan di slot 2. Pengeluaran barang selalu dimulai dari barang yang mempunyai jenis 1 dengan harga > X. Setelah barang dengan kriteria ini habis barulah barang yang lainnya.

40 3. pilihan Suatu pabrik memproduksi suatu jenis barang dengan kualitas A, kualitas B dan C. Pembuatan barang ditentukan berdasarkan ketersediaan bahan baku kualitas 1, 2 dan 3. Jika tersedia bahan baku kualitas 1 maka diproduksilah barang kualitas A demikian seterusnya. Setiap produksi melibatkan dua buah mesin M1 dan M2. Untuk tetap menjaga kualitas pembuatan kulitas barang A harus dikerjakan terlebih dahulu baru kualitas B dan C. Susunlah program untuk mensimulasi masalah ini.

41 Struktur Tugas Halaman judul + Kode Tugas (K[01]_[C]_T[03]) (5%)
Deskripsi masalah (5 %) Rancangan solusi (20 %) Rancangan struktur data umum (10 %) Rancangan class (10 %) Coding + komentar (40 %) Uji coba (10 %) Spek 1 Spek 2 Spek 3 Format A4 , 1 spasi, khusus untuk coding (courier new 9, bold) Queues


Download ppt "Queues 4/14/2017 5:26 PM Queues Queues."

Presentasi serupa


Iklan oleh Google