DAA_Unit-I-Student
DAA_Unit-I-Student
Algorithms and
Problem Solving
Outline
5
Algorithm
s■A tool for solving a well-specified
computational problem
6
Algorithms Cont.
■A well-defined computational procedure that
takes some value, or set of values, as input and
produces some value, or set of values, as output.
7
Correct and incorrect algorithms
■ Algorithm is correct if, for every input instance, it ends
with the correct output. We say that a correct algorithm
solves the given computational problem.
8
Evaluation of Algorithm
Evaluation of Algorithm
●Identify a problem
●Understand the problem
●Identify alternative ways to solve a
problem
●Select beat way to solve a problem
from the list of alternative solutions
●Evaluate the solution
Time Complexity
of Algorithms
Algorithm Analysis
27
Classification of Time Complexity
Notatio Complexit
Description Example
n y
O(1) Constant Simple statement Addition
O(log(n)) Logarithmic Divide in half Binary search
O(n) Linear loop Linear search
O(n*log(
Linearithmic Divide & Conquer Merge sort
n))
O(n2) Quadratic Double loop Check all pairs
Check all
O(n3) Cubic Triple loop
triples
Exhaustive Check all
O(2n) Exponential
search subsets
Recursive
O(n!) Factorial Factorial
function
28
Correctness of
Algorithm
Correctness of Algorithm
Correctness
of Algorithm
By
Using Loop
Mathematica
Invariant
l Induction
What does an algorithm ?
•An algorithm is described by:
•Input data
•Output data
• Preconditions: specifies restrictions on input data
• Postconditions: specifies what is the result
•Example: Binary Search
• Input data: a:array of integer; x:integer;
• Output data: found:boolean;
• Precondition: a is sorted in ascending order
• Postcondition: found is true if x is ina, and
found
is false otherwise
Correct algorithms
•An algorithm is correct if:
• for any correct input data:
• it stops and
• it produces correct output.
s:=0;
k:=0; We use techniques
based on loop
While (k<N)
invariants and
do k:=k+1; induction
s:=s+a[k];
end
Loop
invariants
MERGE-SORT(A,p,q)
MERGE-SORT(A,q+1,r)
MERGE(A,p,q,r)
Precondition:
Postcondition:
Correctness proofs for
Recursive Algorithm
n , n , … n are some values smaller than n but
1 2 r
•Inductive Hypothesis:
• Assume that RECURSIVE works correctly for
n=small_value, ..., k
•Inductive Step:
•Proving that an algorithm is totally
correct means:
1.Proving that it will terminate
2.Proving that the list of actions applied
to the
precondition imply the postcondition