CSC212 Data Structures Section AB - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

CSC212 Data Structures Section AB

Description:

Department of Computer Science. City College of New York ... Method 3: Jervis to the rescue. Eiffel Tower. II y a 2689 marches dan cet escalier ... – PowerPoint PPT presentation

Number of Views:79
Avg rating:3.0/5.0
Slides: 50
Provided by: Zhiga6
Category:

less

Transcript and Presenter's Notes

Title: CSC212 Data Structures Section AB


1
CSC212 Data Structures - Section AB
  • Lecture 1 Introduction
  • Instructor Jianting Zhang
  • Department of Computer Science
  • City College of New York

Some slides courtesy of Prof. Zhigang Zhu, CCNY
2
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

3
CCNY Computer Science Curriculum
http//www-cs.ccny.cuny.edu/academics/programs/und
ergrad-cs-matrix.html
4
How was my last class doing?
Total25
5
Topics (WHAT)
  • Data Structures
  • specification, design, implementation and use of
    basic data types (arrays, lists, queues, stacks,
    trees, graph)
  • OOP and C
  • C classes, container classes , Big Three
  • Standard Template Library (STL)
  • templates, iterators
  • ADTs in our DS course are cut-down version of
    STL
  • Recursion, Searching and Sorting Algorithms
  • important techniques in many applications

6
Importance (WHY)
  • Data Structures (how to organize data) and
    Algorithms (how to manipulate data) are the cores
    of todays 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

7
Goals (WHERE)
understand the data structures 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

8
Course Information (HOW)
  • Textbook and References
  • Textbook Data Structures and Other Objects
    Using C , Third Edition by Michael Main and
    Walter Savitch
  • Reference C How to Program by Dietel
    Dietel, 3rd Ed., Prentice Hall 2001
  • Assignments and Grading
  • 6-7 programming assignments roughly every 2
    weeks (30)
  • 3 exams (60), several in-class quizzes (10)
  • Computing Facilities
  • Linux GCC PC Microsoft Visual C
  • Also publicly accessible at CCNY's Steinman 405
    and NAC 7/105 7/118 labs
  • Blackboard System

9
Tentative Schedule (HOW)
  • Lecture 1. The Phase of Software
    Development (Ch 1)
  • Lectures 2-3. ADT and C Classes (Ch 2)
  • Lecture 4-5. Container Classes (Ch 3)
  • Lectures 6-8. Pointers and Dynamic Arrays
    (Ch 4)
  • Reviews and the 1st exam (Ch. 1-4)
  • Lectures 9-10. Linked Lists (Ch. 5)
  • Lecture 11 Template and STL (Ch 6)
  • Lecture 12. Stacks (Ch 7)
  • Lecture 13 Queues (Ch 8)
  • Reviews and the 2nd exam (Ch. 5-8)
  • Lectures 14-15 Recursive Thinking (Ch 9)
  • Lectures 16-19. Trees (Ch 10, Ch 11)
  • Lectures 20-21. Searching and Hashing (Ch 12)
  • Lectures 22- 23. Sorting (Ch 13)
  • Lecture 24 Introduction to Graph (Ch 15)
  • Reviews (mainly Ch. 9-15)

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

11
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

12
Preconditions and Postconditions
  • Frequently a programmer must communicate
    precisely what a function accomplishes, without
    any indication of how the function does its work.

This separation between what a function does and
how the function works is extremely important -
particularly for large programs which are written
by a team of programmers.
Can you think of a situation where this would
occur ?
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.
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.

15
Example
void write_sqrt( double x) // Precondition x
gt 0. // Postcondition The square root of x
has // been written to the standard output.
...
16
Example
void write_sqrt( double x) // Precondition x
gt 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 functions
    parameter list.

17
Example
void write_sqrt( double x) // Precondition x
gt 0. // Postcondition The square root of x
has // been written to the standard output.
...
  • In this example, the precondition requires that
  • x gt 0
  • be true whenever the function is called.

18
Example
Which of these function calls meet the
precondition ?
19
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.
20
Example
Which of these function calls meet the
precondition ?
But the first call violates the
precondition, since the argument is less than
zero.
21
Example
void write_sqrt( double x) // Precondition x
gt 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.

22
Consequence of Violation
Who are responsible for the crash ?
write_sqrt(-10.0)
Violating the precondition might even crash the
computer.
23
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.

24
. . . so the postcondition becomes true at the
functions end.
  • The programmer who writes the function counts on
    the precondition being valid, and ensures that
    the postcondition becomes true at the functions
    end.

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

26
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.

27
Example
void write_sqrt( double x) // Precondition x
gt 0. // Postcondition The square root of x
has // been written to the standard output.
assert(x gt 0) ...
  • The assert function (described in Section 1.1) is
    useful for detecting violations of a precondition.

28
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
    without any changes.

29
  • Break

30
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.
  • Postcondition
  • The programmer who writes a function ensures that
    the postcondition is true when the function
    finishes executing.

31
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

32
Running Time Analysis Big O
  • Time Analysis
  • Fast enough?
  • How much longer if input gets larger
  • E.g. doubled.
  • Which among several is the fastest?

33
Example Stair Counting Problem
  • How many steps ?
  • Find it out yourself !

1789 (Birnbaum) 1671 (Joseph Harriss) 1652
(others) 1665 (Official Eiffel Tower Website) 
34
Example Stair Counting Problem
  • Find it out yourself !
  • Method 1 Walk down and keep a tally
  • Method 2 Walk down, but let Judy keep the
    tally
  • Method 3 Jervis to the rescue

There are 2689 steps in this stairway
______ (really!)
II y a 2689 marches dan cet escalier
_______ vraiment!
?? 2689 ???_______ ????!
Each time a step down, make a mark
Down1, hat, back, Judy make a mark
One mark per digit
35
(No Transcript)
36
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

37
Example Stair Counting Problem
  • Find it out yourself !
  • Method 1 Walk down and keep a tally
  • Method 2 Walk down, let Judy keep tally
  • Method 3 Jervis to the rescue

2689 (down) 2689 (up) 2689 (marks) 8067
Down 3,616,705 122689 Up 3,616,705
122689 Marks 2,689 111
7,236,099 !
only 4 marks !
38
Example Stair Counting Problem
  • Size of the Input n
  • Method 1 Walk down and keep a tally
  • Method 2 Walk down, let Judy keep tally
  • Trick Compute twice the amount
  • and then divided by two
  • Method 3 Jervis to the rescue

3n
n2(12n) n(n1)n n22n
The number of digits in n log10 n1
39
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
  • Method 2 Quadratic time
  • Method 3 Logarithmic time

3n gt O(n)
n22n gt O(n2)
log10 n1 gt O(log n)
40
A Quiz
Big-O notation O(n2) O(n2) O(n2) O(n) O(log n)
Number of operations n25n 100nn2 (n7)(n-2) n10
0 number of digits in 2n
41
Big-O Notation
  • The order of an algorithm generally is more
    important than the speed of the processor

42
Time Analysis of C Functions
  • Example
  • Printout all item in an integer array of size N
  • Frequent linear pattern
  • A loop that does a fixed amount of operations N
    times requires O(N) time

43
Time Analysis of C Functions
  • Another example
  • Printout char one by one in a string of length N

for (i0 ilt strlen(str) i ) c
stri cout ltlt c
O(N2)!
44
Time Analysis of C Functions
  • Another example
  • Printout char one by one in a string of length N
  • Put a function call outside the loop if you can!

N strlen(str) for (i0 iltN i ) c
stri cout ltlt c
O(N)!
45
Time Analysis of C Functions
  • Worst case, average case and best case
  • search a number x in an integer array a of size
    N
  • Can you provide an exact number of operations?
  • Best case 121
  • Worst case 13N1
  • Average case 13N/21

for (i0 (ilt N) (ai ! x) i ) if (i
lt N) cout ltlt Number ltlt x ltlt is at location
ltlt i ltlt endl else cout ltlt Not Found! ltlt
endl
46
Testing and Debugging
  • Test run a program and observe its behavior
  • input -gt 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 whats
    the error
  • tool debugger

47
Summary
  • Ask yourselves FOUR questions
  • WHAT, WHY, WHERE HOW
  • Topics DSs, C, STL, basic algorithms
  • Data Structure experts
  • Schedule 24 lectures, 6-7 assignments, 3 exams
  • Information Blackboard system
  • Remember and apply two things (Ch 1)
  • Basic design strategy
  • Pre-conditions and post-conditions
  • Running time analysis
  • Testing and Debugging (reading 1.3)

48
Reminder
Lecture 2 ADT and C Classes Reading
Assignment before the next lecture Chapter
1 Chapter 2, Sections 2.1-2.3 Office Hours
Wednesday 200 pm 400 pm (location NAC
8/203A) Website CCNY Blackboard system
49
THE END
Write a Comment
User Comments (0)
About PowerShow.com