Slide 1.pptx-1
Slide 1.pptx-1
Slide 1.pptx-1
Algorithms
What is Algorithm?
► There are several strategies for any algorithm's design and evaluation,
including brute force algorithms, divide and conquer algorithms, dynamic
programming, and greedy algorithms. Each method has its very own strengths
and weaknesses, and the choice of approach depends on the nature of the
problem being solved.
► Efficient algorithm design and evaluation are vital for solving huge-scale
computational problems in areas which include facts technology, artificial
intelligence, and computational biology.
What is meant by Algorithm Analysis?
► Consider the linear search to compute the best time complexity as an example of
best-case analysis. Assume you have an array of integers and need to find a
number.
► Find the code for the above problem below:
1. int linear_search(int arr, int l, int target) {
2. int i;
3. for (i = 0; i < l; i++) {
4. if (arr[i] == target) {
5. return arr[i]
6. }
7. }
8. return 1
9. }
Continued…
► Assume the number you're looking for is present at the array's very first
index. In such instances, your method will find the number in O 1) time in
the best case. As a result, the best-case complexity for this algorithm is O
1, and the output is constant time. In practice, the best case is rarely
required for measuring the runtime of algorithms. The best-case scenario
is never used to design an algorithm.
► Worst-case evaluation: This sort of analysis determines the maximum
quantity of time or memory an algorithm requires to resolve a problem for
any enter length. It is normally expressed in phrases of big O notation.
Continued…
► Consider our last example, where we were executing the linear search.
Assume that this time the element we're looking for is at the very end of
the array. As a result, we'll have to go through the entire array before we
discover the element. As a result, the worst case for this method is ON.
Because we must go through at least NN elements before we discover our
destination. So, this is how we calculate the algorithms' worst case.
Continued…
► One of the most popular examples of the divide and conquer over
technique is the merge kind algorithm, that's used to sort an array of
numbers in ascending or descending order. The merge sort algorithm
works by means of dividing the array into two halves, sorting each half
one by one, and then merging the looked after halves to reap the very last
sorted array. The algorithm works as follows:
1. Divide: The array is split into halves recursively until each half has only
one detail.
2. Conquer: Each sub-array is sorted using the merge type algorithm
recursively.
3. Combine: The sorted sub-arrays are merged to attain the very last sorted
array.
Continued…
► Another example of the divide and conquer method is the binary search
algorithm, that is used to find the position of a target value in a sorted
array. The binary search algorithm works by again and again dividing the
array into two halves till the target value is found or determined to be not
gift inside the array. The algorithm works as follows:
1. Divide: The array is split into two halves.
2. Conquer: The algorithm determines which half of the array the target
position is in or determines that the target position is not there in the array.
3. Combine: The very last position of the target position within the array is
determined.
Searching and traversal techniques
► However, the greedy method does not always lead to the optimal solution,
and in some cases, it may not even find a feasible solution. Therefore, it is
important to verify the correctness of the solution obtained by the greedy
method.
► To analyze the performance of a greedy algorithm, one can use the
greedy-choice property, which states that at each step, the locally optimal
choice must be a part of the globally optimal solution. Additionally, the
optimal substructure property is used to show that the optimal solution to
a problem can be obtained by combining the optimal solutions to its
subproblems.
Continued…
• Divide and Conquer: Breaks the problem into independent subproblems and
combines their solutions.
• Greedy Algorithm: Makes the best local choice at each step without
reconsidering previous choices.
• Dynamic Programming: Solves subproblems recursively and stores their
solutions to avoid redundancy.
?