Académique Documents
Professionnel Documents
Culture Documents
Introduction
3
Course Objectives
Reinforce the concept that costs and benefits exist for every
data structure.
Speed==Fun?
If a software feature can not cope with the scale of tasks users need to perform –
it is as good as dead.
Difference between complexity and
computation time
Computation time: The interval of solving a problem based on the embedded system
architecture. (in the field of computer sciences)
Complexity: The art of handling a problem based on the algorithm designed to solve a case.
7
Suppose we have two algorithms, how can we tell which one is better?
1. Performance:
how much time/memory/disk/... is actually used when a program
is run. This depends on the machine, compiler, etc. as well as the code.
2. Complexity:
how do the resource requirements of a program or algorithm scale, i.e.,
what happens as the size of the problem being solved gets larger?
Big O Notation
Big O notation (with a capital letter O, not a zero), is a symbol used in complexity theory,
computer science, and mathematics to describe the asymptotic behavior of functions.
Basically, it tells you how fast a function grows or declines
Functions defined in big o notation
O(1) constant
O(log(n)) logarithmic
O((log(n))c) polylogarithmic (same as O(log(n)) )
O(n) linear
O(n2) quadratic
O(nc) polynomial
O(cn) exponential
Understanding big o
if (cond) then Here, either block 1 will execute, or block 2 will execute.
block 1 (statements) Therefore, the worst-case time is the slower of the two
else possibilities:
block 2 (statements)
end if; max(time(block 1), time(block 2))
for I in 1 .. N loop
for Jin 1 .. M loop
The statements in the inner loop execute a
sequence of statements
total of N * M times. Thus, the complexity is
end loop;
O(N * M).
end loop;
22
Nested Loop(Quadratic Time)
23
Function Calls
Its behavior is more like nested loop if it is called inside loop and it has an
characteristic loop inside as well
25
Assignment#1
A nested loop in which the number of times the inner loop executes depends
on the value of the outer loop index:
for (i = 0; i < N; i++)
{ for (j = N; j > i; j--)
{ sequence of statements }
27
Arrays
2
Abstract Data Type
Using arrays
Singly linked list
Doubly linked list
Circularly linked list.
3
Abstract Data Type
top 1
top 7 7
top 5 5 5
top 2 2 2 2
push(2) push(5) push(7) push(1)
top 21
top 7 7 top 7
5 5 5 top 5
2 2 2 2 top 2
1 pop() push(21) 21 pop() 7 pop() 5 pop()
8
Stack Operation
top 1
2 5 7 1
7
5 0 1 2 3 4
2 top = 3
11
Stack using an Array
int pop()
{
return A[current--];
}
void push(int x)
{
A[++current] = x;
}
38
Stack Operations with Array
int top()
{
return A[current];
}
int IsEmpty()
{
return ( current == -1 );
}
int IsFull()
{
return ( current == size-1);
}
A quick examination shows that all five operations take constant time.
39
Stack Using Linked List
Please feel free to ask any thing related to topic covered in these slides.
Text Book:
Nell Dale : C++ Plus Data Structures, Latest Edition, Jones & Bartlett
Reference Text Books:
Data Structure using C/C++ by Mark Alan.
Yadidyah Langsam, Moshe J. Augenstein, Aaron M. Tenenbaum: Data
Structures using C and C++.
Handouts:
Dr. Sohail Aslam: Data Structures Handouts.