Title: Data Structure Complexity.: Wednesday, December 8, 2021
Title: Data Structure Complexity.: Wednesday, December 8, 2021
ALGORITHMS
CS 245
PRESENTATION
TITLE:
DATA STRUCTURE COMPLEXITY.
PRESENTERS:-
Introduction.
Two main complexity measure.
Example.
Complexity classes.
Algorithm alnalysis.
2
INTRODUCTION.
3
CONT…
4
CONT…
5
TWO MAIN COMPLEXITY MEASURES
These are:-
i) Time complexity .
6
TIME COMPLEXITY .
7
SPACE COMPLEXITY.
8
COMPARISON BETWEEN ALGORITHMS.
9
CONT…
Input size (n) bubble sort selection sort merge sort insertion sort
0 0.000000 0.000000 0.000000 0.000000
10000 0.680000 0.220000 0.150000 0.270000
20000 2.740000 0.870000 0.590000 1.090000
30000 6.160000 1.950000 1.330000 2.450000
40000 10.880000 3.490000 2.360000 4.340000
50000 17.070000 5.520000 3.690000 6.760000
60000 24.639999 7.870000 5.320000 9.790000
10
CONT...
Now let us try to analyze in a bit details, comparing the
number of array accesses by taking selection sort and
merge sort algorithms.
11
n T(n) Tm(n)
--- ---- -----
20 456 479
21 500 511
22 546 544
23 594 576
24 644 610
25 696 643
26 750 677
27 806 711
28 864 746
29 924 781
30 986 816 12
CONT…
We see that merge sort starts to take a little less time than
selection sort for larger values of n. If we extend the
table to large values:
13
Selection sort on ten million items takes roughly 100
trillion accesses; if each one takes ten nanoseconds it
will take 1,000,000 seconds, or about 11 and a half days
to complete. Merge sort, with a "mere" 1.2 billion
accesses, will be done in 12 seconds. For a billion
elements, selection sort takes almost 32,000 years, while
merge sort takes about 37 minutes. And, assuming a
large enough RAM size
14
AN EXAMPLE: SELECTION SORT
this kind of analysis gives you a good idea of the amount of time
you'll spend waiting, and allows you to compare this algorithms to
other algorithms that have been analyzed in a similar way.
16
COMPLEXITY CLASSES.
There are three main complexity classes in which
algorithm can be placed.
Oh.
Omega.
Theta.
17
BIG O NOTATION.
Big-O notation (also known as big Oh notation, big
Omicron notation, Landau notation, Bachmann–Landau
notation, and asymptotic notation.
18
CONT..
The performance of a program is measured in
milliseconds (i.e. its speed of execution) but complexity
is measured in the form of "Big-O" notation.
19
CONT..
Definition.
Let f(n) and g(n) be functions defined on the set of
natural numbers. A function f(n) is said to be
f (n) = O(g(n)) if there exist positive constants C and n0
such that
21
EXAMPLE.
Definition.
23
CONT…
Means that g(n) is lower bound of f(n).
Graphically:
24
CONT…
There fore, the running time of an algorithm is
Ω(g(n)).we
mean that no matter what particular input of size n is
chosen for each value of n,
the running time on that input is at least a constant times
g(n), for sufficiently
large n.
25
BIG THETA NOTATION.(Ѳ)
26
CONT…
Diffinition.
28
ORDER OF MAGNITUDES.
Examples.
30
Insert and Remove operations for a queue.
O(N) - LINEAR TIME
Examples:
33
CONT…
O(n log n) - "n log n " time
Examples:
Examples:
Towers of Hanoi.
O(l) < O(log n) < O(n) < O(n log n) < O(n2) < O(n3) <
O(an)
35
CONT…
36
ALGORITHM ANALYSIS.
37
CONT…
The worst case running time is the longest possible time
it could take for the algorithm to terminate.
38
EXAMPLE
efficiency is : O(n) .
39
MORE EXAMPLE
Total number of iterations is the product of total number of inner loop iterations and outer loop iterations.
O(n2)
41
MORE EXAMPLE.
F(n) = n+1+n+n
=3n+1
42
THE END.
THANKS
Question…!
Contribution..
Comment…..!
43