Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.

Slides:



Advertisements
Similar presentations
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Advertisements

1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
CMPT 225 Stacks.
Recursion Chapter 8. 2 Chapter Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Cmpt-225 Queues. A queue is a data structure that only allows items to be inserted at the end and removed from the front Queues are FIFO (First In First.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
M180: Data Structures & Algorithms in Java
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Introducing Recursion Date: June 7 th, 2002 Author: Steve Engels, University of Waterloo.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
CMPT 225 Recursion. Objectives Understand how the Fibonacci series is generated Recursive Algorithms  Write simple recursive algorithms  Analyze simple.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
April 27, 2017 COSC Data Structures I Review & Final Exam
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Queues Chapter 8 (continued)
Data Abstraction & Problem Solving with C++
ADT description Implementations
CC 215 Data Structures Queue ADT
Csc 2720 Data structure Instructor: Zhuojun Duan
CENG 213 Data Structure Queue 7/2/2018.
Hassan Khosravi / Geoffrey Tien
Review: recursion Tree traversals
Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
CSC 143 Queues [Chapter 7].
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,
Figure 7.1 Some queue operations. Figure 7.1 Some queue operations.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Recursion: The Mirrors
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Presentation transcript:

Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end of the array “wraps around” to the start of the array

The mod Operator The mod operator (%) is used to calculate remainders: 1%5 = 1, 2%5 = 2, 5%5 = 0, 8%5 = 3 mod can be used to calculate the front and back positions in a circular array, therefore avoiding comparisons to the array size The back of the queue is: (front + count - 1) % items.length where count is the number of items currently in the queue After removing an item the front of the queue is: (front + 1) % items.length;

Array Queue Example //Java Code Queue q = new Queue(); q.enqueue(6); 6 front =0 insert item at (front + count) % items.length count =01

Array Queue Example //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); 6 front = count =12345

Array Queue Example //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); q.dequeue();//front = 1 q.dequeue();//front = 2 q.enqueue(9); 6 front =0 4 make front = (0 + 1) % 6 = count =5434 make front = (1 + 1) % 6 = 2 2

Array Queue Example //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); q.dequeue();//front = 1 q.dequeue();//front = 2 q.enqueue(9); q.enqueue(5); front = insert at (front + count) % 6 = (2 + 4) % 6 = 0 count =45

Queue: Linked List Implementation Removing items from the front of the queue is straightforward But we need to insert items at the back of the queue in constant time So cannot traverse through the list By using an additional reference (and a little administration) we can keep track of the node at the back of the queue

List Queue Example front 6 back //Java Code Queue q = new Queue(); q.enqueue(6);

List Queue Example front 46 back //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4);

List Queue Example front 46 back //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); 7

List Queue Example front 46 back //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); 73

List Queue Example front 46 back //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.dequeue(); 73

Queue: Circular Linked List Implementation Possible implementations of a queue A circular linked list with one external reference A reference to the back Figure 8-4b A reference-based implementation of a queue: b) a circular linear linked list with one external reference

Queue: Circular Linked List Implementation Figure 8-5 Inserting an item into a nonempty queue

Queue: Circular Linked List Implementation Figure 8-6 Inserting an item into an empty queue: a) before insertion; b) after insertion

Queue: Circular Linked List Implementation Figure 8-7 Deleting an item from a queue of more than one item

Queue: ADT List Implementation public void enqueue(Object newItem) { list.add(list.size()+1, newItem); } // end enqueue public Object dequeue() { Object temp = list.get(1); list.remove(1); return temp; } // end dequeue

Queue: ADT List Implementation Efficiency depends on implementation of ADT List – in most common implementations, at least one of operations enqueue() and dequeue() is not efficient On other hand: it was very fast to implement (code is easy, unlikely that errors were introduced when coding).

Application of queues: Recognizing Palindromes A palindrome A string of characters that reads the same from left to right as its does from right to left To recognize a palindrome, a queue can be used in conjunction with a stack A stack can be used to reverse the order of occurrences A queue can be used to preserve the order of occurrences

Recognizing Palindromes A nonrecursive recognition algorithm for palindromes As you traverse the character string from left to right, insert each character into both a queue and a stack Compare the characters at the front of the queue and the top of the stack Figure 8-3 The results of inserting a string into both a queue and a stack

Problems: Recognize palindromes using 3 stacks. Simulate (implement) a queue with 2 stacks. How efficient is this implementation? Simulate (implement) a stack with 1 queue! (This is the problem which I wanted to discuss but didn’t recall it’s formulation exactly.) How efficient is this implementation?

Recursion An extremely powerful problem-solving technique Breaks a problem in smaller identical problems An alternative to iteration An iterative solution involves loops

Example: Rabbits What happens if you put a pair of rabbits in a field? You get more rabbits! Assume that rabbits take one month to reach maturity and that Each pair of rabbits produces another pair of rabbits one month after mating. That is: each pair will produce a new pair 2 months after it was born (and every month after that)

Example: Rabbits How many pairs of rabbits are there after five months? Month 1: 1 pair Month 2: 1 pair after one month the rabbits are mature and can mate Month 3: 2 pairs the first pair gives birth to a new pair of rabbits Month 4: 3 pairs the first pair gives birth to a new pair, her first children are now mature Month 5: 5 pairs

Example: Rabbits After 5 months there are 5 pairs of rabbits i.e. the number of pairs at 4 months (3) plus the number of pairs at 3 months (2) Why? We have count existing pairs of rabbits (the same number as previous month) + the new pairs (the same number as 2 months ago, as only those rabbits can now produce children) This series of numbers is called the Fibonacci series rabbit (n) = rabbit(n-1) + rabbit(n-2)

Multiplying Rabbits (The Fibonacci Sequence) Figure 3-10 Recursive solution to the rabbit problem

Fibonacci Sequence The n th number in the Fibonacci sequence, fib(n), is: 0 if n = 0, and 1 if n = 1 fib(n – 1) + fib(n – 2) for n > 1 So what, say, is fib(23), or how many pairs of rabbits would there be after 23 months? This would be easy if only we knew fib(22) and fib(21) If we did then the answer is just fib(22) + fib(21) What happens if we write a function to calculate Fibonacci numbers in this way?

Calculating the Fibonacci Sequence Here is a function to return nth number in the Fibonacci sequence e.g. fib(1) = 1, fib(3) = 2, fib(5) = 5, fib(8) = 21, … public static int fib(int n){ if(n == 0 || n == 1){ return n; } else{ return fib(n-1) + fib(n-2); } Notice this looks just like the description of the Fibonacci sequence given previously, but does it work!? The function calls itself!!!