Xiaoyan Li, 2007 1 CSC211 Data Structures Lecture 1: Introduction Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Slides:



Advertisements
Similar presentations
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Advertisements

Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
MATH 224 – Discrete Mathematics
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 2: Basics Data Structures.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Algorithm Analysis (Big O) CS-341 Dick Steflik. Complexity In examining algorithm efficiency we must understand the idea of complexity –Space complexity.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 19 Searching Instructor: Zhigang Zhu Department of Computer Science City College.
Zhigang Zhu, CSC212 Data Structures - Section FG Lecture 1: Introduction Instructor: Zhigang Zhu Department of Computer Science City College.
CSCE 210 Data Structures and Algorithms
1 CENG 707 Data Structures and Algorithms Nihan Kesim Çiçekli Department of Computer Engineering Middle East Technical University Fall 2010.
Reviews for Exam 1 Chapter 1-4 CS 211 Data Structures MHC, 2007.
CSCE156: Introduction to Computer Science II Instructor Stephen Scott Website
Graphs & Exam Review 3 Chapter 10 – 13 CS211 CS Dept, MHC.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
Bigointro1 Algorithm Analysis & Program Testing An introduction.
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
Edgardo Molina, CSC212 Data Structures - Section AB Lecture 1: Introduction Instructor: Edgardo Molina Department of Computer Science City College.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
CS223 Algorithms D-Term 2013 Instructor: Mohamed Eltabakh WPI, CS Introduction Slide 1.
Week 2 CS 361: Advanced Data Structures and Algorithms
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
ICS 102 Computer Programming University of Hail College of Computer Science & Engineering Computer Science and Software Engineering Department.
CS Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
CS 140 Computer Programming (I) Second semester (3 credits) Imam Mohammad bin Saud Islamic University College of Computer Science and Information.
1 CSC 222: Computer Programming II Spring 2004 See online syllabus at: Course goals:
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
CMSC 2021 CMSC 202 Computer Science II for Majors Fall 2002 Mr. Frey (0101 – 0104) Mr. Raouf (0201 – 0204)
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
Introduction to ECE 2401 Data Structure Fall 2005 Chapter 0 Chen, Chang-Sheng
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
Software Development p Data structure p A collection of data organized so that the data can be accessed using a set of specific techniques p Object-oriented.
CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Data Structures Using C++ 2E
CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CMSC 2021 CMSC 202 Computer Science II for Majors Spring 2001 Sections Ms. Susan Mitchell.
CMSC 2021 CMSC 202 Computer Science II for Majors Spring 2003 Mr. Frey (0101 – 0104) Mr. Raouf (0201 – 0204)
Xiaoyan Li CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Searching Topics Sequential Search Binary Search.
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
George Wolberg, CSC212 Data Structures Lecture 1: Introduction Instructor: George Wolberg Department of Computer Science City College of New York.
BIT 143: Programming-Data Structures1 Before Class Begins: Sit in front of a computer Log in –IF you don’t yet have an account, you can use the guest account.
Design and Analysis of Algorithms & Computational Complexity CS490 Koji Tajii.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CSCE 210 Data Structures and Algorithms
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 1 The Phases of Software Development
Welcome to CSIS 10B Overview of Course Software Design (from Nyhoff)
Chapter 1 The Phases of Software Development
Algorithm Analysis CSE 2011 Winter September 2018.
Lecture 1: Introduction
Preconditions, Postconditions & Assertions
Introduction to Data Structures
Searching, Sorting, and Asymptotic Complexity
Preconditions and Postconditions
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Preconditions and Postconditions
Presentation transcript:

Xiaoyan Li, CSC211 Data Structures Lecture 1: Introduction Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

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

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

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

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

Xiaoyan Li, Course Information (HOW) p Objectives p Data Structures, with C++ and Software Engineering p Textbook and References p Texbook: Data Structures and Other Objects Using C++, Third Edition by Michael Main and Walter Savitch Michael MainWalter SavitchMichael MainWalter Savitch p Reference: C++ How to Program by Dietel & Dietel, 3rd Ed., Prentice Hall 2001 C++ How to ProgramC++ How to Program p Prerequisites p CS101, Programming; CS102, OOP p Assignments and Grading p 6-7 programming assignments roughly every 2 weeks (30%) p 3 in-class writing exams (60%), several in-class quizzes (10%) p Computing Facilities p Microsoft Visual Studio C++ (in CS Lab or Visil Lab) p GNU g++ (Free downloadable to your own machines)

Xiaoyan Li, Tentative Schedule (HOW) ( 28 classes = 22 lectures + 3 reviews + 3 exams, 6-7 assignments) p Lecture 1. The Phase of Software Development (Ch 1) p Lectures 2-3. ADT and C++ Classes (Ch 2) p Lecture 4-5. Container Classes (Ch 3) p Lectures 6-8. Pointers and Dynamic Arrays (Ch 4) p Reviews and the 1st exam (Ch. 1-4, after mid-semester break) p Lectures Linked Lists (Ch. 5) p Lectures 11. Template and STL (Ch 6) p Lecture 12. Stacks (Ch 7) and Queues (Ch 8) p Lectures Recursion (Ch 9) p Reviews and the 2nd exam (Ch. 5-9, before Thanksgiving) p Lectures Trees (Ch 10, Ch 11) p Lectures Searching and Hashing (Ch 12) p Lectures Sorting (Ch 13) p Reviews and the 3rd exam (mainly Ch , Dec )

Xiaoyan Li, Course Web Page You can find all the information at -Come back frequently for the updating of lecture schedule, programming assignments and exam schedule - Reading assignments & programming assignments

Xiaoyan Li, Outline p Course Objectives and Schedule p Information p Topics p Schedule p The Phase of Software Development p Basic design strategy p Pre-conditions and post-conditions p Running time analysis

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

Xiaoyan Li, p An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions 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.

Xiaoyan Li, 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 ?

Xiaoyan Li, Example p 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.

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

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

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

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

Xiaoyan Li, Example write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 ); Which of these function calls meet the precondition ?

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

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

Xiaoyan Li, Example void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output.... }  The postcondition always indicates what work the function has accomplished. In this case, when the function returns the square root of x has been written.

Xiaoyan Li, 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....

Xiaoyan Li, Another Example is_vowel( 'A' ); is_vowel(' Z' ); is_vowel( '?' ); What values will be returned by these function calls ?

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

Xiaoyan Li, Consequence of Violation write_sqrt(-10.0); is_vowel( '?' ); Who are responsible for the crash ? Violating the precondition might even crash the computer.

Xiaoyan Li, Always make sure the precondition is valid... p 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.

Xiaoyan Li, so the postcondition becomes true at the function’s end. p The programmer who writes the function counts on the precondition being valid, and ensures that the postcondition becomes true at the function’s end. THEN MY FUNCTION WILL EXECUTE, AND WHEN IT IS DONE, THE POSTCONDITION WILL BE TRUE. I GUARANTEE IT.

Xiaoyan Li, 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 wrote that Power Supply function ®Mayor Bloomberg Out of Pen Station The famous skyline was dark on Aug 14 th, 2003.

Xiaoyan Li, 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. The programmer who calls a function is responsible for ensuring that the precondition is valid. Out of Pen Station

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

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

Xiaoyan Li, 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);... p The assert function (described in Section 1.1) is useful for detecting violations of a precondition.

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

Xiaoyan Li, Precondition p The programmer who calls a function ensures that the precondition is valid. p The programmer who writes a function can bank on the precondition being true when the function begins execution. Postcondition p The programmer who writes a function ensures that the postcondition is true when the function finishes executing. Summary of pre- and post-conditions

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

Xiaoyan Li, Running Time Analysis – Big O p Time Analysis p Fast enough? p How much longer if input gets larger? p Which among several is the fastest?

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

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

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

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

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

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

Xiaoyan Li, A Quiz Big-O notation O(n 2 ) O(n) O(log n) Number of operations n 2 +5n 100n+n 2 (n+7)(n-2)n+100 number of digits in 2n

Xiaoyan Li, Big-O Notation p 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 , ,002,000

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

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

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

Xiaoyan Li, Time Analysis of C++ Functions p Worst case, average case and best case p search a number x in an integer array a of size N p Can you provide an exact number of operations? p Best case: p Worst case: 1+3N+1 p Average case: 1+3N/2+1 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;

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

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

Xiaoyan Li, Reminder … Lecture 2: ADT and C++ Classes Reading Assignment before the next lecture: Chapter 1 Chapter 2, Sections Office Hours: T,Th 10:00-11:00 AM (Location: Clapp 227 ) Check website for details

Xiaoyan Li, T HE E ND