The classical algorithm of data structure and algorithm

Source: Internet
Author: User
Tags comparison pow
1 for the number of daffodils within 1000
/**
     * Input Description: Ask for the number of daffodils within 1000
     *
     * narcissus number, refers to a three-bit integer, its number of cubes and equals to the number itself.
       For example: 371 is a number of daffodils, because 371=3*3*3+7*7*7+1*1*1.
     *
    /public static void Numberofdaffodils () {
        int hundred, ten, bits;
        for (int i = +; I <= 999; i++) {
            hundred = i/100;
            Ten = i% 100/10;
            bits = i%;
            if (i = = Math.pow (hundred,3) + Math.pow (ten,3) + Math.pow (bits,3)) {
                log.d (TAG, "numberofdaffodils" + i);
            }
        }
    }
Numberofdaffodils 153
numberofdaffodils 370
numberofdaffodils 371
numberofdaffodils 407

2 Monte Carlo algorithm

2.1 Background

Monte Carlo algorithm is a method to solve the problem by using the random number simulation, which is guided by the rate statistic theory.
For Pi Pi, if the radius is 1, according to the Pythagorean theorem, x²+ y²<= 1 of the landing point are in the circle. Square Area: 2*2=4, round area πr²=π.
Randomly generated a coordinate, the number of times a, false with a B in the circle, then b/a (fall in the circle internal times/fall inside the square) =Π/4 (Circle area/square area), calculation can be π=4*b/a.
2.2 Source Code


/**
     * Monte Carlo algorithm
     *
     * To determine whether the placement in the circle inside the Pythagorean theorem x²+ y²= r², then x²+ y²<= r² are regarded as within the circle
     * Count = 1000000 +/
    P Ublic double getpi (int count) {
        double in = 0;
        for (int i = 0; i < count; i++) {
            Double x = Math.random ();
            Double y = math.random ();
            if (Math.pow (x, 2) + Math.pow (Y, 2) <= 1) {in
                + +;
            }
        }
        Return 4 * in/count;
    }

3 ant colony algorithm 

3.1 Concepts

Ant colony algorithm (ant colony Optimization, ACO), also known as Ant algorithm, is a probability-based algorithm used to find the optimal path in the graph. It was presented by Marco Dorigo in his doctoral dissertation in 1992, inspired by the behavior of ants discovering pathways in the search for food. Ant colony algorithm is a kind of simulated evolutionary algorithm, the preliminary research shows that the algorithm has many excellent properties and is now used in every aspect of our life. 

3.2 Reference Links



Self-talk ant colony algorithm (with simple simulation experiment) 

4 KMP Classical algorithm

4.1 KMP algorithm to solve what type problem



String matches. Give you two strings to find if one of the strings contains another string, and if it contains, returns the containing starting position.

  4.2 Description of the algorithm



When we generally match strings, we select the first subscript from the target string str (assuming length n) to compare with the PTR length (m), and if so, the subscript at the beginning, not the same, choose str next subscript, and also select a string of length n to compare, Until the end of Str (actual comparison, the subscript is moved to n-m). This time complexity is O (n*m).
KMP algorithm: Can achieve the complexity of O (m+n). Why the complexity of time is simplified:
Take advantage of the nature of the target string ptr (for example, the repetition of some strings inside, even if there are no duplicate fields, the maximum amount of movement is achieved when comparing). 

4.3 next array 

4.3.1 Next array is the maximum length of the prefix equal to the preceding string.




 4.3.2 the role of the Next array




The next array holds the position of s in the pattern string that is next compared to the main string J when the main string and the pattern string do not match, i.e. s=next[s].

  4.3.3 the method of next array


Solution for next array
    private static void GetNext (int[] Next, String str) {
        next[0] = -1;//initialize
        int k = -1;//record the current bit nextint j = 0;//Current bit subscript while
        (J < Str.length ()-1) {//after solving all characters of next
            if (k = =-1 | | Str.charat (j) = = Str.charat (k) {//compares the current bit with the current bit next character is equal to
                j + +;
                The next value of the k++;//current bit is +1 as the next value of next bit
                next[j] = k;//This is the value of the calculated K (that is, the same maximum prefix and maximum suffix length) assigned to NEXT[J]
            } else {
                k = next[k];/// /forward backward, back to the previous prefix equal position: If the next difference, then K becomes next[k], notice next[k] is less than k, regardless of k take any value.
            }
        }
    }
4.4 Complete KMP algorithm


In short, the main string matches the pattern string, equal words are +1, if not, the main string J does not backtrack, the pattern string of s=next[s] and the main string J comparison, equal to each other +1, and so on, until the pattern string is completely matched, or the main string match to the last, the end. The algorithm is as follows:


public void main8 () {
        String str1 = "ababbabababbbab"; // Main string
        String str2 = "abababbbab"; // pattern string
        int next [] = new int [str2.length ()];
        getNext (next, str2); // Solve the next array

        System.out.println ("next array" + java.util.Arrays.toString (next));
        List <Integer> pos = new ArrayList <> (); // There may be multiple positions where the starting string matches the pattern string, and record these positions in the main string

        ifMatch (str1, str2, next, pos); // String matching process

        System.out.println ("Matching position:" + pos); // Print all matching positions
    }

    private static void ifMatch (String str1, String str2, int [] next, List <Integer> pos) {
        int j = 0; // The initial position of the main string
        int s = 0; // match string initial position
        while (j <str1.length ()) {
            if (s == -1 || str1.charAt (j) == str2.charAt (s)) {// Compare whether the characters are equal
                j ++;
                s ++;
                if (s> = str2.length ()) {// The pattern string is exactly matched
                    pos.add (j-str2.length ());
                    s = 0;
                    j--;
                }
            } else {
                s = next [s]; // Not equal, the main string j is unchanged, and the pattern string s is changed
            }
        }
    }

    // Solve the next array
    private static void getNext (int [] next, String str) {
        next [0] = -1; // Init
        int k = -1; // next to record the current bit
        int j = 0; // subscript of current bit
        while (j <str.length ()-1) {// solve all characters next
            if (k == -1 || str.charAt (j) == str.charAt (k)) {// Compare whether the current bit is equal to the current next character
                j ++;
                k ++; // the next value of the current bit + 1 is used as the next value of the next bit
                next [j] = k; // This is to assign the value of k (that is, the same maximum prefix and maximum suffix length) to next [j]
            } else {
                k = next [k]; //// backtrack backward, return to the previous suffix equal position: if the next one is different, then k becomes next [k], note that next [k] is less than k k takes any value.
            }
        }
    } 
4.5 Reference Links


"Algorithm" KMP classical algorithm, you really understand it.



The KMP algorithm is the most superficial understanding--one can understand



How do you understand the next array in the KMP algorithm? 

5 Given a, b two files, each store 5 billion URLs, each URL 64 bytes, memory limit is 4G, let you find a, b file common URL?

5.1 Questions



If each URL size is 10bytes, then you can estimate the size of each file is 50gx64=320g, far greater than the memory limit of 4G, so it is not possible to fully load into the memory processing, you can use the idea of partition to solve.

  5.2 Resolution Steps



(1) Traverse file A, hash (URL) for each URL%1000, and then according to the obtained value will be stored in 1000 small files (recorded as A0,a1,..., a999, each small file about 300M);
(2) Traverse file B and take the same method as a to store the URL in 1000 small files (recorded as B0,b1,..., b999).
Ingenious: After this processing, all the possible same URLs are saved in the corresponding small file (a0 vs B0,A1 vs B1,..., a999 vs b999), the corresponding small files cannot have the same URL. Then we only ask for the same URL in the 1000-to-small file.
(3) For each pair of small file AI and bi in the same URL, the AI can be stored in the URL to Hash_set/hash_map. Then traverse each bi URL, see if it is in the hash_set just built, if so, then is the common URL, stored in the file can be.
5.3 Reference Links



Interview-Ali-. Big Data topic-given a, b two files, each store 5 billion URLs, each URL accounted for 64 bytes, memory limit is 4G, let you find a, b file common URL?


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.