Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
unit - 1
What is an Algorithm?
● Step-wise procedure or instruction to solve a given
problem.
Why algorithm analysis?
● Helps to determine an efficient algorithm in terms of
time and space consumed.
Goal – to compare the algorithms – in terms of running
time, memory consumed, developers effort, etc…
How to Compare Algorithms?
1. Time Complexity– running time or processing time
How fast an algorithm is?
What affects its running time?
2. Space complexity – amount of computer memory
required during program execution.
Data structures takes space
What kind of data structure
How does the choice of data structure affects the
space?
Types of Analysis
Mathematical Notations for time complexity
Asymptotic notation
1. Big-O notation - Running time functions of
algorithms are assessed more precisely using Big-Oh
2. Omega notation
3. Theta notation
Big-O notation
● Order notation - Defines that g(n) is the tight upper bound for
a given function f(n). f(n) and g(n) are two different functions to
be compared with.
f(n) = O( g(n) )
This can also be written as, 0<= f(n) <=c g(n) for n >= n0
Big-O notation
Example:
Given algorithm: f(n) = n4+100n2+10n+50
g(n)= n4
Hence f(n)=O(n4)
Proof:
1. f(n)<=c g(n) n4+100n2+10n+50 <= n4
2. If we assign c=2,
then n4+100n2+10n+50 <= 2n4
Big-O notation
Example:
Given algorithm: f(n) = 2n+5
g(n)= n
Hence f(n)=O(n)
Proof:
1. f(n)<=c g(n) 2n + 5 <= n
2. If we assign c=3, then 2n + 5 <= 3n
Big-O notation
c g(n) = 3n
f(n) = 2n+5
g(n) = n
point where 3n
“beats” 2n+5
2n+5 is O( n )
11
Uses of Big-O notation
Big-Oh allows us to ignore constant factors and
lower order (or less dominant) terms
2n2 + 5n – 4 is O( n2 )
f(n) = O(n)
Functions by increasing growth rate
● The constant function: f(n) = 1
● The logarithm function: f(n) = log n
● The linear function: f(n) = n
● The n log n function: f(n) = n log n
● The quadratic function: f(n) = n2
● The cubic function: f(n) = n3
● The exponential function: f(n) = 2n
Example - Functions by increasing growth
rate
log n n n log n n2 n3 2n
0 1 0 1 1 2
1 2 2 4 8 4
2 4 8 16 64 16
3 8 24 64 512 256
4 16 64 256 4096 65536
5 32 160 1024 32768 4294967296
Big-Omega notation
● Defines that g(n) is the tight lower bound for a given
function f(n). f(n) and g(n) are two different functions to be
compared with.
f(n) = (g(n) )
This can also be written as, 0<= c g(n) <= f(n) for n >= n0
Big-Omega notation
Example:
Solution:
f(n)= (n2)
Solution:
f(n)= (n2)
for(i=1;i<=n;i++)
m=m+2;
O(n)
Guidelines for Asymptotic Analysis
Nested Loops
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
m=m+2;
O(n2)
Guidelines for Asymptotic Analysis
Consecutive statements
x=x+1;
for(i=1;i<=n;i++){
m=m+2;
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
k=k+2;}}
if (length()==0){
return false;}
else{
for(n=0;n<length();n++){
if(a==b){
return false;
}}
for(i=1;i<=n;i++)
{
i=i*2;
}
Object creation:
Classname <data type> objectname = new Classname<data type> ();
OR
Classname <data type> objectname = new Classname<>();
Example Program - Generic Classes
Generic Classes
● In the above program, we first passed an Integer type
parameter to the Generic class.
● Then, we passed a String type parameter to the same
Generic class.
● Hence, we reused the same class for two different data
types.
● Thus, Generics helps in code reusability with ease.
●
Generic Classes – with more than one parameter
class Gen <T1,T2> {
T1 name;
T2 value;
Gen(T1 o1,T2 o2) {
name = o1;
value = o2;
}
public T1 getName() {
return name;
}
public T2 getValue() {
return value;
}}
class Test {
public static void main (String[] args) {
Gen < String,Integer> obj = new Gen< String,Integer >("StudyTonight",1);
String s = obj.getName();
System.out.println(s);
Integer i = obj.getValue();
System.out.println(i);
}}
Points to be considered
● Generics Work Only with Objects
Gen<int> iob = new Gen<int>(07); //Error, can't use
primitive type
● Generics Types of different Type Arguments are
never same
iob = sob; //Absolutely Wrong
● An array of Generic type cannot be created
Syntax:
<type-parameter> returntype methodname (parameters) {...}
Example:
Static <T> void display (T t)
{
}
Generic Methods – Example
package javaapplication13;
public class GenMethod {
static <T> void display (T[] t)
{
for(T arr:t)
{
System.out.printf("%s\t", arr);
}
System.out.println();
}
public static void main(String[] args)
{
Integer[] iob={1,2,3,4,5};
Double[] dob={1.1,2.1,3.1,4.1,5.1};
Character[] cob={'a','b','c','d','e'};
System.out.println("integer");
display(iob);
System.out.println("double");
display(dob);
System.out.println("character");
display(cob);
}}
Generic Interfaces
Like classes and methods, you can also create generic interfaces.
Syntax:
Inside class: