Pairing heap learning and re-analysis of Dijkstra algorithms

Source: Internet
Author: User
Tags constant

About the study material, the nest to see is this article
Its definition of the data structure has been more clearly described, I am here to explain only a few small places and give their own implementation code
First of all, we all know Dijkstra, an algorithm used to solve the single source shortest path.
Time complexity O (v^2)
Then its optimization:
Heap +dij
Time complexity O (vlgv+e)
Here are the highlights:
We all thought that the dij+ heap, which was written by two forks, was vlgv+e, but what was it like?
First, for the n-1 of the deletion point, the single-time complexity LGV
Then for each slack to modify the node's operation is the worst e-time (for each edge need to relax operation), a single complexity of LGV.
This is very embarrassing, so that the complexity of the calculation is (E+V) LgV, and not what people expect E+VLGV, but because the common binary heap is better to write, and in the race to pass LgV card off the situation is not too much, so that in order to program efficiency, we generally use it to optimize DIJ, Even useful priority_queue, which of course no problem (: constant is not small if it is also called no problem ...)
Of course, this proves that the time complexity of the average dij+ two fork heap is not the complexity that people expect. It's just a matter of obsessive-compulsive disorder.
So how to achieve the complexity of O (E+VLGV).
First, the minimum value for each selection and the deletion must be done within LGV time
Secondly, for each slack operation, it must be O (1);
So... For the use of what data structure to maintain these operations, I looked at a lot of information, to choose Two, one is the Fibonacci heap, and the other is going to talk about the pairing heap
The related blog I recommend two:
One is this:

http://dsqiu.iteye.com/blog/1714961

See this just know there is pairing heap this thing, writing is quite clear, but the middle description of the heaps part of the picture is cracked, I see a bit laborious (do not know is not nest network problem), this also has source code, but to the pairing heap after I did not see his.
The other one is this:

http://blog.csdn.net/goodluckwhh/article/details/13341893

This blog is just about Fibonacci heap ... The inside of the figure is very clear, the thought description is also good, but no code
and then f-heap it is not a problem of complexity, but I engaged in an afternoon or give up the use of it to optimize DIJ.
Reason:
1. Too difficult to write, the data structure part of the 250+ line (perhaps I am too low, the nest is not willing to play at the time of the game to knock 200 lines such a thing
2. The constant is too big to be small ... Maintenance of a bunch of stuff.
and then I threw it away, made a bit of pairing heap this thing, this is very good to write, probably the data structure part of 70 lines can be finished, pressure line may be less, but I did not try.
This thing I read the first part of the PPT study:
I will only explain the part of the explanation, I said below please be sure to read the PPT before you see (if you do not understand
1. First, the left sibling (when it is the first child of its parent node, this variable points to its father, Right brother (not pointing to the same layer, do not think that the same layer is the brother OK.)
2. About deleting the root node LGV of the complexity of the section, this ppt inside no card, I said:
First, when deleting the root node, you need to merge all of the root nodes of the subtree, this step is the worst case, obviously O (N), there is no problem
But merging will have an impact on your later deletion of nodes:
The first, the most common, in order to merge, then it is possible to merge after the root node is connected to the n-1 point, then each time the back is still O (N);
The second type, is the idea of splitting the merger, two two merges, then two two merges, so that the first time the worst deletion of the root node, the complexity is still O (N), but the shape of the tree has changed, it becomes more "mellow", because each time the merge, the last node as the root node of the degree will be + 1, and this operation will be LgN times therefore, the next time the deletion operation is O (LgN)
3. Its proof of the complexities of optimizing DIJ:
First, the first point + delete the worst O (V);
Then, assuming that each cycle of the EI relaxation operation, and that the EI point has no child nodes directly connected to the root node, and still with the root node is deleted after the root nodes, then each delete operation is EI+LGV
Cycle V-1 times, there is O (v) +o (e1+e2+e3+...+ Ei+...+ev-1) +o (VLGV) =o (E+VLGV)
Proof
Next put the code, I use a static open point method for example with water problem hdu2544

Finally found a better written data structure that can be used to optimize Dij//pairing heap #include <stdio.h> #include <cstdlib> #include <queue> #
Include<iostream> #include <cstring> using namespace std;
const int MXN=100+10;
const int MX=30*MXN;
const int ln=1000000000;
    struct pairingheap{int siz;
    int l[mxn];//Their neighbourhood, if it is the first child node of its parent node, its value is the value of its Father node int r[mxn];//right o int k[mxn];//node int cld[mxn];//The first node in the child list;
    int root;//root node designator int flag[mxn];//dij with int qu[mx];
    int hd,ed;
        void Init () {siz=0;
        memset (L,-1,sizeof (l));
        Memset (r,-1,sizeof (R));
        memset (cld,-1,sizeof (CLD));
        Root=-1;
        hd=1;
    Ed=0;
            } int Merg (int t1,int t2) {//merge two Trees if (K[t1]<k[t2]) {//Let k[t2]<=k[t1] t1^=t2;
            T2^=T1;
        T1^=t2;
        } l[t1]=t2;
        R[T1]=CLD[T2];
        if (cld[t2]!=-1) l[cld[t2]]=t1;
        CLD[T2]=T1;
        L[t2]=r[t2]=-1; Return t2;//returns the root node} void Add (int f,int key) {//Add a node K[++siz]=key with a label of F and a value of key;
        Flag[f]=siz;
            if (root==-1) {root=siz;
        return;
    } Root=merg (Siz,root); } void Decrease (int vis,int val) {//decreases the value of the Vis node to Val (here I write the small top, the value becomes smaller can be so dry, the larger the words will be a little bit troublesome, but DIJ will not increase the situation, so do not worry) if (vis=
        =root) {K[vis]=val;return;}
        if (Cld[l[vis]]!=vis) R[l[vis]]=r[vis];
        else Cld[l[vis]]=r[vis];
        if (r[vis]!=-1) L[r[vis]]=l[vis];
        K[vis]=val;
        L[vis]=r[vis]=-1;
    Root=merg (Vis,root); } void print (int n) {for (int i=1;i<=n;i++) {printf ("%d%d%d,%d%d\n", I,l[i],r[i],k[i],cld[i],ro
        OT);
        }} void Remov () {int fst=cld[root];
            while (fst!=-1) {qu[++ed]=fst;
        FST=R[FST];
            } while (hd<ed) {int q1=qu[hd++];
            int q2=qu[hd++];
        Qu[++ed]=merg (Q1,Q2);
        } fst=qu[hd++];
        Hd=1,ed=0;
    ROOT=FST;
}}p; int MP[MXN][MXN];
int D[MXN];
int VIS[MXN];
vector<int>mmp[mxn];
    void Dij (int st,int n) {int siz=mmp[st].size ();
    memset (vis,0,sizeof (VIS));
    for (int i=1;i<=n;i++) {d[i]=ln;
        } for (int i=0;i<siz;i++) {int des=mmp[st][i];
    D[des]=mp[st][des];
    } d[st]=0;
    P.init ();
    for (int i=1;i<=n;i++) {p.add (i,d[i]);
        } for (int i=1;i<n;i++) {int tt=p.root;//p.print (n);p UTS ("");
        P.remov ();
        Vis[tt]=1;
        Siz=mmp[tt].size ();
            for (int j=0;j<siz;j++) {int des=mmp[tt][j];
                if (!vis[des]) {int dis=d[tt]+mp[tt][des];
                    if (Dis<d[des]) {D[des]=dis;
                P.decrease (Des,dis);
    }}}}} int main (void) {int n,m; while (scanf ("%d%d", &n,&m) && (n| |
        m)) {int a,b,c;
        Memset (Mp,-1,sizeof (MP)); for (int i=1;i<=n;i++) {mmp[i].clEar ();
        mp[i][i]=0;
            } for (int i=1;i<=m;i++) {scanf ("%d%d%d", &a,&b,&c);
            if (mp[a][b]!=-1) mp[a][b]=mp[b][a]=min (MP[A][B],C);
                else {mp[a][b]=mp[b][a]=c;
                Mmp[a].push_back (b);
            Mmp[b].push_back (a);
        }} dij (1,n);
    printf ("%d\n", D[n]);
} return 0; }

Amount ... I was going to use flag to save the mapping between the label and the heap, and later found that because I started to add n nodes, so that the direct access is good, want to remove can be directly removed.
above

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.