Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Come up and say hello! 1. CPSC 221: Algorithms and Data Structures Lecture #0: Introduction Steve Wolfman 2010W2 2."— Presentation transcript:

1 Come up and say hello! 1

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

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

4 Course Information Your Instructor: Steve Wolfman ICCS 239 wolf@cs.ubc.ca Office hours: see website Other Instructor: Alan Hu TAs: Alan Long, Benj Israel, Essex Edwards, Haoyu Zhou, Joel Kitching, Kenny Wan, Koosha Khalvati, Mohamed El-Zohairy, Norman Huang, and Ziyu Wang TA Office hours: TBA; see website Texts: Epp Discrete Mathematics, Koffman C++ (But… feel free to get Epp 3 rd ed or alternate texts) 4

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

6 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 *. 6 *Almost anything causes babies to cry, actually, but the cheating penalties really are severe.

7 Course Mechanics 221 Web page: www.ugrad.cs.ubc.ca/~cs221 221 Matygo site: www.matygo.com –What’s Matygo? Why are we using it? A startup classroom management system created by UBC CS graduates that looks extremely promising. We and CPSC 211 will be early adopters. Should be fun! Labs are in ICCS X350 –lab has SunRay thin clients: use the “Linux” logon All programming projects graded on UNIX/g++ 7

8 What is a Data Structure? data structure - 8

9 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.) 9

10 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! 10

11 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 11

12 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 12

13 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 “Dictionary” ADT –list –binary search tree –AVL tree –Splay tree –B tree –Red-Black tree –hash table –… 13

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

15 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 15

16 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 F EDCBAEDCBA B C D E F A 16

17 Stacks in Practice Function call stack Removing recursion Balancing symbols (parentheses) Evaluating Reverse Polish Notation Depth first search Java VM execution engine 17

18 Abstract Stack Example push R push O pop push T push A push T pop push E pop 18 In order, what letters are popped? (Can we tell, just from the ADT?)

19 Array Stack Data Structure S 0 size - 1 abcde 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 } 19 This is pseudocode. Do not correct my semicolons

20 Linked List Stack Data Structure edcba 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 == null } 20

21 Linked List Stack Data Structure 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 == null } 21 What’s with the red text? edcba back

22 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 22

23 Applications of the Q Hold jobs for a printer Store packets on network routers Hold memory “freelists” Make waitlists fair Breadth first search 23

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

25 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 } 25

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

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

28 Linked List Q Data Structure 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) return_data = front->data temp = front front = front->next delete temp return return_data } bool is_empty() { return front == null } 28

29 Circular Array vs. Linked List 29

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

31 To Do Check out the web page and Matygo Begin working through Chapters P and 1 of Koffman and Wolfgang (C++ background) Read sections 4.5-4.7, 5, and 6 except 6.4 of Koffman and Wolfgang (linked lists, stacks, and queues) 31

32 Coming Up Asymptotic Analysis First Lab (download and look over materials as soon as they’re posted!) Programming Project 1 (solving mazes with stacks and queues) 32


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

Similar presentations


Ads by Google