Storage of graphs-adjacency table

Source: Internet
Author: User

Graph adjacency table before implementation, always a little bit of a problem

It is now clear that the original head node is content and has been used as a marker position

In the final analysis, this is a combination of cis-and chain-like storage structure


But still do not know why, this structure compared to the adjacency matrix in addition to the space storage above has a great advantage, and access to adjacent nodes convenient

I don't know what else he has to be.

(In fact, it is because of the chain structure operation is not very familiar to find an excuse.) )


The adjacency table (adjacency list) is a storage method that combines sequential storage with chained storage for graphs. adjacency table notation is similar to the tree of child linked table notation. That is, for each vertex VI in Figure G, the vertex VJ that is adjacent to VI is chained to a single-linked list, which is called the adjacency table of Vertex VI, and then the adjacency table header of all the points is placed in the array, which makes up the adjacency table of the graph. There are two kinds of node structures in the adjacency table representation.


One is the node structure of a vertex table, which consists of a vertex field (vertex) and a pointer field (Firstedge) pointing to the first adjacency edge, and a side table (that is, an adjacency table) node, which consists of a neighboring point field (Adjvex) and a pointer field (next) pointing to the next adjacent edge. For the edge table of the network diagram need to add a storage edge information (such as weights and so on) of the field (info), the network diagram of the edge table structure 8.10 is shown.


adjacency table representation is described in the following form:

# # Maxvernum 100/* The maximum number of vertices is 100*/typedef struct node{/* Edge table node */int adjvex;/* adjacency Point Field */struct node * NEXT;/* pointer field pointing to next adjacency point *//* To represent information on the edge, you should add a data field info*/}edgenode;typedef struct vnode{/* Vertex table node */vertextype vertex;/* Vertex field */edgenode * firstedge;/* Side table Head pointer */}vertexnode;typedef vertexnode Adjlist[maxvertexnum]; /*adjlist is the adjacency table type */typedef struct{adjlist adjlist;/* adjacency table */int n,e;/* Vertex count and edge number */}algraph; /*algraph is the type of diagram stored as adjacency table.

The algorithm for establishing an adjacency table storage for a graph is as follows:

void Createalgraph (Algraph *g) {/* Create an adjacency table for the graph to store */int i,j,k; Edgenode * S;PRINTF ("Enter the number of vertices and the number of sides (input format: top points, number of sides): \ n"), scanf ("%d,%d",& (g->n),& (g->e)); /* Read in vertices and sides */printf (Enter the vertex information (input format: vertex number <CR>): \ n "), for (i=0;i<g->n;i++)/* Create a vertex table with n vertices */{scanf (" \n%c ", & (G->adjlist[i].vertex)); /* Read in vertex information */g->adjlist[i].firstedge=null; /* The edge header pointer of the vertex is set to null */}printf ("Enter the Edge information (input format: i,j): \ n"); for (k=0;k<g->e;k++)/* Establish the side table */{scanf ("\n%d,%d",&i,& j); /* Vertex corresponding to read-in Edge <Vi,Vj> */s= (edgenode*) malloc (sizeof (Edgenode)); /* Create New Edge table node s*/s->adjvex=j; /* adjacency point sequence number is j*/s->next=g->adjlist[i].firstedge; /* Insert the new Edge table node s into the Benzi head of Vertex VI */g->adjlist[i].firstedge=s;}} /*createalgraph*/

If there are n vertices and e edges in the graph, then its adjacency table needs n head node and 2e table nodes. Obviously, in the case of Edge thinning (e<<n (n-1)/2), the adjacency table is used to represent the graph than the adjacency matrix to save storage space, especially when the information associated with the edge is more.

In the adjacency table of the non-direction graph, the degree of Vertex VI is exactly the number of nodes in the list I, and in the graph, the nodes in the list I list are only the degree of Vertex VI, for the degree of penetration, the entire adjacency table must be traversed. In all linked lists, the number of nodes whose adjacency point field is the value of I is the degree of Vertex VI. Sometimes, in order to make it easier to determine the degree of the vertex or the arc with Vertex VI as the head, you can create an inverse adjacency table of the graph, that is, a linked list of arcs with the VI as the head of each vertex VI. Example 8.12 shows the adjacency table and the inverse adjacency table of the G2 graph (Figure 8.2).


when the adjacency table or the inverse adjacency table is established, if the input vertex information is the vertex number, then the complexity of establishing adjacency table is O (n+e), otherwise, the time complexity is O (n e) If the vertex is to be located in the figure by searching.

It is easy to find the first adjacency point and the next adjacency point of any vertex on the adjacency table, but to determine whether there is an edge or an arc connected between any two vertices (vi and VJ), it is necessary to search for the I or J list, so it is less convenient than the adjacency matrix.

Storage of graphs-adjacency table

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.