LCA to solve the problem in several ways

LCA to solve the problem in several ways

This essay to explain the problem in graph theory LCA (common ancestor) Several solving methods and implementation. LCA is an advanced problem in graph theory, so I hope readers learn through the primary graph theory know some of the basics of drawing and know deep search algorithm implementation. So this blog will understand fast, comfortable.

Intellectual preparation

LCA understand the problem, understand the depth of the nodes is crucial, we can draw a tree. In a tree, all nodes have a depth. 1 is the depth of the root node, other nodes can use the depth of the deep search through the tree to handle it. In this way, we will be able to achieve the LCA algorithm to solve the problem through the depth of the array.

LCA simple algorithm

LCA simple algorithm implementation process is about is this: For two points of inquiry \ (the X-, the y-\) , the first two points determine who deeper, then deeper point along its parent, step by step lifting up, until and \ (Y \) depth point is equal. Then while improving \ (x, y \) two points, two points until this becomes a single point, then the point is that we are asking for LCA.

According to the characteristics of this algorithm, we call him "climb a climbing" algorithm. This algorithm is very easy to understand, but very unresponsive. So we will not give code.

LCA multiplication algorithm

LCA simple algorithm just mentioned easier to understand, but very unresponsive. So we launched a little more advanced methods: doubling LCA. Doubling idea is actually very easy to use an optimization idea, there are many instances of the use of optimization algorithms. Such problems by using brute force approach RMQ after multiplication becomes optimized table ST (ST algorithm), such as a climbing LCA climbing algorithm optimized after multiplication becomes LCA multiplication algorithm.

The so-called multiplier LCA, in fact, well understood, is the original of a simple algorithm is a climb, we now become a crawl \ (2 ^ k \) months, which would greatly optimized complexity.

Steps to achieve and there is no change: are the first small climb that, after climbing together. But this time we will deal with a two-dimensional array f. \ (f [x] [k ] \) represents \ (x \) the first \ (2 ^ k \) whose generation ancestor Yes. Thus, we can come to a recursive formula:
\ [F [X] [K] = F [F [X] [-K. 1]] [-K. 1] \]
It should be well understood: \ ( X \) of \ (2 ^ k \) generation ancestor equal to \ (X \) of \ (2 ^ {k-1 } \) ancestor generation of \ (2 ^ {k-1 } \ ) ancestor's generation.

(In fact, this is a dynamic planning process)

The rest is just details. Please a lot of attention, ask questions later when LCA depending on the questions asked, to change the template.

Code:

int lca(int x,int y)
{
    int ret;
    if(deep[x]>deep[y])
        swap(x,y);
    for(int i=20;i>=0;i--)
        if(deep[f[y][i]]>=deep[x])
            y=f[y][i];
    if(x==y)
        return y;
    for(int i=20;i>=0;i--)
    {
        if(f[x][i]!=f[y][i])
        {
            x=f[x][i];
            y=f[y][i];
        }
        else
            ret=f[x][i];
    }
    return ret;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11494132.html