"Introduction to Algorithms" The 12th lesson jumping table

Source: Internet
Author: User

This lesson introduces a new kind of data structure--jumping table


Jumping table is a simple and interesting dynamic search data structure, its main advantage is that it is easy to implement, and it is good to ensure its efficient performance, that is, 2*o (LGN) Search performance


Before I would like to first talk about the list, the advantage of the list is that it needs to insert and delete only the constant entry time (plus the need for additional O (n) time to find the element), but its lookup efficiency is only O (n), here incidentally to add the list of problems, The following is a list of the two bat companies that are passionate about the two classic questions they have in the interview:


1. How to quickly find the penultimate element of a one-way linked list

2. How to quickly determine whether a one-way linked list exists ring


For linked list class problems, its core idea is two points, 1 is open double pointer (even multi-pointer), 2 is open doubly linked list (even multi-linked list), in fact, the above two problems open double hands can be cleverly solved, the first problem, first open a pointer walk m step, and then open a pointer synchronous walk, the current a pointer to the end of the list, The last pointer is just pointing to the bottom m element, the second problem, open a fast pointer and a slow pointer, the fast pointer each move two steps, the slow pointer each move one step, if there is a ring, then the fast pointer will be able to catch the slow pointer, you can imagine two people in the playground race, fast people run for a long time will be super slow people a lap.


Next we continue to talk about the jumping table, in fact, the jumping table is the second idea, open the double linked list even multiple linked list


First of all consider the establishment of two linked lists L1 and L2,L1 as a fast table, that is, to save only part of the elements, L2 for the slow table to save all elements, note that the following list of linked lists are well-ordered list


When we want to search for an element, we go to the fast table, because the fast table only retains part of the elements, so it is jumping forward, until the fast table through the element, we return to the fast table before a node to the slow table to continue to go, so efficiency is obviously better than on the slow table on the linear lookup, The speed table here is like the subway line of the United States subway, the fast line of the subway only a few stops, and the slow line will stop at all stops, passengers can first take the express line to a nearest destination of the previous station, and then transfer to the slow line to the ground

So that's the problem?

How to Build table L1 and L2? L2 is undoubtedly a one-way list containing all the nodes, then the L1 should be set how many nodes the most reasonable, intuitive feeling L1 should be evenly distributed best, then the density distribution is the most appropriate?


It is not difficult for us to conclude that the search time upper bound is | l1|+| l2|/| L1|+ the constant of the transfer,here | L1| indicates the length of the L1(The worst case is that L1 walks to the end, walks back to a node and enters L2, because L2 can be seen as being divided into L1 segments by L1, so each length is | l2|/| l1|, so for | l1|+| l2|/| l1|+ transfer constant ), because the L2 length is n (including the entire list), the transfer is the link list L1 down to L2 time, as a constant, so our goal is to make | l1|+n/| l1| minimum , i.e. | L1| is optimal when the sqrt (n) is(may be directed to or through other mathematical methods, prove slightly), the time consumption is 2*SQRT (n), that is, every sqrt (n) set up a fast table node, a total of sqrt (n) Fast table nodes

What the? SQRT (n) not yet enjoyable?

So what can we do to optimize it? The answer is to add more lists, let's see how many of the three linked lists should be, and intuitively tell us that it's 3*n 1/3 times.
In fact, it is possible to prove that the K-linked list is k*n 1/k

Because n is a constant, how much more suitable is k? lgn! Let's see how much time K takes LGN, that is, lgn*n^ (1/LGN), that is, Lgn*n^logn (2), remember when we calculate the recursive time complexity of the change-bottom formula? Here N^logn (2) is 2^logn (n) i.e. 2^1, i.e. 2, so the entire time complexity is 2*LGN, which is a very good performance.

In this case, the jumping table is called the ideal jumping table, the number of each layer is reduced by half, a total of LGN-linked list, from the most advanced linked list to search, not search down, up to the next Logn layer, up to 2 elements per layer, so the search complexity of O (2LGN)


So again, how to dynamically maintain such a jumping table?

First look at the deletion function, delete the function as long as from the parent chain list after the search, you can directly delete, and down all the linked list of the node is deleted, this is relatively simple, then insert it?


Insert (x) First search (x) in the position of the base table and then insert the element, is it over? No, because after inserting several nodes in a certain section, this section will become very long, the balance structure of the whole jumping table will undoubtedly be broken, so how to maintain the structure of the ideal segment table?

1. Keep the ideal distance between each paragraph, if the distance is too large, split from the middle, and then raise the midpoint to a layer of nodes

This approach is very intuitive, but it is difficult to implement because you have to record the length of each segment in real time.


2. Using our favorite randomization algorithm, toss a coin if the front, the node is raised to a level (that is, the node is also added to the list in the first-order list), and then toss the coin (see if it continues to raise levels), because two adjacent list of the length of the ratio of 1:2, and the probability of the positive coin is 50% It turns out that this is possible, it is worth mentioning that the teacher in this class sent two coins to the classmate, a use of tossing coins to generate random numbers, a flip coin to determine whether the current insertion node need to raise level, in the classroom directly to do the experiment, the whole course atmosphere is also very good, Also let the students have an intuitive understanding of the algorithm, this kind of teaching method is worthy of reference


Note that there is a special case to consider here, that is, when the element we insert is the smallest element, if it does not raise a level, then the beginning of the parent list is not the first element, which also disrupts the ideal structure of the entire jumping table, So we need to make a patch: Insert a negative infinity value into all the linked headers, so that even if a minimum element is inserted, each table is guaranteed to start with a negative infinity, that is, each linked list can start at the far left.

In the classroom, the experiment shows that the algorithm 2 seems to be able to get a good jumping table on average, in fact, not only the average situation can get a good jumping table, in most cases can get a good jumping table.

Can prove that get a good jump table probability P>=1-o (1/n^a) Here A is a parameter between 0 and 1, related to N, at the end of the class, the teacher spent 20 minutes to prove that the specific way we skipped here (in fact, I did not read the proof of the process of escape ~ ~)

"Introduction to Algorithms" The 12th lesson jumping table

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.