Storage of graphs: Chain-forward stars (array of edge sets)

Source: Internet
Author: User

Disclaimer: All of the stored image structures mentioned in this article are implemented in static arrays, not linked lists.

0 What is a chain-forward star

The chain forward star is a kind of diagram structure, such as forward star, adjacency matrix, edge table, adjacency table and so on.

1. What are the advantages of a chain-forward star?

Chain forward star: High space utilization, often used in various competitions.

Adjacency matrix: Need to open n*n space, in all kinds of competitions are often card.

adjacency table: The spatial complexity is slightly smaller than the adjacency matrix, but it can be exploded by the extreme data, and the weights cannot be recorded.

Edge table: Unable to quickly determine the two-point connectivity, not suitable for most graphs of the algorithm.

Forward star: Has a sort operation with a high degree of time complexity.

2. Introduction to similar structures

Adjacency Matrix: Open a two-dimensional array, the first dimension represents the starting point, and the second dimension represents the end point.

Example: The value of A[i][j] indicates whether the first and second points are connected (0/1) to the first J point.

Adjacency table: Open a two-dimensional array, the first dimension represents the starting point, and the second dimension stores the end point.

Example: The value of A[i][j] indicates the ordinal of the J point connected by the point I, where a[i][0] represents the number of points connected by the point I.

Edge table: The information of the pure storage edge, open one-dimensional array, with the structure of the storage, the structure contains three of the information of this edge, that is, the starting point and the weight value.

Example: A[i] Stores the information for the I edge, where A[i].s represents the starting point A[I].E represents the end point A[I].W the weight value.

Forward star: Can be understood as the improvement of the edge table, the elements in the edge table according to the beginning of the first keyword sort, the end of the second keyword to sort.

    No, give me an example.

3. How the chain-forward star is implemented.

The following three arrays are recommended for personal use:head[n], next[m], End[m]

  Head[i] The first edge, which is the starting point of I, Next[i] represents the next edge of the beginning with the edge of article I, and End[i] represents the end point of the edge of article I.

Where the next array is aligned with the end array.

How to build a map?

int num=0;  for (int i=1; i<=m;i++) {    cin>>x>>y;    next[++num]=head[x];    Head[x]=num;    End[num]=y;    next[++num]=head[y];    Head[y]=num;end[num]=x;} // I wrote it two times because I built two-way sides.

Example:

Then input data at a glance: 5 8 1 2 1 3 1 5 2 4 2 5 3 4 3 5 4 5

So how does the program run through a few arrays?

Head 5 9 13 15 16
next 0 Span style= "font-size:15px;" >0 1 0 3 0 2 0 7 6 4 8 11 10 12 14

end

2 1 3 1 5 1 4 2 5 2 4 3 5 3 5

4

Note that the part labeled Red, Head[5] has a value of 16, indicating that the first point connected by 5 is in the end array labeled 16 (end[16]=4 means that the first point that 5 is connected to is 4).

So we only know the first point, how to know the last point? Implemented with the next array.

Next[16]=14, which indicates that the second point of the 5 connection is in the end array labeled 14, and next[14]=10, which indicates that the third point is labeled 10, so that the loop points until the last point next[6]=0, stating that subscript is end[6] is the last.

So how do you implement the poor lifting of the 5 connected points?

  

1 //First Kind2  for(intK=head[x];k>0; k=Next[k])3cout<<end[k]<<Endl;4 //The second Kind5 intk=Head[x];6  while(k>0){7cout<<end[k]<<Endl;8k=Next[k];9}

Here only the approximate meaning and use of the chain forward star, the more detailed principle and its application within the algorithm will be explained in my next essay.

Storage of graphs: Chain-forward stars (array of edge sets)

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.