Come up and say hello! 1. CPSC 221: Algorithms and Data Structures Lecture #0: Introduction Steve Wolfman 2011W2 2.

Slides:



Advertisements
Similar presentations
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,
Advertisements

ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Come up and say hello! 1. CPSC 221: Algorithms and Data Structures Lecture #0: Introduction Steve Wolfman 2009W1 2.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
CSE 326: Data Structures Introduction 1Data Structures - Introduction.
Come on down! Take and fill out a survey Get a copy of lecture slides Please sit in the first 5 rows!
Administrivia- Introduction CSE 373 Data Structures.
COMP171 Data Structures and Algorithm Qiang Yang Lecture 1 ( Fall 2006)
CSE 326: Data Structures Lecture #0 Introduction Steve Wolfman Winter Quarter 2000.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Stacks, Queues, and Deques
Come on down! Take and fill out a survey Get a copy of lecture slides Please sit in the first 5 rows!
Chapter 1 Introduction Definition of Algorithm An algorithm is a finite sequence of precise instructions for performing a computation or for solving.
CSE 326: Data Structures Lecture #0 Introduction Bart Niswonger Summer Quarter 2001.
CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues Dan Grossman Fall 2013.
Building Java Programs
CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues Lauren Milne Summer 2015.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
Data Structures and Programming.  Today:  Administrivia  Introduction to 225, Stacks  Course website: 
Data Structures Lecture 1: Introduction Azhar Maqsood NUST Institute of Information Technology (NIIT)
Come up and say hello! (Welcome!) 1. CPSC 221: Algorithms and Data Structures Lecture #0: Introduction Kendra Cooper 2014W1 2.
CSE332: Data Abstractions Lecture 1: Introduction; Stacks/Queues Tyler Robison Summer 2010.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
CSCA48 Course Summary.
CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues Nicki Dell Spring 2014.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
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,
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’
Come up and say hello! 1. CPSC 221: Algorithms and Data Structures Lecture #0: Introduction Steve Wolfman 2010W2 2.
CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues Kevin Quinn Fall 2015.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues Catie Baker Spring 2015.
Stacks And Queues Chapter 18.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues Linda Shapiro Winter 2015.
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.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Data Structures and Algorithms in Java AlaaEddin 2012.
CSE 326 More Lists, Stacks and Queues David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
0 of 36 Andries van Dam  /05/15 Reminder: TA Hour Policies If you hit a bug, you must be able to show significant effort of fixing it on your own.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues Aaron Bauer Winter 2014.
CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues Linda Shapiro Spring 2016.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Week 15 – Monday.  What did we talk about last time?  Tries.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
March 27 – Course introductions; Adts; Stacks and Queues
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Cinda Heeren / Geoffrey Tien
Week 15 – Monday CS221.
Cse 373 April 26th – Exam Review.
structures and their relationships." - Linus Torvalds
Introduction CSE 373 Data Structures.
CSC 143 Queues [Chapter 7].
Ben Lerner Summer Quarter 2007 Lecture 1
Stacks and Queues CLRS, Section 10.1.
Spring 2016 Richard Anderson Lecture 1
Stacks and Queues 1.
Administrivia- Introduction
Administrivia- Introduction
structures and their relationships." - Linus Torvalds
Presentation transcript:

Come up and say hello! 1

CPSC 221: Algorithms and Data Structures Lecture #0: Introduction Steve Wolfman 2011W2 2

Fibonacci 1, 1, 2, 3, 5, 8, 13, 21, … Applications, in order of importance: -Fun for CSists -Brief appearance in Da Vinci Code -Endlessly abundant in nature: u#p/u/1/ahXIMUkSXX0 3 =+ 0

Fibonacci Definition: 4 =+ 0

Fibonacci (Exact) Approximation: But how long to raise phi to the n power? What algorithm? 5 =+ 0

Today’s Outline Administrative Cruft Overview of the Course Queues Stacks 6

Course Information Your Instructor: Steve Wolfman ICCS 239 Office hours: see website Other Instructor: Alan Hu ( OHs: coming soon, see website ) TAs: Brendan Shillingford, Chuan Zhu, Jordan Cherry, Kenn Wan, Lawrence Cahoon, Mike Enescu, Riley Chang, Shailendra Agarwal, Stephanie Van Dyk; TA Office hours: coming soon, see website Texts: Epp Discrete Mathematics, Koffman C++ (But… feel free to get alternate texts or versions) 7

Course Policies No late work; may be flexible with advance notice Programming projects (~4) due 9PM on due date Written homework (~4) due 5PM on due date Grading –labs:5% –assignments:20% –midterm:30% –final:45% 8 Must pass the final to pass the course.

Collaboration READ the collaboration policy on the website. You have LOTS of freedom to collaborate! Use it to learn and have fun while doing it! Don’t violate the collaboration policy. There’s no point in doing so, and the penalties are so severe that just thinking about them causes babies to cry *. 9 *Almost anything causes babies to cry, actually, but the cheating penalties really are severe.

Course Mechanics 221 Web page: Vista site: Labs are in ICCS X350 –lab has SunRay thin clients: use the “Linux” logon All programming projects graded on UNIX/g++ 10

What is a Data Structure? data structure - 11

Observation All programs manipulate data –programs process, store, display, gather –data can be information, numbers, images, sound Each program must decide how to store and manipulate data Choice influences program at every level –execution speed –memory requirements –maintenance (debugging, extending, etc.) 12

Goals of the Course Become familiar with some of the fundamental data structures and algorithms in computer science Improve ability to solve problems abstractly –data structures and algorithms are the building blocks Improve ability to analyze your algorithms –prove correctness –gauge, compare, and improve time and space complexity Become modestly skilled with C++ and UNIX, but this is largely on your own! 13

What is an Abstract Data Type? Abstract Data Type (ADT) - 1) An opportunity for an acronym 2) Mathematical description of an object and the set of operations on the object 14

Data Structures as Algorithms Algorithm –A high level, language independent description of a step-by-step process for solving a problem Data Structure –A set of algorithms which implement an ADT 15

Why so many data structures? Ideal data structure: fast, elegant, memory efficient Generates tensions: –time vs. space –performance vs. elegance –generality vs. simplicity –one operation’s performance vs. another’s –serial performance vs. parallel performance “Dictionary” ADT –list –binary search tree –AVL tree –Splay tree –B tree –Red-Black tree –hash table –concurrent hash table –… 16

Code Implementation Theoretically –abstract base class describes ADT –inherited implementations implement data structures –can change data structures transparently (to client code) Practice –different implementations sometimes suggest different interfaces (generality vs. simplicity) –performance of a data structure may influence form of client code (time vs. space, one operation vs. another) 17

ADT Presentation Algorithm Present an ADT Motivate with some applications Repeat until browned entirely through –develop a data structure for the ADT –analyze its properties efficiency correctness limitations ease of programming Contrast data structure’s strengths and weaknesses –understand when to use each one 18

Queue ADT Queue operations –create –destroy –enqueue –dequeue –is_empty Queue property: if x is enqueued before y is enqueued, then x will be dequeued before y is dequeued. FIFO: First In First Out F E D C B enqueue dequeue GA 19

Applications of the Q Store people waiting to deposit their paycheques at a bank (historical note: people used to do this!) Hold jobs for a printer Store packets on network routers Hold memory “freelists” Make waitlists fair Breadth first search 20

Abstract Q Example enqueue R enqueue O dequeue enqueue T enqueue A enqueue T dequeue enqueue E dequeue 21 In order, what letters are dequeued? (Can we tell, just from the ADT?)

Circular Array Q Data Structure void enqueue(Object x) { Q[back] = x back = (back + 1) % size } Object dequeue() { x = Q[front] front = (front + 1) % size return x } bcdef Q 0 size - 1 frontback bool is_empty() { return (front == back) } bool is_full() { return front == (back + 1) % size } This is pseudocode. Do not correct my semicolons But.. is there anything else wrong? 22

Circular Array Q Example enqueue R enqueue O dequeue enqueue T enqueue A enqueue T dequeue enqueue E dequeue 23 What are the final contents of the array?

Circular Array Q Example enqueue R enqueue O dequeue enqueue T enqueue A enqueue T dequeue enqueue E dequeue 24 What are the final contents of the array? Assuming we can distinguish full and empty (could add a boolean)…

Linked List Q Data Structure (C++ linked list  Racket list) bcdef frontback void enqueue(Object x) { if (is_empty()) front = back = new Node(x); else { back->next = new Node(x); back = back->next; } Object dequeue() { assert(!is_empty); char result = front->data; Node * temp = front; front = front->next; delete temp; return result; } bool is_empty() { return front == NULL; } 25 This is not pseudocode.

Linked List Q Data Structure (C++ linked list  Racket list) bcdef frontback void enqueue(Object x) { if (is_empty()) front = back = new Node(x); else { back->next = new Node(x); back = back->next; } Object dequeue() { assert(!is_empty); char result = front->data; Node * temp = front; front = front->next; delete temp; return result; } bool is_empty() { return front == NULL; } 26 What’s with the red code? Welcome to manual memory management! Tip: “a delete for every new ”

Circular Array vs. Linked List 27

Stack ADT Stack operations –create –destroy –push –pop –top –is_empty Stack property: if x is pushed before y is pushed, then x will be popped after y is popped LIFO: Last In First Out A BCDEFBCDEF E D C B A F 28

Stacks in Practice Store pancakes on your plate (does it bother you that you eat them in the opposite order they were put down?) Function call stack Removing recursion Balancing symbols (parentheses) Evaluating Reverse Polish Notation Depth first search 29

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 1, n=4 30 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 2, n=4, a = fib(3) 31 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 1, n=3 Line 2, n=4, a = fib(3) 32 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 2, n=3, a = fib(2) Line 2, n=4, a = fib(3) 33 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 1, n=2 Line 2, n=3, a = fib(2) Line 2, n=4, a = fib(3) 34 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 1, n=2, return 1 Line 2, n=3, a = fib(2) Line 2, n=4, a = fib(3) 35 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 2, n=3, a = 1 Line 2, n=4, a = fib(3) 36 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 3, n=3, a = 1, b = fib(1) Line 2, n=4, a = fib(3) 37 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 1, n=1 Line 3, n=3, a = 1, b = fib(1) Line 2, n=4, a = fib(3) 38 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 1, n=1, return 1 Line 3, n=3, a = 1, b = fib(1) Line 2, n=4, a = fib(3) 39 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 3, n=3, a = 1, b = 1 Line 2, n=4, a = fib(3) 40 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 4, n=3, a = 1, b = 1, return 2 Line 2, n=4, a = fib(3) 41 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 3, n=4, a = 2, b = fib(2) 42 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 1, n=2 Line 3, n=4, a = 2, b = fib(2) 43 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 1, n=2, return 1 Line 3, n=4, a = 2, b = fib(2) 44 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 3, n=4, a = 2, b = 1 45 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } Line 3, n=4, a = 2, b = 1, return 3 46 Suppose we call fib(4) : (location of fib(4) call goes here)

Example Stolen from Alan “Call Stack” and Recursion int fib(int n) { 1.if (n <= 2) return 1; 2.int a = fib(n-1); 3.int b = fib(n-2); 4.return a+b; } 47 Suppose we call fib(4) : (code that called fib(4) resumes w/value 3)

Array Stack Data Structure S 0 size - 1 fedcb void push(Object x) { assert(!is_full()) S[back] = x back++ } Object top() { assert(!is_empty()) return S[back - 1] } back Object pop() { assert(!is_empty()) back-- return S[back] } bool is_empty() { return back == 0 } bool is_full() { return back == size } 48

Linked List Stack Data Structure bcdef back void push(Object x) { temp = back back = new Node(x) back->next = temp } Object top() { assert(!is_empty()) return back->data } Object pop() { assert(!is_empty()) return_data = back->data temp = back back = back->next delete temp return return_data } bool is_empty() { return back.is_empty(); } 49

Data structures you should already know (a bit) Arrays Linked lists Trees Queues Stacks 50

To Do Check out the web page and Vista Download and read over Lab 1 materials Begin working through Chapters P and 1 of Koffman and Wolfgang (C++ background) –DO the exercises! –ASK questions! Read sections , 5, and 6 except 6.4 of Koffman and Wolfgang (linked lists, stacks, and queues) 51

Coming Up Asymptotic Analysis Theory Assignment 1 Programming Project 1 52