Professional Documents
Culture Documents
Lecture 1
Lecture 1
8
What does “size of the input” mean?
If we are searching an array, the “size” of the input could be the size of the
array
If we are merging two arrays, the “size” could be the sum of the two array
sizes
If we are computing the nth Fibonacci number, or the nth factorial, the
“size” is n
We choose the “size” to be the parameter that most influences the actual
time/space required
It is usually obvious what this parameter is
Sometimes we need two or more parameters
9
Characteristic operations
In computing time complexity, one good approach
is to count characteristic operations
What a “characteristic operation” is depends on the
particular problem
If searching, it might be comparing two values
If sorting an array, it might be:
comparing two values
swapping the contents of two array locations
both of the above
Sometimes we just look at how many times the
innermost loop is executed
10
Average, best, and worst cases of algorithm
Usually we would like to find the average time to perform an algorithm
However,
Sometimes the “average” isn’t well defined
Example: Sorting an “average” array
Time typically depends on how out of order the array is
11
Constant time is (usually)
better than linear time
Suppose we have two algorithms to solve a task:
Algorithm A takes 5000 time units
Algorithm B takes 100*n time units
Which is better?
Clearly, algorithm B is better if our problem size is small,
that is, if n < 50
Algorithm A is better for larger problems, with n > 50
So B is better on small problems that are quick anyway
But A is better for large problems, where it matters more
We usually care most about very large problems
But not always!
12
Common time complexities
BETTER O(1) constant time
O(log n) log time
O(n) linear time
O(n log n) log linear time
O(n2) quadratic time
O(n3) cubic time
O(2n) exponential time
WORSE
13
Example
14
Best, worst and average case analysis
• Best case is the function which performs the minimum number of
steps on input data of n elements.
Definition:
• Is a set of operation
• Mathematical abstraction
• No implementation detail
Example:
• Lists, sets, graphs, stacks are examples of
ADT along with their operations
17
Why ADT?
Modularity
• divide program into small functions
• easy to debug and maintain
• easy to modify
• group work
Reuse
• do some operations only once
Easy to change the implementation
• transparent to the program
18
Implementing an ADT
To implement an ADT, you need to choose:
• A data representation
must be able to represent all necessary values of the
ADT
should be private
• An algorithm for each of the necessary operation:
must be consistent with the chosen representation
all auxiliary (helper) operations that are not in the
contract should be private
Remember: Once other people are using it
• It’s easy to add functionality
19
The List ADT
The List is an
• Ordered sequence of data items called
elements
• A1, A2, A3, …,AN is a list of size N
• size of an empty list is 0
• Ai+1 succeeds Ai
• Ai-1 preceeds Ai
• Position of Ai is i
• First element is A1 called “head”
• Last element is AN called “tail”
20
List – An Example
21
List - Implementation
22
ADT in C
The C language only has support for structs with data
members (i.e. member variables). While this is sufficient to
represent the data of an ADT, the functions that operate on
the ADT must be defined separately from the struct. The
following is the data definition of an ADT to represent
triangles:
int main() {
Triangle t1 = { 3, 4, 5 };
Triangle t2 = { 2, 2, 2 };
}