You are on page 1of 60

Algorithms

Dr. Rupam Bhattacharya


“…When ever there is a problem,
human being tries to find it’s
solution…”

2
Problem solving
• There may be different solutions of a specific problem….but..

• To solve a particular problem, we need to follow certain


steps in sequence.

• The problem may be so simple or may be very complex

3
Example…
• Preparing tea…We need to
follow a sequence of steps.

• Switch on the computer

4
Conclusion
• One problem (task)can be solve in different ways.
(Solutions)

• Each solution must have a sequence of finite number


of steps.

• For each problem-solution set, there will be set of


input and a set of output.

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

• In engineering, particularly in computer science, a


step by step solution of a problem is popularly known
as algorithm
• Definition: An algorithm is a finite number of steps
to solve a particular problem.

7
Algorithm
• In a very informal way, algorithm is a planning to solve a
particular problem.

• Suppose, you are coming to college from your home. There


must have a plan. So, in this case, that plan is your algorithm
to come college from your home.

8
Algorithm
Definition:-

Algorithm is a finite steps of instructions to


solve a specific problem.

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”. 

• The very first of the many recognized algorithms have been


attributed to the Babylonians around 1600 BC.
These algorithms used for factorization and finding square
roots.

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.

• Algorithms must have a finite number of steps. An algorithm


can not take infinite number of time/steps to solve a problem.

• Every algorithm must a have a specific set of input, on which


it works and produce the final result/outcome.

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.

• For single problem, if there exists multiple algorithms, then


input-output of the algorithm is same but internal process on
input to produce output is different. Example: There exists
different sorting algorithms. For all sorting algorithms, input
is same, that is an unsorted data set and output is sorted
data set. But internal process of converting unsorted to
sorted data is different for different algorithm.

12
Algorithm-- types
• Different algorithms have different approaches to solve
specific problems.

• On the basis of their approaches, algorithms can be


categorized into different categories.

13
Algorithm-- types
• Recursive algorithms.

• Dynamic programming algorithm

• Backtracking algorithm.

• Divide and conquer algorithm.

• Greedy algorithm

• Brute Force algorithm

• Heuristic algorithm
14
Algorithm-- types
• Recursive algorithms

 A recursive algorithm is an algorithm which calls itself with "smaller (or


simpler)" input values.

 obtains the result for the current input by applying simple operations to
the returned value for the smaller (or simpler) input.

Example: Factorial and Fibonacci number series are


the examples of recursive algorithms.

15
Algorithm-- types
• Dynamic programming algorithm

 It is both a mathematical optimization method and a computer


programming method.

 This method was developed by Richard Bellman in the 1950s and has
found applications in numerous fields, from aerospace engineering to
economics.

 In both contexts it refers to simplifying a complicated problem by breaking


it down into simpler sub-problems in a recursive manner.

16
Algorithm-- types
• Dynamic programming algorithm— Some applications

• 0/1 knapsack problem.


• Mathematical optimization problem.
• All pair Shortest path problem.
• Reliability design problem.
• Longest common subsequence (LCS)
• Flight control and robotics control.
• Time sharing: It schedules the job to maximize CPU usage.

17
Algorithm-- types
• Backtracking algorithm

 Backtracking is an algorithmic-technique for solving problems


recursively by trying to build a solution incrementally, one
piece at a time.

 removing those solutions that fail to satisfy the constraints of


the problem at any point of time

18
Algorithm-- types
• Backtracking algorithm– An example

 consider the SudoKo solving Problem, we try filling digits one by one.

 Whenever we find that current digit cannot lead to a solution, we remove


it (backtrack) and try next digit.

 This is better than naive approach that is generating all possible


combinations of digits and then trying every combination one by one as it
drops a set of permutations whenever it backtracks.

19
Algorithm-- types
• Divide and conquer algorithm

 In Computer science, divide and conquer is an algorithm design paradigm


based on multi-branched recursion.

 A divide-and-conquer algorithm works by recursively breaking down a


problem into two or more sub-problems of the same or related type, until
these become simple enough to be solved directly.

 The solutions to the sub-problems are then combined to give a solution to


the original problem.

20
Algorithm-- types
• Divide and conquer algorithm– The process

This technique can be divided into the following three


parts:

• Divide: This involves dividing the problem into some sub problem.

• Conquer: Sub problem by calling recursively until sub problem solved.

• Combine: The Sub problem Solved so that we will get find problem


solution.

21
Algorithm-- types
• Divide and conquer algorithm– An example

 Binary Search is a searching algorithm. In each step, the algorithm


compares the input element x with the value of the middle element in
array.

 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

 A greedy algorithm is a simple, intuitive algorithm that is used in


optimization problems.

 The algorithm makes the optimal choice at each step as it attempts to find


the overall optimal way to solve the entire problem.

 However, in many problems, a greedy strategy does not produce an


optimal solution.

 Such algorithms are called greedy because while the optimal solution to


each smaller instance will provide an instant output, the algorithm doesn't
consider the larger problem as a whole. 23
Algorithm-- types
• Greedy algorithm

 A greedy algorithm is a simple, intuitive algorithm that is used in


optimization problems.

 The algorithm makes the optimal choice at each step as it attempts to find


the overall optimal way to solve the entire problem.

 However, in many problems, a greedy strategy does not produce an


optimal solution.

24
Algorithm-- types
• Brute Force algorithm

 Brute Force Algorithms refers to a programming style that does not


include any shortcuts to improve performance, but instead relies on sheer
computing power to try all possibilities until the solution to a problem is
found.

 A classic example is the traveling salesman problem (TSP).


 

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.

 Therefore they may be considered as approximately and not accurate


algorithms.

 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.

• Time complexity: It is basically the amount of time an


algorithm taken to solve the problem or producing the output
by processing input.

There can be Best case complexity, Average case


complexity and Worst case complexity, depending upon the
input set.

28
Algorithm– Measuring efficiency

• Space complexity: It is basically the amount of space required


by an algorithm to solve the problem or producing the output
by processing input.

There may be different space complexity for different


algorithms aiming to solve same problem.

29
Writing algorithm
• Remember, algorithm is not a program. So while writing
algorithm, DO NOT USE or TRY TO AVOID programming syntax.

• Use structured English to write algorithm.

• Clearly mention the Input/output of the algorithm.

• Use step number to indicate how many steps the algorithm


need to produce the result.

30
Introduction to variable
• Let us introduce an important concept of algorithm(as well as
computer program), it is called variable.

• In algorithm, we need to store several values such as input,


some intermediate results and output. We can store values in
variables.
• Variables are something, which can store values, one at a
time.
• Say, x=9. Here, x is the variable whose value is currently 9 or
we can say that 9 is stored in variable ‘x’.

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

Here, value of A and B variable is 1 and 2


respectively.

At last line, value of A and B are getting added and stored


on another variable C.

So, value of C is 3. We can say that C storing the sum of


A and B

33
Introduction to variable
• Type of a variable:

 All variable must be declared before use.

 A variable must be declared with its name along with its type.

 Type of a variable means, type of value we need to store on that


variable.

Suppose,
we need to store values such as 1,2, 101 etc. , then type of
variable will be integer.

I we need to store 2.5, or 10.20029 etc. we need variables


of type decimal or float or double 34
Example-1

Algorithm - Addition of two numbers and display


STEP-1: START

SETP-2: Declare variable a, b, s of type integer

STEP-3: Input two integer value from user and store it in variable a and b

STEP-4: Do addition of a and b and store the summation in variable s


s=a+b

STEP-5: Display value of variable s

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.

 START/STOP – Common convention to start and terminate an algorithm. In place


of START-STOP, we can use BEGIN-END combination.

 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

Algorithm – Calculate area of a triangle


STEP-1: START

SETP-2: Declare variable area, base, height of type decimal.

STEP-3: Initialize base and height from user.

STEP-4: calculate area of triangle using formula and store the result in variable area.
area=0.5 * base * height

STEP-5: Display value of area.

STEP-6: STOP

38
Algorithm – Explanation

 Here variables are declared as decimal as there can be fractional part in area,
base and height.

remember, In a decimal variable we can keep integer value. But opposite is


not true

 Variable name need not to be always one alphabet. It can be anything.

 Here, we have used the formula: half x base x height to calculate area.

 In computer ‘*’ has been used as multiplication operator.

39
Example-3

Algorithm – Celsius to Fahrenheit conversion


STEP-1: START

SETP-2: Declare variable C, F as decimal.

STEP-3: Initialize C from user.

STEP-4: calculate Fahrenheit using standard formula.


F=(9*C+160)/5

STEP-5: Display F.

STEP-6: STOP

40
Algorithm – Explanation

 Here algorithm taking temperature in Celsius as input and convert in to


Fahrenheit as output

 We know the relationship between Celsius and Fahrenheit as follows:

C/5=(F-32)/9; C=> Celsius and F=> Fahrenheit

 In algorithm we convert the above equation in terms of F as follows:

F=(9*C+160)/5;

41
Algorithm – Decision making
 An algorithm must have the capability of taking logical decisions.

 This is called branching.

 IF-ELSE statements are the building blocks of a decision making unit.

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 the condition is true then statements/calculation under that IF will work.

 If the condition of a IF is false, then it will move to next IF condition checking.

 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

Algorithm – Temperature is freezing or not


STEP-1: START

SETP-2: Declare variable temp of type decimal.

STEP-3: Initialize temp from user.

STEP-4: IF temp>0 THEN


Display Temperature is not freezing
ELSE
Display Temperature is freezing

STEP-6: STOP

45
Algorithm – Explanation

 Here variable temp has been declared to store temperature from user.

 Temperature can have fractional part, so declared as decimal

 STEP-4 is the logical decision making unit.

 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

Algorithm – Grade calculation


Problem statement: In an examination, if a student
score above 599 then his/her grade will be A. If marks
is 599 or less, but more than 449 then grade will be B.
If marks is 449 or less, but more than 299 grade will be
C. Otherwise grade will be F

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

Algorithm – Electric bill calculation


Problem statement: If bill amount is up to Rs. 1000/-,
no tax need to pay. Just pay the bill amount. If amount
is more than 1000/- but less than 1501/- then 10% tax
will be added with bill amount. If bill amount is more
than 1500/- but less than 2001, 15% will be added
with bill. For bill amount more than 2000/-, 17%tax
will be added with bill amount.

Calculate final bill amount.

49
Example-5

Algorithm – Electric bill calculation


STEP-1: START
SETP-2: Declare variable bill, final_bill of type decimal.
STEP-3: Initialize bill from user.

STEP-4: IF bill<=1000 THEN


final_bill=bill
ELSE IF bill<=1500
final_bill=bill+bill*.10
ELSE IF bill<=2000
final_bill=bill+bill*.15
ELSE
final_bill=bill+bill*.17

STEP-6: Display final_bill


STEP-7: STOP
50
Algorithm – Looping
 An algorithm must have the capability of repeating something over and over.
---For example, calculation of factorial

 This is called looping.

 WHILE THEN statements are the building blocks of a looping making unit.

WHILE <condition> is true


THEN follow this……
back to condition checking
END WHILE

 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.

 This process will goes on until the condition is true.

 We have to ensure that condition must be false somewhere,


otherwise the loop will be infinite.

 All real-life problem must have a end o repetition.

52
Example-6

Algorithm – Calculate Factorial of a number


STEP-1: START

SETP-2: Declare variable n, fact of type integer.

STEP-3: Initialize n from user.


fact=1

STEP-4: WHILE n>0


fact=fact*n
n=n-1
END WHILE

STEP-6: Display fact

STEP-7: STOP
53
Algorithm – Explanation

 fact is initialize by 1. Because, it will be multiplied with the


number.

 In factorial, multiplication starts with that number and ends


when reach to 1. So the condition written in WHILE.

 Value of n reduced by 1 at each looping-step to implement


the logic. This also establish the termination condition .

54
Example-7

Algorithm – Fibonacci series


Problem statement: It is a series of numbers, starts
with 0 and 1. Then next digits onwards, it will be
summation of previous two digits.

Fib. Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, ……

Loop is essential to generate this series as repeated


addition is required.

55
Example-7

Algorithm – Fibonacci series up to a given term


STEP-1: START

SETP-2: Declare variable n,p1,p2,tmp type integer.

STEP-3: Input n from the user


Initialize p1=0, p2=1,tmp=p1+p2.

STEP-4: Display p1, p2


STEP-5: WHILE tmp<n
display tmp
p1=p2
p2=tmp
tmp=p1+p2
END WHILE

STEP-7: STOP
56
Example-8

Algorithm – Addition of odd numbers within a range

Problem statement: User will provide a range by


giving upper and lower limits. Algorithm will check for
odd numbers(not divisible by 2) within the range and
add them.
Example: lower limit: 10
Upper limit: 15
Result: 11+13+15=39(final answer)

57
Example-8

Algorithm – Addition of odd numbers within a range

The % operator : % (modulus operator) operator gives


the reminder of a division.

Example: 10%2 is equal to 0


10%3 is equal to 1
15%4 is equal to 3

58
Example-8

Algorithm – Addition of odd numbers within a range


STEP-1: START

SETP-2: Declare variable sum, upper, lower of type integer.

STEP-3: Input upper and lower from user as upper and lower limit
sum=0

STEP-5: WHILE lower<=upper


IF lower%2!=0 THEN
sum=sum + lower
END IF
lower=lower+1
END WHILE
STEP-6: Display sum

STEP-7: STOP
59
Thank you
all
60

You might also like