Good books read together (85): Algorithmic notes

Source: Internet
Author: User

I've seen two algorithmic books, algorithms and introduction to algorithms.

The previous one was easy to read, the content was basically overlapping with the university data structure course, the sample code was written in Java, the learning curve was flat, and for the application programmer, reading it was good.

The latter I was watching MIT's "Introduction to Algorithms," the public class reading, inadequate, because I am not good at math (detailed), if you do not look at the mathematical proof, its content in front of a similar, mathematical basis is better, the algorithm is interested in friends, you can try it.

It is strongly recommended that the learning resources of the Open class platform are very high quality, Brief Encounter! At home, you can learn the courses of prestigious schools. But do not forget to follow the exercise, take notes, preview review.

Here is my algorithm note.

0. Thought

The most serious of all. The real essence of the algorithm. The heart of the internal organs. In contrast, the following are specific moves.

The meaning of internal internal heart is in, one day you encounter a problem with no existing experience, you can design the appropriate data structures and algorithms, rather than just use other people's wheels.

The fundamental idea of the algorithm is that the big problem turns into several small problems and gradually solves it. ("The Art of War": the rule of the people as a rule, scores are also, the crowd is like a fight, the shape of the name is also.) )

1. Division and Treatment

If the big problem can be split into small problems of the same nature, when small to a certain extent, can be directly resolved.

The method of division and treatment is used to solve the problem recursively.

2. Greed

If the optimal solution is found for a small problem, it can solve a larger problem by adding a little bit of step, and still the optimal solution.

Then the greedy method, starting from 0, gradually eats away the big problem.

3. Dynamic planning

If the same problem is common to the small problems that are separated by the split method, the cost is too high to repeat the calculation.

Then use the memo method, calculate the results of each small problem, record, the next time you see the same small problem, no longer repeat the calculation.

First, the basis of mathematics

The introduction to algorithms gives 4 essential mathematical bases in the appendix.

I looked after, very envious of the students, they have a few courses are very solid.

These 4 mathematics in addition to is the Postgraduate examination mathematics subject, the algorithm foundation, actually is observes the world, guides the life (not only produces! Tools, I decided to take a look at the public class, and gradually re-learn the way.

1. Calculus

2. Discrete Mathematics

3. Probability theory

4. Linear algebra

Second, linear table

1. Classification by implementation

Sequential table: Convenient random access, insert delete cost is high, because one side of the element to "Da da" move in turn a grid.

Linked list: Random access Trouble, because to the beginning of the "123" number past, insert delete convenient, the specific two or three-node pointer "Ka Bar" to re-connect it.

2. Classification by action

Stack: Last first out, intuitive understanding is to take the elevator. Use is mainly in dealing with large problems, refinement as a small problem, small problem solved before continuing to deal with big problems. The core data structure of the computer's function call. If the unfortunate small problem is endless, such as infinite recursion, then StackOverflow.

Queue: FIFO, the data structure of "first served". The typical use is the "task queue", first-come-first processing. If "Queue jump" is allowed, it is "priority queue", let the leader go first!

Third, sort

Suppose there is an initial queue of 4132, the goal is to sort from small to large into 1234.

1. Bubble sort

The simplest and slowest algorithm, slow to a lot of books do not speak.

The idea is to let the last element try to climb up, meet the larger than it is over the (Exchange order), if the encounter is smaller than it stopped, from the small continue to climb upward. When you crawl to the first element, you ensure that the first element is the smallest of all elements.

Repeat this process by not considering the first element. 4132,1/423,12/43,123/4.

2. Select sort

Each time a minimum element is selected from the queue and placed in the sorted queue until the selection is complete.

/4132,1/432,12/43,12/34.

3. Insert Sort

Select the first element from the unordered queue each time, and plug in the appropriate position of the sorted queue until you are finished.

I use this method when I play poker.

4/132,14/32,134/2,1234

4. Hill sort

First, the "distance between the greater spacing" of the elements are sorted again, and then moderately reduce the spacing and then again, the last time a distance of 0 rows.

4132, first spaced large spacing (this humble example, leaving the interval 1), 4 and 3 sort, 1 and 2 sorted, 3142, and then spaced smaller spacing (in this case directly to 0) sort.

5. Quick Sort

The sort that is most liked, both easy to understand and fast.

Choose a number for the axis, the left side is smaller than it, put it on the right side, and then make this change on both sides of it.

4132, the first step selected 4, the second step 1, are all the remaining elements on the same side, not cost-effective, so the random selection of axis elements is critical.

Suppose the first step selected 3,12 left, 4 put on the right side, the right end, left to continue, assuming that 2 is the axis, 1 left, the right is not, that results 1234.

6. Merge sort

So-called merger, such as a class and the second class has been ranked from short to high orderly queue, want to merge into a team, how to do? Compare the first and second class of the Pai, the shorter the election to stand in the "Results queue", the class of the new Pai Tau and the other side of the Pai, and so on.

Merge into a new queue, then merge with the other queues until they are merged into a single team.

1234, 123|4, 12|4/3, 1|4/23, 14/23,

7. Heap Sequencing

The so-called heap, is a tree each parent node value is greater than all of its descendants of the value of the full binary tree, if you do not understand, first jump to see the tree section below.

For the convenience of saying (put the map too troublesome, I believe that pure text can say clear), this time we from the big to the small sort, the truth of course is the same.

In two steps, one is to construct a tree from an unordered queue, and the other is to output a valid queue from a tree, and to combine it is to get an orderly queue from an unordered queue.

The first step is to take a new node, place it in the last position of the original tree, and then make it try to bubble up until it does not move, and then win a new node. such as processing 4132:

4-4 left lower 1-4 left lower 1 right lower 3--4-left lower 1 right lower 3,1 left Lower 2-(2 bubbling)-4 left Lower 2 right down 3,2 left lower 1

The second step, take out the root node into the result queue, the last node to the root position, and then examine its two leaf nodes, if there is more strength, then to the most powerful abdication takers, exchange position, to the new post continue to inspect subordinates ability, until reduced to their competent position.

4 left Lower 2 right lower 3,2 1-(remove 4, 1 upper)-1 left Lower 2 right lower 3-(3 usurper), 3 left lower 2 right lower 1-(remove 3, 1 upper), 1 left 2-(2 usurper), 2 left lower 1-(remove 2), 1 Loner-(remove 1)-& Gt Results 4321

Four, tree

A number of nodes, and a number of connected nodes of the undirected edge, a diagram, if starting from each node, can reach any one node (strong connectivity), and there is no ring in the diagram, known as a tree.

The tree in the data structure, on this basis, then strictly point, divided into several layers. If a commander has a number of commanders, each company commander has a number of monitor, painting Organization chart, direct leadership and direct subordinate link up. The direct leader is called the parent node, if the head is the parent of the company commander, the company commander is the parent node of each monitor; The direct subordinate is called the child node; The supreme chief is called the root node, the node without the parent node, and the node without the child node is called the leaf node, for example, "Soldier Assault", the steel Seven , more than three is a leaf node, if at this time more than three by old a dig away (the actual plot is the old seven go first, not fat), then the old seven commander, will unfortunately become a leaf node.

1. Two fork Tree

If the tree has a maximum of two child nodes per parent, the tree is called a binary tree.

The most typical use is a two-fork lookup, which is called a binary sort tree if the value of each root node is larger than its Zuozi (this concept is words too literally, not redundant) and has a smaller value than any node in its right subtree. When you want to find a value, you can first compare the value to the root node, if it is larger than the root node, then go to its right sub-tree root node ratio, if smaller than the root node, then go to its Zogen node ratio, until found or compared to the leaf node.

Like the "Guess number" game, a good book worth 1 yuan to 256 yuan, if you are stupid enough to see that the book can not be 1 yuan, and smart to know the use of binary search, then the first guess 128 yuan, according to the moderator said "high" low "and then decided to guess 64 or 192 yuan, if 64 or" Higher, you should continue to guess 32, and so on.

2. Red and black Trees

It is highly recommended to see the chapters of the red and black trees in the algorithm, starting with the 2-3 trees, and speaking of red and black trees, the learning curve is very smooth.

Both the binary tree and the red and black tree have a clear logic for preserving the balance of the tree, limited in length. The goal of the balance is to maintain the worst-case lookup level of Logn (base 2, the logarithm of the total number of nodes).

3. Hash table

This is not a tree ... But it's best to put it logically, because it's a data structure for "find."

The two most basic programming data structures, List and map,list is the above-mentioned linear table, the core is in order, map is here to say the hash list (HashMap) or tree (TREEMAP), the core is a key value pair. Set, more map to do the underlying structure.

The tree says, here's the hash table.

Hash list is also called a dictionary, from this literal, the main purpose is to find, by key (key) lookup value (value), first of all, to calculate the key into a number, the algorithm (hash function) is the Java class to implement the Hashcode method.

Then, there are two options for implementation, one is the link method, first there is a main list of keys, such as 123, and then 1 corresponding to a list,2 corresponding to a list, I calculate the key is 1, to 1 corresponding to the list element to find the element. Obviously, the 1 corresponding to the list length, the shorter the lookup time, it seems that the main list of the keys are more good, such as 1 to 30, the length of each sub-list will become the original length of One-tenth, but the main list of the keys can not be too many, such as 1 to 30000, the majority of the key corresponds to is empty, There is no element in it, it wastes space, so weigh it.

Another way of implementation is the hash method, there is no secondary list, the key is calculated as a number, directly to the main list corresponding to the location of the position, if the position has been occupied by other elements (collision) How to do? A new number is calculated using an algorithm, and then the new location is tried (and then hashed), and so on. Obviously, this method, the length of the main list is also very critical, if too short, then too many collisions, insertion, lookup is troublesome, if too long, and waste of space.

For both methods, the hash function is very important, and if the hash function of all the elements computes the same value, the hash table is meaningless.

V. Fig.

Finally to the picture! This is my favorite part, fun, and familiar, because I know a classmate, the data structure hangs every year, until the senior, so I have to accompany him to review the exam for a time, the teacher examination papers in the high points of the problem, is a number of algorithms, Dijkstra, Primm, Kruskal ... Well, I miss that part of the day.

What is a graph? A bunch of nodes, and a number of sides, it constitutes a diagram, according to the direction of the line, divided into a directed graph, no map, according to whether there is a ring in the picture, divided into a ring diagram, no-ring diagram.

If any of the two nodes have a line connection, it is called a strong connectivity graph, which is the main chat here.

1. Depth-first, breadth-first traversal

A Lian B,b Lian C,b Lian D,c even E

What if you want to study all the nodes in the diagram again?

Start with a node, go to the next node, and then move on to the next node in the next node until the "no way to look back" is "depth-first traversal." In the example, from A to B, from B to C, from C to e,e back to c,c back to B, from B to D, complete the traversal.

"Breadth-first traversal" is, starting from a node, the first fan-like to be able to sweep the nodes (canton), and then "move" to the child nodes. In the example, the traversal is done from A to B, from B to C, from B to D, from C to E.

2. Minimum spanning tree

If each edge of the tree has a weight, ask to select some of the lowest total weights, so that all nodes can connect to each other, how to do?

This is the "minimum spanning tree" problem, obviously, the chosen result is definitely a tree, the two conditions of the tree, connected and non-ring, connectivity Needless to say, why no ring? If there is a ring, remove any one edge, the nodes are still interconnected, the weight of the value has become smaller, thereby proving no ring.

To solve this problem, two methods, one is the Primm algorithm, the second is the Kruskal algorithm, a good professional, finally a little algorithmic feeling.

In fact, they are very simple, first say Primm, the figure is zoned "resolved" and "unresolved" two parts, the first "resolved" is empty, "unresolved" is the complete set, first of all, select a point to "resolved", and then investigate all from "resolved" to the "unresolved" edge, which is the shortest, the edge of the " Unresolved "nodes are included in the resolved" collection, so repeatedly, until all nodes are "resolved", the result is a minimal spanning tree.

Again Kruskal, at first, consider each node as a "component", looking for the shortest of all "connecting two sides of different components", merging the two components of its connection into one component and repeating the behavior until all nodes are of the same component, and the result is the smallest spanning tree.

3. Single Source Shortest path

The minimum spanning tree is a graph with no direction and the shortest path is a graph.

As the name implies, the shortest path is required to give the most convenient way from one place to another, there are two algorithms, one is the Dijkstra algorithm, the other is the Bell Ford algorithm.

First say Dijkstra, this is similar to the Primm "greedy algorithm", from a very small "resolved" start, gradually nibble, until the "unresolved" eat. The difference is that there is a number on each node that represents its distance from the starting point, and each time you select the smallest of the number of "unresolved" nodes that are pointed to from resolved to unresolved edges. For example, the starting point is home, home from the school 500 meters, home from the market 600 meters, the school is 200 meters from the park, according to the algorithm, the school labeled 500, the market labeled 600,500 is less than 600, the school into the "resolved", the school into the "resolved" after the park has become a "resolved" can be reached Unresolved "node, with the school's 500 plus 200, the park labeled 700, compared with the market's 600 meters, 600 smaller, the market into" resolved. " Note that this step, if it is the PRIMM algorithm, the inclusion of the park rather than the market, because Primm does not exist starting point, are connected on the OK, only consider which side shortest, but Dijkstra has a starting point, to compare is "from the starting point until the distance here."

And, again, Bertelsmann Ford, this algorithm is to give each node a "distance from the beginning of how far" value, in addition to the starting point, the other starting point is infinite, and then examine each side, if the edge can make the point of the value of the node is smaller, the node value is smaller (contraction), all the edges are examined once again, If the "Shrink operation" is performed in this pass, then all the edges are inspected again until a certain time has not shrunk at all. The above example, such as the study order is 200-500-600, then the first time 200 what does not, 500 will be the school's +∞ into 500,600 will be the market +∞ into 600, this has been contracted, and again, this time 200 useful, the park's +∞ into 700, 500 and 600 are not, this time also contracted, again, 200/500/600 are useless, end.

Six, string

The string essence is an array of characters. When it comes to algorithms, the most common word is the KMP algorithm for string lookups.

For example want from "Grone World matchless" search "son Longzi", "Son" equals "Son", "Dragon" equals "dragon", "son" equals "Son", "dragon" is not equal to "wing", bad! At this time the source text of the "searched sequence" of "suffix" has "Grone" babies "Zilong" dragon "four, and the target text" prefix "has" son Longzi "Kotong" Zilong "son" four, examine two "people", found that there is a same element, namely " Zilong ", length 2 (if there are multiple identical elements, whichever is the longest) means that the last two words in the source text are the same as the first two words of the target text, and the next word" world "with the source text will be compared with the third word" child "of the target text. If it is a simple algorithm, then use "Grone" the first "dragon" Word and "sub-Longzi" The first "sub" word, KMP algorithm than the naïve algorithm faster!

Seven, written in the back

The water of the algorithm is too deep, I just entered a door, look back, the above is written, all is the university "data structure" content, heaved a sound.

There is also a willingness to learn more deeply, but before this is necessary to consolidate the basis of mathematics, I am looking at NetEase Open class, "linear Algebra" and "Calculus" open class.

Fortunately, these "getting Started", weekdays work is enough, and mathematics, computer-based consolidation and learning, is a lifelong thing, not too urgent.

Do not lament the past barren, the only way is based on the present state, select the best course of progress, diligently preach.

Those who have learned a little bit of the fundamentals of economics will not be frustrated by the sunk costs.

Mathematics is too beautiful, logic is too beautiful, the algorithm is too beautiful.

And I just found out lately. I didn't save it when I was reading.

Not to save also does not matter, honestly obedient good, now think, is "independent thinking" too early.

This is my life course, there is nothing to regret, write this is just want to remonstrating still studying friends.

Don't rush your own way, in the future life, there are opportunities to make your own decisions, while still in school, please obey.

Listen to the teacher and study hard.

Good books read together (85): Algorithmic notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.