Data Structures and Algorithms Analysis - Copy
Data Structures and Algorithms Analysis - Copy
problem.
With abstraction you create a well-defined
Data Type
What operations can be done on/by
Abstraction is a process of
classifying characteristics as relevant
and irrelevant for the particular
purpose at hand and ignoring the
irrelevant ones.
Algorithms
a data structure
An algorithm may change the data structure
itself.
1.2.1. Properties of an algorithm
Finiteness: Algorithm must complete after
a finite number of steps.
Definiteness: Each step must be clearly
problem completely.
Effectiveness: It must be possible to
the program
◦Input Size
◦Input Properties
Operating Environment
1.2.3. Complexity Analysis
1.int count(){
int k=0;
cout<< “Enter an integer”;
cin>>n;
for (i=0;i<n;i++)
k=k+1;
return 0;}
Time Units to Compute
1 for the assignment statement: int k=0
1 for the output statement.
1 for the input statement.
In the for loop:
1 assignment, n+1 tests, and n increments.
n loops of 2 units for an assignment, and an
addition.
1 for the return statement.
----------------------------------------------------------------
---
T (n)= 1+1+1+(1+n+1+n)+2n+1 = 4n+6 = O(n)
Example2
int total(int n)
{
int sum=0;
for (int i=1;i<=n;i++)
sum=sum+1;
return sum;
}
Time Units to Compute
two multiplications.
1 for the return statement.
---------------------------------------------------------
T (n)= 1+(1+n+1+n)+4n+1 = 6n+4 = O(n)
Exercise
Suppose we have hardware capable of
executing 106 instructions per second.
How long would it take to execute an
algorithm whose complexity function was:
T (n) = 2n2 on an input size of n=108?
The total number of operations to be
.
Measures of Times
In order to determine the running time
of an algorithm it is possible to
define three functions Tbest(n), Tavg(n)
and Tworst(n) as the best, the average
and the worst case running time of
the algorithm respectively.
Average Case (Tavg): The amount of time the
algorithm takes on an "average" set of inputs.
Worst Case (T
worst): The amount of time the
algorithm takes on the worst possible set of
inputs.
Best Case (T ): The amount of time the
best
algorithm takes on the smallest possible set of
inputs.
We are interested in the worst-case time, since it
an algorithm is n2 – n, n is insignificant
compared to n2 for large values of n. Hence
the n term is ignored.
Big-Oh is mainly concerned with large
values of n.
Formal Definition:
(n).
Examples:
The following points are facts that
you can use for Big-Oh problems:
1<=n for all n>=1
n<=n2 for all n>=1
2n <=n! for all n>=4
n>=1.
(c=15
,k=1).
1.4.2. Big-Omega Notation ()
not o(n2).
1.4.5. Little-Omega ( notation)
property.
Max-Heap: The value of the root node must be
the greatest among all its descendant nodes
and the same thing must be done for its left and
right sub-tree also.
Min-Heap: The value of the root node must
be the smallest among all its descendant
nodes and the same thing must be done for
its left and right sub-tree also.
Operations on Heaps
Insertion:
•Insert the new element at the end of the heap
(maintaining completeness).
Deletion (typically root deletion):
Replace the root with the last element in the
heap.
Peek:
•Access the root element (max in max-heap or min in
min-heap) without removing it.
Heapify Operation:
Used to convert an arbitrary array into a heap.
Heap Complexities