CPSC 221: Algorithms and Data Structures Lecture #0: Introduction. Come up and say hello! Fibonacci. Fibonacci. Fibonacci. Fibonacci. (Welcome!

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

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

Unit Outline. Course Work

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

CSE 332: Data Abstractions. Ruth Anderson Spring 2014 Lecture 1

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Lauren Milne Summer 2015

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

COURSE MECHANICS. Welcome! CSE 332 Data Abstractions: Introduction and ADTs. Concise to-do list. Today in Class. Instructor: Kate Deibel

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

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

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.

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

12 Abstract Data Types

COMP Data Structures

COMP Data Structures

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

Unit #3: Recursion, Induction, and Loop Invariants

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Unit 10: Data Structures CS 101, Fall 2018

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

COMP251: Algorithms and Data Structures. Jérôme Waldispühl School of Computer Science McGill University

Data Structures and Algorithms

CSE373: Data Structures and Algorithms Lecture 1: Introduc<on; ADTs; Stacks/Queues

CSE 373: Data Structures and Algorithms

infix expressions (review)

3137 Data Structures and Algorithms in C++

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

CS 241 Data Organization. August 21, 2018

CSC148 Week 2. Larry Zhang

Lecture 2: Implementing ADTs

LIFO : Last In First Out

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Lecture 4 Stack and Queue

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

CS2013 Course Syllabus Spring 2018 Lecture: Mon/Wed 2:00 P.M. 2:50 P.M. SH C259 Lab: Mon/Wed 2:50 P.M. 4:00 P.M. SH C259

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Stacks and queues (chapters 6.6, 15.1, 15.5)

15-122: Principles of Imperative Computation, Fall 2015

Name CPTR246 Spring '17 (100 total points) Exam 3

CS 210 Algorithms and Data Structures College of Information Technology and Engineering Weisberg Division of Engineering and Computer Science

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

COMP Analysis of Algorithms & Data Structures

COLLEGE OF THE DESERT

VE281 Data Structures and Algorithms. Introduction and Asymptotic Algorithm Analysis

csci 210: Data Structures Stacks and Queues

CS 240 Fall 2015 Section 004. Alvin Chao, Professor

Lecture 3: Stacks & Queues

ECE250: Algorithms and Data Structures Midterm Review

CS 216 Exam 1 Fall SOLUTION

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

ITI Introduction to Computing II

STACKS AND QUEUES. Problem Solving with Computers-II

CS 240 Fall Mike Lam, Professor. Just-for-fun survey:

Standard ADTs. Lecture 19 CS2110 Summer 2009

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Welcome to CS61A! Last modified: Thu Jan 23 03:58: CS61A: Lecture #1 1

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

Lecture Data Structure Stack

Postfix (and prefix) notation

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

Arrays and Linked Lists

Data Abstraction and Specification of ADTs

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Data Structures. Lecture 1: Introduction CSC212. Instructor: George Wolberg Department of Computer Science City College of New York

Announcements. 1. Forms to return today after class:

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Computer Science 210: Data Structures

Stacks and Queues. CSE 373 Data Structures Lecture 6

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Recursion. Recursion [Bono] 1


Unit #2: Recursion, Induction, and Loop Invariants

ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

UNIVERSITY REGULATIONS

Queues Fall 2018 Margaret Reid-Miller

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

CS 4349 Lecture August 21st, 2017

Welcome to CS 241 Systems Programming at Illinois

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees. Linda Shapiro Spring 2016

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

Draw a diagram of an empty circular queue and describe it to the reader.

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

Review of asymptotic analysis Empirical performance evaluation Review of foundational data structures

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

MLR Institute of Technology

Introduction to Data Structures

== isn t always equal?

Algorithms and Data Structures Spring 2008

CSCI 136 Data Structures & Advanced Programming. Spring 2018 Instructors Bill Jannen & Jon Park

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Syllabus CS 301: Data Structures Spring 2015

15. Stacks and Queues

Transcription:

Come up and say hello! (Welcome!) CPSC 221: Algorithms and Data Structures Lecture #0: Introduction Kendra Cooper 2014W1 1 2 Fibonacci 0 + = Fibonacci 0 + = 1, 1, 2, 3, 5, 8, 13, 21, Definition: Applications, in order of importance: - Fun for CSists - Brief appearance in Da Vinci Code - Endlessly abundant in nature: http://www.youtube.com/user/vihart?feature=g-u#p/u/1/ahximuksxx0 3 4 Fibonacci 0 + = Fibonacci 0 + = Easy to implement as recursive code, right? (Exact) Approximation: Let s try it. (Then, we ll open up a big can of data structures and algorithms on that problem.) But how long to raise phi to the n power? What algorithm? 5 6 1

Organizational Administrative Overview of the Course First course topics Queues Stacks Today s Outline Course Information Your Instructor: Kendra Cooper ICCS 235 ckendra@cs.ubc.ca Office hours: see website Other Instructor: Steve Wolfman (OHs: see website) Tas and their office hours: see website, more may be posted! Texts: Epp Discrete Mathematics, Koffman C++ (But feel free to get alternate texts or versions) 7 8 Course Policies Collaboration No late work; may be flexible with advance notice (why? so we can post solutions/discussion quickly!) Programming projects (~3?) due 9PM, usually Fridays Quizzes (~5?) online on Fridays (due time TBD) Written corrections of quizzes due 5PM the following Wed Grading labs: 10% quizzes/written assns: 15% programming projects: 15% midterms: 25% final: 30% your best of the above: 5% Must pass the final to pass the course. 9 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 *. 10 *Almost anything causes babies to cry, actually, but the cheating penalties really are severe. Course Mechanics 221 Web page: www.ugrad.cs.ubc.ca/~cs221 221 Piazza site at piazza.com Quizzes and grades on Connect Labs are in ICCS X350 and X251 use the Linux logon to the computers All programming projects graded on UNIX/g++ Organizational Administrative Overview of the Course First course topics Queues Stacks Today s Outline 11 12 2

What is a Data Structure? data structure - Observation All programs manipulate data programs process, store, display, gather data can be numbers, images, sound (information!) Each program must decide how to store and manipulate data Choice influences program at every level execution speed memory requirements maintenance (debugging, extending, etc.) 13 14 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! 15 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 Maybe more usefully: a description of how a data structure works.. which could be implemented by 16 many different actual data structures. Data Structures as Algorithms Why so many data structures? 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 17 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 or Map ADT list binary search tree AVL tree Splay tree B+ tree Red-Black tree hash table concurrent hash table 18 3

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) 19 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 20 understand when to use each one Today s Outline Queue ADT Organizational Administrative Overview of the Course First course topics Queues Stacks 21 Queue operations create destroy enqueue is_empty enqueue G F E D C B A Queue property: if x is enqueued before y is enqueued, then x will be d before y is d. FIFO: First In First Out 22 Applications of the Q Abstract Q Example 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 UBC s waitlists fair! Breadth first search 23 enqueue R enqueue O enqueue A enqueue E In order, what letters are d? (Can we tell, just from the ADT?) 24 4

Circular Array Q Data Structure void enqueue(object x) { Q[back] = x back = (back + 1) % size Object () { x = Q[front] front = (front + 1) % size return x Q 0 size - 1 b c d e f front back bool is_empty() { return (front == back) bool is_full() { return front == (back + 1) % size This is pseudocode. Do not correct my semicolons 25 But.. is there anything else wrong? enqueue R enqueue O enqueue A enqueue E Circular Array Q Example What are the final contents of the array? 26 enqueue R enqueue O enqueue A enqueue E Circular Array Q Example Assuming we can distinguish full and empty (could add a boolean) What are the final contents of the array? 27 Linked List Q Data Structure (C++ linked list Racket list) b c d e f front void enqueue(object x) { if (is_empty()) { front = back = new Node(x); else { back->next = new Node(x); back = back->next; This is not pseudocode. Let s draw a memory diagram of how it works! back Object () { assert(!is_empty()); char result = front->data; Node * temp = front; front = front->next; delete temp; return result; bool is_empty() { return front == NULL; 28 Linked List Q Data Structure (C++ linked list Racket list) Circular Array vs. Linked List b c d e f front back void enqueue(object x) { if (is_empty()) { front = back = new Node(x); else { back->next = new Node(x); back = back->next; What s with the red code? Welcome to manual memory management! Tip: a delete for every new Object () { assert(!is_empty()); char result = front->data; Node * temp = front; front = front->next; delete temp; return result; bool is_empty() { return front == NULL; 29 30 5

Organizational Administrative Overview of the Course First course topics Queues Stacks Today s Outline 31 Stack operations create destroy push top is_empty Stack ADT A B C D E F E D C B A Stack property: if x is pushed before y is pushed, then x will be ped after y is ped LIFO: Last In First Out F 32 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 Implementing/removing recursion Balancing symbols (parentheses) Evaluating Reverse Polish Notation Depth first search 33 Example Stolen from Alan Hu This is our stack. It s a stack of program points and variable values. Line 1, n=4 34 Making a function call pushes a new frame onto the stack. Making a function call pushes a new frame onto the stack. 35 Line 1, n=3 36 6

Line 2, n=3, a = fib(2) 37 Line 1, n=2 Line 2, n=3, a = fib(2) 38 Returning from a call s the old frame off the stack. Returning from a call s the old frame off the stack. Line 1, n=2, return 1 Line 2, n=3, a = fib(2) 39 Line 2, n=3, a = 1 40 Line 3, n=3, a = 1, b = fib(1) 41 Line 1, n=1 Line 3, n=3, a = 1, b = fib(1) 42 7

Line 1, n=1, return 1 Line 3, n=3, a = 1, b = fib(1) 43 Line 3, n=3, a = 1, b = 1 44 Line 4, n=3, a = 1, b = 1, return 2 45 Line 3, n=4, a = 2, b = fib(2) 46 Line 1, n=2 Line 3, n=4, a = 2, b = fib(2) 47 Line 1, n=2, return 1 Line 3, n=4, a = 2, b = fib(2) 48 8

Line 3, n=4, a = 2, b = 1 49 Line 3, n=4, a = 2, b = 1, return 3 50 51 (code that called fib(4) resumes w/value 3) Array Stack Data Structure S 0 size - 1 f e d c b back void push(object x) { assert(!is_full()) S[back] = x back++ Object top() { assert(!is_empty()) return S[back - 1] Object () { assert(!is_empty()) back-- return S[back] bool is_empty() { return back == 0 bool is_full() { return back == size 52 push K push C push A push B Let s Trace How It Works 53 Linked List Stack Data Structure b c d e f back void push(object x) { temp = back back = new Node(x) back->next = temp Object top() { assert(!is_empty()) return back->data Object () { assert(!is_empty()) return_data = back->data temp = back back = back->next delete temp return return_data bool is_empty() { return back.is_empty(); 54 9

push K push C push A push B Let s Trace How It Works Data structures you should already know (a bit) Arrays Linked lists Trees Queues Stacks 55 56 To Do Coming Up Check out the web page and Piazza Download and read over Lab 1 materials Begin working through Chapters P and 1 of Koffman and Wolfgang (C++ background) Read 4.5-4.7, 5, and 6 (except 6.4) of Koffman and Wolfgang (linked lists, stacks, queues) ALWAYS when reading the text: DO the exercises! ASK questions! 57 Asymptotic Analysis Quiz/Written Assignment 1 Programming Project 1 58 10