Lecture 9 Feb 26 Announcement/discussion: mid-term # 1 (March 10?) Goals: Queue – implementation using array Application to BFS (breadth-first search)

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
More on Stacks and Queues. As we mentioned before, two common introductory Abstract Data Type (ADT) that worth studying are Stack and Queue Many problems.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Lecture 8 Feb 19 Goals: l applications of stack l Postfix expression evaluation l Convert infix to postfix l possibly start discussing queue.
Queues.
Breadth-first search using a queue BFS: application that can be implemented using a queue. Our application involves finding the number of distinct letters.
Stacks and Queues.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Objectives of these slides:
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
Stacks and Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
CE 221 Data Structures and Algorithms
CSCS-200 Data Structure and Algorithms
Queues Dr. Anwar Ghani Lecture 06: Queue Data Structures
Presentation transcript:

Lecture 9 Feb 26 Announcement/discussion: mid-term # 1 (March 10?) Goals: Queue – implementation using array Application to BFS (breadth-first search)

Queue Overview Queue ADT –FIFO (first-in first-out data structure) Basic operations of queue –Insert, delete etc. Implementation of queue –Array

Queue ADT Like a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion is performed at the other end. Accessing the elements of queues follows a First In, First Out (FIFO) order. –Like customers standing in a check-out line in a store, the first customer in is the first customer served.

Insert and delete Primary queue operations: insert and delete Like check-out lines in a store, a queue has a front and a rear. insert - add an element at the rear of the queue delete – remove an element from the front of the queue Insert Remove rearfront

Implementation of Queue Queues can be implemented as arrays Just as in the case of a stack, queue operations ( insert delete, isEmpty, isFull, etc.) can be implemented in constant time

Implementation using Circular Array When an element moves past the end of a circular array, it wraps around to the beginning, e.g. –OOOOO7963  (insert(4)) 4OOOO7963 How to detect an empty or full queue, using a circular array? –Use a counter for the number of elements in the queue. –size == MSIZE means queue is full –Size == 0 means queue is empty.

Queue Class Attributes of Queue –front/rear: front/rear index –size: number of elements in the queue –Q: array which stores elements of the queue Operations of Queue –IsEmpty() : return true if queue is empty, return false otherwise –IsFull() : return true if queue is full, return false otherwise –Insert(k): add an element to the rear of queue –Delete(): delete the element at the front of queue

class queue { private: point* Q[MSIZE]; int front, rear, size; public: queue() { // initialize an empty queue front = 0; rear = 0; size = 0; for (int j=0; j < MSIZE; ++j) Q[j] = NULL; } void insert(point* x) { if (size != MSIZE) { rear++; size++; if (rear == MSIZE) rear = 0; Q[rear] = x; }; else cout << “queue is full. Insertion failed” << endl; } point objects have two fields p.x and p.y both of which are int values. (We need a queue to hold the pixel coordinates in project # 3.)

point delete() { if (size != 0) { point temp(Q[front]->getx(), Q[front]->gety()); front++; if (front == MSIZE) front = 0; size--; return temp; }; else cout << “attempt to delete from an empty queue” << endl; } bool isEmpty() { return (size == 0); } bool isFull() { return (size == MSIZE); } };

Breadth-first search using a queue BFS: application that can be implemented using a queue. Our application involves finding the number of distinct letters that appear in an image and draw bounding boxes around them. Taken from the output of the BFS algorithm

Overall idea behind the algorithm Scan the pixels of the image row by row, skipping over black pixels until you encounter a white pixel p(j,k). (j and k are the row and column numbers of this pixel.) Now the algorithm uses a queue Q to identify all the white pixels that are connected together with p(j,k) to form a single symbol/character in the image. When the queue becomes empty, we would have identified all the pixels that form this character (and assigned the color Green to them.) While identifying these connected pixels, also maintain the four variables lowx, highx, lowy and highy. These are the bounding values. When the queue is empty, these bounding values are the x- and y-coordinates of the bounding box.

Let the image width be w and the height be h; Set visited[j,k] = false for all j,k; count = 0; Initialize Q; // count is the number of symbols in the image. for j from 1 to w – 1 do for k from 0 to h – 1 do if (color[j,k] is white) then count++; lowx = j; lowy = k; highx = j; highy = k; insert( [j,k]) into Q; visited[j,k] = true; while (Q is not empty) do p = delete(Q); let p = [x,y]; color[x,y] = green; if x < lowx then lowx = x; if y < lowy then lowy = y; if x > highx then highx = x; if y > highy then highy = y; for each neighbor n of p do if visited[n] is not true then visited[n] = true; if (color[n] is white) insert(n) into Q; end if; end if; end for; end while; draw a box with (lowx-1,lowy-1) as NW and (highx + 1, highy + 1) as SE boundary; // make sure that highx+1 or highy+1 don’t exceed the dimensions else visited[j,k] = true; end if; end do;

Example: (2,3)(3, 3)(4,3)(5,3) (3, 4) (3, 5) (3, 6) (3, 7)

Example: (2, 3)(3, 3)(4, 3)(5,3) (3, 4) (3, 5) (3, 6) (3, 7) when the algorithm stops, count would be 1 xlow = 2 xhigh = 6 ylow = 1 yhigh = 7 Draw the bounding box (2,1), (2,6), (7,1) and (7, 6).

Example: (2, 3)(3, 3)(4, 3)(5, 3) (3, 4) (3, 5) (3, 6) (3, 7) when the algorithm stops, count would be 1 xlow = 2 xhigh = 6 ylow = 2 yhigh = 7 Draw the bounding box (2,1), (2,6), (7,1) and (7, 6).

Example: tt t t t t t t (2, 3)(3, 3)(4, 3)(5, 3) (3, 4) (3, 5) (3, 6) (3, 7) count = 0 Q = { } visited changes as shown in fig. tt t t t t t t tt t t t t t t t

Example: tt t t t t t t (2, 3)(3, 3)(4, 3)(5, 3) (3, 4) (3, 5) (3, 6) (3, 7) count = 1; lowx = highx = 2 lowy = highy = 3 Q = {(2,3)} tt t t t t t t tt t t t t t t t t

Example: tt t t t t t t (2, 3)(3, 3)(4, 3)(5, 3) (3, 4) (3, 5) (3, 6) (3, 7) After first Q.delete(): Q = { } count = 1; lowx = highx = 2 lowy = highy = 3 Q = {(3, 3)} tt t t t t t t tt t t t t t t t t t

Example: tt t t t t t t (2, 3)(3, 3)(4, 3)(5, 3) (3, 4) (3, 5) (3, 6) (3, 7) After second Q.delete(): Q = { } P = (3,3) color[p] = green lowx = 2 highx = 3 lowy = highy = 3 Q = {(4,3), (3,4)} tt t t t t t t tt t t t t t t t ttt t

Example: tt t t t t t t (2, 3)(3, 3)(4, 3)(5, 3) (3, 4) (3, 5) (3, 6) (3, 7) After third delete(Q): Q = {(3,4)} P = (4,3) color[p] = green lowx = 2 highx = 4 lowy = highy = 3 Q = {(3,4),(5,3)} tt t t t t t t tt t t t t t t t tttt t

Example: tt t t t t t t (2, 3)(3, 3)(4, 3)(5, 3) (3, 4) (3, 5) (3, 6) (3, 7) After fourth delete(Q): visited[3,4]= true Q = {(5, 3)} P = (3, 4) color[p] = green lowx = 2 highx = 4 lowy = 3 highy = 4 Q = {( 5,3),(3,5)} tt t t t t t t tt t t t t t t t tttt t t

Example: tt t t t t t t (2, 3)(3, 3)(4, 3)(5, 3) (3, 4) (3, 5) (3, 6) (3, 7) After fifth delete(Q): visited[5,3]= true Q = {(3,5)} P = (3, 5) color[p] = green lowx = 2 highx = 5 lowy = 3 highy = 4 Q = {(3,5)} tt t t t t t t tt t t t t t t t tttt t t

Example: tt t t t t t t (2, 3)(3, 3)(4, 3)(5, 3) (3, 4) (3, 5) (3, 6) (3, 7) After sixth delete(Q): visited[5, 3]= true Q = {} P = (5, 3) color[p] = green lowx = 2 highx = 5 lowy = 3 highy = 5 Q = {(3,6)} tt t t t t t t tt t t t t t t t tttt t t

Example: tt t t t t t t (2, 3)(3, 3)(4, 3)(5, 3) (3, 4) (3, 5) (3, 6) (3, 7) After seventh delete(Q): visited[3,6]= true Q = { } P = (3,6) color[p] = green lowx = 2 highx = 5 lowy = 3 highy = 6 Q = {(3,7)} tt t t t t t t tt t t t t t t t tttt t t t

Example: tt t t t t t t (2, 3)(3, 3)(4, 3)(5, 3) (3, 4) (3, 5) (3, 6) (3, 7) After eighth delete(Q): visited[3,7]= true Q = { } P = (3,7) color[p] = green lowx = 2 highx = 5 lowy = 3 highy = 7 Q = { } At this point the while loop ends. tt t t t t t t tt t t t t t t t tttt t t t t