7. 7
T h e R o l e o f A l g o r i t h m i n C o m p u t i n g
An algorithm is thus a sequence of computational steps
that transform the input into the output.
An Instance of a problem consists of the input needed to
compute a solution to the problem.
8. 8
W h a t k i n d s o f p r o b l e m s a r e s o l v e d b y
a l g o r i t h m s ?
The Human Genome Project
Identifying 100,000 genes in human DNA
determining the sequences of the 3 billion chemical
base pairs that make up human DNA
storing this information in databases
9. 9
W h a t k i n d s o f p r o b l e m s a r e s o l v e d b y
a l g o r i t h m s ?
E-Commerce
public-key cryptography and digital signatures
recommendation system
logistics
10. 10
W h a t k i n d s o f p r o b l e m s a r e s o l v e d b y
a l g o r i t h m s ?
11. 11
W h a t k i n d s o f p r o b l e m s a r e s o l v e d b y
a l g o r i t h m s ?
If you can show that the problem is NP-complete, you can instead
spend your time developing an efficient algorithm that gives a good, but not
the best possible, solution.
25. 25
Finding factorial of integer n
int factorial(int n)
{
int i, fact = 1;
for (i = 2; i <= n; i++)
{
fact *= i;
}
return fact;
}
}
26. 26
Finding factorial of integer n
int factorial(int n)
{
int i, fact = 1;
for (i = 2; i <= n; i++)
{
fact *= i;
}
return fact;
}
}
int factorial(int n)
{
int i, fact = 1;
if (n<=1) return 1;
else return n*factorial(n-1);
}
27. 27
Fibonacci Series: Algorithm I
int fun(int x)
{
int f;
if (x==1) return(0);
else
if (x==2) return(1);
else
f= fun(x-1)+fun(x-2);
return(f);
}
28. 28
Fibonacci Series: Algorithm II
int fun(int x)
{
int A[x+1];
A[0]=0;
A[1]=1;
for (i=0; i<=x, i++)
{
A[i] = A[i-1]+A[i-2];
}
return A[i];
}
34. 34
Evaluating algorithms
Two questions we ask about an algorithm
1. Is it correct?
2. Is it efficient?
Correctness is of utmost importance.
highly efficient but incorrect algorithm ???
Efficiency with respect to:
1. Running time
2. Space (amount of memory used)
3. Network traffic
4. Other features (e.g. number of times secondary storage is
accessed)
35. 35
Loop-invariant approach to correctness proof
Initialization: true before the first execution of the loop.
Maintenance: If true before an iteration of the loop, it should be
true also after the iteration.
Termination: Prove that when the loop terminates, the invariant,
and the reason that the loop terminates, imply the correctness
of the loop-construct.
Temporarily be false during the body of the loop
36. 36
int j = 9;
for(int i=0; i<10; i++)
j--;
Loop invariant: i + j == 9.
A weaker invariant: i >= 0 && i <= 10.
Characteristics of Algorithm
INPUT, OUTPUT, DEFINITE, FINITE, CORRECT
DEPENDS ON INPUT DATA, ACCURACY, ENVIRONMENT, PROBLEM DOMAIN
DEPENDS ON INPUT DATA, ACCURACY, ENVIRONMENT, PROBLEM DOMAIN
DEPENDS ON INPUT DATA, ACCURACY, ENVIRONMENT, PROBLEM DOMAIN
DEPENDS ON INPUT DATA, ACCURACY, ENVIRONMENT, PROBLEM DOMAIN
DEPENDS ON INPUT DATA, ACCURACY, ENVIRONMENT, PROBLEM DOMAIN
DEPENDS ON INPUT DATA, ACCURACY, ENVIRONMENT, PROBLEM DOMAIN
polynomial time
Definition: When the execution time of a computation, m(n), is no more than a polynomial function of the problem size, n. More formally m(n) = O(nk) where k is a constant.
Note: Broadly speaking, polynomial time algorithms are reasonable to compute. The time to run exponential algorithms grows too fast to expect to be able to compute exact solutions in all cases.
polynomial time
Definition: When the execution time of a computation, m(n), is no more than a polynomial function of the problem size, n. More formally m(n) = O(nk) where k is a constant.
Note: Broadly speaking, polynomial time algorithms are reasonable to compute. The time to run exponential algorithms grows too fast to expect to be able to compute exact solutions in all cases.
tractable, i.e. there exists an algorithm that can solve it in a reasonable amount of time on a computer.
intractable, meaning that for large values it would take far too long for even the best supercomputer to solve it - in some cases, this means millions or even billions of years.
tractable, i.e. there exists an algorithm that can solve it in a reasonable amount of time on a computer.
intractable, meaning that for large values it would take far too long for even the best supercomputer to solve it - in some cases, this means millions or even billions of years.
DEPENDS ON INPUT DATA, ACCURACY, ENVIRONMENT, PROBLEM DOMAIN
1. NO OF LINES OF CODE
2. EASY TO UND/IMP
3. TIME TO EXECUTE
4. SPACE TO EXECUTE
5. NO OF STATEMENTS