Use of the adjacent table and comparison with the vector

Source: Internet
Author: User
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. The storage method of the 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: n. in the past few days, I have encountered some problems with high requirements on edge creation. 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
 < pre>
  
The representation is exactly the same as the array ..
  
Correct your understanding...

<>

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.