Alogorithms
Alogorithms
Unit 2: Algorithms
• Definition and meaning of algorithm, properties of
algorithm, formulating problems; steps of an algorithm;
representing solution to problems by means of an
algorithm.
Definition and meaning of algorithm
• An algorithm is a precise, step-by-step
procedure or set of rules for solving a
specific problem or performing a particular
task.
• It is a well-defined sequence of
instructions that, when followed correctly,
leads to a solution to the problem or the
completion of a task.
• Computer Science
• Mathematics
• Operations Research
• Artificial Intelligence
• Data Science
What is the need for Algorithms?
• Algorithms are necessary for solving complex problems
efficiently and effectively.
• They help to automate processes and make them more
reliable, faster, and easier to perform.
• Algorithms also enable computers to perform tasks that
would be difficult or impossible for humans to do
manually.
• They are used in various fields such as mathematics,
computer science, engineering, finance, and many
others to optimize processes, analyze data, make
predictions, and provide solutions to problems.
Properties of algorithm
Properties of algorithm
•Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps should
be clear in all aspects and must lead to only one meaning.
Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It
may or may not take input.
Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it
should be well-defined as well. It should produce at least 1 output.
Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
Feasible: The algorithm must be simple, generic, and practical, such that it can be executed
with the available resources. It must not contain some future technology or anything.
Language Independent: The Algorithm designed must be language-independent, i.e. it
must be just plain instructions that can be implemented in any language, and yet the output
will be the same, as expected.
Properties of algorithm
•Input: An algorithm has zero or more inputs. Each that contains a fundamental operator
must accept zero or more inputs.
• Output: An algorithm produces at least one output. Every instruction that contains a
fundamental operator must accept zero or more inputs.
•Definiteness: All instructions in an algorithm must be unambiguous, precise, and easy
to interpret. By referring to any of the instructions in an algorithm one can clearly
understand what is to be done. Every fundamental operator in instruction must be
defined without any ambiguity.
•Finiteness: An algorithm must terminate after a finite number of steps in all test cases.
Every instruction which contains a fundamental operator must be terminated within a
finite amount of time. Infinite loops or recursive functions without base conditions do not
possess finiteness.
•Effectiveness: An algorithm must be developed by using very basic, simple, and
feasible operations so that one can trace it out by using just paper and pencil.
Property 1: Definiteness
•Algorithm: Bubble Sort
•Description: Bubble sort is a sorting algorithm that repeatedly steps through
the list, compares adjacent elements, and swaps them if they are in the wrong
order. This process is repeated until the list is sorted. Each step is well-defined,
involving comparisons and swaps as necessary.
Property 2: Effectiveness
•Algorithm: Binary Search
•Description: Binary search is used to find a specific item in a sorted list. It
repeatedly divides the list in half and compares the target item to the middle
element, eliminating half of the remaining elements at each step. This algorithm
is effective in finding items in large sorted datasets efficiently.
Property 3: Finiteness
•Algorithm: Factorial Calculation
•Description: To calculate the factorial of a non-negative integer n, you multiply all
positive integers from 1 to n. The algorithm terminates when it reaches n and computes
the final result.
Property 4: Input-Output
•Algorithm: Dijkstra's Shortest Path
•Description: Dijkstra's algorithm is used to find the shortest path between two nodes in a
weighted graph. It takes the graph as input, including the source and target nodes, and
produces the shortest path as output. The input is well-defined, and the output is the
desired solution to the shortest path problem.
Types of an Algorithm
1. Brute Force Algorithm:
It is the simplest approach to a problem. A brute force algorithm is the first approach that comes to finding when we see a problem.
2. Recursive Algorithm:
A recursive algorithm is based on recursion. In this case, a problem is broken into several sub-parts and called the same function again
and again.
3. Backtracking Algorithm:
The backtracking algorithm builds the solution by searching among all possible solutions. Using this algorithm, we keep on building
the solution following criteria. Whenever a solution fails we trace back to the failure point build on the next solution and continue this
process till we find the solution or all possible solutions are looked after.
4. Searching Algorithm:
Searching algorithms are the ones that are used for searching elements or groups of elements from a particular data structure. They
can be of different types based on their approach or the data structure in which the element should be found.
5. Sorting Algorithm:
Sorting is arranging a group of data in a particular manner according to the requirement. The algorithms which help in performing this
function are called sorting algorithms. Generally sorting algorithms are used to sort groups of data in an increasing or decreasing
manner.
Note: To learn more about the types of algorithms refer to the article about “
Types of Algorithms“.
• Advantages of Algorithms:
• It is easy to understand.
• An algorithm is a step-wise representation of a solution to a given
problem.
• In an Algorithm the problem is broken down into smaller pieces or
steps hence, it is easier for the programmer to convert it into an actual
program.
• Disadvantages of Algorithms:
• Writing an algorithm takes a long time so it is time-consuming.
• Understanding complex logic through algorithms can be very difficult.
• Branching and Looping statements are difficult to show in
Algorithms(imp).
Formulating problems
Here are the key steps and considerations when formulating problems:
1.Understand the Problem: Begin by thoroughly understanding the problem at hand. What is
the problem you're trying to solve or the task you need to accomplish? Gather all available
information and context related to the problem.
2.Identify the Objectives: Determine the specific objectives or goals of solving the problem.
What do you want to achieve as the outcome of the solution? Define success criteria and
measurable results.
3.Specify the Input: Clearly define the input data or information required to solve the problem.
Identify the variables, parameters, or data sources that the algorithm will use as inputs. Specify
the format and constraints of the input.
4.Define the Output: Describe what the desired output or solution should look like. This might
be a single value, a set of values, a decision, or a sequence of actions. Ensure that the output
aligns with the problem's objectives.
5. Boundary Conditions: Identify any constraints or boundary conditions that limit the problem's
scope or affect the solution. Consider factors such as time constraints, resource limitations, or legal
and ethical considerations.
6.Consider Special Cases: Think about any special cases or edge cases that the algorithm needs to
handle. These are scenarios that may require different approaches or considerations compared to the
general case.
7.Formulate Assumptions: Explicitly state any assumptions made during the problem formulation.
Assumptions can help simplify the problem, but they should be clearly documented and validated as
necessary.
8.Clarify Ambiguities: Eliminate any ambiguity or vagueness in the problem statement. Ensure that
all terms and requirements are well-defined, and there is no room for interpretation.
9.Break Down Complex Problems: If the problem is complex, consider breaking it down into
smaller, more manageable subproblems. Define how these subproblems relate to the overall problem
and how they will be solved.
10. Explore Problem-Solving Strategies: At this stage, you can start thinking about potential
problem-solving strategies and algorithms that might be applicable. Consider common algorithmic
paradigms, such as divide and conquer, dynamic programming, or greedy algorithms, depending on
the nature of the problem.
11.Document the Problem Statement: Write a formal problem statement that includes all the
information gathered in the previous steps. The problem statement should be concise, complete, and
clear, allowing others (and yourself) to understand the problem without ambiguity.
12.Validate and Refine: Review the formulated problem statement to ensure it accurately represents
the problem and aligns with your objectives. If necessary, refine the problem statement based on
feedback or further analysis.
How to Design an Algorithm?
• The problem that is to be solved by this algorithm: Add 3 numbers and print their sum.
• The constraints of the problem that must be considered while solving the problem:
The numbers must contain only digits and no other characters.
• The input to be taken to solve the problem: The three numbers to be added.
• The output to be expected when the problem is solved: The sum of the three numbers
taken as the input i.e. a single integer value.
• The solution to this problem, in the given constraints: The solution consists of adding
the 3 numbers. It can be done with the help of the ‘+’ operator, or bit-wise, or any other
method.
• Step 2: Designing the algorithm
• Now let’s design the algorithm with the help of the above pre-
requisites:
• START
• Declare 3 integer variables num1, num2, and num3.
• Take the three numbers, to be added, as inputs in variables num1, num2,
and num3 respectively.
• Declare an integer variable sum to store the resultant sum of the 3 numbers.
• Add the 3 numbers and store the result in the variable sum.
• Print the value of the variable sum
• END
Example: Write an algorithm to find the average of 3
subjects.