Introduction To Algorithm
Introduction To Algorithm
Introduction To Algorithm
Topperworld.in
Introduction to algorithm
❖ What is Algorithm ?
• An algorithm is a step-by-step procedure or set of rules designed to
perform a specific task or solve a particular problem.
• It is a finite sequence of well-defined, unambiguous instructions that,
when followed, lead to a solution for a given problem or the
achievement of a particular goal.
• In essence, an algorithm is a systematic way of performing a
computation.
Well-defined:
©Topperworld
Design and Analysis of Algorithms
Finite:
• An algorithm should produce the correct output for all valid inputs.
• It should solve the specified problem accurately.
Optimality:
©Topperworld
Design and Analysis of Algorithms
©Topperworld
Types of Algorithm Analysis:
Best case
Worst case
Average case
Based on the above three notations of Time Complexity there are three cases
to analyze an algorithm:
©Topperworld
Design and Analysis of Algorithms
• For Linear Search, the worst case happens when the element to be
searched (x) is not present in the array.
• When x is not present, the search() function compares it with all the
elements of arr[] one by one.
• Therefore, the worst-case time complexity of the linear search would
be O(n).
Best Case Analysis (Very Rarely used)
• In the best-case analysis, we calculate the lower bound on the running
time of an algorithm.
• We must know the case that causes a minimum number of operations
to be executed.
• In the linear search problem, the best case occurs when x is present at
the first location.
• The number of operations in the best case is constant (not dependent
on n). So time complexity in the best case would be.
Average Case Analysis (Rarely used)
• In average case analysis, we take all possible inputs and calculate the
computing time for all of the inputs.
• Sum all the calculated values and divide the sum by the total number
of inputs.
• We must know (or predict) the distribution of cases.
• For the linear search problem, let us assume that all cases are uniformly
distributed (including the case of x not being present in the array).
• So we sum all the cases and divide the sum by (n+1).
Following is the value of average-case time complexity.
©Topperworld
Design and Analysis of Algorithms
Need of Algorithm
✓ To understand the basic idea of the problem.
✓ To find an approach to solve the problem.
✓ To improve the efficiency of existing techniques.
✓ To understand the basic principles of designing the algorithms.
✓ To compare the performance of the algorithm with respect to other
techniques.
✓ It is the best method of description without describing the
implementation detail.
✓ The Algorithm gives a clear description of requirements and goal of the
problem to the designer.
✓ A good design can produce a good solution.
✓ To understand the flow of the problem.
✓ 12. With the help of algorithm, we convert art into a science.
✓ 13. To understand the principle of designing.
• 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).
©Topperworld
Design and Analysis of Algorithms
Properties of Algorithm:
• It should terminate after a finite time.
©Topperworld
• It should produce at least one output.
• It should take zero or more input.
• It should be deterministic means giving the same output for the same
input case.
• Every step in the algorithm must be effective i.e. every step should do
some work.
Application of Algorithm:
Here are some key applications of algorithms in DAA:
➢ Sorting and Searching:
• Algorithms are extensively used for sorting and searching data.
• Sorting algorithms like quicksort, mergesort, and heapsort are applied
to arrange data in a specific order.
• Searching algorithms like binary search efficiently locate items in a
sorted collection.
➢ Graph Algorithms:
• Algorithms are fundamental in solving problems related to graphs.
• Graph algorithms, such as Dijkstra's algorithm for shortest paths,
Kruskal's algorithm for minimum spanning trees, and depth-first
search, are widely used in network optimization, routing, and
connectivity analysis.
➢ Dynamic Programming:
• Dynamic programming is a technique that involves breaking down a
problem into smaller subproblems and solving each subproblem only
once, storing the solutions to subproblems in a table to avoid
redundant computations.
©Topperworld
Design and Analysis of Algorithms
➢ Greedy Algorithms:
• Greedy algorithms make locally optimal choices at each stage with the
hope of finding a global optimum.
• These algorithms are used in problems like Huffman coding for data
compression, scheduling, and certain optimization problems.
©Topperworld