"Concept" loose graphs & dense graphs:
The loose graph refers to a graph with few edges connected to it, whereas the opposite (point-connected Bendo) is a dense graph.
Tips: The adjacency matrix is more contiguous than the adjacency table, and the adjacency matrix is used for dense graphs.
Adjacency Matrix:
Open a two-dimensional array graph[] [] to record the connection between the midpoint A and point B of the graph, and initialize it to 0 (or 1); If there are negligible heavy edges (such as the smallest or largest edge in a heavy edge only), save the Benquan of the desired edge, but if there are heavy edges that cannot be ignored, You must not use the adjacency matrix.
int GRAPH[MAXN][MAXN]; void Graphinit () { memset (graph,0,sizeof);} void Graph_addedge (intfrom,int to ) { graph[from ][to]=1; // If there is a graph of edge rights, assign weights to Graph[from][to] // in the case of a non-graph[from][to]=graph[to][from]=x, the graph can be written as a symmetric matrix;}
adjacency table:
Still give each node number, adjacency table is to declare a to in the struct, from point A to the connected point B, is vertex[a].to.push_back (b); Remember to initialize.
Furthermore, since the adjacency table is a vector edge (push_back), there is no need to worry about the loss of heavy edges; however, using adjacency tables to store graphs, for queries that are connected between two points, compared to the adjacency matrix, The adjacency table is at a disadvantage (because it must traverse the entire current point in the adjacency list to determine if it is connected to another point).
//using vectors to achievestructnode{Vector<int>to ; //If you want to hang edge, add int val in the struct;}VERTEX[MAXN];voidGraph_init (intN) { for(intI=1; i<=n;i++) Vertex[i].to.clear ();}voidGraph_addedge (int from,intTo ) {vertex[ from].to.push_back (to); //If there is no edge, write the following two steps://Vertex[from].to.push_back (to); //Vertex[to].to.push_back (from);}
Chain-forward Star:
is essentially a list of all the edges in the graph that are made up of a particular way.
By adding Edge method, you can know how to query the edge of a point to connect the method:
To query the edge of a point, we need to look at the head, we know where this point was recently added (the query results here is J), and then a little earlier than this edge is Next[j], and a little earlier is NEXT[NEXT[J]], a little earlier is Next[next[next[j]], and a little earlier is ..., and that's it. We always add time to the time to check until we find the empty node . (Used to mark the end of the list).
The following is a chain-forward star template, including the addition edge operation, traversal operation method:
structgraph{intHEAD[MAXN];//The position of the first edge corresponding to each node in the container (array) intNEXT[MAXN];//The position of the next edge of each edge in the container corresponding to the same starting point intTO[MAXN];//really store the point that one edge points to//to know the starting point for each edge, you need to open an array from[maxn];inlinevoidAddedge (int_from,int_to) { //How to add edges Static intq=1; //Q is a static variable, each adding edge, first with q indicates the end of the container of the current storage edge (implied already end)To[q]=_to;//write new plus edge information at the end of tonext[q]=Head[_from]; //Head[_from] represents the position of the starting point _from the most recently added edge, and then the next edge of the new plus point points to the position of the edgehead[_from]=q++; //modifies the head so that the most recently added edge is updated to a new edge, and the end moves backwards (q++;) for next addition}} graph;voiditeration () {//Methods of Traversal intNow//now is the node number that is currently located for(intJ=idx.head[now]; J j=Idx.next[j]) {} //operate node J,j is the number of nodes to which now is connected}
Storage structure of graphs: adjacency matrix (adjacency table) & Chain forward Star