Algorithms and Data Structures

Similar documents
Algorithms and Data Structures

Elementary Data Structures and Hash Tables

CMSC Introduction to Algorithms Spring 2012 Lecture 7

1 P age DS & OOPS / UNIT II

8. Fundamental Data Structures

3. Fundamental Data Structures

STACKS AND QUEUES. Problem Solving with Computers-II

if (Q.head = Q.tail + 1) or (Q.head = 1 and Q.tail = Q.length) then throw F ullqueueexception if (Q.head = Q.tail) then throw EmptyQueueException

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

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

Stacks, Queues and Hierarchical Collections

Programming. Lists, Stacks, Queues

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

Algorithms and Data Structures

Week 6. Data structures

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

DATA STRUCTURE UNIT I

811312A Data Structures and Algorithms, , Exercise 1 Solutions

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

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

Stacks and Queues. CSE 373 Data Structures Lecture 6

Ashish Gupta, Data JUET, Guna

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

CMPT 225. Lecture 6 linked list

data structures and algorithms lecture 6

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #13

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Postfix (and prefix) notation

Stack and Queue. Stack:

Information Science 2

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

Dynamic Storage Allocation

Linked Structures. See Section 3.2 of the text.

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

Lecture 3: Stacks & Queues

ITI Introduction to Computing II

Data Structures and Algorithms for Engineers

Data Structures -- Introduction

CSCA48 Winter 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

A simple correctness proof of the MCS contention-free lock. Theodore Johnson. Krishna Harathi. University of Florida. Abstract

DATA STRUCTURES AND ALGORITHMS

Elementary Data Structures. CS 114/116 and CS 505 material. Chapters , of GT

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

CS350 - Exam 1 (100 Points)

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Lecture Data Structure Stack

.:: UNIT 4 ::. STACK AND QUEUE

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

12 Abstract Data Types

Programming Abstractions

Objectives. Upon completion you will be able to:

Data Structure Advanced

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

COMP 250 Winter stacks Feb. 2, 2016

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

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

Queues. Queue ADT Queue Implementation Priority Queues

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

CS301 - Data Structures Glossary By

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Linked Lists and Abstract Data Structures A brief comparison

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

COMP 250 Midterm #2 March 11 th 2013

15. Stacks and Queues

Stacks and queues (chapters 6.6, 15.1, 15.5)

Computer Science Foundation Exam

CS510 Concurrent Systems. Jonathan Walpole

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

LIFO : Last In First Out

Motivation for Queues

Lecture 4 Stack and Queue

BBM 201 DATA STRUCTURES

CS24 Week 4 Lecture 2

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Algorithms and Data Structures for Mathematicians

DATA STRUCTURES AND ALGORITHMS

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Rob Legrand, and Ron K Cytron Washington University in St Louis 1 Brookings Dr Saint Louis, MO

Your Topic Goes Here Queue

III Data Structures. Dynamic sets

Section 5.3: Event List Management

Matriculation number:

V.S.B ENGINEERING COLLEGE DEPARTMENT OF INFORMATION TECHNOLOGY I IT-II Semester. Sl.No Subject Name Page No. 1 Programming & Data Structures-I 2

Linked Lists, Stacks, and Queues

CS 216 Exam 1 Fall SOLUTION

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

CS PROGRAMMING & ATA STRUCTURES I. UNIT I Part - A

PA3 Design Specification

The questions will be short answer, similar to the problems you have done on the homework

Propedéutico de Programación

CMSC351 - Fall 2014, Homework #2

Transcription:

Algorithms and Data Structures CMPSC 465 LECTURE 10 Abstract Data Types Queues Adam Smith 1

Data Structures So far in this class: designing algorithms Inputs and outputs were specified, we wanted to design the fastest algorithm The representation was fixed (e.g. a sorted array) Another important question: How can we represent information so that there are fast algorithms for performing important operations? This is the study of data structures 2

Some important data structures arrays linked lists graphs binary search trees heaps What about stacks? queues? Not exactly data structures. These are abstract data types (note: the text book doesn t distinguish data structures from abstract data types, but we will in this class) 3

Abstract Data Types Interface between the real data and the outside world Collection of operations to be performed on data No algorithms! Just a description of desired outcomes Important tool in the design of computer programs First, figure out what you need to do with your data Worry about implementing it later. Sort of like a class, an interface or a template in object-oriented programming (but not exactly like any of these) 4

Example: Queues Suppose you manage the list of cases waiting for trial at a courthouse You maintain a bunch of court cases As cases come in you add them to your list When the court finishes a trial, you find the next case in line and it goes to trial What s the ADT you re using? A Queue holds a set of elements and supports Enqueue(Q, x): add x to the rear of the queue Dequeue(Q): get element from the front of the queue and remove it from the queue MakeNew(): create a new, empty queue Enqueue Dequeue 5

How should we implement a queue? One option: an array along with two indices head and tail Q 1 2 3 4 5 6 7 8 9 10 11 12 3 7 2 5 6 head[q]=7 tail[q]=11 As elements are added, increment tail[q] As elements are removed, increment head[q] Wrap around as necessary After Enqueue(Q,17), Enqueue(Q,11), Enqueue(Q,40), Dequeue(Q), we get: Q 1 2 3 4 5 6 7 8 9 10 11 12 11 40 7 2 5 6 17 tail[q]=2 head[q]=7 6

Satellite data May have other satellite data along with each record (case details, name of plaintiff, etc) Typically: include a pointer for each element Q 1 2 3 4 5 6 7 8 9 10 11 12 3 7 2 5 6 head[q]=7 tail[q]=11 Case number 3 Name: Adam Smith Charge: Assigning hard problems Case number 7... Case number 2... Case number 5... Case number 6... 7

Pseudocode ENQUEUE(Q, x) 1. Q[tail[Q]] x 2. if tail[q] =length[q] 3. then tail[q] 1 4. else tail[q] tail[q] +1 Notice that this code doesn t handle what happens when the queue fills up or when it is empty! DEQUEUE(Q,x) 1. x Q[tail[Q]] 2. if head[q] =length[q] 3. then head[q] 1 4. else head[q] head[q] +1 5. return x 8

How long do the operations take? Enqueue: O(1) Dequeue: O(1) MakeNew: O(1) if memory implemented well Storage space = length of array n Maximum queue size limited to n Wastes space is size of L is much smaller than n What do you do when queue is full? Crash the program? (sometimes) Better solution: allocate bigger array 9

What about using a linked list? Dynamic structure uses memory flexibly Doubly linked list is a data structure collection of nodes Each node has at least three fields next (pointer) previous (pointer) key (depends on application: case number?) may have satellite data here too Keep two pointers for each list: head[q], tail[q] 3 7 2 5 5 10

Pseudocode for list-based queue Enqueue(Q, x) 1. newnode New node 2. key[newnode] x 3. prev[newnode] tail[q] 4. next[newnode] nil 5. next[tail[q]] newnode 6. tail[q] newnode Note no check for an empty list. Dequeue(Q, x) 1. oldnode head[q] 2. head[q] next[oldnode] 3. prev[head[q]] nil 4. return key[oldnode] Note: no deallocation, no check for an empty list. 11

What about using a linked list? How long do operations take? Enqueue: O(1) Dequeue: O(1) MakeNew: O(1) Storage: O(size(Q)), i.e. the number of elements currently in the queue Better storage use than array, right? But constants are better for arrays Clever allocation of memory can make array also use O(size(Q)) memory (we may see this in later lectures) 12

Stacks A stack holds a set of elements and supports Push(S,x): add element x to the top of stack S Pop(S): remove the top element from the stack and return its value MakeNew(): Create a new, empty stack 13

Example: Stacks Suppose you need to check if delimiters (parentheses and brackets) are properly balanced {(({}){()})} versus {(({}){})(})} Scan through the input, keeping a list of currently open delimiters When I meet an opening delimiter, add it to my list When I meet a closing delimiter, check if the last thing I added to my list was of the same type. If so, remove it and continue. If not, then output delimiters not matched. 14

How should we implement a stack? Usually: an array along with an index top Q 1 2 3 4 5 6 7 8 9 10 11 12 3 7 2 5 5 top[q]=11 As elements are added, increment top[q] As elements are removed, decrement top[q] Q 1 2 3 4 5 6 7 8 9 10 11 12 3 7 2 5 5 5 5 top[q]=11 15