Data Structures - Data Structures - : Lecture 1: Introduction
Data Structures - Data Structures - : Lecture 1: Introduction
Data Structures
- Section AB
Lecture 1: Introduction
Instructor: Hao Tang
Tao Wang
Department of Computer Science
City College of New York
1
ccvcl.org/~tang/cs212.html
-Come back frequently for updates of lecture schedule,
programming assignments and exam schedule
- Reading assignments & programming assignments
WHAT (Topics)
WHY (Importance)
WHERE (Goals)
HOW (Information and Schedule)
Topics (WHAT)
Data Structures
templates, iterators
ADTs in our DS course cut-down version of STL
Importance (WHY)
Goals (WHERE)
understand the data types inside out
Objectives
Data Structures, with C++ and Software Engineering
Prerequisites
Computing Facilities
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)
Lectures 11. 11a. Template and STL (Ch 6)
Lecture 12.
Stacks (Ch 7) and Queues (Ch 8)
Lectures 13-14. Recursion (Ch 9)
Reviews and the 2nd exam (Ch. 5-9)
Lectures 15-18. Trees (Ch 10, Ch 11)
Lectures 19-20. Searching and Hashing (Ch 12)
Lectures 21- 22. Sorting (Ch 13)
Lecture 23. Graphs (Ch 15)
Reviews and the 3rd exam (mainly Ch. 10-13, ~July 23 or 24)
8
class
Quizzes
5-15
minutes in class
Talks
CCNY
Topics including Machine Learning, Computer
Vision, HCI, Robotics and Visualization.
Outline
Information
Topics
Schedule
Design Technique
12
Example
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
15
Example
void write_sqrt( double x )
// Precondition: x >= 0.
// Postcondition: The square root of x has
// been written to the standard output.
...
16
Example
void write_sqrt( double x)
// Precondition: x >= 0.
// Postcondition: The square root of x has
// been written to the standard output.
...
17
Example
void write_sqrt( double x)
// Precondition: x >= 0.
// Postcondition: The square root of x has
// been written to the standard output.
...
}
18
Example
Which of these function calls
meet the precondition ?
write_sqrt( -10 );
write_sqrt( 0 );
write_sqrt( 5.6 );
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.
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.
21
Example
void write_sqrt( double x)
// Precondition: x >= 0.
// Postcondition: The square root of x has
// been written to the standard output.
...
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.
...
23
Another Example
What values will be returned
by these function calls ?
is_vowel( 'A' );
is_vowel(' Z' );
is_vowel( '?' );
24
Another Example
What values will be returned
by these function calls ?
is_vowel( 'A' );
is_vowel(' Z' );
is_vowel( '?' );
true
false
Consequence of Violation
Who is responsible for the crash ?
write_sqrt(-10.0);
is_vowel( '?' );
THEN MY FUNCTION
WILL EXECUTE, AND WHEN
IT IS DONE, THE
POSTCONDITION WILL BE
TRUE.
I GUARANTEE IT.
28
A Quiz
The famous skyline was
dark on Aug 14th, 2003.
You
The programmer who
wrote that Power
Supply function
Mayor Bloomberg
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.
30
31
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);
...
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.
34
Break
35
Postcondition
The programmer
who writes a
function ensures
that the
postcondition is
true when the
function finishes
executing.
36
Design Technique
Analysis
Fast enough?
How much longer if input gets larger?
Which among several is the fastest?
38
1789 (Birnbaum)
1671 (Joseph Harriss)
1652 (others)
1665 (Official Eiffel Tower Website)
Find
it out yourself !
Eiffel Tower
39
a tally
Each time a step down, make a mark
Method
There
II
y a are
2689
2689
2689
steps in
marches
this
dan
cet
stairway
escalier
_______
_______
______
!
vraiment!
(really!)
40
Count
certain operations
Eiffel Tower
41
3,616,705 = 1+2++2689
Marks:
Method
7,236,099 !
2,689 = 1+1++1
only 4 marks !
Eiffel Tower42
of the Input : n
Method
3n
Method
Method
Eiffel Tower43
Use
1: Linear time
3n => O(n)
Method
2 : Quadratic time
3: Logarithmic time
A Quiz
Number of operations
Big-O notation
n2+5n
O(n2)
100n+n2
O(n2)
(n+7)(n-2)
O(n2)
n+100
O(n)
number of digits in 2n
O(log n)
45
Big-O Notation
The
O (n)
O (n2)
# of stairs: n [log10n]+1
3n
n2+2n
10
30
120
100
300
10,200
1000
3000
1,002,000
46
2 C++
operations or
more?
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;
}
2
O(N )!
48
Another example
N = strlen(str);
for (i=0; i<N; i++ )
{
c = str[i];
cout << c;
}
O(N)!
49
50
rule: never change if you are not sure whats the error
tool: debugger
51
Summary
Reminder
Lecture 2: ADT and C++ Classes
Reading Assignment before the next lecture:
Chapter 1
Chapter 2, Sections 2.1-2.3
Office Hours: Mon, Wed 12 am 1 pm
(location: NAC 8/210)
53
name!
Once
54
THE END
55