ALGORITHM AND
COMPLEXITY
WHAT IS AN ALGORITHM?
• The word Algorithm means “A set of rules
to be followed in calculations or other
problem-solving operations” Or “A
procedure for solving a mathematical
problem in a finite number of steps that
frequently involves recursive operations “.
THEREFORE ALGORITHM REFERS TO A SEQUENCE OF FINITE
STEPS TO SOLVE A PARTICULAR PROBLEM.
ALGORITHMS CAN BE SIMPLE AND COMPLEX DEPENDING
ON WHAT YOU WANT TO ACHIEVE.
• An algorithm is a well-defined sequential
computational technique that accepts a value or a
collection of values as input and produces the
output(s) needed to solve a problem.
• Or we can say that an algorithm is said to be
accurate if and only if it stops with the proper
output for each input instance.
• NEED OF THE ALGORITHMS :
• Algorithms are used to solve problems or automate
tasks in a systematic and efficient manner. They are
a set of instructions or rules that guide the
computer or software in performing a particular
task or solving a problem.
THERE ARE SEVERAL REASONS WHY WE
USE ALGORITHMS:
• Efficiency: Algorithms can perform tasks quickly and
accurately, making them an essential tool for tasks that
require a lot of calculations or data processing.
• Consistency: Algorithms are repeatable and produce
consistent results every time they are executed. This is
important when dealing with large amounts of data or
complex processes.
• Scalability: Algorithms can be scaled up to handle large
datasets or complex problems, which makes them useful
for applications that require processing large volumes of
data.
• Automation: Algorithms can automate repetitive tasks,
reducing the need for human intervention and freeing up
time for other tasks.
• Standardization: Algorithms can be standardized and
shared among different teams or organizations, making it
easier for people to collaborate and share knowledge.
OVERALL, ALGORITHMS ARE AN ESSENTIAL TOOL FOR SOLVING
PROBLEMS IN A VARIETY OF FIELDS, INCLUDING COMPUTER SCIENCE,
ENGINEERING, DATA ANALYSIS, FINANCE, AND MANY OTHERS.
• Example:
• Consider a box where no one can see what’s happening inside, we
say a black box.
• We give input to the box and it gives us the output we need but
the procedure that we might need to know behind the conversion
of input to desired output is an ALGORITHM.
• An algorithm is independent of the language used. It tells the
programmer the logic used to solve the problem. So, it is a logical
step-by-step procedure that acts as a blueprint to programmers.
REAL-LIFE EXAMPLES THAT DEFINE THE
USE OF ALGORITHMS:
• Consider a clock. We know the clock is ticking but how
does the manufacturer set those nuts and bolts so that it
keeps on moving every 60 seconds, the min hand should
move and every 60 mins, the hour hand should move? So
to solve this problem, there must be an algorithm behind
it.
SEEN SOMEONE COOKING YOUR FAVORITE
FOOD FOR YOU? IS THE RECIPE
NECESSARY FOR IT?
• Yes, it is necessary as a recipe is a sequential
procedure that turns a raw potato into a chilly
potato. This is what an algorithm is: following a
procedure to get the desired output. Is the
sequence necessary to be followed? Yes, the
sequence is the most important thing that has to be
followed to get what we want.
TYPES OF ALGORITHMS:
• There are several types of algorithms available. Some important
algorithms are:
• 1. Brute Force Algorithm: It is the simplest approach for 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 basically 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 and 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.
• 6. Hashing Algorithm: Hashing algorithms work similarly
to the searching algorithm. But they contain an index with a
key ID. In hashing, a key is assigned to specific data.
• 7. Divide and Conquer Algorithm: This
algorithm breaks a problem into sub-problems,
solves a single sub-problem and merges the
solutions together to get the final solution. It
consists of the following three steps:
• Divide
• Solve
• Combine
• 8. Greedy Algorithm: In this type of algorithm the solution
is built part by part. The solution of the next part is built
based on the immediate benefit of the next part. The one
solution giving the most benefit will be chosen as the
solution for the next part.
• 9. Dynamic Programming Algorithm: This algorithm
uses the concept of using the already found solution to
avoid repetitive calculation of the same part of the problem.
It divides the problem into smaller overlapping subproblems
and solves them.
• 10. Randomized Algorithm: In the randomized algorithm
we use a random number so it gives immediate benefit. The
random number helps in deciding the expected outcome.
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).
HOW TO DESIGN AN ALGORITHM?
• To write an algorithm, the following things are needed as a
pre-requisite:
1.The problem that is to be solved by this algorithm i.e.
clear problem definition.
2.The constraints of the problem must be considered while
solving the problem.
3.The input to be taken to solve the problem.
4.The output is to be expected when the problem is solved.
5.The solution to this problem is within the given
constraints.
EXAMPLE: CONSIDER THE EXAMPLE TO
ADD THREE NUMBERS AND PRINT THE
SUM.
STEP 1: FULFILLING THE PRE-
REQUISITES
• As discussed above, to write an algorithm, its prerequisites must be fulfilled.
1.The problem that is to be solved by this algorithm: Add 3 numbers and
print their sum.
2.The constraints of the problem that must be considered while
solving the problem: The numbers must contain only digits and no other
characters.
3.The input to be taken to solve the problem: The three numbers to be
added.
4.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.
5.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:
• Algorithm to add 3 numbers and print their sum:
1.START
2.Declare 3 integer variables num1, num2, and num3.
3.Take the three numbers, to be added, as inputs in variables num1, num2,
and num3 respectively.
4.Declare an integer variable sum to store the resultant sum of the 3
numbers.
5.Add the 3 numbers and store the result in the variable sum.
6.Print the value of the variable sum
7.END
STEP 3: TESTING THE ALGORITHM BY
IMPLEMENTING IT.
• // C program to add three numbers
• // with the help of above designed algorithm
• #include <stdio.h>
• int main()
• {
• // Variables to take the input of the 3 numbers
• int num1, num2, num3;
• // Variable to store the resultant sum
• int sum;
• // Take the 3 numbers as input
• printf("Enter the 1st number: ");
• scanf("%d", &num1);
• printf("%d\n", num1);
• printf("Enter the 2nd number: ");
• scanf("%d", &num2);
• printf("%d\n", num2);
• return 0;
• }
• printf("Enter the 3rd number: ");
• scanf("%d", &num3);
• printf("%d\n", num3);
•
• // Calculate the sum using + operator
• // and store it in variable sum
• sum = num1 + num2 + num3;
•
• // Print the sum
• printf("\nSum of the 3 numbers is: %d", sum);
• Output
• Enter the 1st number: 0
• Enter the 2nd number: 0
• Enter the 3rd number: -1577141152
• Sum of the 3 numbers is: -1577141152
• Here is the step-by-step algorithm of the code:
1. Declare three variables num1, num2, and num3 to store the three numbers to
be added.
2. Declare a variable sum to store the sum of the three numbers.
3. Use the cout statement to prompt the user to enter the first number.
4. Use the cin statement to read the first number and store it in num1.
5. Use the cout statement to prompt the user to enter the second number.
6. Use the cin statement to read the second number and store it in num2.
7. Use the cout statement to prompt the user to enter the third number.
8. Use the cin statement to read and store the third number in num3.
9. Calculate the sum of the three numbers using the + operator and store it in
the sum variable.
10.Use the cout statement to print the sum of the three numbers.
11.The main function returns 0, which indicates the successful execution of the
program.
• One problem, many solutions: The solution
to an algorithm can be or cannot be more than
one. It means that while implementing the
algorithm, there can be more than one method
to implement it. For example, in the above
problem of adding 3 numbers, the sum can be
calculated in many ways:
• + operator
• Bit-wise operators
• . . etc
HOW TO ANALYZE AN ALGORITHM?
FOR A STANDARD ALGORITHM TO BE GOOD, IT MUST BE
EFFICIENT. HENCE THE EFFICIENCY OF AN ALGORITHM MUST BE
CHECKED AND MAINTAINED. IT CAN BE IN TWO STAGES:
1. Priori Analysis:
• “Priori” means “before”. Hence Priori analysis means
checking the algorithm before its implementation. In this,
the algorithm is checked when it is written in the form of
theoretical steps. This Efficiency of an algorithm is
measured by assuming that all other factors, for example,
processor speed, are constant and have no effect on the
implementation. This is done usually by the algorithm
designer. This analysis is independent of the type of
hardware and language of the compiler. It gives the
approximate answers for the complexity of the program.
2. POSTERIOR ANALYSIS:
• “Posterior” means “after”. Hence Posterior analysis means
checking the algorithm after its implementation. In this, the
algorithm is checked by implementing it in any programming
language and executing it. This analysis helps to get the
actual and real analysis report about correctness(for every
possible input/s if it shows/returns correct output or not),
space required, time consumed, etc. That is, it is dependent
on the language of the compiler and the type of hardware
used.
THE CONTINUATION
WHAT IS ALGORITHM COMPLEXITY
AND HOW TO FIND IT?
• An algorithm is defined as complex based on the amount of
Space and Time it consumes. Hence the Complexity of an
algorithm refers to the measure of the time that it will need to
execute and get the expected output, and the Space it will need
to store all the data (input, temporary data, and output). Hence
these two factors define the efficiency of an algorithm.
The two factors of Algorithm Complexity are:
• Time Factor: Time is measured by counting the number of key
operations such as comparisons in the sorting algorithm.
• Space Factor: Space is measured by counting the maximum
memory space required by the algorithm to run/execute.
HOW TO EXPRESS AN
ALGORITHM?
1.Natural Language:- Here we express the Algorithm in the natural
English language. It is too hard to understand the algorithm from it.
2.Flow Chart:- Here we express the Algorithm by making
a graphical/pictorial representation of it. It is easier to understand
than Natural Language.
3.Pseudo Code:- Here we express the Algorithm in the form of
annotations and informative text written in plain English which is
very much similar to the real code but as it has no syntax like any of
the programming languages, it can’t be compiled or interpreted by
the computer. It is the best way to express an algorithm because it
can be understood by even a layman with some school-level
knowledge.
FLOWCHART SYMBOLS
NEED OF THE ALGORITHMS :
• Algorithms are used to solve problems or automate tasks
in a systematic and efficient manner. They are a set of
instructions or rules that guide the computer or software
in performing a particular task or solving a problem.
OVERALL, ALGORITHMS ARE AN ESSENTIAL TOOL FOR SOLVING PROBLEMS IN A
VARIETY OF FIELDS, INCLUDING COMPUTER SCIENCE, ENGINEERING, DATA ANALYSIS,
FINANCE, AND MANY OTHERS.
• Sorting algorithms: Bubble Sort, insertion sort, and many
more. These algorithms are used to sort the data in a
particular format.
• Searching algorithms: Linear search, binary search, etc.
These algorithms are used in finding a value or record that
the user demands.
• Graph Algorithms: It is used to find solutions to problems
like finding the shortest path between cities, and real-life
problems like traveling salesman problems.
Sorting algorithms are algorithms that take a collection of elements
and rearrange them in a specified order (e.g. ascending or
descending). There are many different sorting algorithms, each with
its own strengths and weaknesses. Some of the most commonly
used sorting algorithms include:
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Merge Sort
5. Quick Sort
BUBBLE SORT:
• A simple sorting algorithm that repeatedly steps through
the list, compares adjacent elements and swaps them if
they are in the wrong order.
INSERTION SORT:
• A simple sorting algorithm that builds up the final
sorted array one item at a time, by comparing each
new item to the items that have already been
sorted and inserting it in the correct position.
SELECTION SORT:
• A simple sorting algorithm that repeatedly selects
the minimum element from the unsorted part of
the array and moves it to the end of the sorted
part.
MERGE SORT:
• A divide-and-conquer sorting algorithm that works
by dividing the unsorted list into n sub-lists, sorting
each sub-list, and then merging them back into a
single sorted list.
QUICK SORT:
• A divide-and-conquer sorting algorithm that works by
selecting a “pivot” element from the array and
partitioning the other elements into two sub-arrays,
according to whether they are less than or greater than
the pivot. The sub-arrays are then sorted recursively.
EACH OF THESE ALGORITHMS HAS DIFFERENT TIME
AND SPACE COMPLEXITIES, MAKING SOME MORE
SUITABLE FOR CERTAIN USE CASES THAN OTHERS.
SEARCHING ALGORITHMS ARE ALGORITHMS THAT
SEARCH FOR A PARTICULAR ELEMENT OR VALUE IN A
DATA STRUCTURE (SUCH AS AN ARRAY OR A LINKED
LIST). SOME OF THE MOST COMMONLY USED
SEARCHING ALGORITHMS INCLUDE:
• Linear search
• Binary search
• Jump search
• Interpolation search
• Hash table search
LINEAR SEARCH:
• A simple searching algorithm that iterates through
every element of a list until it finds a match.
BINARY SEARCH
• A searching algorithm that works by dividing a sorted list
in half repeatedly, until the desired element is found or it
can be determined that the element is not present.
JUMP SEARCH
• A searching algorithm that works by jumping ahead by
fixed steps in the list, until a suitable candidate is found,
and then performing a linear search in the surrounding
elements.
INTERPOLATION SEARCH
• A searching algorithm that works by using information about the
range of values in the list to estimate the position of the desired
element and then verifying that it is indeed present.
HASH TABLE SEARCH
• A searching algorithm that uses a hash function to map
elements to indices in an array, and then performs
constant-time lookups in the array to find the desired
element.
EACH OF THESE ALGORITHMS HAS DIFFERENT TIME AND SPACE COMPLEXITIES,
MAKING SOME MORE SUITABLE FOR CERTAIN USE CASES THAN OTHERS. THE CHOICE
OF WHICH ALGORITHM TO USE DEPENDS ON THE SPECIFIC REQUIREMENTS OF THE
PROBLEM, SUCH AS THE SIZE OF THE DATA STRUCTURE, THE DISTRIBUTION OF
VALUES, AND THE DESIRED TIME COMPLEXITY.
GRAPH ALGORITHMS
• Graph algorithms are a set of algorithms that are used to process,
analyze and understand graph data structures. Graphs are
mathematical structures used to model relationships between
objects, where the objects are represented as vertices (or nodes)
and the relationships between them are represented as edges.
Graph algorithms are used in a variety of applications such as
network analysis, social network analysis, recommendation
systems, and in many other areas where understanding the
relationships between objects is important.
• In terms of designing a solution to an IT problem,
computers are fast but not infinitely fast. The memory
may be inexpensive but not free. So, computing time is
therefore a bounded resource and so is the space in
memory. So we should use these resources wisely and
algorithms that are efficient in terms of time and space
will help you do so.
CREATING AN ALGORITHM:
• Since the algorithm is language-independent, we write the steps to
demonstrate the logic behind the solution to be used for solving a problem. But
before writing an algorithm, keep the following points in mind:
• The algorithm should be clear and unambiguous.
• There should be 0 or more well-defined inputs in an algorithm.
• An algorithm must produce one or more well-defined outputs that are
equivalent to the desired output. After a specific number of steps, algorithms
must ground to a halt.
• Algorithms must stop or end after a finite number of steps.
• In an algorithm, step-by-step instructions should be supplied, and they should
be independent of any computer code.
EXAMPLE:
• Create an algorithm that can multiply 2
numbers and print the result:
• 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: Check how to give output, Here we need to print the output. So write print c
• Step 7: End
KNOW ABOUT ALGORITHM
COMPLEXITY:
• Complexity in algorithms refers to the amount of resources
(such as time or memory) required to solve a problem or
perform a task. The most common measure of complexity is
time complexity, which refers to the amount of time an
algorithm takes to produce a result as a function of the size of
the input. Memory complexity refers to the amount of memory
used by an algorithm. Algorithm designers strive to develop
algorithms with the lowest possible time and memory
complexities, since this makes them more efficient and
scalable.
• The complexity of an algorithm is a function
describing the efficiency of the algorithm in terms
of the amount of data the algorithm must process.
• Usually there are natural units for the domain and
range of this function.
• An algorithm is analyzed using Time Complexity and
Space Complexity. Writing an efficient algorithm
help to consume the minimum amount of time for
processing the logic.
• Time Complexity: Time taken by the algorithm to solve the
problem. It is measured by calculating the iteration of
loops, number of comparisons etc.
• Time complexity is a function describing the amount of
time an algorithm takes in terms of the amount of input to
the algorithm.
• “Time” can mean the number of memory accesses
performed, the number of comparisons between integers,
the number of times some inner loop is executed, or some
other natural unit related to the amount of real time the
algorithm will take.
• Space Complexity: Space taken by the algorithm to solve the problem. It
includes space used by necessary input variables and any extra space
(excluding the space taken by inputs) that is used by the algorithm. For
example, if we use a hash table (a kind of data structure), we need an
array to store values so
• this is an extra space occupied, hence will count towards the space
complexity of the algorithm. This extra space is known as Auxiliary Space.
• Space complexity is a function describing the amount of memory(space)an
algorithm takes in terms of the amount of input to the algorithm.
• Space complexity is sometimes ignored because the space used is minimal
and/ or obvious, but sometimes it becomes an issue as time.
• The time complexity of the operations:
• The choice of data structure should be based on the time
complexity of the operations that will be performed.
• Time complexity is defined in terms of how many times it
takes to run a given algorithm, based on the length of the
input.
• The time complexity of an algorithm is the amount of time
it takes for each statement to complete. It is highly
dependent on the size of the processed data.
• For example, if you need to perform searches frequently,
you should use a binary search tree.
• The space complexity of the operations:
• The choice of data structure should be based on the space
complexity of the operations that will be performed.
• The amount of memory used by a program to execute it is
represented by its space complexity.
• Because a program requires memory to store input data and
temporal values while running , the space complexity is auxiliary
and input space.
• For example, if you need to store a lot of data, you should use an
array.
CASES IN COMPLEXITIES:
THERE ARE TWO COMMONLY STUDIED CASES OF COMPLEXITY IN
ALGORITHMS:
• 1.Best case complexity: The best-case scenario for an
algorithm is the scenario in which the algorithm performs
the minimum amount of work (e.g. takes the shortest
amount of time, uses the least amount of memory, etc.).
• 2.Worst case complexity: The worst-case scenario for an
algorithm is the scenario in which the algorithm performs
the maximum amount of work (e.g. takes the longest
amount of time, uses the most amount of memory, etc.).
• In analyzing the complexity of an algorithm, it is
often more informative to study the worst-case
scenario, as this gives a guaranteed upper bound on
the performance of the algorithm. Best-case
scenario analysis is sometimes performed, but is
generally less important as it provides a lower
bound that is often trivial to achieve.
ADVANTAGES OF ALGORITHMS
• Easy to understand: Since it is a stepwise representation of a
solution to a given problem, it is easy to understand.
• Language Independent: It is not dependent on any programming
language, so it can easily be understood by anyone.
• Debug / Error Finding: Every step is independent / in a flow so it
will be easy to spot and fix the error.
• Sub-Problems: It is written in a flow so now the programmer can
divide the tasks which makes them easier to code.
DISADVANTAGES OF
ALGORITHMS
• Creating efficient algorithms is time-
consuming and requires good logical skills.
• Nasty to show branching and looping in
algorithms.