DSA-ch1
DSA-ch1
2
Algorithm
Algorithms are steps that need to be followed to solve a
problem.
A recipe
An algorithm takes the input to a problem and transforms
it to the output.
A mapping of input to output problem
algorithm
4
Example
• Two algorithms for computing the Factorial
• Which one is better?
2)
1) int factorial (int n)
int factorial (int n) {
{ if (n<=1) return 1;
if (n <= 1) return 1; else {
else fact = 1;
return n * factorial(n-1); for (k=2; k<=n; k++)
} fact *= k;
return fact;
}
5
Algorithm vs Program
Algorithm Program
Design Implementation
Analyze Testing
6
Priori Analysis Posteriori Testing
Algorithm Program
7
Properties of Algorithms
1. Finiteness:
• Algorithm must complete after a finite number
of steps.
• Algorithm should have a finite number of steps.
17
How to Measure Efficiency/performance?
Two approaches to measure algorithms efficiency/performance
Empirical
Implement the algorithms and
Trying them on different instances of input
Use/plot actual clock time to pick one
Theoretical/Asymptotic Analysis
Determine quantity of resource required
mathematically needed by each algorithms
18
Example- Empirical
Actual clock time
Input size
19
Drawbacks of empirical methods
It is difficult to use actual clock because clock time varies based on
Specific processor speed
1.78GHz 2.12GHz
10s <10s
Current processor load
• Only the work 10s
• With printing 15s
• With printing & browsing the internet >15s
20
Example
Problem: Need to multiply two positive integers a and b
Algorithm 1: Multiply a and b
Algorithm 2:
1. V = a, W = b
2. While W > 1
V =V + b; W=W-1
3. Output V
First algorithm has 1 multiplication.
Second has b additions and subtractions.
For some computer architectures, 1 multiplication is more expensive than
b additions and subtractions.
Ideally, we would like to program all choices and run all of them in the
machine we are going to use and find which is efficient!-Very difficult
21
Machine independent analysis
Critical resources:
Time, Space (disk, RAM), Programmer’s effort, Ease of use
(user’s effort).
Factors affecting running time:
System dependent effects.
Hardware: CPU, memory, cache, …
Software: compiler, interpreter, garbage collector, …
System: operating system, network, other apps, …
System independent effects
Algorithm.
Input data/ Problem size
22
Cont..
For most algorithms, running time depends on “size” of the
input.
Size is often the number of inputs processed
Example:- in searching problem, size is the no of items to be
sorted
Running time is expressed as T(n) for some function T
on input size n.
23
Machine independent analysis
Efficiency of an algorithm is measured in terms of the number of basic
operations it performs.
Not based on actual time-clock
We assume that every basic operation takes constant time.
Arbitrary time
Examples of Basic Operations:
Single Arithmetic Operation (Addition, Subtraction, Multiplication)
Assignment Operation
Single Input/Output Operation
Single Boolean Operation
Function Return
We do not distinguish between the basic operations.
Examples of Non-basic Operations are
Sorting, Searching.
24
Machine independent analysis
Problem: Need to multiply two positive integers a and b
Algorithm 1: Multiply a and b
Algorithm 2:
1. V = a, W = b
2. While W > 1
3. V =V + a; W=W-1
4. Output V
25
Rules for computing complexity
The complexity function T(n) of a given algorithm is
determined by counting the number of operations of the
algorithm. But there are two kinds of counting the
operations:
o In-formal method: tries to count all the operations of the algorithm
o Formal method: counts operations by ignoring the variable
initializations, loop controls…
26
Analysis Rules:
1. We assume an arbitrary time unit.
2. Execution of one of the following operations takes time 1:
Assignment Operation
Single Input/Output Operation
Single Boolean Operations
Single Arithmetic Operations
Function Return
3. Running time of a selection statement (if, switch) is the time for the condition
evaluation + the maximum of the running times for the individual clauses in the
selection.
4. Loops: Running time for a loop is equal to the running time for the statements
inside the loop * number of iterations.
The total running time of a statement inside a group of nested loops is the
running time of the statements multiplied by the product of the sizes of all the
loops.
For nested loops, analyze inside out.
Always assume that the loop executes the maximum number of iterations
possible.
5. Running time of a function call is 1 for setup + the time for any parameter
27
calculations
By:Y&M + the time required for the execution of the function body.
Examples: Count of Basic Operations T(n)
Sample Code
int count()
{
Int k=0;
cout<< “Enter an integer”;
cin>>n;
for (i = 0;i < n;i++)
k = k+1;
return 0;
}
28
Examples: Count of Basic Operations T(n)
Count of Basic Operations
Sample Code (Time Units)
int count()
{
1 for the assignment statement: int k=0
Int k=0;
cout<< “Enter an integer”; 1 for the output statement.
29
Examples: Count of Basic Operations T(n)
int total(int n)
{
Int sum=0;
for (int i=1;i<=n;i++)
sum=sum+i;
return sum;
}
30
Examples: Count of Basic Operations T(n)
Count of Basic Operations
Sample Code (Time Units)
int total(int n)
{
1 for the assignment statement: int sum=0
Int sum=0;
In the for loop:
for (int i=1;i<=n;i++) 1 assignment, n+1tests, and n increments.
sum=sum+i; n loops of 2 units for an assignment, and an
return sum; addition.
1 for the return statement.
}
31
Examples: Count of Basic Operations T(n)
void func()
{
Int x=0;
Int i=0;
Int j=1;
cout<< “Enter an Integer value”;
cin>>n;
while (i<n){
x++;
i++;
}
while (j<n)
{
j++;
}
}
32
Examples: Count of Basic Operations T(n)
33
Examples: Count of Basic Operations T(n)
Sample Code
int sum (int n)
{
int partial_sum= 0;
for (int i = 1; i <= n; i++)
partial_sum= partial_sum+ (i * i * i);
return partial_sum;
}
34
Examples: Count of Basic Operations T(n)
35
Simplified Rules to Compute Time Units
for Loops: Formally
In general, a for loop translates to a summation. The index and bounds of
the summation are the same as the index and bounds of the for loop
36
Simplified Rules to Compute Time Units
Nested Loops: Formally
Nested for loops translate into multiple summations, one for
each for loop.
37
Simplified Rules to Compute Time Units
Consecutive Statements: Formally
Add the running times of the separate blocks of your code.
38
Simplified Rules to Compute Time Units
Conditionals:
If (test) s1 else s2: Compute the maximum
of the running time for s1 and s2.
if (test == 1) {
for ( int i = 1; i <= N; i++) {
sum = sum+i;
}}
else
{
for ( int i = 1; i <= N; i++) {
for ( int j = 1; j <= N; j++) {
sum = sum+i+j;
}}
39
Example: Computation of Run-time
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?
40
Example: Computation of Run-time
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?
41
Types of Algorithm complexity analysis
Best case. Exam ple
int search(int a[ ] ,int &x, int n)
T(n) = minimum time of algorithm on any input of size IL
for(int i=0;i<=n && a[i]!=x;i++)
Lower bound on cost. {
Determined by “easiest” input. if(i==n-1)
Provides a goal for all inputs. {
44
Growth rates
The growth rate for an algorithm is the rate at which the
cost of the algorithm grows as the size of its input grows.
45
Rate of Growth
Consider the example of buying elephants and goldfish:
Cost: cost_of_elephants + cost_of_goldfish
Cost ~ cost_of_elephants (approximation)
since the cost of the gold fish is insignificant when
compared with cost of elephants
Similarly, the low order terms in a function are relatively
insignificant for large n
n4 + 100n2 + 10n + 50 ~ n4
i.e., we say that n4 + 100n2 + 10n + 50 and n4 have the same rate of growth
More Examples: fB(n)=n2+1 ~ n2
fA(n)=30n+8 ~ n
46
Visualizing Orders of Growth
On a graph, as
you go to the
right, a faster
growing
Value of function
function fA(n)=30n+8
eventually
becomes
larger...
fB(n)=n2+1
Increasing n
47
Asymptotic analysis
Refers to the study of an algorithm as the input size "gets big"
or reaches a limit.
To compare two algorithms with running times f(n) and g(n),
we need a rough measure that characterizes how fast each
function grows- growth rate.
Ignore constants [especially when input size very large]
But constants may have impact on small input size
49
Big-Oh Notation - Visually
50
Big-O Visualization
O(g(n)) is the set of functions
with smaller or same order of
growth as f(n)
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
log2n <= n for all n >= 2
n <= nlog2n for all n >= 2
52
Examples
f(n) = 10n + 5 and g(n) = n. Show that f(n) is in O(g(n)).
To show that f(n) is O(g(n)) we must show constants c and no
such that
f(n) <= c.g(n) for all n >= no
53
Examples
2n2 = O(n3): 2n2 ≤ cn3 2 ≤ cn c = 1 and n0= 2
1000n2+1000n = O(n2):
1000n2+1000n ≤ 1000n2+ n2
<= 1001n2 c=1001 and n0 = 1000
54
No Uniqueness
There is no unique set of values for n0 and c in proving the
asymptotic bounds
55
Exe rcis e s :
58
Implication of Big-Oh notation
We use Big-Oh notation to say how slowly code might
run as its input grows.
Suppose we know that our algorithm uses at most
O(f(n)) basic steps for any n inputs, and n is sufficiently
large, then we know that our algorithm will terminate
after executing at most constant times f(n) basic steps.
We know that a basic step takes a constant time in a
machine.
Hence, our algorithm will terminate in a constant times
f(n) units of time, for all large n.
59
Lower Bounds - Omega ()
We say that “ f(n) is omega g(n),”' which we write f(n) =
(g(n)), if there exists an integer n0 and a constant C>0
such that for all integers n ≥ n0, f(n) ≥ Cg(n).
60
Omega ()- Visually
61
Example
Consider the function f(n)=5n2-64n+256. Clearly, f(n) is non-
negative for all integers n ≥ 0.
We wish to show that f(n) = (n2). Therefore, we need to find an
integer n0 and a constant C>0 such that for all integers n ≥ n0,
f(n) ≥ C n2.
Suppose we choose C=1. Then
f(n) ≥ C n2. 5n2 – 64n + 256 ≥ n2
4n2 – 64n +256 ≥ 0
4(n-8)2 ≥ 0
Since (n-8)2 > 0 for all values of n ≥ 0, we conclude that n0 = 0.
So, we have that for C=1 and that n0 = 0, f(n) ≥ C n2 for all
integers n ≥ n0. Hence, f(n) = (n2). .
62
Theta ()
For a given function g(n), we denote by (g(n)) the
set of functions.
(g(n))
f (n):thereexistpositive
constantsc1,c2,andn0 s.t.
0c1g(n) f (n) c2g(n)forallnn0
A function f (n) belongs to the set (g(n)) if there
exist positive constants c1 and c2 such that it can be
“sand- wiched” between c1g(n) and c2g(n) or sufficienly
large n.
f (n) (g(n))means that there exists some constant c1
and c2 s.t. c1g(n) f (n) c2g(n) for large enough n.
63
f(n
)
f(n) =
0 (g(n))
Little o
We say that ``f(n) is little o g(n),'' which we write f(n)=o(g(n)),
if and only if f(n) is O(g(n)) but f(n) is not (g(n)).
66
Reading Assignment
Review of elementary data structures
Heap and Heap Sort
Hashing
sets representation
UNION, FIND operation
67
End of Lecture One
68