Foundations of Data Structures Practical Session #4 Recurrence ADT 08/04/2013Amihai Savir & Ilya Mirsky - 2013.

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

Stacks, Queues, and Linked Lists
Data Structure HKOI training /4/2010 So Pak Yeung.
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.
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
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
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Chapter 12: Data Structures
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.
Queues.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
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.
Stacks, Queues, and Deques
Data Structures - Queues
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
Stack and Queue.
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Stacks and Queues Introduction to Computing Science and Programming I.
Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Chapter.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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,
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
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.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linear Data Structures
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CISC220 Spring 2010 James Atlas Lecture 10: Queues.
Java Collections Framework The client view. The Big Picture.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
Review Array Array Elements Accessing array elements
Cpt S 122 – Data Structures Abstract Data Types
G64ADS Advanced Data Structures
QueueStack CS1020.
Chapter 15 Lists Objectives
Chapter 12: Data Structures
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Java Collections Framework
ADTs: Array, Queue, Stack, Linked List
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Abstract Data Type (ADT)
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Presentation transcript:

Foundations of Data Structures Practical Session #4 Recurrence ADT 08/04/2013Amihai Savir & Ilya Mirsky

Recurrence solution methods 2

The master method 3

Master method example 4

Example cont’d 5

6

ADT From Wikipedia: In computer science, an abstract data type (ADT) is a mathematical model for a certain class of data structures that have similar behavior. An abstract data type is defined indirectly, only by the operations that may be performed on it and by mathematical constraints on the effects (and possibly cost) of those operations. 7

Vector (Array) 8

Queue FIFO - First In First Out ADT that supports the following operations: enqueue(element) enqueue(element) – inserts new element at the tail of the queue. dequeue() dequeue() – removes and returns an element from the head of the queue. peek peek() – (optional) returns an element from the head of the queue, without removing it. isEmpty() isEmpty() – returns true if the queue is empty, 9

Stack LIFO - Last In First Out ADT that supports the following operations: push(element) push(element) – adds an element to the head of the stack. pop() pop() – removes and returns an element from the head of the stack. top() top() – (optional) returns the element from the head of the stack, without removing it. isEmpty isEmpty – returns true if the stack is empty. 10

Linked List A data structure that supports linear access to its elements. Each element points to the next element in the list. In a doubly-linked list: each element has 2 pointers, one to the next element in the list and the other to the previous element. 11

Question 1 Suggest 2 ways to implement a Queue using 2 Stacks. Solution Notice that the queue/stack is abstract, meaning the implementation isn't known, only the interface. In order to implement a queue we need to implement the following methods: enqueue(x), dequeue(), isEmpty() We will use stack A to hold the queue's elements and stack B as a temporary stack. We will use the Stack ADT operations: push(x), pop() and isEmpty(). 12

Question 1 cont’d enqueue (x) { moveElements(A, B) A.push(x) moveElements(B, A) } dequeue ( ) { element = A.pop() return element } isEmpty ( ) { return A.isEmpty() } moveElements(X, Y) { while (! X.isEmpty()){ temp = X.pop() Y.push(temp) } } 13 * In a very similar manner one can implement stack using 2 queues.

Question 1 cont’d Another solution enqueue (x) { moveElements(A, B) A.push(x) } dequeue ( ) { moveElements(B,A) element = A.pop() return element } isEmpty ( ) { return (A.isEmpty() && B.isEmpty()) } 14

Question 2* Suggest a way to implement a Stack using Linked List. Solution We will use an inner List L in order to hold the stack’s elements. 15

Question 2 cont’d pop() { element ← L.itemAt(0) L.removeItemAt(0) return element } push(x) { L.addToHead(x) } top() { element ← L.itemAt(0) return element } isEmpty() if L.size()=0 then return true else return false } 16

Question 3* 17 OperationTimeinit(n)O(n) isElement(k)O(1) insert(x, k) O(1) remove(k) isEmpty() hasAll () O(1)

Question 3 cont’d Insert (x, k) { A[k] = x count++ } remove (k) { A[k] = null count-- } isEmpty () { return (count == 0) } hasAll () { return (count == n) } 18

Question 4 19 OperationTime init(n)Initialize A with the value 1O(n 2 ) flip(i, j)Flip the value in A[i, j], i.e., A[i, j] = !A[i, j]O(1) hasRowOf1()Return true iff A has a row that contains only 1-sO(1) hasRowOf0()Return true iff A has a row that contains only 0-sO(1)

Question 4 cont’d flip(i,j) if (A[i][j] == 0) { if (Sum[i] == 0) count0- - Sum[i]++ if (Sum[i] == n) count1++ A[i][j] = 1 } else { if (Sum[i] == n) count1- - Sum[i]-- if (Sum[i] == 0) count0++ A[i][j] = 0 } 20

Question 5 21 OperationTime add(k, isSpecial) O(n) removeMin()O(1) removeMax() getOldest() Returns the oldest special number, according to insertion order O(1)

Question 5 cont’d 22