Data Structures Lecture 5

Similar documents
Stacks Goodrich, Tamassia Stacks

Stacks Goodrich, Tamassia, Goldwasser Stacks

Stacks. Lecture6: Stacks. Stack Operations. Stack Interface in Java

Stacks Goodrich, Tamassia

Foundations of Data Structures

CS212:Data Structure

COMP9024: Data Structures and Algorithms

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

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.

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

DataStruct 5. Stacks, Queues, and Deques

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

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.

Lecture 3 Linear Data Structures

Lecture 3 Linear Data Structures

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Ch02 Data Structures

Ch02 Data Structures

csci 210: Data Structures Stacks and Queues

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

Lecture 3 Linear Data Structures

data structures and algorithms lecture 6

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

This lecture. Abstract Data Types (ADTs) The Stack ADT ( 4.2) Stacks. Stack Interface in Java. Exceptions. Abstract data types Stacks Queues

How many times is the loop executed? middle = (left+right)/2; if (value == arr[middle]) return true;

Data structure and algorithm in Python

COMP 250. Lecture 8. stack. Sept. 25, 2017

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

Abstract Data Types (ADTs) Stacks. The Stack ADT ( 4.2) Stack Interface in Java

infix expressions (review)

More Data Structures (Part 1) Stacks

Queues Fall 2018 Margaret Reid-Miller

Data Abstraction and Specification of ADTs

Data Structures Lecture 6

CSC148H Week 3. Sadia Sharmin. May 24, /20

COSC This week. List ADT. Abstract Data Type (ADT) Lis t ADT Implementations. Data Struc tures and Algorithms

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

Your Topic Goes Here Queue

PA3 Design Specification

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

CS 211 Programming Practicum Spring 2017

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

ITI Introduction to Computing II

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

COMP 250 Midterm #2 March 11 th 2013

CT 229 Object-Oriented Programming Continued

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

CSC148 Week 2. Larry Zhang

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

CMSC 132: Object-Oriented Programming II. Stack and Queue

CS 211 Programming Practicum Spring 2018

DS L9: Queues

Abstract Data Types Spring 2018 Exam Prep 5: February 12, 2018

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists.

Queues. Carlos Delgado Kloos Dep. Ingeniería Telemática Univ. Carlos III de Madrid. Java: Queues / 1

Linear Data Structures

Stacks Fall 2018 Margaret Reid-Miller

LIFO : Last In First Out

Stacks, Queues (cont d)

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Queues. Virtuelle Fachhochschule. Prof. Dr. Debora Weber-Wulff

Data Structures Lecture 7

CS/CE 2336 Computer Science II

STACKS AND QUEUES. Problem Solving with Computers-II

Lecture 3: Stacks & Queues

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Lecture 4. The Java Collections Framework

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

Data Structure: Lists and Iterators. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

The Java Collections Framework. Chapters 7.5

Stacks and queues (chapters 6.6, 15.1, 15.5)

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

CSC212:Data Structure


== isn t always equal?

ITI Introduction to Computing II

Summer Final Exam Review Session August 5, 2009

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 8, February 4, 2016

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

COMP 250 Winter stacks Feb. 2, 2016

CS 211 Programming Practicum Fall 2018

push(d), push(h), pop(), push(f), push(s), pop(), pop(), push(m).

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Stacks. Revised based on textbook author s notes.

Transcription:

Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 5

Announcement Project Proposal due on Nov. 9 Remember to bring a hardcopy of your project proposal to the class next week Also submit your project proposal file to your TAs Discuss potential difficulties of your project We can select some topics/challenges and explain them in the lab

Announcement We will have programming lectures and tests on Nov. 6 in 逸仙樓 5F. HWs Review BMI Generic Progression Keyword Counting The Ordered List

Abstract Linear Data Structures Stacks, Queues, Deques, and Priority Queues

Abstract Data Type (ADT) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error conditions associated with operations We have discussed Array ADT and List ADT

Stacks The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out scheme

Stack Operations Main operations: push(object): inserts an element object pop(): removes and returns the last inserted element Others: object top(): returns the last inserted element without removing it integer size(): returns the number of elements stored boolean isempty(): indicates whether no elements are stored

Stack Interface in Java public interface Stack<E> { } public int size(); public boolean isempty(); public E top() In the Stack ADT, operations pop and top cannot be performed if the stack is empty throws EmptyStackException; Attempting the execution of pop or top on an empty stack public void push(e element); throws an public E pop() EmptyStackException throws EmptyStackException;

Common Applications Page-visited history in a Web browser Yahoo à news à drama http://tw.yahoo.com/ Undo sequence in a text editor For example, I type something here

Method Call Stack in the JVM The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack When a method is called, the JVM pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack

An Example of Method Stack main() { int i = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { } main PC = 2 i = 5 Invoke foo() foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 Invoke bar() bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 bar() return foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 foo() return main PC = 2 i = 5

Array-based Stack 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 Algorithm size() return t + 1 Algorithm pop() if isempty() then throw EmptyStackException else t t - 1 return S[t + 1] S 0 1 2 t

Array-based Stack The array storing the stack elements may become full A push operation will then throw a FullStackException Limitation of the array-based implementation Not intrinsic to the Stack ADT Algorithm push(o) if t = S.length - 1 then throw FullStackException else t t + 1 S[t] o S 0 1 2 t

Array-based Stack Performance Let n be the number of elements in the stack The space used is O(n) Each operation runs in time O(1) Limitations The maximum size of the stack must be defined a priori and cannot be changed Trying to push a new element into a full stack causes an implementation-specific exception Use a growthable array instead

Parenthesis Matching Each (, {, or [ must be paired with a matching ), }, or ] ( )(( )){([( )])} ((( )(( )){([( )])})) )(( )){([( )])} ({[ ])} (

Parenthesis Matching Each (, {, or [ must be paired with a matching ), }, or ] correct: ( )(( )){([( )])} correct: ((( )(( )){([( )])})) incorrect: )(( )){([( )])} incorrect: ({[ ])} incorrect: (

Parentheses Matching Algorithm ParenMatch(X,n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match For example: (2+3)*(3+6) X is an array of { (, 2, +, 3, ), *, (, 3, +, 6, ) } and n is 11 Output: true

Parentheses Matching Algorithm Let S be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then // e.g., ( S.push(X[i]) else if X[i] is a closing grouping symbol then // e.g., ) if S.isEmpty() then return false //nothing to match with if S.pop() does not match the type of X[i] then return false //a wrong type if S.isEmpty() then return true //all symbols are matched else return false //symbols that can not be matched

Evaluating Arithmetic Expressions 14 3 * 2 + 7 = (14 (3 * 2) ) + 7 Operator precedence * has precedence over +/ Associativity operators of the same precedence group evaluated from left to right Example: x y + z (x y) + z rather than x (y + z)

Evaluating Arithmetic Expressions Idea: push each operator on the stack, but first pop and perform higher and equal precedence operations. Two stacks: opstk holds operators valstk holds values To clean up the stack at the end, we use $ as special end of input token with lowest precedence

Evaluating Arithmetic Expressions Algorithm EvalExp() Input: a stream of tokens representing an arithmetic expression (with numbers) Output: the value of the expression while there s another token z if isnumber(z) then valstk.push(z) //push number else repeatops(z); //calculate the higher precedence operators opstk.push(z); //before push the operator z repeatops($); //calculate all the rest operators return valstk.top()

Evaluating Arithmetic Expressions Algorithm repeatops( refop ): while ( valstk.size() > 1 doop() prec(refop) prec(opstk.top()) //op in the Stack needs to be done first Algorithm doop() x valstk.pop();" y valstk.pop();" op opstk.pop();" valstk.push( y op x )

An Example 14 4 3 * 2 + 7 4 14 3 4 14 2 3 4 14 * * 2 3 4 14 + * $ 7-2 + 14 6 4-2 + 14 14 + $ 5 14 $ F Operator has lower precedence than +/

Computing Spans Span: number of equal or smaller consecutive terms (including itself) Given an array X, compute the array S, where S[i] is the maximum number of consecutive elements X[j] immediately preceding X[i], where X[j] X[i] and j i Spans have applications to financial analysis E.g., stock at 52-week high 7 6 5 4 3 2 1 0 X S 0 1 2 3 4 6 3 4 5 2 1 1 2 3 1

A Quadratic Algorithm Algorithm spans1(x, n) Input array X of n integers Output array S of spans of X # S new array of n integers n for i 0 to n - 1 do n s 1 n while s i X[i - s] X[i] 1 + 2 + + (n - 1) s s + 1 1 + 2 + + (n - 1) S[i] s n return S 1 Algorithm spans1 runs in O(n 2 ) time

A Linear Algorithm Using a stack as an auxiliary data We keep in a stack the indices of the elements visible when looking back We scan the array from left to right Let i be the current index We pop indices from the stack until we find index j such that X[i] < X[j] We set S[i] i - j We push x onto the stack

A Linear Algorithm Each index of the array Is pushed into the stack exactly one Is popped from the stack at most once The statements in the whileloop are executed at most n times Algorithm spans2 runs in O(n) time Algorithm spans2(x, n) # S new array of n integers n A new empty stack 1 for i 0 to n - 1 do n while ( A.isEmpty() X[A.top()] X[i] ) do n A.pop() n if A.isEmpty() then n S[i] i + 1 n else S[i] i - A.top() n A.push(i) n return S 1

Queues The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out schema Insertions are at the rear of the queue and removals are at the front of the queue

Queue Operations Main 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 Others: 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

An 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)

Array-based Queue Use an array of size N Insert at the front, and remove at the rear size(): return n; isempty(): return n==0; enqueue(object): add(n, obeject) dequeue(): remove(0) Removing the front is costly in an array, i.e., O(n).

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 Q normal configuration 0 1 2 f r Q wrapped-around configuration 0 1 2 r f

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

Queue Operations Operation enqueue throws an exception if the array is full Algorithm enqueue(o) if size() = N - 1 then throw FullQueueException else Q[r] o r (r + 1) mod N Q Q 0 1 2 f r 0 1 2 r f

Queue Operations 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 Q 0 1 2 f r 0 1 2 r f

Queue Interface in Java public interface Queue<E> { } public int size(); public boolean isempty(); public E front() throws EmptyQueueException; public void enqueue(e element); public E dequeue() throws EmptyQueueException; Similar to the Stack ADT, operations front and dequeue cannot be performed if the queue is empty Attempting the execution of front or dequeue on an empty queue throws an EmptyQueueException

Application: Round Robin Schedulers We can implement a round robin scheduler using a queue Q by repeatedly performing the following steps: 1. e = Q.dequeue() 2. Service element e 3. Q.enqueue(e) Queue Shared Service

Dequeus Pronounce de ke Double-ended queues addfirst(), addlast(), removefirst(), removelast(), getfirst(), getlast(), size(), isempty() Implemented by doubly linked list Operation Output D addfirst(3) - (3) addfirst(5) - (5,3) removefirst() 5 (3) addlast(7) - (3,7) removefirst() 3 (7) removelast() 7 () removefirst() error () isempty() true ()

Priority Queues Queues whose elements are in order You have implemented a priority queue in HW4 Priority queues can be used for sorting

Insertion Sort Sequence S Priority queue P Input: (7,4,8,2,5,3,9) () Phase 1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (4,7) (c) (2,5,3,9) (4,7,8) (d) (5,3,9) (2,4,7,8) (e) (3,9) (2,4,5,7,8) (f) (9) (2,3,4,5,7,8) (g) () (2,3,4,5,7,8,9) Phase 2 (a) (2) (3,4,5,7,8,9) (b) (2,3) (4,5,7,8,9)...... (g) (2,3,4,5,7,8,9) ()

HTML Tag Matching For fully-correct HTML, each <name> should pair with a matching </ name> <body> <center> <h1> The Little Boat </h1> </center> <p> The storm tossed the little boat like a cheap sneaker in an old washing machine. The three drunken fishermen were used to such treatment, of course, but not the tree salesman, who even as a stowaway now felt that he had overpaid for the voyage. </p> <ol> <li> Will the salesman die? </li> <li> What color is the boat? </li> <li> And what about Naomi? </li> </ol> </body> The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine. The three drunken fishermen were used to such treatment, of course, but not the tree salesman, who even as a stowaway now felt that he had overpaid for the voyage. 1. Will the salesman die? 2. What color is the boat? 3. And what about Naomi?

HW 5 (Due on 11/2) Matching/Analyzing HTML Tags. Check whether a given (simplified) HTML web page is valid By HTML Tag Matching (TB, page. 212-213) Output: all the matched tags if the html is valid The first mis-matched tag if the html is invalid Assume all matched tags are in the form: <name> </name>

Coming Up So far all the mentioned data structures are sequential, i.e., data are stored in a sequence We will talk about hierarchical data structures on Nov. 2 We will have programming tests in the lab on Nov. 6 Read Chapter 7