


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of divide and conquer algorithms, focusing on dynamic programming and greedy approach. It covers the concepts of optimal substructure and overlapping sub-problems, as well as various applications and examples of dynamic programming. The document also discusses the advantages and disadvantages of both dynamic programming and greedy algorithms.
Typology: Study Guides, Projects, Research
1 / 26
This page cannot be seen from the preview
Don't miss anything!



















Definition: Types of algorithms:
1. Greedy algorithm: In this algorithm, best option got selected without considering the future. This means that some of the best locals are selected and considered to be the best in the world. There are two features of this algorithm : Greedily choose the best option Excellent underground structure: If the right solution can be found by finding a solution to its minor problems. Cons of greedy algorithm: The greedy algorithm does not always work but when it does work, it works like a charm! This algorithm does not always produce the best optimal solution, which is its biggest disadvantage. This algorithm is easy to use and often the simplest. But making the best decisions in a place doesn't always work as it sounds. Therefore, it is replaced by a reliable solution called the Dynamic Programming approach. Different types of greedy algorithm: Sorting: Selection Sort, Topological sort Coin Change problem Fractional Knapsack Problem Job Scheduling algorithm Single source short path problem Dijkstra's Minimal Spanning Tree Algorithm Huffman Coding Minimum spanning tree Kruskal's Minimal Spanning Tree Algorithm Ford-Fulkerson Algorithm Prim's Minimal Spanning Tree Algorithm For example :
This clearly shows that a problem of size ‘n’ has been divided to sub problems of size ‘n- 1’ and ‘n-2’. Hence, Fibonacci numbers have optimal substructure property. Overlapping sub problems: A recurring solution contains a small number of different minor problems. It includes solving the same sub problem multiple times. Take the example of the Fibonacci numbers; to find the fib(4), first we have to break it down into the following sub-problems: Figure 2. Here it can be seen that overlapping sub problem pattern here, as fib (2) has been evaluated twice and fib (1) has been evaluated three times. Dynamic programming methods: Bottom-up approach with tabulation: Table addition is outlandish and keeps away from duplication. Along these lines, we take care of the issue "down to the top" (for example by taking care of the multitude of little related issues first). This is usually completed by filling in a n-layered table. The answer for the high/genuine issue is then coordinated with the outcomes of table. Table establishment is in opposition to Memorization, as in Memorization we take care of an issue and keep a guide of minor issues that have previously been settled. We should apply Tabulation to our illustration of Fibonacci numbers. Realizing that each Fibonacci number is the amount of the two past numbers, we can utilize this reality to fill in our table. Code for our bottom up dynamic programming approach,
Java: Figure 2. JS: Figure 2. Python: Figure 2.
Figure 2. Python3: Figure 2. C++: Figure 2. Applications:
Longest Increasing Subsequence, Longest Common Subsequence, Longest Common substring etc. Bellman-Ford algorithm Knapsack Problem Subset Sum Chain Matrix multiplication Example: Finding the Fibonacci Sequence. Figure 2. Recursive stack of the function for n = 4 Figure 2. Time complexity: O (n) Space complex: O (n)
target value. The process continues until the target is reached. If we find another segment empty after completing a search, it can be concluded that the target is not listed. Quick Sort: It is an exceptionally compelling sifting algorithm, also known as a separate rotation type. It starts by selecting a pivot number from the same members followed by dividing all parts of the same member into two smaller parts. Distinguishing is done by comparing each element with the pivot value. It compares whether an element holds a value greater or less than a pivot and then filters the same members over and over again. Merge sort: It is a filtering algorithm that filters the same members by making comparisons. It starts by sorting the same members into a small list and then sorting each of them over and over again. After filtering, it reassembles them. Closest pair of points: It is a problem with arithmetic geometry. This algorithm emphasizes finding a pair of points very close to the metric space, if given n points, so that the distance between points should be minimal. Strassen’s algorithm: It is a matrix multiplication algorithm, called Volker Strassen. It has formulated to be a lot quicker than the standard calculation while dealing with huge matrices. Cooley-Tukey Fast Fourier Transform (FFT) algorithm: This algorithm is named after J. W. Cooley and John Turkey. It follows divide and Conquer Method and sets the O (nlogn) complexity. Karatsuba algorithm for fast multiplication: It is one of the fastest repetitive algorithms of the traditional period, developed by Anatoly Karatsuba in the late 1960s and published in 1962. It multiplies two-digit numbers n thus by reducing it to a very single digit. Advantages of divide and conquer algorithm: Divide and Win often solves one of the biggest problems, like the Tower of Hanoi, a math puzzle. It is a challenge to solve complex problems that do not have a basic concept, but with the help of a method of division and overcoming, it reduces the effort as it works in dividing the big problem into two parts and solving it over and over again. It makes good use of cache memory without taking up too much space because it solves small problems within cache memory instead of accessing slow-moving primary memory. It works much better than its counterparts Brute Force technique. Since these algorithms prevent parallelism, it does not involve any modification and is hosted by systems that include the same processing. Cons of divide and conquer algorithm:
Most its algorithms are formed by combining repetition, so it requires advanced memory management. An obvious stack can overuse the space. It might try and crash the framework assuming the recursion is performed with more prominent power than the ongoing stack on the CPU. Now we discuss the simplest application of algorithm. Previously it was discussed how searching of an element in a sorted array takes O( n ) time, we will apply divide and conquer algorithm to reduce its complexity to O( logn ). Figure 3. Output 1: Figure 3.
- SECTION Definition: A programming paradigm is a style of solving a problem by using specific programming languages or we can say that the problem-solving method is using the tools and techniques we acquire by following a specific method. Programming paradigms: There are three programming paradigms are:
Figure 1. Cons of procedural programming paradigm: Another criticism of the process paradigm is that it targets on what needs to be done, rather than the integrity of the data it uses. Your teacher may not encourage you to use the variables in the world. Here is a problem with global variables is that, as they can be accessed and modified by all subroutines in the system, it is really difficult to determine where the value is converted. If you are trying to remove a bug in the system, you may need to consider all sub-methods that change the state of the world. That is possible, but with a really great application, it is very difficult to do. Obviously, use of global variables is not a necessary part of the process paradigm, but it is permissible. International variables should be used with extreme caution. Indeed, there is nothing in the process paradigm that really protects data. Data gets transmitted to subroutines, used, and sometimes a bit of changes are made, and the editor must be fully aware of what he or she should and should not do and comply with the rules of good performance to ensure the integrity of the data.
2. Object oriented programming: Object Oriented Programming (OOP) is a arranging paradigm in view of the idea of classes and items. It is used to organize a software program into simple, reusable pieces of code plans (commonly referred to as classes), which are used to create unique objects. There are many programming languages that focus on things including JavaScript, C ++, Java, and Python. The classroom is an invisible plan used to create clear, tangible objects. Classes often represent broad categories, such as a car or a dog that shares attributes. These classification defines what an example of this type consists with any attributes, such as color, but not the number of those attributes in a particular object.
Classes can contain activities, called methods that are only available on such items. These activities are defined within the classroom and perform a useful action on that particular type of object. For example , our Car Class may have a re-paint that changes the color of our car. This function is only useful for Car-type features, so we advertise it within the Car category thus making it the way it is. Class templates are used as a blueprint for building individual objects. These represent some examples of an invisible class, such as myCar or the goldenRetriever. Each item might have different values for the features described in the class. For example , let’s say we created a class, a car, to contain all the features a car should have, color, type, and model. Then we created an example of a car type, myCar to represent my specific car. Then we can set the number of structures defined in the classroom to describe my car, without touching other objects or a template. Then we can use this class to represent any number of vehicles. Figure 2.