COMPSCI 107 Computer Science Fundamentals

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
COMPSCI 105 SS 2015 Principles of Computer Science Queues.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Topic 15 Implementing and Using Stacks
Stacks.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Topic 15 Implementing and Using Stacks
Topic 3 The Stack ADT.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
Stacks and Queues Introduction to Computing Science and Programming I.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
Computer Science 112 Fundamentals of Programming II Introduction to Stacks.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Introduction. 2COMPSCI Computer Science Fundamentals.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Stacks And Queues Chapter 18.
Arrays and Collections Tonga Institute of Higher Education.
COMP 1321 Digital Infrastructure Richard Henson University of Worcester October 2012.
Data Structures & Algorithms
COMPSCI 105 SS 2015 Principles of Computer Science 09 ADT & Stacks.
CSC 231 Stacks 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in Office.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Stack – Data Structure. Stack Definition class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item):
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
Click to edit Master text styles Stacks Data Structure.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
BCA II Data Structure Using C
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
ADT’s, Collections/Generics and Iterators
Understand Computer Storage and Data Types
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
CSE 373: Data Structures and Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
i206: Lecture 10: Lists, Stacks, Queues
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
COMPUTER 2430 Object Oriented Programming and Data Structures I
Recall: ROM example Here are three functions, V2V1V0, implemented with an 8 x 3 ROM. Blue crosses (X) indicate connections between decoder outputs and.
Introduction to Data Structure
Topic 15 Implementing and Using Stacks
Stacks Chapter 5.
Some Assembly (Part 2) set.html.
CS210- Lecture 3 Jun 6, 2005 Announcements
Computer Organization and Assembly Language
Data Structures and Algorithms 2/2561
Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Abstract Data Types Stacks CSCI 240
Introduction to Computer Science
Presentation transcript:

COMPSCI 107 Computer Science Fundamentals Lecture 13 – ADTs and Stacks

Lab 4 this week PeerWise questions Remember to keep in mind: Your question must be very clearly worded You should provide a useful, detailed explanation You may also improve explanations that are missing or inadequate Avoid “trick” questions!

Test Thursday 13th April Content Format During normal lecture time All material covered up until end of lectures this week Labs 01 – 05 Format Short answer questions Code writing Code reading Order analysis

Test Structure Assigned seats in groups of 4 Used for discussion phase and bonus calculation Test1: Determines marks for the test 20 minutes Discussion phase in assigned groups 10 minutes Test2: Determines possible bonus marks Possible bonus marks: (group average for Test1) – (group average for Test2)

Abstraction A fundamental idea

Abstraction A fundamental idea

Abstraction Where have you seen “abstraction” so far?

count = count + 1 Abstraction Where have you seen “abstraction” so far? count = count + 1

Abstraction Where have you seen “abstraction” so far? CPU instructions Process of addition mov eax 0x33fa2c mov ebx 1 add eax ebx mov 0x33fa2c eax Move data from memory address to register ‘eax’ Move value 1 to register ‘ebx’ Add values in registers and store result in ‘eax’ Store the result of the addition back to memory The main memory (the "RAM") in personal computers is dynamic RAM (DRAM). It is the RAM in desktops, laptops and workstation computers as well as some of the RAM of video game consoles.

(dynamic random access memory) Abstraction Where have you seen “abstraction” so far? Addressable data (bytes made up of bits) 1 0 0 0 1 1 0 1 0x33fa2c The main memory (the "RAM") in personal computers is dynamic RAM (DRAM). It is the RAM in desktops, laptops and workstation computers as well as some of the RAM of video game consoles. “main memory” RAM chip (dynamic random access memory)

Abstraction Where have you seen “abstraction” so far? A memory cell (dynamic random access memory) transistor (controls access) A transistor and a capacitor are paired to create a memory cell, which represents a single bit of data. The capacitor holds the bit of information -- a 0 or a 1 (see How Bits and Bytes Work for information on bits). The transistor acts as a switch that lets the control circuitry on the memory chip read the capacitor or change its state. capacitor (stores charge) Source: http://en.wikipedia.org/wiki/Memory_cell_(binary)

count = count + 1 Abstraction Where have you seen “abstraction” so far? relevant, simpler ideas count = count + 1

count = count + 1 Abstraction Where have you seen “abstraction” so far? relevant, simpler ideas count = count + 1 Abstraction complex details

Abstraction Other kinds of abstraction that you are familiar with Procedural abstraction

a function to test if n is prime Abstraction Other kinds of abstraction that you are familiar with Procedural abstraction A function definition encapsulates a set of program statements. The function name then becomes an abstraction that we can use to help simplify the logic of our program. Abstraction a function to test if n is prime

Abstraction Other kinds of abstraction that you are familiar with Data abstraction We can encapsulate related data items into a single type of object

a Person object has a name and an age Abstraction Other kinds of abstraction that you are familiar with Data abstraction A class definition encapsulates a set of data values along with a set of methods that operate on those values. The class name then becomes an abstraction that allows us to work with objects that simplify the logic of our program. Abstraction a Person object has a name and an age

Abstract data types An abstract data type (ADT) is a specification of: a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations

Abstract data types An abstract data type (ADT) is a specification of: a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations Request to perform ADT operation Program Result of ADT operation ADT operations

Abstract data types An abstract data type (ADT) is a specification of: a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations Request to perform ADT operation Data structure implementing the ADT interface Program Result of ADT operation ADT operations

Abstract data types An abstract data type (ADT) is a specification of: a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations Data structure implementing the ADT interface Request to perform ADT operation Data structure implementing the ADT interface Program Result of ADT operation A KEY IDEA: We can easily replace one implementation with another (better) one, if the program only accesses the agreed ADT operations ADT operations

An example – balanced brackets Consider the following pairs of brackets: () <> [] {}

An example – balanced brackets Consider the following pairs of brackets We can have expressions (consisting of arbitrary letters and brackets) where the brackets are perfectly balanced, like this: ([x{y}z<>]) brackets are balanced when read from left to right

An example – balanced brackets Consider the following pairs of brackets We can have expressions (consisting of arbitrary letters and brackets) where the brackets are perfectly balanced However we can also have expressions where the brackets are not balanced, like this: ([x{y<z}>])

An example – balanced brackets Are the brackets in this expression balanced or not? <<j{g{h{{[h[a{[j](a{q(<<[(){t}[[](j<p<a><s{(t{d<j[[c({x<y>({<<y<>{({}){f}<>(j)}{}[b<k>]>[x]>}){}})]]>})}>>)]]>>)})}]]}}}}>> These are balanced

What is a “stack”? We are going to cover: The “Stack” Abstract Data Type Two different implementations of the Stack ADT We will then use a stack to solve our balanced brackets problem

What is a “stack”? A stack is an ordered collection of items where the addition of new items and the removal of existing items always takes place at the same end, referred to as the top of the stack. i.e. add new elements to the top, remove existing elements from the top Last-in, first-out (LIFO) property The last item placed on the stack will be the first item removed Example: A stack of dishes in a cafeteria

Last In - First Out (LIFO) What is a “stack”? We add only to the top of a stack (called a “push”) We remove only from the top of the stack (called a “pop”) Add a new element push “103” Remove top element pop Remove top element pop 34 57 12 34 57 12 103 34 57 12 34 57 Last In - First Out (LIFO)

The Stack ADT The “Stack” ADT Data: Operations: an ordered collection of elements Operations: create a new empty stack (Stack()) determine whether a stack is empty (is_empty()) add a new element to the stack (push) remove the most recently added element from the stack (pop) look at (but don’t remove) the most recently added element (peek) determine the size of a stack (how many elements) (size)

The Stack ADT An example s = Stack() print(s.is_empty()) s.push(5) s.push('cat') print(s.peek()) s.push(True) print(s.size()) s.push(8.4) print(s.pop())

Exercise Show the contents of the stack, and the output produced. s = Stack() print(s.is_empty()) s.push(5) s.push('cat') print(s.peek()) s.push(True) print(s.size()) s.push(8.4) print(s.pop())

The Stack ADT An example True cat 3 False 8.4 2 s = Stack() print(s.is_empty()) s.push(5) s.push('cat') print(s.peek()) s.push(True) print(s.size()) s.push(8.4) print(s.pop()) True cat 3 False 8.4 2

Exercise OK, so how can we actually implement the Stack ADT? What data structure would be appropriate?

Stack Implementation OK, so how can we actually implement the Stack ADT? What data structure would be appropriate? We will use a Python list Remember: the addition of new items and the removal of existing items always takes place at the same end, referred to as the top of the stack But which end of the Python list is better for our Stack implementation? Python List

Stack Implementation Version 1: the beginning of the list will hold the top element of the stack push(…) pop() class Stack: ... def push(self, item): self.items.insert(0,item) def pop(self): return self.items.pop(0) What is the Big-O of the push()/pop() here?

Stack Implementation Version 2: the end of the list will hold the top element of the stack class Stack: ... def push(self, item): self.items.append(item) def pop(self): return self.items.pop() push(…) pop() What is the Big-O of the push()/pop() here?

Balanced brackets Back to our example of balanced brackets Ensure that pairs of brackets are properly matched: e.g. Possible errors: {a,(b+f[4])*3,d+f[5]} (..)..) // too many closing brackets (..(..) // too many open brackets [..(..]..) // mismatched brackets

Balanced brackets initialise the stack to empty for every char read Algorithm: initialise the stack to empty for every char read if it is an open bracket then push onto stack if it is a close bracket, then if the stack is empty, return ERROR pop from the stack if they don’t match then return ERROR if it is a non-bracket, skip the character if the stack is NON-EMPTY, ERROR

Balanced brackets Example: {a,(b+f[4])*3,d+f[5]} top { top ( { top [ (

[ { ( ) ] Balanced brackets Example: pop: ] and { Not matched!! top [

Exercise Implement the Stack ADT using a Python list, such that all operations are O(1). class Stack:   def __init__(self): def is_empty(self): def push(self, item): def pop(self): def peek(self): def size(self):