Lecture 1

advertisement
Introduction to Data
Structures and
Algorithms
CS 110: Data Structures and Algorithms
First Semester, 2010-2011
Learning Objectives
► Define
introductory terms, such as
algorithm and data structure
► Write pseudo-code according to
conventions
► Review mathematical concepts such as
summation, logarithms, and induction
Introduction
Beware of bugs in the above code; I have
only proven it correct, not tried it.
--Donald Knuth
Definitions
► Algorithm
– a step-by-step procedure to
perform a task
► Real
world: Balancing a checkbook
► CS:
Adding a list of numbers
► Data
Structure – a systematic way of
organizing and accessing data
► Real
world: Filing Cabinet
► CS:
Hierarchical file system
Why Study Algorithms?
► The
obvious solution to a problem is not
always the most efficient.
► Example: Adding integers from 1 to n
► Obvious
method:
int sum = 0;
for (int i = 1; i <= n; i ++)
sum = sum + i;
► Is
there a better way?
Why Study Data Structures?
► Algorithms
and data structures are usually
developed hand-in-hand
►
Example: Pushing and popping from a stack
► The
behavior of an algorithm depends on
how the data is structured.
►
Example: Searching a disc vs. searching a tape
► Tape:
fast-forward, rewind
► Disc:
select a track
Design Goals
► Correctness
►
Should correctly
solve the task it is
designed for
► For
all possible
inputs!
► Always
depends on
the specific task.
► Efficiency
►
Should not use any
more of the
computer’s resources
than necessary
► Processing
► Memory
time
Implementation Goals
► Robustness
Gracefully handle incorrect input
► Example: Therac-25
►
► Adaptability
Evolve in response to change
► Example: Y2K bug
►
► Reusability
Allow use in many applications
► Example: Java class libraries
►
How will we study it?
► Write
algorithms in Java or another
programming language?
► Pitfalls:
► Solutions
become tied to a particular
language or paradigm
► How do we test the correctness and
efficiency of an algorithm before its
written?
Pseudo-Code
► Combine
high-level descriptions with familiar
programming language structures
► Written for humans, not computers
Algorithm addFromOneToN(n)
Input: An integer n
Output: The sum of all integers from 1 to n
sum ← 0
for i ← 1 to n do
sum ← sum + i
return sum
Pseudo-code Conventions
► Expressions
► Algorithm
Structures
► Control Structures
Pseudo-code Conventions:
Expressions
► Standard
►
Math Symbols
+-*/()
► Relational
►
<>≤≥=≠
► Boolean
►
Operators
Operators
and or not
► Assignment
Operator: ←
► Array Indexing: A[i]
Pseudo-code Conventions:
Algorithm Structure
► Algorithm
heading
Algorithm name(param1, param2,...):
Input: input elements
Output: output elements
► Statements
call
return statement
control structures
object.method(arguments)
return value
Pseudo-code Conventions:
Control Structure
► Decision
► If
Structures
... then ... [else]
► Loops
► While
... do
► Repeat ... until
► For ... do
General Rules
► Communicate
high level ideas and not
implementation details (programming
language specifics)
► Clear and informative
Pseudo-Code
► Given
an array A with size n, write pseudocode
to find the maximum element in A
Algorithm arrayMax(A,n):
Input: An array A storing n integers.
Output: The maximum element in A.
currentMax ← A[0]
for i ←1 to (n - 1) do
if currentMax < A[i] then
currentMax ← A[i]
return currentMax
Review: Summation
Review: Summation
Review: Logarithms
► logb
m = x  bx = m
► logb (mn) = logb m + logb n
► logb (m/n) = logb m - logb n
► logb (mn) = n logb m
► logb b = 1
► logb m = (loga m) / (loga b)
► 0 < a < b  log a < log b
Logarithm Conventions
► In…
► Calculus:
log is implied to be base e
► Physics: log is implied to be base 10
► CS: log is implied to be base 2
► For
clarification, we may use lg or lb to
denote log base 2
► i.e.
lg a = lb a = log2 a
Review: Mathematical Induction
► Step
►
Is the statement true for n = 0 or 1?
► Step
►
1 – Check the base case
2 – State the induction assumption
“The statement is true for all n ≤ k”
► Step
3 – Prove the next case in the sequence
Is the statement true for n = k + 1?
► This will (normally) use the Step 2 assumption in
its proof
►
Mathematical Induction
► Step
1: Base Case
► Step
2: Inductive Step assume for all n ≤ k
Mathematical Induction
► Step
3: Proof of next case – Show that
Download