Use of the adjacent table and comparison with the vector.

Source: Internet
Author: User

Use of the adjacent table and comparison with the vector.
In the past few days, I have encountered some questions that have high requirements for Edge building. However, it is difficult for a vector to build edges, So we learned the adjacent table .. The following are my views on the adjacent table.
Storage Method of the adjacent table
An adjacent table is a linked list of each node and a linked list created by the header insertion method. Here we first use an array for simulation .. First [u] And next [e] represent the number of the first edge of node u and the number of the next edge of the e edge respectively .. The implementation code is:

next[e]=head[u[e]];head[u[e]]=e;

Then it will be very useful if it is used together with the struct ..
struct Edge{     int to,val,next;}edge[maxn];int head[maxn];void addedge(int x,int y,int val){    edge[++cnt].to=y;    edge[cnt].val=val;    edge[cnt].next=head[x];    head[x]=cnt;}
The Traversal method of the corresponding adjacent table is also available.
for(int i=head[x],i!=-1;i=next[i])
The second Traversal method is:
for(int i=head[x],i!=-1;i=edge[i].next)
While vector can achieve the same effect as the adjacent table, the vector container is actually a dynamic array, but unlike the adjacent table, it is stored in sequence, so the storage and traversal are very simple.
Traverse for (int I = 0; I <vec [x]. size (); I ++)
The representation is exactly the same as the array ..
Correct your understanding...


Is it better to use vector or list for the adjacent table?

Obviously, you can use list to facilitate insertion and deletion of nodes.

What are the differences between a vector container and a list container?

First, I want to talk about the java syntax.

In essence, if you have learned the data structure, you should understand the difference between array and list. Vector is a reliable array.

If you don't understand, let me give you an analogy. For example, if you finish the second stop in the first stop and the third stop in the second stop in the game, this chain structure is suitable for using list, some unrelated data sets, such as the random monsters that appear in a scenario in your online game, can use vector. Of course, I am only talking about the example, the actual data structure is much more complicated ..

In addition, if your data set often involves insert and delete operations, it is suitable for using list, but the search operations are more suitable for using vector.

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.