Daa Unit 1algorithms Problem Solving
Daa Unit 1algorithms Problem Solving
on machine.
output.
Problem
Problem
Algorithm: A sequence
of instructions describing
how to do a task (or process)
C++ Program
Prepared By Mr. Vipin K. Wani
Computational Problem
5
➢ A computational problem specifies an input-output relationship
➢ a computational problem is a problem that may be solved by an algorithm.
There can be many algorithms for a particular problem. So, how to classify an
algorithm to be good and others to be bad?
Correctness: An algorithm is said to be correct if for every set of input it halts
with the correct output. If you are not getting the correct output for any
particular set of input,then your algorithm is wrong.
Finiteness: The algorithm must always terminate after a finite number of steps.
Efficiency: An efficient algorithm is always used. By the term efficiency, we
mean to say that:
i. The computational time should be as less as possible.
ii. The memory used by the algorithm should also be as less as possible.
Unambiguity: Instructions written in a algorithms must be unambiguous
Prepared By Mr. Vipin K. Wani
Design & Analysis of Algorithm
9
Algorithm deals with two steps designing & analyzing the algorithms.
The Analysis:
➢ The Analysis deals with performance evaluation of an algorithm i.e. complexity
analysis.
➢ Complexity is not dependent on machine or programing language.
10
Step 1: Start
Step 2: Get the knowledge of input.
Here we need 3 variables; a and b will be the user input and c will hold the
result.
Step 3: Declare a, b, c variables.
Step 4: Take input for a and b variable from the user.
Step 5: Know the problem and find the solution using operators, data
structures and logic We need to multiply a and b variables so we use * operator
and assign the result to c.
That is c <- a * b
Step 6: Here we need to print the output. So write print c
Step 7: End
1. Implementation Method
2. Design Method
3. Design Approaches
4. Other Classifications
Classification by complexity:
Etc…
Computer Computer
A B
Faster Slower
Can Execute 10000 Instructions /Sec Can Execute 100 Instructions /Sec
100 times Faster than B 100 times Slower than A
i.e. 104 /sec i.e. 102 /sec
Consider we are applying Insertion Sort algorithm on Computer A with time complexity C1 n2
and
we are applying Marge Sort algorithm on Computer B with time complexity C2nlogn
T(A)= C1 n* n
T(B)= C2 n log n
➢ Ada Lovelace, an English mathematician and daughter of the poet Lord Byron, wrote the first
algorithm for a machine in the 1800s and is considered the first computer programmer.
➢ An algorithm Is called totally correct for the given specification and only if for any
correct input data:
➢ Stops and
➢ Returns correct output
➢ The only way to prove the correctness of an algorithm over all possible inputs is by
reasoning formally or mathematically about it.
➢ Proving correctness of algorithm is crucial. For many problems, algorithms are very
complex.
➢ Reliability and efficiency of an algorithm cannot be claimed unless and until it gives
the correct output for each of the valid inputs.
➢ The correctness of an algorithm can be quickly proved by checking certain
conditions.
For any algorithm, we must prove that it always returns the desired output for all legal
instances of the problem.
For sorting, this means even if the input is already sorted or it contains repeated
elements.
Prepared By Mr. Vipin K. Wani
Correctness of Algorithm
17
Ex 4n +n is always multiple of 8
consider n=2
So 4(2)+2 =10 is not multiple of 8
Step 2(Hypothesis ): Assume true for arbitrary value k i.e for n=k k(k+1)/2 is
true
So for n=k+1
n(k+1) =1+ 2 +3+…….+ K+ K+1 = (k+1)((k+1)+1)/2
= (k+1)(k+2)/2
Consider LHS
N(k+1) =1+ 2 +3+…….+ K+ K+1
= k(k+1)/2 + (k+1) …….from Step 2
= k(k+1)/2 + 2(k+1) /2
=(k+1)(k+2)/2
=RHS
➢ Use of Loops
• Writing initialization
• Proper condition that will terminate after some iteration
➢ Efficiency of Algorithm
➢ Efficiency of algorithm
• Use of resources
• CPU time and internal memory
• There are four way to improve efficiency
1. Remove Redundant computation Outside the loop
2. Referencing Array Elements
3. Inefficiency due to late termination
4. Early detection of desired output condition
➢ Efficiency of algorithm
1. Remove Redundant computation Outside the loop
• Version 1:
Int y, x=0;
For i=1 to n do
Begin
int a, b;
x=x+0.02
y=(a*a*a+c)*x*x+b*b*x;
Print (X & Y)
end
➢ Efficiency of algorithm
1. Remove Redundant computation Outside the loop
• Version 2:
Int y, x=0, a, b;
For i=1 to n do
Begin
x=x+0.02
//y=(a*a*a+c)*x*x+b*b*x;
y=(a3+c)*x2+xb2
Print (X & Y)
end
➢ Efficiency of algorithm
2. Referencing Array Elements
Version 1: Version 2:
➢ Efficiency of algorithm
3. Inefficiency due to late termination
Version 1:
Version 2:
While name roll_no > current_rollno & ! EOF do get names from list.
if current_rollno == roll_no
➢ Efficiency of algorithm
4. Early detection of desired output condition
• Example of bubble sort
Version 1:
Int i=n;
For i=1 to n-1 do
For j=1 to 1 do
Begin
if currkey>nextkey
exchange the item
endif
end
➢ Efficiency of algorithm
4. Early detection of desired output condition
• Example of bubble sort
Version 2:
Int i=n;
For i=1 to n-1 do
For j=1 to n-1 do
Begin
if currkey>nextkey
exchange the item
endif
if no exchange done
retuen
else
i=i+1;
end
b. For solving some problem, series of actions are taken to reach to the solution.
procedure(Series of actions)