Lecture 2 DS
Lecture 2 DS
Introduction:
Basic Concepts and Notations
Specification of
Specification of
input ? output as a
function of input
Specification of
Specification of
Algorithm output as a
input
function of input
• Efficient:
– Running time
– Space used
• Efficiency as a function of input size:
– The number of bits in an input number
– Number of data elements(numbers, points)
• Complexity Of Algorithms
• Time and space are two measures for
efficiency of an algorithm.
• Time is measure by counting number of key
operation.
• Space is measure by counting maximum of
memory needed by the algorithm.
Time Complexity
• Worst-case
– An upper bound on the running time for any input
of given size
• Average-case
– Assume all inputs of a given size are equally likely
• Best-case
– The lower bound on the running time
Time Complexity – Example
• Sequential search in a list of size n
– Worst-case:
• n comparisons
– Best-case:
• 1 comparison
– Average-case:
• n/2 comparisons
Example 1
• //linear
• for(int i = 0; i < n; i++) {
• cout << i << endl;
• }
• Ans: O(n)
Example 2
• //quadratic
• for(int i = 0; i < n; i++) {
• for(int j = 0; j < n; j++){
• //do swap stuff, constant time
• }
• }
• Ans O(n^2)
Example 3
• for(int i = 0; i < 2*n; i++) {
• cout << i << endl;
• }
• At first you might say that the upper bound is
O(2n); however, we drop constants so it
becomes O(n)
Example 4
• //linear
• for(int i = 0; i < n; i++) {
• cout << i << endl;
• }
•
• //quadratic
• for(int i = 0; i < n; i++) {
• for(int j = 0; j < n; j++){
• //do constant time stuff
• }
• }
• Ans : In this case we add each loop's Big O, in
this case n+n^2. O(n^2+n) is not an acceptable
answer since we must drop the lowest term.
The upper bound is O(n^2). Why? Because it
has the largest growth rate
Example 5
• for(int i = 0; i < n; i++) {
• for(int j = 0; j < 2; j++){
• //do stuff
• }
• }
• Ans: Outer loop is 'n', inner loop is 2, this we
have 2n, dropped constant gives up O(n)
Example 6
• for(int i = 1; i < n; i *= 2) {
• cout << i << endl;
• }
• There are n iterations, however, instead of
simply incrementing, 'i' is increased by 2*itself
each run. Thus the loop is log(n).
Example 7
• for(int i = 0; i < n; i++) { //linear
• for(int j = 1; j < n; j *= 2){ // log (n)
• //do constant time stuff
• }
• }
• Ans: n*log(n)
Question
f(n) = O(g(n))
read as “f(n) is big oh of g(n)”) means
that f(n) is asymptotically smaller than or
equal to g(n).
Therefore, in an asymptotic sense g(n) is
an upper bound for f(n).
f(n) = Ω(g(n))
(read as “f(n) is omega of g(n)”) means
that f(n) is asymptotically bigger than
or equal to g(n).
Therefore, in an asymptotic sense, g(n)
is a lower bound for f(n).
1 < logn < n < nlogn < n^2 < n^3 < 2^n <n!
Θ notation
g(n) is an asymptotically tight bound of f(n)
f(n) = Θ(g(n))
(read as “f(n) is theta of g(n)”)
Means that f(n) is asymptotically equal to
g(n)
• f(n) = ω(g(n)) if for any positive constant c>0, there exist a constant
n0>0 such that
f(n) > c g(n) 0 for all nn0.
34
Intuition for Asymptotic Notation
Big-Oh
f(n) is O(g(n)) if f(n) is less than or equal to g(n)
Big-Omega
f(n) is (g(n)) if f(n) is greater than or equal to g(n)
Big-Theta
f(n) is (g(n)) if f(n) is equal to g(n)
Relations Between , O,
Comp 122
time space tradeoff
• The best algorithm used to solved a problem is the one that
requires less time to complete and takes less space in
memory.
• But in practice it is not possible to achieve both of these
objective. Thus we have to sacrifice one at the cost of the
other.
• A time space tradeoff is a situation where the memory use
can be reduced at the cost of slower program execution (and,
conversely, the computation time can be reduced at the cost
of increased memory use).
Classification of Data Structures
Data Structures
Primitive Non-Primitive
Data Structures Data Structures
• Integer
Linear Non-Linear
• Real Data Structures Data Structures
• Character
• Boolean • Array • Tree
• Stack • Graph
• Queue
• Linked List
Data Structure Operations
Data Structures are processed by using certain operations.
1.Traversing: Accessing each record exactly once so that certain
items in the record may be processed.