FIG three kinds of storage methods

   FIG three storage methods

One. Adjacency matrix  

  Advantages: clear, concise, easy to call, easy to write;

  Disadvantages: large memory footprint, and no way to keep the heavy side (it may be possible, but I will not), the number of points over 3000 direct explosion

  Scope: little number of points, FIG dense, generally used in combination floyed be transitive closure.

  Code:

    scanf("%d%d",&u,&v,&w);
    a[u][v]=w;
    a[v][u]=w;// 双向边

two. Adjacency list

  Advantages: small space, you can quickly find out the degree of each point, you can save the heavy side, that says more convenient

  Disadvantages: very easy to find and delete side, if you want to delete an edge, you need to look for an undirected graph on the two lists and delete with the STL, the speed will be slow

  Scope: In most cases, are not required to delete the line side

  Code:

struct Edge{
    int v,w;
};
vector <Edge> edge[maxn];
void addedge(int u,int v,int w) { edge[u].push_back({v,w}); edge[v].push_back({u,w});//双向边 }

 

three. Former chain to star

  Advantages: more than adjacency table also save space, some cards can solve the problem of space, remove the side is also very convenient, only you need to change the pointer to point to the next, faster speed

  Disadvantages: if that is written in trouble, trouble understanding, performance seems to be fierce

  Application: need to remove the side of the subject, the time required high speed title

  Code:

struct Edge{
    int to,w,next;
}edge[maxn*2];
int cnt,head[maxn],s,t,n,m;
void addedge(int u,int v,int w)
{
    edge[++cnt].to=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=top;
}
struct Pre{
    int v,edge;
}pre[maxn];

 

   Explanation: This is more difficult to understand in a way, so do some interpretation, mainly to see someone else's blog to read it

  For the figure, input

  1 2

  2 3

  3 4

  1 3

  4 1

  1 5

  4 5

  For the above structure, wherein the edge [i] .to indicates the end of the i th edge, Edge [i] indicates the storage position of the lower .next one side edge with the i-th starting point , edge [i] .w is edge weights.

  Array head [], which is used to indicate the position of the starting point i stored in the first edge, head [] array is generally initialized to -1  first actually stored position side you will find here in fact i the number of the last input as a starting point for all sides.

  With the lower edge of the first storage location and with the same starting point as a starting point i storage position of one side we can facilitate each side of the point of this i

  

  Initialization cnt = 0, so now we still follow the above chart and input to simulate the look:


  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;

 

  Clearly, head [i] i saved all the edges are numbered starting from the largest one, but this location as the starting edge of the first vertex i.

   So when traversing is traversed backwards, that is to say the opposite, but this does not affect the accuracy of the results of the input sequence.

  FIG example above Example, starting from the node 1 has three sides, they are numbered 0,3,5 and head [1] = 5

  We traverse all sides to node u starting position when something like this:

   

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

 

 

  That number is then traversed first edge 5, i.e., head [1], and that edge [5] .next, number 3 is the side, and then continue edge [3] .next, i.e. the edge number 0, It can be seen is the reverse order.

  

The third part thanks to the original author quoted https://blog.csdn.net/ACdreamers/article/details/16902023

 

Guess you like

Origin www.cnblogs.com/ztdf123/p/11391062.html