CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
Stacks. Queues. Double-Ended Queues. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Data Structures Lecture 5 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
Queues.
Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Implementing and Using Stacks
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Stacks And Queues Chapter 18.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Queues CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
CSCI 3333 Data Structures Stacks.
Stacks.
Stacks and Queues.
Queues Queues Queues.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 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.
Circular queue.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
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,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Recall What is a Data Structure Very Fundamental Data Structures
Stacks and Queues DSA 2013 Stacks n Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
Lecture 8: Stacks, Queues
Stacks and Linked Lists
Presentation transcript:

CS 221 Analysis of Algorithms Data Structures

Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis and Internet Examples, 2002  and  Material provided by the publisher’s John Wiley & Son, Inc.) companion website for this book

Remember this  “Algorithms + Data Structures = Programs”  Niklaus Wirth, Swiss Computer Scientist  Or, perhaps- Programs = Programming_language(Algoritms + Data Structures)

Data Structures  What are they and  Why do we care?

Stacks  Stack – container for a collection of objects where you add objects to one end and retrieve those objects from the same end  -- add object to the top of the stack and must retrieve from the top of the stack

Stacks  Last-In-First-Out -- LIFO  Think Stack of dishes in the cafeteria  Put a plate on top  take a plate from the top Stack of poker chips Stack of anything Bread-crumbs

Stacks  Algorithms - Stack Operations Push(o) Pop()

Stacks  Other Stack Operations size()  how many objects are on the stack? isEmpty() boolean  is the stack empty? top()  what is on top of stack? but don’t pop it

Stacks  What do we use stacks for? Web – Browsers  session visit history Text Editors – Word Processors  edit/undo Procedure Calls  transfer of execution/return

Stacks  What do we use stacks for? Web – Browsers  session visit history Text Editors – Word Processors  edit/undo Procedure Calls  transfer of execution/return

Stack - Array implementation Algorithm push(o) if t = S.length  1 then throw “StackFullError” else t  t + 1 S[t]  o  A simple way of implementing the Stack ADT uses an array  We add elements from left to right  A variable keeps track of the index of the top element S 012 t from: Goodrich and Tamassia, 2002

Stack - Array implementation Algorithm pop() if isEmpty() then throw “StackEmptyError” else t  t  1 return S[t + 1]  A simple way of implementing the Stack ADT uses an array  We add elements from left to right  A variable keeps track of the index of the top element S 012 t from: Goodrich and Tamassia, 2002

Stacks for procedure calls  To call a procedure Push state of procedure (local variables and PC) on stack  To return from a procedure Pop state of calling procedure from stack main() { int i = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … } bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 from: Goodrich and Tamassia, 2002

Stack – array implementation  Issues run-time?  size()  isEmpty()  top()  push(o)  pop() Algorithm push(o) if t = S.length  1 then throw “StackFullError” else t  t + 1 S[t]  o Algorithm pop() if isEmpty() then throw “StackEmptyError” else t  t  1 return S[t + 1]

Stack – array implementation  Memory limitations? growth?  How would you handle this? Algorithm push(o) if t = S.length  1 then throw “StackFullError” else t  t + 1 S[t]  o Algorithm pop() if isEmpty() then throw “StackEmptyError” else t  t  1 return S[t + 1]

Queues  Queue – container for objects Objects are added/retrieved from queue on First-In-First-Out rule FIFO That is, oldest thing in container must be removed first, then next oldest thing, etc. Objects are added to the end of queue, retrieved from front of queue

Queues  Queue – conceptually parts on a conveyer belt standing line to buy movie tickets  In Computer systems Printer queues job scheduling process scheduling Web page request service

Queues  Queue algorithms – main operations enqueue(o) – placing object at end of queue dequeue() – retreiving object from front of queue  Other queue operations 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

Queues  Array-based Queue Algorithm enqueue(o) if size() = N-1 then throw “QueueFullError” Q[r] <- o r <- r + 1 Q 012rf queue

Queues  Array-based Queue Algorithm dequeue(o) if isEmpty() then throw “QueueEmptyError” temp <- Q[f] Q[f] <- Null f <- f + 1 return temp Q 012rf queue

Queues  Array-based Queue Algorithm dequeue(o) if isEmpty() then throw “QueueEmptyError” temp <- Q[f] Q[f] <- Null f <- f + 1 return temp Q 012rf queue Algorithm enqueue(o) if size() = N-1 then throw “QueueFullError” Q[r] <- o r <- r + 1 Issues?  Run-time?  Memory?

Queues  Array-based Queue circular  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 Q 012rf normal configuration Q 012fr wrapped-around configuration

Queue - circular  We use the modulo operator (remainder of division) Algorithm size() return (N  f + r) mod N Algorithm isEmpty() return (f  r) Q 012rf Q 012fr

Queue - circular 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 Q 012rf Q 012fr

Queue - circular  Operation dequeue throws an exception if the queue is empty Algorithm dequeue() if isEmpty() then throw EmptyQueueException else o  Q[f] f  (f + 1) mod N return o Q 012rf Q 012fr