Chain--Forward star Algorithm (reprint learning) * key * "Template"

Source: Internet
Author: User

Reprint Address: http://blog.csdn.net/acdreamers/article/details/16902023

Let's first look at what the forward star is.


The forward star is a special array of edge sets, and we sort each edge in the array of edges by the starting point from small to large, and if the starting point is the same from small to large,

and records the starting position and storage length of all the edges in the array starting at a point, then the forward star is constructed.


Use Len[i] to record the length of the storage in the array for all edges starting with I.

Use Head[i] to record the first storage location in the array with I as the edge set.


So for:





The order in which we enter the edges is:


0 S

2 3

3 4

1 3

4 1

1 5

4 5


Then you get the following sequence:


Ref.: 1 2 3 4 5 6 7

Start U:1 1 1 2 3 4 4

End V:2 3 5 3 4 1 5


Get:


HEAD[1] = 1 Len[1] = 3

HEAD[2] = 4 Len[2] = 1

HEAD[3] = 5 Len[3] = 1

HEAD[4] = 6 len[4] = 2


However, the use of the forward star will have a sort operation, if the use of a fast schedule of at least O (Nlog (n))



If you use a chain-forward star, you can avoid sorting.


We build the edge structure as:


struct Edge

{

int next;

int to;

int W;

};


Where edge[i].to represents the end point of the edge of section I, Edge[i].next represents the storage position of the next edge of the same starting point as the first edge, EDGE[I].W is the edge weight value.


There is also an array head[], which is used to represent the location where the first edge is stored as the starting point of I, and you will actually find that the first side of this place is stored in fact

At the last input of all sides with I for the beginning of the number.


Head[] Array is generally initialized to-1, for the add function of the edge:


[CPP]View Plaincopy
    1. void Add (int u,int V,int w)
    2. {
    3. EDGE[CNT].W = W;
    4. Edge[cnt].to = v;
    5. Edge[cnt].next = Head[u];
    6. Head[u] = cnt++;
    7. }


Initialize CNT = 0, so we can now simulate the diagram and input as above:


Edge[0].to = 2;      Edge[0].next =-1; HEAD[1] = 0;

edge[1].to = 3;      Edge[1].next =-1; HEAD[2] = 1;

edge[2].to = 4;      Edge[2],next =-1; HEAD[3] = 2;

edge[3].to = 3;       Edge[3].next = 0; HEAD[1] = 3;

edge[4].to = 1;      Edge[4].next =-1; HEAD[4] = 4;

Edge[5].to = 5;       Edge[5].next = 3; HEAD[1] = 5;

Edge[6].to = 5;       Edge[6].next = 4; HEAD[4] = 6;


It is obvious that Head[i] holds the largest number of all edges in the beginning of I, and treats this as the position of the first starting edge of vertex i.


In this way, the time elapsed is backwards traversal, that is, the input order is the opposite, but this does not affect the correctness of the result.

For example, in the case of nodes 1 as the beginning of the Edge has 3, their numbers are 0,3,5 and head[1] = 5


This is the case when we traverse all the edges with the U node as the starting position:


for (int i=head[u];~i;i=edge[i].next)


So the first step is to traverse the edge numbered 5, which is head[1], then Edge[5].next, which is the edge of number 3, and then continue Edge[3].next, also

Is the number 0 side, can be seen in reverse order.

Chain--Forward star Algorithm (reprint learning) * key * "Template"

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.