Algorithm Design and Analysis: Divide and Conquer, Dynamic Programming, and Graph Algorithms
Introduction
How did the algorithm come up? The algorithms are designed to understand the rules, but what other people think of the answer. Why can't we figure out how to handle this.
We need to think of a way to deal with the problem, one that can be universalized in design, one that analyses the problem with one set of ideas, not one that relies on the light.
Basic thinking for scientific research
- First of all, we'll have a real problem, choosing or finding a real problem.
- Next, we want to turn the real problem into an algorithm or a math problem.
- And finally, let's deal with this.
Basic thinking for dealing with mathematical issues
- Try to deal with it from the front.
- If you can't deal with this problem in front, maybe you can't even think about it.
Way1
- In this case,Can the simplest case be handled??
- When the simplest of things can't be dealt with, just give up.
- When the simplest question can be dealt with, deal with him.
- When a simple case can be handled, but a complex one can't.Does decompose work?What? When it works, then.Summary methodIt's possible.
- When decomposition is feasible, and the original problem is one of optimization, andThe best solution to the original problem can be the best combination of small problems., so you can takeDynamic planning
- When the conditions are met, andThe best solution can be seen by the local.♪ Then you can take ♪Greedy.
Way2 When problems can't be decomposed, but need to be studied to solve variations, then other algorithms can be considered, such as linear planning, network flow, etc.
Way3 When it's not possible, it's necessary to study the enumerator, so we can think about a better enumeration strategy.
Last Way And finally, there's no simir question, try simir algorithms.
The design of algorithms in the AI era, in which there are many randomly tried methods, may be solidified by the use of neural networks and large amounts of pre-collected data, which sometimes increase the efficiency of computing. That's...Use NN to replace a very complex module
Divide Conquer
Basic thinking: Many of the problems in the real world are regressive, and the only difference between big and small is size. To solve big problems, we can break them down into small ones and deal with small ones through retrogression, so we can combine the solution of the original problems.
- Divide solves small problems
- Conquer handles minor issues (procedural regression)
- Combine got a big problem.
The core of Divide and Combine is how to observe the form of the data structure for output and output, and he determines how we divide and merge, and we might look at how they divide and merge, arrays, arrays, pools, trees, maps, most commonly used data structures.
For an array, each element has two attributes: value and subscript;
So it's easy to get a basic subdivision based on the subscripts, taking one of the elements out, divided into two arrays.$n$individual$n-1$An element. Then, for the larger group, the split is repeated until the simplest form of the two elements that we will be dealing with is finally recombined. Corresponds to sorting tasks, which is inserting sorting.
In another way, it divides from the middle into essentially large sequences and repeats them. The final merger should start in two groups and select the smallest of the elements. And the two groups that have been sorted, the smallest elements must be on the left-hand side, starting with the left-hand side, and each time they can remove the smallest elements, reducing the consumption of the sort. It's sorted.
One way to divide is to consider dividing from the values of the arrays; randomly choosing a pivot dollar, with elements larger than it as group A, with elements smaller than him as group B, and then retrieving the call sorting functions of the two groups separately until all the elements are sorted correctly, and now there is no need for cobine, a sorting sequence that can be regrouped, which is quick.
There's a lot of questions to use if we want to find out.$A$Number of elements$k$Large, we can use a similar fast-paced method, by value. Select a pivot randomly, using the larger elements as group A, the smaller elements as group B, and then write a logic to determine which group the larger element of k is in and then revert to the function. Finally back to the element found.
For graphic data, which are also more common in algorithms, we can consider achieving diviide by reducing the vertex and side, and starting with the simplest case.
It's natural for divide to be a matrix, and we'd like to split the matrix according to location until it becomes smaller. This is used in high-speed matrix multiplication (or more matrix-related issues). In fact, the big multiplication is using the same divide approach.
To speed up diviide conquer, the two core lines are:
- Fewer fractions that need to be calculated directly, i.e., new fractions are processed using the addition of the fraction
- Smaller fractions of one-time fractions, smaller fractions of time. degrees
If an issue that can divide is a matter of optimization and can be synthesized by sub-issues, we will have to consider using dynamic planning, which is a further reflection of divide conquer. The greed behind it would also be an improvement in dynamic planning.
Dynamic Planning
DP Foundation
The issue of dynamic planning continues to be one that needs to be divided, except that here we would like to present more ideas for turning issues into sub-issues, which this section hopes to learn — the way to think and deal with issues using DP thinking.
If our core problem is finding optimal decision-making, and the problem can be modelled as a multi-step decision-making process, then we can consider using DP.
Let us begin by introducing a basic example: there are three aircraft, 10 missiles, each with its own probability of shooting down the number of missiles and its own value, and how best to achieve the highest overall value of shooting down.
It's not realistic for larger sizes, but for this problem involving pure integers, we can consider using a different type of diviide that uses multi-step decision-making methods (because pure integers mean that options for decision-making are limited).
Definitions: Functions$max{V(x,y)}$ For...$y$A missile. Destroy.$x$Maximum value an aircraft can get
So at this point, we want to calculate$max{V(3,10)}$ We're actually making decisions about how many missiles each aircraft needs to use, so we can start with the first decision, directly listing the first one.$maxV(3,10)=max{max(2,x)+EV(x)}$ Yes.$10-x$The desired value of one aircraft attacking the first aircraft and the other being transferred from another.$maxV$function.
By using such a computational procedure, it would be possible to simplify the problem to the case of an aircraft and to finalize the resolution. Because the simplest form of this model, an aircraft and any number of missiles are easy to solve.
The idea of multistep decision-making and optimizing total value isBellman equationSuch a solution would be feasible if the problem could be summed up as a multi-step decision-making process and there was a regressive relationship between optimal resolution.
The idea of a multi-step decision-making process is understood.Modelling issues related to decision-making into multi-step decision-making structuresOne step at a time to optimize the solution to complex problems.The choice of each step of decision-making should be simple in itself and should not have too many branches, after decision-making results and further back-to-back decision-making should be made as easily as possible by code and not generate a large number of branches of f
The multi-step decision-making described above is in fact a step-by-step approach, with multiple layers of back-to-back and all calculations producing many performance costs, and we need to consider ways to reduce multi-step decision-making costs. dp The result of storing a sub-issue when it is actually used, and when the sub-issue needs to be solved again, can be directly identified to reduce the eventual complexity.
3 classic DPs
Minimum number of array operations: a series of arrays of product, total$A_1,...,A_n$Total$n$A matrix, each size$p_0,...p_n$ The minimum total number of operations can be obtained in the order of operation, note:$p_0,p_1;p_1,p_2$The total number of arrays multiplied is $p_0p_1p_2$
Such an issue can naturally be transformed into a situation of multi-step decision-making, and we need to decide which matrix to multiply first. And how to turn this long matrix product sequence into a short matrix with similar problems requires only the addition of brackets and a break point for the final product.
Definitions:$OPT(i,j)$ Yes.$i,j$Minimum number of operations for the upper matrix product (for this type of string, even if it is a single string, but is still used to tracking in two positions, e.g. number of returns, valid brackets length), so an iterative formula can naturally be given $$OPT(i,j)=min{OPT(i,k)+OPT(k+1,j)+p_{i-1}p_kp_j}$$ Use this type of regression formula to reduce the length of the longest series by two matrix multipliers.
String Matching: Give two strings OCCURCE,OCUORCE The match between the two strings is judged by adding one point when the matching position is perfect, and three points when the matching error or omission is subtracted to calculate the largest matching score.
It's two long string sequences, too long string that we can't handle, so we want to reduce the length. The simple idea is to start at the end, and so choose.$OPT(i,j)$For string 1 to$i$Zen, two-by-two.$j$location, the degree to which two strings match.
There are three possible matches for the last character of the two strings: $E-E;E-NULL;NULL-E$ Then you can give it to me.$OPT$One form of decision-making is $OPT(i,j)=max~begin{aligned} &s(i,j)+OPT(i-1,j-1)\ &s(i,NULL)+OPT(i-1,j)\ &s(NULL,j)+OPT(i,j-1)\end{aligned} $ This is actually how to determine the match of the last element, and then turn the long string into more strings, thereby making the OPT defined here iterative.
Maximum common subseries: given two strings text1 and text2returns the longest of these two strings Common Subseries The length. If not present Common Subseries Other Organiser 0 I don't know. A string. Subseries refers to a new string: it is a new string formed by the original string without changing the relative order of the characters.
We still have two strings that need to be tracked, too long strings that are difficult to process, so we still want to return to reduce the length of the string, or to track the string from the tail.$OPT(i,j)$String 1 $i$Previous with string 2 $j$The longest public sub-series before that can be given in an iterative manner by determining the match of the last position.
$OPT(i,j)=\begin{aligned}
&0if i=j\
&OPT(i-1,j-1)+1 ifT[i]=T[j]\
&max{OPT(i-1,j),OPT(i,j-1)ifT[i]\ne T[j]}\end{aligned}$$
The idea of a back-to-back issue in dp does not require that we have to re-entry to use the function that produces the question, and we define ourselves a new function that produces some transitional results.
This OPT function is defined in order to simplify the problem into the simplest form (which we can deal with directly) by using them in a step-by-step decision-making that reduces the complexity of the problem.
Define new OPT function
Maximum reply: Give you a string s, find the longest entry in s. "bab" is a 3-long substring reply.
Naturally, for the length of 1 we know that he must have satisfied his reply and that we should not create an OPT function to reduce the length (which must not be a reduction from the head or tail, but rather a symmetrical reduction).$OPT(i,j)$This means the longest reply string in this section, which is used to determine whether or not the string in this section is returned, so you can give the attribute $$OPT(i,j)= OPT(i+1,j-1) ~~if ~~s[i]==s[j] $$ When?$j-i==1$Or...$j-i==2$When you don't think about the strings.
Maximum valid brackets: A string containing only `(' and `) ' finds the length of the maximum valid (in the correct format and continuous) string.
The maximum valid parenthesis length of a single-string tracking study ending with i could be considered. This would make it possible to establish a regression; it could also consider the use of two indicators to track the validity of the brackets in the study compartment, which could be created by removing the last brackets (the last brackets would have to be)')'It's legal.
DP on array
Many of the questions that deal with arrays, they do not have visible decision-making, but the simplest questions we would like to discuss are short arrays, while the real problem is long arrays, and we are actually building a regression equation (bellman equation) to shorten the length of arrays. Creates a new opt function, where the solution of a real problem is hidden in the opt function, and then studies how to deal with this opt function in a reverse manner.
The function of the opt that can be created should be diverse, and it is important to change the idea of designing the opt function in a timely manner when dealing with difficulties. The probability of multi-step decision-making is not the only one, so when dp is used, it is also necessary to be flexible in thinking and to use different methods to minimize big problems.
The purpose of decision-making is to simplify complex issues, and because the natural length of the array itself determines the complexity of the problem, the core of this dynamic planning lies in finding the sub-problems of shorter and less complex problems, and in finding the relegation of problems between less and less complex. Finds a retrieving relationship between the increasingly short opt function. The reduction of arrays is simple, except that the length is reduced before/from.
In sum, it is not scary not to find a decision when dealing with arrays, and it is often easier to deal with arrays, with more fixed and rigid thinking.
Maximum upscaling subseries: Give you an integer array of Nums to find the length of the maximum strict increment sequence. Subsequences are sequences derived from arrays, removing (or not removing) elements from arrays without changing the order of the remaining elements.
This issue is well dealt with for an array of only one or two elements, but it has not grown so well, so we need to consider using the DP approach to reduce the length of the array. For this single-string array, the best method of decision-making is to create an OPT function with a reduced length from the end of the array.
So define$OPT(i)$For the right time.$i$An element, the maximum ascending subseries of an array. I'll give you an extension.
$OPT(n)=OPT(n-1)+1 if num[n]>num[n-1]$$
It's used here to fit the first element, not the first one, so it's easier to give this line of regression., and we've made this self-definition of the use of strings before, and finally all of them.$OPT[i]$Just one max.
Maximum subsequences: set an integer array Nums, find a continuous sub-set with the maximum sum of one element.
It is also the problem of arrays, which naturally are easy to handle and complicated, and we want to reduce the length of arrays to make them simple. Definitions$OPT(i)$It's the largest number of consecutive sub-sets at the end of the number i, naturally giving in. $$OPT(i) = max(OPT(i-1)+num[i],num[i])$$ As with the last small question, what we've created is the exact end of the number I, because it's easier to construct and process it.
DPs that may be in circulation
There is a sequence of a sequence to the ringless; the shortest road question above him can be understood as a multistep decision-making process in reverse and sequence.
When there is a ring on it, the shortest-path problem is circular dependence, and the return of a dead cycle is called, which must be addressed. The most common method at this point isAdds a parameter for a recursive function to keep him down while he returns. It's actually increasing the particle size of the decomposition structure to make the problem more detailed.I don't know. In fact, this idea of making matters more detailed is the same as the one that preceded the construction of the new opt. The right PT is the core.
Greedy
Greedy Thought
In dynamic planning, we don't know what the best decision is at this time when we are somewhere, so we need to count back to the full to know the final result. And sometimes we know where the best decisions are, so we can introduce Greedy.
Selection issues: giving$N$Courses, each with the corresponding time and number of participants$W_i$How the curriculum is scheduled to be supplemented and maximized
The big questions are difficult to solve, but the sub-structure (only 1-2 lessons) is easy to judge by a few ifs and an optimisation problem, and dynamic planning is likely to address this problem well. The problem of scheduling can also be seen in some way as an array. Shorter length is at the heart of the solution, so we can start from the end of the class backwards so that we can trace less information and define it.$OPT(S,T)$Yes.$T$Time is available. There's still time.$S$The maximum number of scheduled classes can be cancelled in reverse. $$OPT(S,T)=max{OPT(S-课x-冲突课,课x的上课时间)+W_x,OPT(S-课x,T) }$$ The last class of multi-step decision-making does not lead to the final solution of the big problem, and the reduction from the last class is just to reduce design.$OPT$The difficulty.
♪ When all$W_i$In the first place, we can use Greedy to deal with this problem, and we can find the best solution without having to go through all the scenarios by constantly choosing the first class/last-night course and synchronized conflict-free courses.We don't need to solve the problem, we choose the best part.
Dynamic planning and Greedy are very similar, they're mainly used to handle optimization and are based on sub-structure analysis, and all Greedy has a corresponding DP behind him, listing all situations instead of choosing local merits, and they can deal with such issues.
Or is there a question of what kind of decision should we make with Greedy? The selection of the right option by its own Idea is wrong, for example, by scheduling the earliest course/show the shortest course, and finding the greed strategy ahead is not an easy task.
You can't rely entirely on the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the rules of the law of the law of the law of the world, but of the rules of the rules of the rules of the law of the world, of the rules of the rules of the law of the world, of the rules of the world, of the people of the world, of the world, of the people of the world, of the world, of the world, of the world, of the world, of the world, of the world, of the world, but of the world, on the rules of the law, of the rules of the law of the world, of the world, of the world, of the world, and the world
Greedy's theory.
The Greedy method is used to deal with the following problems, and to set up a collection.$S$Find a subset of him.$A$Jean.$F(A)$Max. In the absence of a greedy approach, we can only go through the whole assembly to find all possible subsets. Of course, Greedy can only deal with two of these kinds of problems.
- $f(S)=\sum w_i$ That means that each sub-component has nothing to do with the whole function.
- $f(x)$It's not linear, but he's condensed.
Question 1: Finding a very irrelevant group of vectors, at which time each vector has its own value, and we want to maximize the value of the last very irrelevant group.
This is naturally summarised as multi-step decision-making, using the DP algorithm to deal with the issue, which can be addressed through iterative end-to-end decision-making on whether or not to add the last vector (similar to the scheduling issue).
But at this point, we want to maximize value functions to be linear for each subset, so we can consider the greed strategy and keep introducing the highest value vector until we do not satisfy the unrelated group.
The Prim algorithm, Kruskal algorithm, and the Dijkstra algorithm also use greedy tactics, because their total distance is also a linear function, and the search for the smallest-generated tree and the shortest path are a subset, so the Greedy strategy is feasible and the next time a similar problem is encountered, it is possible to consider Greedy instead of the DP that uses multi-step decision-making.
We also discussed a situation where Greedy could be considered, since it was a pool function at this point, and when he satisfied the nature below, he could consider Greedy.
$$f(A)+f(B)\ge f(A\cup B)+f(A\cap B)$$
Definition of Equivalence
$$$ifS_{1}\subset S_{2} ~thenf(S e)-f (S )\gef (S 2+e)-f (S 2) $
This is a near-uniform, and at least the subset extension function will not be reduced.
This definition reflects a quasi-combust nature, where the addition of elements increases the overall function faster when the subset is smaller, and when the subset is larger, the increase in the function is less obvious, and the increase is related to the base.
Why does this thing work? Because he can provide a higher line,$S\subset T,T-S=E$ And...$e_i$Yes.$E$All elements of the list are available $$f(T)\le f(S)+\sum e_i$$
What kind of greedy strategy should be followed when confronted with such functions? Find the largest addition, the largest marginal increase. So that the increase will be smaller and smaller, and the solution that we finally find will be near the best. If similar problems are encountered (e.g., backpack problems), a near-negative strategy may be considered to find the most expensive elements.Find a greedy indicator and then implement greed.
For context, compare the data structures introduction and the search and sorting algorithms notes.
- Title: Algorithm Design and Analysis: Divide and Conquer, Dynamic Programming, and Graph Algorithms
- Author: Hyacehila
- Created at : 2025-05-13 10:32:25
- Link: https://hyacehila.github.io//blog/2025/05/13/algorithm-design-and-analysis/
- License: This work is licensed under CC BY-NC-SA 4.0.