CS532: Design and Analysis of Algorithms. Overview. Linked Lists. Data Structures Intro. Classes, Objects, and Pointers.

Similar documents
Algorithms. 演算法 Data Structures (focus on Trees)

Elementary Data Structures and Hash Tables

3. Fundamental Data Structures

Introduction to Data Structures

Introduction to Data Structures. Introduction to Data Structures. Introduction to Data Structures. Data Structures

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

Algorithms and Data Structures for Mathematicians

1 Lower bound on runtime of comparison sorts

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

data structures and algorithms lecture 6

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

Lecture 4: Elementary Data Structures Steven Skiena

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

III Data Structures. Dynamic sets

Unit 10: Data Structures CS 101, Fall 2018

Data Structures I: Linked Lists

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

Data Structure (CS301)

Linked Lists and Abstract Data Structures A brief comparison

Basic Data Structures (Version 7) Name:

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Programming Abstractions

CS301 - Data Structures Glossary By

CS 331 Midterm Exam 2

Elementary Data Structures and Hash Tables

Postfix (and prefix) notation

Linked Lists. Linked List vs Array vs Python List Singly Linked List Bag ADT Revisited Building Linked Lists

Largest Online Community of VU Students

CMSC Introduction to Algorithms Spring 2012 Lecture 7

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

811312A Data Structures and Algorithms, , Exercise 1 Solutions

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Summer Final Exam Review Session August 5, 2009

Chapter 8: Data Abstractions

CS350 - Exam 1 (100 Points)

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Basic Data Structures and Heaps

1 P age DS & OOPS / UNIT II

Design and Analysis of Algorithms

Data Structure. IBPS SO (IT- Officer) Exam 2017

The Tail has the pointer value z.next = NIL. the pointer to the next object x.next, down arrows.

CS 331 Midterm Exam 2

Heaps. Heaps Priority Queue Revisit HeapSort

n 1 i = n + i = n + f(n 1)

CS 331 Midterm Exam 2

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Linked Structures Chapter John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Structure and Interpretation of Computer Programs

Programming II (CS300)

Queues. Queue ADT Queue Implementation Priority Queues

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

8. Fundamental Data Structures

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

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Chapter 8: Data Abstractions

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

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

CSE 230 Intermediate Programming in C and C++

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Homework notes. Homework 2 grades posted on canvas. Homework 3 due tomorrow. Homework 4 posted on canvas. Due Tuesday, Oct. 3

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

CP2 Revision. theme: dynamic datatypes & data structures

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

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Fundamentals of Data Structure

Problem Set 5 Due: Friday, November 2

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists

Structure and Interpretation of Computer Programs

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

Algorithm Design and Analysis

Introduction to Data Structures

CS 331 Fall 2017 Midterm Exam 2

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

CSCA48 Term Test 1 Seminar

Announcements. CS18000: Problem Solving And Object-Oriented Programming

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

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

Course Review. Cpt S 223 Fall 2009

WYSE Academic Challenge State Finals Computer Science 2007 Solution Set

Ashish Gupta, Data JUET, Guna

Trees Algorhyme by Radia Perlman

DATA STRUCTURES AND ALGORITHMS

Department of Computer Science and Technology

Linked Lists

Spring 2017 B-TREES (LOOSELY BASED ON THE COW BOOK: CH. 10) 1/29/17 CS 564: Database Management Systems, Jignesh M. Patel 1

PA3 Design Specification

Data Structures Question Bank Multiple Choice

CS1210 Lecture 28 Mar. 27, 2019

Data Structures and Algorithms

INSTITUTE OF AERONAUTICAL ENGINEERING

What is an algorithm?

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

Dictionaries. Priority Queues

Programming. Lists, Stacks, Queues

Computer Science Fundamentals 107

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

Transcription:

CS533 Class 01: 1 c P. Heeman, 2017 What is the Course About? Data Structures Intro Classes, Objects, and Pointers Linked Lists Rooted Trees Overview CS533 Class 01: 2 c P. Heeman, 2017 CS532: Design and Analysis of Algorithms Monday & Wednesday at 4:00pm to 5:30pm Starts June 25th Learning Objectives: - Improve your programming skills + Including complex data structures + Object oriented programming - Know how to analyze the efficiency of an algorithm + How much time does it take as the size of the input grows - Know how to prove the correctness of an algorithm

CS533 Class 01: 3 c P. Heeman, 2017 Background Needed Intended to be taken earlier in your education Anyone admitted to the program - At a minimum, expected to have 1st year undergraduate CS major courses A course in discrete math also useful CS533 Class 01: 4 c P. Heeman, 2017 Logistics Grading 50% homework 25% midterm 25% final Course website: cslu.ohsu.edu/ heeman/cs532

CS533 Class 01: 5 c P. Heeman, 2017 Homework Homework is due Friday at 11:59pm - If needed, you can email me with a request for an extension - Extension for one homework will be automatically granted Homework must be submitted through Sakei - If you have problems, email it to me heemanp@ohsu.edu - Single pdf with your answers on it + Homework should be typeset + Can include pictures of hand-drawn solutions taken with cellphone - Turn in code as well + We will supply you with a code that will run your code on several examples + Make sure that your code interfaces with this correctly + We will test it other cases CS533 Class 01: 6 c P. Heeman, 2017 Critique Answer key will be emailed to you when you submit your answers - You will have until Sunday at 11:59pm to submit a critique - Explain what you did wrong, and why you made that mistake - Worth up to half the marks that you lost + Really good explanations might even get more - Should show that you reviewed and understood answer key and understood whether you answer was correct - See sample homework for how to format this

CS533 Class 01: 7 c P. Heeman, 2017 Academic Integrity You can do the homeworks with your colleagues - Document how you collaborated - e.g., worked on entire question together, versus clarified what the question meant After you have come up with a solution, redo it on your own - Nothing in, nothing out, wait at least an hour between CS533 Class 01: 8 c P. Heeman, 2017 C versus Python Could have taught this course in C - Pointers are built into the language - C does not have a built-in dictionary datatype - But few people know C, so would spend a lot of time teaching this Instead using Python - Don t use dictionaries or advanced packages - Use classes/objects to get pointers - Plus: get more in-depth experience with a programming language - Plus: get a solid foundation in object-oriented programming + Not in textbook

CS533 Class 01: 9 c P. Heeman, 2017 What is the Course About? Data Structures Intro Classes, Objects, and Pointers Linked Lists Rooted Trees Overview CS533 Class 01: 10 c P. Heeman, 2017 Dynamic Sets From: III Data Structures of textbook Set: collection of elements (like integers) Dynamic sets - Can be manipulated by algorithms - Can grow, shrink, or otherwise change over time Different algorithms might require different operations - Many algorithms just need to insert, delete, and test membership + Dynamic set that supports this is called a dictionary - Some algorithms need to insert, and extract the smallest element + Min-priority queue - What operations are needed by an algorithm and how efficiently they need to be done determines what data structure type to use

CS533 Class 01: 11 c P. Heeman, 2017 Chapter 10: Elementary Data Structures Arrays - Elements are numbered starting at 0 (or 1) - Can access any element: A[0] A[5] + Read, write any element Stacks and Queues - For some tasks, you want to restrict how elements are accessed Stack: last-in, first-out - Write an element to the stack, can read/pop most recent element added - When it is read/popped, no longer on stack Queue: first-in, first-out - Can write an element to queue, can read/dequeue the oldest element - When it is read/dequeued, no longer in queue CS533 Class 01: 12 c P. Heeman, 2017 Implementation for Stack Stack - Assume a maximum size of stackn - Use an array of sizens[0...n-1] - Start pushing elements starting at index 0 - S top keeps track of the most recently added element

CS533 Class 01: 13 c P. Heeman, 2017 def Stack-Empty(S): if S_top == 0: return True else: return False def Push(S,x): S_top = S_top + 1 S[S_top] = x Code for Stack def Pop(S): if Stack-Empty(S): error "underflow" S_top = S_top - 1 return S[S_top+1] CS533 Class 01: 14 c P. Heeman, 2017 Implementation for Queues Queues - Assume a maximum size ofn - Use an array of sizens[0...n-1] - Enqueue: put on queue - Dequeue: take off of queue - Need two variables: + Q head (next to be dequeued) + Q tail (most recently added) 1 2 3 4 5 6 7 8 9 10 11 12 (a) Q 15 6 9 8 4 Q:head D 7 Q:tail D 12 1 2 3 4 5 6 7 8 9 10 11 12 (b) Q 3 5 15 6 9 8 4 17 Q:tail D 3 Q:head D 7 1 2 3 4 5 6 7 8 9 10 11 12 (c) Q 3 5 15 6 9 8 4 17 Q:tail D 3 Q:head D 8

CS533 Class 01: 15 c P. Heeman, 2017 What is the empty queue? Continued What is maximum number of elements that can be stored? Why not have Q tail point at last element, rather than next space? CS533 Class 01: 16 c P. Heeman, 2017 Code for Queues def Enqueue(Q,x): Q[Q_tail] = x if Q_tail == Q_length: Q_tail = 1 else: Q_tail = Q_tail + 1 def Dequeue(Q): x = Q[Q_head] if Q_head == Q.length: Q_head = 1 else: Q_head = Q_head + 1 return x Code assumes queue array starts at 1

CS533 Class 01: 17 c P. Heeman, 2017 What is the Course About? Data Structures Intro Classes, Objects, and Pointers Linked Lists Rooted Trees Overview CS533 Class 01: 18 c P. Heeman, 2017 Groups of Information Will need more complex data structures Need data structure that can encapsulate pieces of information - like an street address, city, zipcode, phone number Elements of a dynamic set - Usually consist of a number of types of information - Usually one type is viewed as the key, used for searching, or sorting - Rest is viewed as satellite data

CS533 Class 01: 19 c P. Heeman, 2017 Reference other set elements Also, need a way to refer to other pieces of data Example: family tree - For each person, want to store name, date of birth, gender - Want to list who their parents are + Not just their names, but a way that we can access all the data about them CS533 Class 01: 20 c P. Heeman, 2017 Python How can we do this in python? - Have an array for name, date of birth, gender - Each person has an unique index into arrays - Can have an array for father and mother to, which has the index for the father and mother of each person Kind of ugly

CS533 Class 01: 21 c P. Heeman, 2017 Python with Classes Python has classes Classes allow you to - Group data fields together as an object - Refer to an object with a constant or a variable - Include a var in the object that refers to another object - Associate functions on the group An instance of a group (or class) is called an object Will use classes in constructing complex data types CS533 Class 01: 22 c P. Heeman, 2017 A Simple Class class Person: def init (self,name,gender,birthday): self.birthday = birthday self.gender = gender self.name = name d = Person("donald","male","1946-6-14") m = Person("melania","female","1970-4-26") b = Person("baron","male","2006-3-20") print b.name class specifies how the class works Function init specifies how to create a new object - Called by using the name of the class - Should initialize all of the object fields Object has 3 fields in it: birthday, gender, name

CS533 Class 01: 23 c P. Heeman, 2017 Referencing variables inside of an object class Person: def init (self,name,gender,birthday): self.birthday = birthday self.gender = gender self.name = name self.father = None self.mother = None d = Person("donald","male","1946-6-14") m = Person("melania","female","1970-4-26") b = Person("baron","male","2006-3-20") b.father = d b.mother = m print b.father.name CS533 Class 01: 24 c P. Heeman, 2017 Pointers d = Person("donald","male","1946-6-14") m = Person("melania","female","1970-4-26") b = Person("baron","male","2006-3-20") b.father = d b.mother = m e = d e.name = "donnie" print b.father.name d, m, b are not the actual objects, but pointers to them

CS533 Class 01: 25 c P. Heeman, 2017 What is the Course About? Data Structures Intro Classes, Objects, and Pointers Linked Lists Rooted Trees Overview CS533 Class 01: 26 c P. Heeman, 2017 Operations on Dynamic Sets S is a set, k is a key, x is a pointer to an element For any dynamic set: - Search(S,k) returns pointer to an element or None - Insert(S,x) - Delete(S,x) For totally ordered sets: - Minimum(S) - Maximum(S) - Successor(S,x) - Predecessor(S,x)

CS533 Class 01: 27 c P. Heeman, 2017 Linked List (Chapter 10.2) Linked list is a data structure - objects are arranged in a linear order - More flexible than an array as you can insert/delete objects faster Singly or doubly linked - Singly: elements point to next element - Doubly: also point to prev element Sorted or not - Sorted: min element is at head of list, next pointer points to next largest element Circular or not: - Circular: prev pointer of head element points to tail element, and next pointer of tail points to head Remainder of section: assume unsorted doubly linked CS533 Class 01: 28 c P. Heeman, 2017 Operation prev key next (a) L:head 9 16 4 1 (b) L:head 25 9 16 4 1 (c) L:head 25 9 16 1

CS533 Class 01: 29 c P. Heeman, 2017 class Element: def init (self,k): self.key = k self.next = None self.prev = None class List: def init (self): self.head = None def ListSearch(L,k): x = L.head while x is not None and x.key!= k: x = x.next return x Searching a linked list l = List() a = Element(1); l.head = a b = Element(4); a.next = b; b.prev = a c = Element(16); b.next = c; c.prev = b print ListSearch(l,4).key CS533 Class 01: 30 c P. Heeman, 2017 Methods versus Functions Previous example had ListSearch as a function - Can work on any type of structure that has a head field, and the head points to something that has a next, and that thing has a next etc - Works because python is not tightly typed Alternative: make ListSearch a method - Method is defined in a class and operates on an object + Defined with the first argument named self, which is the object being operated on + Called by objectname followed by a dot followed by method name and any additional parameters + Similar to how list objects have methods for append, insert, remove, sort, etc

CS533 Class 01: 31 c P. Heeman, 2017 class List: def init (self): self.head = None Code def Search(self,k): x = self.head while x is not None and x.key!= k: x = x.next return x l = List()... print l.search(4).key CS533 Class 01: 32 c P. Heeman, 2017 Discussion Methods allow you to associate functions on class objects directly with the definition of the class Makes it so that you can encapsulate code better so that only code inside of the class needs to know how the class works Forces you to think about what data you want, and what operations will work on it

CS533 Class 01: 33 c P. Heeman, 2017 Inserting into a List Inserting at end of list: simpler, but need pointer to end of list l = List() a = Element(1); b = Element(4); l.head = a a.next = b; b.prev = a c = Element(16); b.next = c; c.prev = b Inserting at start of list: more complex but have pointer to start of list l = List() a = Element(1); b = Element(4); l.head = a a.prev = b; b.next = a; l.next = b c = Element(16); b.prev = c; c.next = b; l.next = c List Method: insert at start def Insert(self,x): x.next = self.head if L.head is not none: L.head.prev = x L.head = x x.prev = None CS533 Class 01: 34 c P. Heeman, 2017 Deleting from a linked List List Method: - This has to be method for list, not for element + It might have to update the head pointer - Boundary conditions force additional checks def Delete(self,x): if x.prev is not None: x.prev.next = x.next else: self.head = x.next if x.next is not None: x.next.prev = x.prev

CS533 Class 01: 35 c P. Heeman, 2017 Sentinels To get rid of boundary case, have a sentinel - Makes this into a circular list Sentinel is a dummy object always in the list (a) (b) L:nil L:nil 9 16 4 1 (c) L:nil 25 9 16 4 1 (d) L:nil 25 9 16 4 CS533 Class 01: 36 c P. Heeman, 2017 Code with Sentinels def Delete(self,x): x.prev.next = x.next x.next.prev = x.prev def Insert(self,x): x.next = self.sentinel.next self.sentinel.next.prev = x self.sentinel.next = x x.prev = self.sentinel Main point: logic is simpler rather than the slight gain in speed

CS533 Class 01: 37 c P. Heeman, 2017 Continued class List: def init (self): self.sentinel = Element(None) self.sentinel.next = self.sentinel self.sentinel.prev = self.sentinel... def Search(self,k): x = self.sentinel.next while x!= self.sentinel and x.key!= k: x = x.next return x l = List(); l.insert(element(4)); l.insert(element(8)); l.insert(element(16)) print l.search(8).key print l.search(5).key CS533 Class 01: 38 c P. Heeman, 2017 Questions about Code Code for search is from textbook. - What does search return if item is not in the list? + How would you check for this? - What is a better option? + How would the code be changed? + How would you now check for not-found? How do we change code to store satellite data? - Where should satellite data be stored? - We do not want to make a new instance of List for each different type of satellite data we want to store

CS533 Class 01: 39 c P. Heeman, 2017 class Element: def init (self,k): self.key = k self.next = None self.prev = None class MyEle(Element): def init (self,k,age,name): Element. init (self,k) self.age = age self.name = name Class Inheritance l = List(); l.insert(myele(4,35,"peter")); l.insert(myele(8,25,"paul")) print l.search(8).name print l.search(5).name List uses Element class to define sentinel - Add entries using MyEle, which saves satellite data - List doesn t care as long as entry has next, prev, and key fields CS533 Class 01: 40 c P. Heeman, 2017 Overview What is the Course About? Data Structures Intro Classes, Objects, and Pointers Linked Lists Rooted Trees

CS533 Class 01: 41 c P. Heeman, 2017 Rooted Trees (Chapter 10.4) Define a class for nodes - has fields: parent, left child and right child pointers, and key Define a class for trees - Has field: root (points to the root node) T:root CS533 Class 01: 42 c P. Heeman, 2017 Unbounded Branching Parent cannot reference every child, like in a binary tree Define a class for nodes - left-child: points to left-most child (like a single linked list) + If no children, pointer is None T:root - right-sibling: points to next sibling + If last sibling, pointer is None - parent