Algorithm
Algorithm
2
Problem solving
• There may be different solutions of a specific problem….but..
3
Example…
• Preparing tea…We need to
follow a sequence of steps.
4
Conclusion
• One problem (task)can be solve in different ways.
(Solutions)
5
Input-Output
• To do any task/job or to solve any problem we need
to work on a set of input and after processing we get
output.
Example:
2+3=5, here 2 and 3 are input and 5 is output.
In making tea, water, sugar, tea-leaves are input
and after processing, tea is the output or final
product.
6
Algorithm
7
Algorithm
• In a very informal way, algorithm is a planning to solve a
particular problem.
8
Algorithm
Definition:-
9
Algorithm-- History
• Algorithms have a long history and the word can be traced
back to the 9th century. At this time the Persian scientist,
astronomer and mathematician Abdullah Muhammad bin
Musa al-Khwarizmi, often cited as “The father of Algebra”,
was indirect responsible for the creation of the term
“Algorithm”.
10
Algorithm-- Properties
• One algorithm is to solve a particular problem, but one
problem can be solved by many algorithms. Example: There
are several searching and sorting algorithms present in
computer science.
11
Algorithm-- Properties
• For some algorithm, there may have requirement of certain
prerequisites. Example: To search a data using binary search,
data must be in sorted order.
12
Algorithm-- types
• Different algorithms have different approaches to solve
specific problems.
13
Algorithm-- types
• Recursive algorithms.
• Dynamic programming algorithm
• Backtracking algorithm.
• Greedy algorithm
• Brute Force algorithm
• Heuristic algorithm
14
Algorithm-- types
• Recursive algorithms
obtains the result for the current input by applying simple operations to
the returned value for the smaller (or simpler) input.
15
Algorithm-- types
• Dynamic programming algorithm
This method was developed by Richard Bellman in the 1950s and has
found applications in numerous fields, from aerospace engineering to
economics.
16
Algorithm-- types
• Dynamic programming algorithm— Some applications
17
Algorithm-- types
• Backtracking algorithm
18
Algorithm-- types
• Backtracking algorithm– An example
consider the SudoKo solving Problem, we try filling digits one by one.
19
Algorithm-- types
• Divide and conquer algorithm
20
Algorithm-- types
• Divide and conquer algorithm– The process
21
Algorithm-- types
• Divide and conquer algorithm– An example
If the values match, return the index of the middle. Otherwise, if x is less
than the middle element, then the algorithm recurs for left side of middle
element, else recurs for the right side of the middle element.
22
Algorithm-- types
• Greedy algorithm
24
Algorithm-- types
• Brute Force algorithm
25
Algorithm-- types
• Heuristic algorithm
Sometimes these algorithms can be accurate, that is they actually find the
best solution.
But the algorithm is still called heuristic until this best solution is proven to
be the best.
The method used from a heuristic algorithm is one of the known methods,
such as greediness, but in order to be easy and fast the algorithm ignores
or even suppresses some of the problem's demands.
26
Algorithm-- types
• Heuristic algorithm
The term heuristic is used for algorithms which find solutions among all
possible ones ,but they do not guarantee that the best will be found.
These algorithms, usually find a solution close to the best one and they
find it fast and easily.
27
Algorithm– Measuring efficiency
• Efficiency of an algorithm can be estimated using Time
complexity and space complexity.
28
Algorithm– Measuring efficiency
29
Writing algorithm
• Remember, algorithm is not a program. So while writing
algorithm, DO NOT USE or TRY TO AVOID programming syntax.
30
Introduction to variable
• Let us introduce an important concept of algorithm(as well as
computer program), it is called variable.
31
Introduction to variable
• A variable san store only one value at a time. For multiple
initialization, Last value will be the value of the variable.
Example:
x=9
…..
x=2
So now, value of x is 2.
• Simultaneous initialization is not possible.
Example:
x=1,2
Not possible
32
Introduction to variable
• Consider following segment:
A=1
B=2
C=A+B
33
Introduction to variable
• Type of a variable:
A variable must be declared with its name along with its type.
Suppose,
we need to store values such as 1,2, 101 etc. , then type of
variable will be integer.
STEP-3: Input two integer value from user and store it in variable a and b
STEP-6: STOP
35
Algorithm – Explanation
STEP— A step is a logical unit of an algorithm. It can be one line or it can be
multiple lines.
All STEPS must have distinct STEP number. It indicates the finiteness of the
algorithms.
DECLERATION – All variables must be declared with its type before use. In this
example variable a, b, s has been declared in STEP-2.
36
Algorithm – Explanation
After variable declaration and before final result, algorithm does it’s logical steps
on input in one or more than STEPs.
Inside logical operation STEPS, any operator can be used and can be considered
as predefined.
At end, algorithm must produce it’s output; displaying summation in this case.
37
Example-2
STEP-4: calculate area of triangle using formula and store the result in variable area.
area=0.5 * base * height
STEP-6: STOP
38
Algorithm – Explanation
Here variables are declared as decimal as there can be fractional part in area,
base and height.
Here, we have used the formula: half x base x height to calculate area.
39
Example-3
STEP-5: Display F.
STEP-6: STOP
40
Algorithm – Explanation
F=(9*C+160)/5;
41
Algorithm – Decision making
An algorithm must have the capability of taking logical decisions.
IF <condition> is true
THEN follow this……
ELSE
THEN follow this……
If there are multiple condition testing, there can have multiple IF <condition>
42
Algorithm – Decision making
If there are multiple condition testing, there can have multiple IF <condition>
IF <condition> is true
THEN follow this……
ELSE IF <condition>
THEN follow this……
ELSE IF <condition>
THEN follow this……
ELSE
THEN follow this……
A condition can be either true or false.
43
Algorithm – Decision making
A condition can be either true or false.
If all IF condition is false, then last ELSE will be treated as true and code/calculation
under that ELSE will work.
44
Example-3
STEP-6: STOP
45
Algorithm – Explanation
Here variable temp has been declared to store temperature from user.
If the “IF” condition is true, it will execute “IF” statement, otherwise “FALSE” will
execute.
Since, there will be only two decisions(Freezing or not Freezing) so one IF-ELSE
block is sufficient.
46
Example-4
47
Example-4
Algorithm – Grade calculation
STEP-1: START
SETP-2: Declare variable total of type integer.
STEP-3: Initialize total from user.
STEP-4: IF total>599 THEN
Display Grade =A
ELSE IF total>449
Display Grade =B
ELSE IF total>299
Display Grade =C
ELSE
Display Grade =F
STEP-6: STOP
48
Example-5
49
Example-5
WHILE THEN statements are the building blocks of a looping making unit.
Unlike IF-ELSE, WHILE will check it’s condition again and again still the condition is
true.
51
Algorithm – Looping
If the condition in WHILE is true, it will execute codes under
WHILE and goes back to condition checking again.
52
Example-6
STEP-7: STOP
53
Algorithm – Explanation
54
Example-7
55
Example-7
STEP-7: STOP
56
Example-8
57
Example-8
58
Example-8
STEP-3: Input upper and lower from user as upper and lower limit
sum=0
STEP-7: STOP
59
Thank you
all
60