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

Similar documents
Data Structures - Section FG

Preconditions and Postconditions. What are Preconditions and Postconditions? Example. Example

Preconditions and Postconditions

Can you think of a situation where this would occur?

C++ CODE DESIGN INTRO TO ABSTRACT DATA TYPES

C++ CODE DESIGN INTRO TO ABSTRACT DATA TYPES

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

2. COURSE DESIGNATION: 3. COURSE DESCRIPTIONS:

CPSC 2380 Data Structures and Algorithms

COLLEGE OF DUPAGE CIS 2542 Advanced C++ with Data Structure Applications Course Syllabus

Computer Science Foundation Exam

CS 241 Data Organization using C

Lecture 18 Heaps and Priority Queues

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

CS 132 Exam #1 - Study Suggestions

CSC212. Data Structure. Lecture 4 Container Classes. Instructor: George Wolberg Department of Computer Science City College of New York

CS 216 Exam 1 Fall SOLUTION

KOMAR UNIVERSITY OF SCIENCE AND TECHNOLOGY (KUST)

Introduction to Programming System Design CSCI 455x (4 Units)

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

Outline. Computer Science 331. Course Information. Assessment. Contact Information Assessment. Introduction to CPSC 331

CENG 707 Data Structures and Algorithms

CSC212. Data Structure. Lecture 12 Reasoning about Recursion. Instructor: George Wolberg Department of Computer Science City College of New York

CSC212 Data Structure - Section FG

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

COMP Data Structures

CSC212. Data Structure. Lecture 13 Trees and Tree Traversals. Instructor: Prof. George Wolberg Department of Computer Science City College of New York

Review for Test 1 (Chapter 1-5)

COMP Data Structures

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Object-Oriented Programming for Managers

Database Systems (INFR10070) Dr Paolo Guagliardo. University of Edinburgh. Fall 2016

San Jose State University College of Science Department of Computer Science CS151, Object-Oriented Design, Sections 1,2 and 3, Spring 2017

B. Subject-specific skills B1. Problem solving skills: Supply the student with the ability to solve different problems related to the topics

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

CSCE 2014 Final Exam Spring Version A

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Course Syllabus of Microprocessor I (Fall 2006)

Lectures 4 & 5 Container Classes

Computer Science Foundation Exam

San José State University Department of Computer Science CS-144, Advanced C++ Programming, Section 1, Spring 2018

COLLEGE OF THE DESERT

CS Lecture 19: Loop invariants

CSC 216 Data Structures Dr. Melissa Wiggins MCC 306 (601)

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

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Violations of the contract are exceptions, and are usually handled by special language constructs. Design by contract

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions

CSI33 Data Structures

Lecture 6 Dynamic Classes and the Law of the Big Three. Instructor: George Wolberg Department of Computer Science City College of New York

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

Optimized C++ o Websites and handouts Optional: Effective C++, Scott Meyers. Fall 2013

ASYMPTOTIC COMPLEXITY

Design and Analysis of Algorithms. Comp 271. Mordecai Golin. Department of Computer Science, HKUST

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

Advanced Algorithms and Data Structures

CSC212. Data Structure. Lecture 11 Recursive Thinking. Instructor: George Wolberg Department of Computer Science City College of New York

Course: Honors AP Computer Science Instructor: Mr. Jason A. Townsend

San Jose State University - Department of Computer Science

Course Syllabus for CS3358 (Data Structures and Algorithms) by Lee S. Koh

CSC212 Data Structure - Section FG

What is an algorithm?

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

During the first 2 weeks of class, all students in the course will take an in-lab programming exam. This is the Exam in Programming Proficiency.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

CoSci 440 SYLLABUS Programming in C++ INSTRUCTOR Mari Rettke cell SECTION : and 13345

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu

Practical Programming Methodology

CSci 4211: Data Communications and Computer Networks. Time: Monday and Wednesday 1 pm to 2:15 pm Location: Vincent Hall 16 Spring 2016, 3 Credits

Absolute C++ Walter Savitch

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Problem Solving with C++

CE221 Programming in C++ Part 1 Introduction

Announcements. 1. Forms to return today after class:

Data Structures and Algorithms

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Lecture 7. Log into Linux New documents posted to course webpage

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

EECE.2160: ECE Application Programming Spring 2017

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

Abstraction and Specification

CSCE 206: Structured Programming in C

Part I: Short Answer (12 questions, 65 points total)

CS 206 Introduction to Computer Science II

CSE wi: Practice Midterm

AP Computer Science AB

CMPE 180A Data Structures and Algorithms in C++


CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

VE281 Data Structures and Algorithms. Introduction and Asymptotic Algorithm Analysis

CSE 114, Computer Science 1 Course Information. Spring 2017 Stony Brook University Instructor: Dr. Paul Fodor

Advanced Algorithms and Data Structures

Transcription:

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

Outline of this lecture Course Objectives and Schedule WHAT (Topics) WHY (Importance) WHERE (Goals) HOW (Information and Schedule) The Phase of Software Development Basic design strategy Pre-conditions and post-conditions Running time analysis George Wolberg, 2016 2

Topics (WHAT) Data Structures specification, design, implementation and use of basic data types (arrays, lists, queues, stacks, trees ) OOP and C++ C++ classes, container classes, Big Three Standard Template Library (STL) templates, iterators ADTs in our DS course cut-down version of STL Recursion, Searching and Sorting Algorithms important techniques in many applications George Wolberg, 2016 3

Importance (WHY) Data Structures (how to organize data) and Algorithms (how to manipulate data) are the cores of today s computer programming The behavior of Abstract Data Types (ADTs) in our Date Structures course is a cut-down version of Standard Template Library (STL) in C++ Lay a foundation for other aspects of real programming OOP, Recursion, Sorting, Searching George Wolberg, 2016 4

Goals (WHERE) understand the data types inside out Implement these data structures as classes in C++ Determine which structures are appropriate in various situations Confidently learn new structures beyond what are presented in this class also learn part of the OOP and software development methodology George Wolberg, 2016 5

Course Information (HOW) Objectives Data Structures, with C++ and Software Engineering Textbook and References Texbook: Data Structures and Other Objects Using C++, by Michael Main and Walter Savitch, 4th Ed., 2011. Reference: C++ How to Program by Dietel & Dietel, 8th Ed., Prentice Hall 2011 Prerequisites CSc103 C++ (Intro to Computing), CSc 104 (Discrete Math Structure I) Assignments and Grading 6-7 programming assignments roughly every 2 weeks (50%) 2 in-class writing exams (50%) Computing Facilities PCs: Microsoft Visual C++ ; Unix / Linux : g++ also publicly accessible at Computer Science labs George Wolberg, 2016 6

Tentative Schedule (HOW) ( 14 weeks = 28 classes = 23 lectures + 3 reviews + 2 exams, 6-7 assignments) Week 1. The Phase of Software Development (Ch 1) Week 2. ADT and C++ Classes (Ch 2) Week 3. Container Classes (Ch 3) Week 4. Pointers and Dynamic Arrays (Ch 4) Reviews and the 1st exam (Ch. 1-4) Week 5. Linked Lists (Ch. 5) Week 6 Template and STL (Ch 6) Week 7. Stacks (Ch 7) and Queues (Ch 8) Week 8. Recursion (Ch 9) Reviews and the 2nd exam (Ch. 5-9) Week 9/10. Trees (Ch 10, Ch 11) Week 11. Searching and Hashing (Ch 12) Week 12. Sorting (Ch 13) Week 13. Graphs (Ch 15) Reviews and the 3rd exam (mainly Ch. 10-13) George Wolberg, 2016 7

Course Web Page You can find all the information at http://www-cs.ccny.cuny.edu/~wolberg/cs212/index.html or via my web page: http://www-cs.ccny.cuny.edu/~wolberg -Come back frequently for the updating of lecture schedule, programming assignments and exam schedule - Reading assignments & programming assignments George Wolberg, 2016 8

Piazza All class-related discussion will be done on Piazza. Ask questions on Piazza (rather than via emails) Benefit from collective knowledge of classmates Ask questions when struggling to understand a concept. You can even do so anonymously. Signup: piazza.com/ccny.cuny/spring2016/csc212bc Class link: piazza.com/ccny.cuny/spring2016/csc212bc/home George Wolberg, 2016 9

Outline Course Objectives and Schedule Information Topics Schedule The Phase of Software Development Basic design strategy Pre-conditions and post-conditions Running time analysis George Wolberg, 2016 10

Phase of Software Development Basic Design Strategy four steps (Reading: Ch.1 ) Specify the problem - Input/Output (I/O) Design data structures and algorithms (pseudo code) Implement in a language such as C++ Test and debug the program (Reading Ch 1.3) Design Technique Decomposing the problem Two Important Issues (along with design and Implement) Pre-Conditions and Post-Conditions Running Time Analysis George Wolberg, 2016 11

Preconditions and Postconditions An important topic: preconditions and postconditions. They are a method of specifying what a function accomplishes. Precondition and Postcondition Presentation copyright 1997, Addison Wesley Longman For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. George Wolberg, 2016 12

Preconditions and Postconditions Frequently a programmer must communicate precisely what a function accomplishes, without any indication of how the function does its work. Can you think of a situation where this would occur? George Wolberg, 2016 13

Example You are the head of a programming team and you want one of your programmers to write a function for part of a project. HERE ARE THE REQUIREMENTS FOR A FUNCTION THAT I WANT YOU TO WRITE. I DON'T CARE WHAT METHOD THE FUNCTION USES, AS LONG AS THESE REQUIREMENTS ARE MET. George Wolberg, 2016 14

What are Preconditions and Postconditions? One way to specify such requirements is with a pair of statements about the function. The precondition statement indicates what must be true before the function is called. The postcondition statement indicates what will be true when the function finishes its work. George Wolberg, 2016 15

Example void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. George Wolberg, 2016 16

Example void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. The precondition and postcondition appear as comments in your program. They are usually placed after the function s parameter list. George Wolberg, 2016 17

Example void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. In this example, the precondition requires that x >= 0 be true whenever the function is called. George Wolberg, 2016 18

Example Which of these function calls meet the precondition? write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 ); George Wolberg, 2016 19

Example Which of these function calls meet the precondition? write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 ); The second and third calls are fine, since the argument is greater than or equal to zero. George Wolberg, 2016 20

Example Which of these function calls meet the precondition? write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 ); But the first call violates the precondition, since the argument is less than zero. George Wolberg, 2016 21

Example void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. George Wolberg, 2016 22

Another Example bool is_vowel( char letter ) // Precondition: letter is an uppercase or // lowercase letter (in the range 'A'... 'Z' or 'a'... 'z'). // Postcondition: The value returned by the // function is true if letter is a vowel; // otherwise the value returned by the function is // false. George Wolberg, 2016 23

Another Example What values will be returned by these function calls? is_vowel( 'A' ); is_vowel(' Z' ); is_vowel( '?' ); George Wolberg, 2016 24

Another Example What values will be returned by these function calls? true is_vowel( 'A' ); is_vowel(' Z' ); is_vowel( '?' ); false Nobody knows, because the precondition has been violated. George Wolberg, 2016 25

Consequence of Violation Who is responsible for the crash? write_sqrt(-10.0); is_vowel( '?' ); Violating the precondition might even crash the computer. George Wolberg, 2016 26

Always make sure the precondition is valid... The programmer who calls the function is responsible for ensuring that the precondition is valid when the function is called. AT THIS POINT, MY PROGRAM CALLS YOUR FUNCTION, AND I MAKE SURE THAT THE PRECONDITION IS VALID. George Wolberg, 2016 27

... so the postcondition becomes true at the function s end. The programmer who writes the function counts on the precondition being valid, and ensures that the postcondition becomes true at the function s end. The precondition is enforced in C++ through use of assert() function. THEN MY FUNCTION WILL EXECUTE, AND WHEN IT IS DONE, THE POSTCONDITION WILL BE TRUE. I GUARANTEE IT. George Wolberg, 2016 28

A Quiz The famous skyline was dark on Aug 14 th, 2003. Suppose that you call a function, and you neglect to make sure that the precondition is valid. Who is responsible if this inadvertently causes a 1-day long blackout in NYC or other disaster? You The programmer who wrote that Power Supply function Mayor Bloomberg Out of Penn Station George Wolberg, 2016 29

A Quiz Suppose that you call a function, and you neglect to make sure that the precondition is valid. Who is responsible if this inadvertently causes a 1-day long blackout in NYC or other disaster? You The programmer who calls a function is responsible for ensuring that the precondition is valid. Out of Penn Station George Wolberg, 2016 30

On the other hand, careful programmers also follow these rules: When you write a function, you should make every effort to detect when a precondition has been violated. If you detect that a precondition has been violated, then print an error message and halt the program. George Wolberg, 2016 31

On the other hand, careful programmers also follow these rules: When you write a function, you should make every effort to detect when a precondition has been violated. If you detect that a precondition has been violated, then print an error message and halt the program......rather than causing a chaos. The famous skyline was dark on Aug 14 th, 2003. George Wolberg, 2016 32

Example void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. { assert(x >= 0);... The assert function (described in Section 1.1) is useful for detecting violations of a precondition. George Wolberg, 2016 33

Advantages of Using Pre- and Post-conditions Concisely describes the behavior of a function...... without cluttering up your thinking with details of how the function works. At a later point, you may reimplement the function in a new way...... but programs (which only depend on the precondition/postcondition) will still work with no changes. George Wolberg, 2016 34

Summary of pre- and post-conditions Precondition The programmer who calls a function ensures that the precondition is valid. The programmer who writes a function can bank on the precondition being true when the function begins execution. Careful programmers enforce this anyway! Postcondition The programmer who writes a function ensures that the postcondition is true when the function finishes executing. George Wolberg, 2016 35

Phase of Software Development Basic Design Strategy four steps (Reading: Ch.1 ) Specify Input/Output (I/O) Design data structures and algorithms Implement in a language such as C++ Test and debug the program (Reading Ch 1.3) Design Technique Decomposing the problem Two Important Issues (along with design and Implement) Pre-Conditions and Post-Conditions Running Time Analysis George Wolberg, 2016 36

Running Time Analysis Big O Time Analysis Fast enough? How much longer if input gets larger? Which among several is the fastest? George Wolberg, 2016 37

Example : Stair Counting Problem How many steps? 1789 (Birnbaum) 1671 (Joseph Harriss) 1652 (others) 1665 (Official Eiffel Tower Website) Find it out yourself! Eiffel Tower George Wolberg, 2016 38

Example : Stair Counting Problem Find it out yourself! Method 1: Walk down and keep a tally Each time a step down, make a mark Method 2 : Walk down, but let Judy keep the tally Down+1, hat, back, Judy make a mark Method 3: Jervis to the rescue II There y a are 共有 2689 2689 marches 2689 steps in dan 级台阶 this cet escalier stairway 千真万确! vraiment! (really!) One mark per digit Eiffel Tower George Wolberg, 2016 39

Example : Stair Counting Problem How to measure the time? Just measure the actual time vary from person to person depending on many factors Count certain operations each time walk up/down, 1 operation each time mark a symbol, 1 operation Eiffel Tower George Wolberg, 2016 40

Example : Stair Counting Problem Find it out yourself! Method 1: Walk down and keep a tally 2689 (down) + 2689 (up) + 2689 (marks) = 8067 Method 2 : Walk down, let Judy keep tally Down: 3,616,705 = 1+2+ +2689 Up: 3,616,705 = 1+2+ +2689 Marks: 2,689 = 1+1+ +1 Method 3: Jervis to the rescue only 4 marks! 7,236,099! Eiffel Tower George Wolberg, 2016 41

Example : Stair Counting Problem Size of the Input : n Method 1: Walk down and keep a tally 3n Method 2 : Walk down, let Judy keep tally n+2(1+2+ +n) = n+(n+1)n = n 2 +2n Trick: Compute twice the amount and then divided by two Method 3: Jervis to the rescue The number of digits in n = [log 10 n]+1 Eiffel Tower George Wolberg, 2016 42

Example : Stair Counting Problem Big-O Notation the order of the algorithm Use the largest term in a formula Ignore the multiplicative constant Method 1: Linear time 3n => O(n) Method 2 : Quadratic time n 2 +2n => O(n 2 ) Method 3: Logarithmic time [log 10 n]+1 => O(log n) Eiffel Tower George Wolberg, 2016 43

A Quiz Number of operations n 2 +5n 100n+n 2 (n+7)(n-2) n+100 number of digits in 2n Big-O notation O(n 2 ) O(n 2 ) O(n 2 ) O(n) O(log n) George Wolberg, 2016 44

Big-O Notation The order of an algorithm generally is more important than the speed of the processor Input size: n O(log n) O (n) O (n 2 ) # of stairs: n [log 10 n]+1 3n n 2 +2n 10 2 30 120 100 3 300 10,200 1000 4 3000 1,002,000 George Wolberg, 2016 45

Time Analysis of C++ Functions Example- Quiz ( 5 minutes) Printout all item in an integer array of size N for (i=0; i< N; i++ ) { val = a[i]; cout << val; } 2 C++ operations or more? Frequent linear pattern A loop that does a fixed amount of operations N times requires O(N) time George Wolberg, 2016 46

Time Analysis of C++ Functions Another example Printout char one by one in a string of length N for (i=0; i< strlen(str); i++ ) { c = str[i]; cout << c; } What is a single operation? O(N 2 )! If the function calls do complex things, then count the operation carried out there Put a function call outside the loop if you can! George Wolberg, 2016 47

Time Analysis of C++ Functions Another example Printout char one by one in a string of length N N = strlen(str); for (i=0; i<n; i++ ) { c = str[i]; cout << c; } What is a single operation? O(N)! If the function calls do complex things, then count the operation carried out there Put a function call outside the loop if you can! George Wolberg, 2016 48

Time Analysis of C++ Functions Worst case, average case and best case search a number x in an integer array a of size N for (i=0; (i< N) && (a[i]!= x); i++ ); if (i < N) cout << Number << x << is at location << i << endl; else cout << Not Found! << endl; Can you provide an exact number of operations? Best case: 1+2+1 Worst case: 1+3N+1 Average case: 1+3N/2+1 George Wolberg, 2016 49

Testing and Debugging Test: run a program and observe its behavior input -> expected output? how long? software engineering issues Choosing Test Data : two techniques boundary values fully exercising code (tool: profiler) Debugging find the bug after an error is found rule: never change if you are not sure what s the error tool: debugger George Wolberg, 2016 50

Summary Often ask yourselves FOUR questions WHAT, WHY, WHERE & HOW Topics DSs, C++, STL, basic algorithms Data Structure experts Schedule 23 lectures, 7 assignments, 2 exams some credits (10) for attending the class Information website Remember and apply two things (Ch 1) Basic design strategy Pre-conditions and post-conditions Running time analysis Testing and Debugging (reading 1.3) George Wolberg, 2016 51

Reminder Lecture 2: ADT and C++ Classes Reading Assignment before the next lecture: Chapter 1 Office Hours: Chapter 2, Sections 2.1-2.3 Wed 2:00 pm - 3:00 pm (Location: NAC 8/202N) check website for details George Wolberg, 2016 52

THE END George Wolberg, 2016 53